Module: Audition::Rewriters

Defined in:
lib/audition/rewriters.rb

Overview

Unsafe-tier, multi-site rewrites planned at fix time from a parsed file plus its findings. MagicComments.plan returns a file-level edit plus the list of checks it makes redundant. Memoization.plan and WriteOnce.plan return one bundle per converted variable group (sites:, safety :unsafe); Rewriters.resolve flattens them to edits after dropping groups whose edits would swallow another converted group's sites.

Defined Under Namespace

Modules: MagicComments, Memoization, WriteOnce

Class Method Summary collapse

Class Method Details

.bundle(ops, edits) ⇒ Object

A planned conversion for one variable group: the group's edits plus the byte spans of every recorded site, so cross-group nesting is visible before edits are applied. Nil when the group produced no edits.



18
19
20
21
22
23
24
25
26
# File 'lib/audition/rewriters.rb', line 18

def self.bundle(ops, edits)
  return nil if edits.empty?

  sites = ops.map do |op|
    location = op[:node].location
    [location.start_offset, location.end_offset]
  end
  {edits: edits, sites: sites}
end

.resolve(bundles) ⇒ Object

The fixer keeps the first of two overlapping edits, so an edit whose span contains a site of a different converted group would keep that nested site's old text while the rest of the other group moves (a stale read at runtime). Such groups are skipped here and keep their findings.



33
34
35
36
37
38
39
40
# File 'lib/audition/rewriters.rb', line 33

def self.resolve(bundles)
  bundles.flat_map do |bundle|
    clobbers = bundles.any? do |other|
      !other.equal?(bundle) && swallows?(bundle, other)
    end
    clobbers ? [] : bundle[:edits]
  end
end

.swallows?(bundle, other) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
# File 'lib/audition/rewriters.rb', line 42

def self.swallows?(bundle, other)
  bundle[:edits].any? do |edit|
    next false if edit.start_offset >= edit.end_offset

    other[:sites].any? do |from, upto|
      edit.start_offset <= from && upto <= edit.end_offset
    end
  end
end