reweave 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3a61ab66969a390dbf6a1535b20242f5c7ea3ea02bf5f96c0896cff82cdccfd7
4
- data.tar.gz: 78f2a9767e05a1ca92bff45090c67d5fa691b4e5e89794ae2b51ac079e71f88b
3
+ metadata.gz: 107a7342818e4a66e4b511e324ad829eded3cd588b886ffa0c8b96e3d1b96591
4
+ data.tar.gz: e54e02252b3a5bc3bddce93ed798deb19b4592bedcc4f1210f5628263191fbd4
5
5
  SHA512:
6
- metadata.gz: 247811aeb99c58b0d31e1f52fd7c550352e970f7d859185190871886d9ae849b39a9b511ccf771c72317f049c9c6f2c8b071dc5dd9c4d42fbf3fca68b2ae42a1
7
- data.tar.gz: ee63ad2fb09fb5f9b522775b7470bcee6960412741e8564a674ea3476cee5091cb0a597c8323d64e26e1ad90bf1b8c4a0c125b35e9b521c487723c6aed72cccb
6
+ metadata.gz: 36499f4274ef50e4e394018defc5ea626fd1a63cbe814feaeb48c509fe1d630689bad17d2704bdf45178efffcc8fcdb75c8f6b947e414ef4c8d6d0d372d80df1
7
+ data.tar.gz: eaff740612662e2a2c359f02962bf8ff83e5d4325c24cce67ff2a0228a021890918f849cf8d932351d8d479c950ba1f9050a1f340cfbbc8e375389401f5f6aa3
data/bin/reweave CHANGED
@@ -26,8 +26,15 @@ end
26
26
  def parse_common_options(args)
27
27
  options = {}
28
28
  options[:dry_run] = args.delete("--dry-run") || args.delete("-n")
29
- options[:path] = args.delete_at(args.index("--path") || args.index("-p") || 0)
30
- options[:path] = "." if options[:path] == "--path" || options[:path] == "-p" || options[:path].nil?
29
+
30
+ # Consume --path/-p VALUE (flag + value together), like --include/--exclude
31
+ path_idx = args.index("--path") || args.index("-p")
32
+ options[:path] = args.delete_at(path_idx + 1) if path_idx
33
+ args.delete_at(path_idx) if path_idx
34
+
35
+ # Fallback: first remaining positional is the path (or . if none)
36
+ options[:path] = "." if options[:path].nil? || options[:path].empty?
37
+
31
38
  options[:include] = args.delete_at(args.index("--include") + 1) if args.include?("--include")
32
39
  options[:exclude] = args.delete_at(args.index("--exclude") + 1) if args.include?("--exclude")
33
40
  options[:max_cycles] = args.delete_at(args.index("--max-cycles") + 1)&.to_i if args.include?("--max-cycles")
@@ -253,10 +253,16 @@ module Reweave
253
253
  @context.stats[:total_edits] += edits.size
254
254
 
255
255
  if !@dry_run
256
+ # Capture original before mutating the in-memory source
257
+ original = source_file.source
256
258
  source_file.apply_edits(edits)
259
+ # Persist to disk
260
+ full_path = File.join(@context.project_root, source_file.path)
261
+ FileUtils.mkdir_p(File.dirname(full_path))
262
+ File.write(full_path, source_file.source)
257
263
  @change_set.add(Results::FileChange.new(
258
264
  path: source_file.path,
259
- original_source: source_file.source,
265
+ original_source: original,
260
266
  updated_source: source_file.source,
261
267
  recipes: [recipe.display_name],
262
268
  change_type: :modified
@@ -298,6 +304,10 @@ module Reweave
298
304
  if recipe.is_a?(ScanningRecipe)
299
305
  accumulator = accumulators[recipe]
300
306
  recipe.get_visitor(accumulator, @context)
307
+ elsif recipe.method(:get_visitor).arity == 2
308
+ # Transformation recipes opting in (arity 2) receive the whole
309
+ # accumulators hash so they can read another recipe's scan results.
310
+ recipe.get_visitor(accumulators, @context)
301
311
  else
302
312
  recipe.get_visitor(@context)
303
313
  end
@@ -14,23 +14,29 @@ module Reweave
14
14
  end
15
15
 
16
16
  def self.from_path(root, patterns: ["**/*.rb"], exclude: [])
17
+ root = File.expand_path(root)
17
18
  files = Reweave::Discovery::FileDiscoverer.discover(root, patterns: patterns, exclude: exclude)
18
- source_files = SourceSet.from_paths(files)
19
+ # Store paths relative to project root so writes go to the right place
20
+ rel_files = files.map { |f| f.delete_prefix(root + "/") }
21
+ source_files = SourceSet.from_paths(rel_files, root: root)
19
22
  new(root, source_files: source_files)
20
23
  end
21
24
 
22
25
  def add_source_file(path)
23
- file = SourceFile.new(path: File.join(@root, path))
26
+ # path is relative to project root
27
+ abs = File.join(@root, path)
28
+ file = SourceFile.new(path: path)
29
+ file.source = File.read(abs, encoding: "bom|utf-8")
24
30
  @source_files.add(file)
25
31
  file
26
32
  end
27
33
 
28
34
  def remove_source_file(path)
29
- @source_files.remove(File.join(@root, path))
35
+ @source_files.remove(path)
30
36
  end
31
37
 
32
38
  def find_source_file(path)
33
- @source_files[File.join(@root, path)]
39
+ @source_files[path]
34
40
  end
35
41
 
36
42
  def valid_source_files
@@ -13,8 +13,7 @@ module Reweave
13
13
 
14
14
  def initialize(**options)
15
15
  scanner = FindMethodDefinitions.new(**options)
16
- remover = RemoveUnusedMethodVisitor.new(scanner)
17
- super([scanner, remover], **options)
16
+ super([scanner, RemoveUnusedMethodsRemover.new(scanner)], **options)
18
17
  end
19
18
 
20
19
  # Scanning phase: find all method definitions and calls
@@ -22,14 +21,11 @@ module Reweave
22
21
  display_name "Find method definitions"
23
22
  description "Scans for all method definitions and calls"
24
23
 
25
- attr_reader :accumulator
26
-
27
24
  def initial_accumulator
28
25
  {definitions: {}, calls: Set.new}
29
26
  end
30
27
 
31
28
  def scan(source_file, accumulator, context)
32
- @accumulator = accumulator
33
29
  visitor = DefinitionScanner.new(context, source_file, accumulator)
34
30
  visitor.traverse(source_file.root)
35
31
  end
@@ -63,15 +59,33 @@ module Reweave
63
59
  end
64
60
  end
65
61
 
66
- # Transformation phase: remove unused methods
67
- class RemoveUnusedMethodVisitor < Reweave::Visitor::RubyVisitor
68
- def initialize(scanner)
69
- super(nil, nil)
62
+ # Transformation phase: removes definitions the scan phase found unused.
63
+ # A proper Recipe (not a bare visitor) so the engine can drive it.
64
+ class RemoveUnusedMethodsRemover < Recipe
65
+ display_name "Remove unused method definitions"
66
+ description "Deletes method definitions that are never called anywhere in the project"
67
+
68
+ def initialize(scanner = nil)
70
69
  @scanner = scanner
70
+ super()
71
+ end
72
+
73
+ # The engine hands arity-2 get_visitor the accumulators hash so this
74
+ # recipe can read the scan phase's results (Engine#create_visitor).
75
+ def get_visitor(accumulators, context)
76
+ return unless accumulators.key?(@scanner)
77
+
78
+ RemoveUnusedMethodVisitor.new(context, accumulators[@scanner][:calls])
79
+ end
80
+ end
81
+
82
+ class RemoveUnusedMethodVisitor < Reweave::Visitor::RubyVisitor
83
+ def initialize(context, calls)
84
+ super(context, nil)
85
+ @calls = calls
71
86
  end
72
87
 
73
88
  def run(source_file)
74
- @accumulator = @scanner.accumulator
75
89
  @source_file = source_file
76
90
  traverse(source_file.root)
77
91
  edits
@@ -80,8 +94,7 @@ module Reweave
80
94
  def visit_def_node(node)
81
95
  method_name = node.name.to_s
82
96
  # Check if this method is called anywhere
83
- calls = @accumulator[:calls]
84
- return super if calls.include?(method_name)
97
+ return super if @calls.include?(method_name)
85
98
 
86
99
  # Method is unused, remove it
87
100
  delete(node)
@@ -64,7 +64,7 @@ module Reweave
64
64
  transform_block_params do |node, instance|
65
65
  next unless instance.config[:target_cops].empty? || instance.config[:target_cops].include?("Style/SingleLineBlockParams")
66
66
 
67
- inner_params = node.parameters
67
+ inner_params = node.prism_node.parameters
68
68
  requireds = inner_params.requireds
69
69
  if requireds.size == 1 && inner_params.optionals.empty? && inner_params.rest.nil?
70
70
  param = requireds.first
@@ -83,7 +83,7 @@ module Reweave
83
83
  transform_block_params do |node, instance|
84
84
  next unless instance.config[:target_cops].empty? || instance.config[:target_cops].include?("Lint/UnusedBlockArgument")
85
85
 
86
- inner_params = node.parameters
86
+ inner_params = node.prism_node.parameters
87
87
  inner_params.requireds
88
88
  # Check if any required params are unused
89
89
  # See https://github.com/tachyons/reweave/issues/36
@@ -14,7 +14,7 @@ module Reweave
14
14
  end
15
15
 
16
16
  # Get cached analysis result for a project
17
- # Returns nil if cache is invalid or doesn't exist
17
+ # Returns nil if cache is invalid, absent, or cannot be reconstructed.
18
18
  def get(project)
19
19
  cache_key = cache_key_for_project(project)
20
20
  cache_file = File.join(@cache_dir, "#{cache_key}.json")
@@ -29,6 +29,11 @@ module Reweave
29
29
  # Load cached result
30
30
  data = JSON.parse(File.read(cache_file))
31
31
  reconstruct_analysis_result(data)
32
+ rescue
33
+ # ponytail: cache is a best-effort optimization; a corrupt or
34
+ # incompatible entry just means recompute. Add schema versioning if
35
+ # cache hits ever need to be reliable.
36
+ nil
32
37
  end
33
38
 
34
39
  # Store analysis result in cache
@@ -44,6 +49,9 @@ module Reweave
44
49
  # Store metadata for invalidation
45
50
  meta = build_metadata(project)
46
51
  File.write(meta_file, JSON.pretty_generate(meta))
52
+ rescue
53
+ # Never let a cache write break an analysis run.
54
+ nil
47
55
  end
48
56
 
49
57
  # Invalidate cache for a project
@@ -145,20 +153,29 @@ module Reweave
145
153
  }
146
154
  end
147
155
 
148
- def serialize_type(type)
156
+ def serialize_type(type, seen = Set.new)
149
157
  case type
150
158
  when Reweave::Semantic::PrimitiveType
151
159
  {type: "PrimitiveType", name: type.name}
152
160
  when Reweave::Semantic::ClassType
153
- {
154
- type: "ClassType",
155
- name: type.name,
156
- namespace: type.namespace,
157
- superclass: type.superclass&.name,
158
- methods: type.defined_methods.transform_values { |m| {return_type: serialize_type(m[:return_type]), param_types: m[:param_types].map { |pt| serialize_type(pt) }} }
159
- }
161
+ # Class types can reference themselves (e.g. a method returning `self`).
162
+ # Serialize repeats shallowly to avoid infinite recursion.
163
+ if seen.include?(type.object_id)
164
+ {type: "ClassType", name: type.name, namespace: type.namespace, superclass: nil, methods: {}}
165
+ else
166
+ seen.add(type.object_id)
167
+ {
168
+ type: "ClassType",
169
+ name: type.name,
170
+ namespace: type.namespace,
171
+ superclass: type.superclass&.name,
172
+ methods: type.defined_methods.transform_values do |m|
173
+ {return_type: serialize_type(m[:return_type], seen), param_types: m[:param_types].map { |pt| serialize_type(pt, seen) }}
174
+ end
175
+ }
176
+ end
160
177
  when Reweave::Semantic::UnionType
161
- {type: "UnionType", types: type.types.map { |t| serialize_type(t) }}
178
+ {type: "UnionType", types: type.types.map { |t| serialize_type(t, seen) }}
162
179
  when Reweave::Semantic::UnknownType
163
180
  {type: "UnknownType"}
164
181
  else
@@ -250,7 +250,7 @@ module Reweave
250
250
  # Block parameters
251
251
  def visit_block_parameters_node(node)
252
252
  if node.parameters
253
- params = node.parameters.parameters
253
+ params = node.parameters
254
254
  params.requireds.each do |param|
255
255
  param_name = param.name.to_sym
256
256
  @local_variables[param_name] = TypeFactory.unknown
@@ -322,17 +322,19 @@ module Reweave
322
322
  TypeFactory.unknown
323
323
  end
324
324
  when Prism::IfNode
325
- # Union of both branches
326
- then_type = node.then_clause ? infer_type(node.then_clause) : TypeFactory::NIL_CLASS
327
- else_type = node.else_clause ? infer_type(node.else_clause) : TypeFactory::NIL_CLASS
325
+ # Union of both branches. Prism stores the then-branch in `statements`
326
+ # (a StatementsNode) and the else/elsif branch in `subsequent`.
327
+ then_type = branch_result_type(node.statements)
328
+ else_type = branch_result_type(node.subsequent)
328
329
  if then_type == else_type
329
330
  then_type
330
331
  else
331
332
  TypeFactory.union(then_type, else_type)
332
333
  end
333
334
  when Prism::CaseNode
334
- # Union of all when branches
335
- branch_types = node.when_clauses.map { |wc| infer_type(wc.body) }
335
+ # Union of all when branches. Prism names these `conditions`.
336
+ branch_types = node.conditions.map { |condition| branch_result_type(condition.statements) }
337
+ branch_types << branch_result_type(node.else_clause&.statements) if node.else_clause
336
338
  if branch_types.uniq.size == 1
337
339
  branch_types.first
338
340
  else
@@ -343,6 +345,23 @@ module Reweave
343
345
  end
344
346
  end
345
347
 
348
+ # Infer the result type of a branch body. Prism wraps branches in
349
+ # StatementsNode; `subsequent` may be an ElseNode or a nested IfNode (elsif).
350
+ def branch_result_type(node)
351
+ return TypeFactory::NIL_CLASS unless node
352
+
353
+ case node
354
+ when Prism::StatementsNode
355
+ node.body.empty? ? TypeFactory::NIL_CLASS : infer_type(node.body.last)
356
+ when Prism::ElseNode
357
+ branch_result_type(node.statements)
358
+ when Prism::IfNode
359
+ infer_type(node)
360
+ else
361
+ infer_type(node)
362
+ end
363
+ end
364
+
346
365
  def infer_call_type(node)
347
366
  receiver_type = node.receiver ? infer_type(node.receiver) : TypeFactory.unknown
348
367
  method_name = node.name
@@ -419,12 +438,10 @@ module Reweave
419
438
  def infer_return_type(body)
420
439
  return nil unless body
421
440
 
422
- # Last expression in the body is the return value
423
- if body.is_a?(Prism::StatementsNode) && body.body.any?
424
- infer_type(body.body.last)
425
- elsif body.is_a?(Prism::CallNode) || body.is_a?(Prism::LiteralNode) || body.is_a?(Prism::VariableReadNode)
426
- infer_type(body)
427
- end
441
+ # Last expression in the body is the return value. `infer_type` already
442
+ # returns unknown for any node it does not recognize.
443
+ node = body.is_a?(Prism::StatementsNode) ? body.body.last : body
444
+ infer_type(node)
428
445
  end
429
446
 
430
447
  def infer_type_from_literal(node)
@@ -7,9 +7,10 @@ module Reweave
7
7
  class SourceFile
8
8
  attr_reader :path, :source, :parse_result, :root, :diagnostics, :encoding, :identity
9
9
 
10
- def initialize(path:, source: nil)
10
+ def initialize(path:, source: nil, root: ".")
11
11
  @path = path
12
- @source = source || File.read(path, encoding: "bom|utf-8")
12
+ abs_path = File.absolute_path?(path) ? path : File.join(root, path)
13
+ @source = source || File.read(abs_path, encoding: "bom|utf-8")
13
14
  @encoding = @source.encoding
14
15
  @parse_result = Parser.parse(@source, path: @path)
15
16
  @root = @parse_result.root
@@ -78,8 +79,8 @@ module Reweave
78
79
  @by_path = files.each_with_object({}) { |f, h| h[f.path] = f }
79
80
  end
80
81
 
81
- def self.from_paths(paths)
82
- new(paths.map { |p| SourceFile.new(path: p) })
82
+ def self.from_paths(paths, root: ".")
83
+ new(paths.map { |p| SourceFile.new(path: p, root: root) })
83
84
  end
84
85
 
85
86
  def each(&block)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Reweave
4
- VERSION = "1.0.0"
4
+ VERSION = "1.0.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reweave
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Reweave Contributors