lowkey 0.7.3 → 0.7.4

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: a3f67a40b7a6bac54260d413736dd90bc33a2c6f0bda31f80827236675d6ed05
4
- data.tar.gz: 86a57542a05bd71806a9b1cb0e97cb1b399df974c5044cca6a03b86801be03ff
3
+ metadata.gz: 68c11d0bf42929bb136e505d079922296c220680cce1412729ca336d968413d4
4
+ data.tar.gz: 23a78b8a98d7ad37d2a5ec7e202ddf6707c6527b83f2a98e851b60e99376bb6a
5
5
  SHA512:
6
- metadata.gz: ab5e11078fda54b8c9b7b47865b8a5881dc3f603ea30adb3d599ca1d2a93c88d594e83c05976d07cf73369d7ddea810e4c94e5c891f201f252f72abe0b7935eb
7
- data.tar.gz: fb743d0414d28efd6cb7564322871cb6f08c802bed158020e41deb4b6bfd646c34652cb8d09c7c9a72feebfd90daa65d6892cf3f518b380d39a4050c7e19e9f1
6
+ metadata.gz: db2f55d46929f94e7feb221a250092184706f824cbcb87dfbfac96c2b9206d3719126e6468ba5d9e43dff4250c6d3ab075e379619b14faff10c0f5aaa8c0ae97
7
+ data.tar.gz: d0b993346e45f61ed66c008c6a3a2dfa400166e3993d7cc2b0ab124c9932153fb2a3d0d9fc982d4aaf07e9fbe4fcfe50f591542f67e225e226040acc69f12f2a
@@ -14,8 +14,8 @@ require_relative '../proxies/return_proxy'
14
14
  module Lowkey
15
15
  class ProxyFactory
16
16
  class << self
17
- def file_proxy(root_node:, file_path:)
18
- source = SourceFactory.file_source(root_node:, file_path:)
17
+ def file_proxy(root_node:, file_path:, lines:)
18
+ source = SourceFactory.file_source(root_node:, file_path:, lines:)
19
19
  FileProxy.new(root_node:, source:)
20
20
  end
21
21
 
@@ -33,7 +33,7 @@ module Lowkey
33
33
  def method_proxy(method_node:, file_proxy:)
34
34
  name = method_node.name
35
35
 
36
- method_source = SourceFactory.method_source(method_node:, file_path: file_proxy.file_path, lines: file_proxy.lines)
36
+ method_source = SourceFactory.method_source(method_node:, file_proxy:)
37
37
  body_source = SourceFactory.body_source(method_source:)
38
38
 
39
39
  param_proxies = param_proxies(parameters_node: method_node.parameters, file_path: file_proxy.file_path, source: method_source)
@@ -5,10 +5,11 @@ require_relative '../models/source'
5
5
  module Lowkey
6
6
  class SourceFactory
7
7
  class << self
8
- def file_source(root_node:, file_path:)
8
+ # The lines are the file's own, as Prism may have parsed it with its templates masked.
9
+ def file_source(root_node:, file_path:, lines:)
9
10
  end_line = root_node.respond_to?(:end_line) ? root_node.end_line : nil # TODO: Get file line count.
10
11
 
11
- Source.new(file_path:, scope: 'file', lines: root_node.script_lines, start_line: 1, end_line:)
12
+ Source.new(file_path:, scope: 'file', lines:, start_line: 1, end_line:)
12
13
  end
13
14
 
14
15
  def module_source(node:, namespace:, file_path:, lines:)
@@ -27,14 +28,14 @@ module Lowkey
27
28
  Source.new(file_path:, scope: node.name, lines:, start_line:, end_line:)
28
29
  end
29
30
 
30
- def method_source(method_node:, file_path:, lines:)
31
- scope = method_node.name
31
+ def method_source(method_node:, file_proxy:)
32
+ lines = file_proxy.lines
32
33
  start_line = method_node.start_line
33
34
  end_line = method_node.end_line
34
35
  # TODO: Brittle conditional. What we really want to identify is if the method/rest of file was able to be parsed into an AST.
35
36
  end_line = end_line_from_indent(method_node:, lines:) if end_line >= lines.count
36
37
 
37
- Source.new(file_path:, scope:, lines:, start_line:, end_line:, location: method_node.location)
38
+ Source.new(file_path: file_proxy.file_path, scope: method_node.name, lines:, start_line:, end_line:, location: method_node.location)
38
39
  end
39
40
 
40
41
  def param_source(param_node:, file_path:, lines:)
data/lib/lowkey.rb CHANGED
@@ -7,7 +7,8 @@ module Lowkey
7
7
  { AdapterLoader: 'adapters/adapter_loader', SinatraAdapter: 'adapters/sinatra_adapter',
8
8
  ProxyFactory: 'factories/proxy_factory', SourceFactory: 'factories/source_factory', Adapter: 'interfaces/adapter',
9
9
  Proxy: 'interfaces/proxy', ReleasableNode: 'interfaces/releasable_node', ReleasedNodeError: 'interfaces/releasable_node',
10
- ParentMap: 'maps/parent_map', Source: 'models/source', WriteError: 'models/source', BodyProxy: 'proxies/body_proxy',
10
+ ParentMap: 'maps/parent_map', MaskedSource: 'models/masked_source',
11
+ Source: 'models/source', WriteError: 'models/source', BodyProxy: 'proxies/body_proxy',
11
12
  ClassProxy: 'proxies/class_proxy', FileProxy: 'proxies/file_proxy', MethodCallProxy: 'proxies/method_call_proxy',
12
13
  MethodProxy: 'proxies/method_proxy', ModuleProxy: 'proxies/module_proxy', ParamProxy: 'proxies/param_proxy',
13
14
  ReturnProxy: 'proxies/return_proxy', Query: 'queries/query' }.each { |name, path| autoload(name, File.join(__dir__, path)) }
@@ -28,8 +29,8 @@ module Lowkey
28
29
 
29
30
  def load(file_path, cache: true, release_nodes: config.release_nodes)
30
31
  load_prism
31
- root_node = Prism.parse(File.read(file_path), filepath: file_path).value
32
- file_proxy = ProxyFactory.file_proxy(root_node:, file_path:)
32
+ root_node, lines = parse(file_path)
33
+ file_proxy = ProxyFactory.file_proxy(root_node:, file_path:, lines:)
33
34
 
34
35
  parent_map = ParentMap.new(root_node:)
35
36
  visitor = Visitor.new(file_proxy:, parent_map:)
@@ -46,6 +47,19 @@ module Lowkey
46
47
  file_proxy
47
48
  end
48
49
 
50
+ # Parses a file, and parses it again with the methods Prism couldn't parse masked, like an RBX file's templates, as
51
+ # Prism's guesses at where they end can run on through the rest of the file. Returns the file's own lines too.
52
+ def parse(file_path)
53
+ source = File.read(file_path)
54
+ result = Prism.parse(source, filepath: file_path)
55
+ unless result.errors.empty?
56
+ masked = MaskedSource.new(source:, error_lines: result.errors.map { it.location.start_line }).to_s
57
+ result = Prism.parse(masked, filepath: file_path)
58
+ end
59
+
60
+ [result.value, source.lines]
61
+ end
62
+
49
63
  # Prism takes a while to load, so it's loaded when a file or value is first parsed rather than with Lowkey.
50
64
  def load_prism
51
65
  return if @prism_loaded
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lowkey
4
+ # A file's source with the bodies of methods Prism couldn't parse, like an RBX file's templates, blanked out with
5
+ # spaces, so Prism parses the rest of the file. Each byte becomes a space and newlines stay, so lines, columns and
6
+ # offsets still point at the file itself.
7
+ class MaskedSource
8
+ DEF_PATTERN = /\A(\s*)def\b/
9
+
10
+ def initialize(source:, error_lines:)
11
+ @lines = source.lines
12
+ @error_lines = error_lines
13
+ end
14
+
15
+ def to_s
16
+ unparsed_bodies.each do |body|
17
+ body.each { |index| @lines[index] = @lines[index].b.gsub(/[^\n]/n, ' ') }
18
+ end
19
+
20
+ @lines.join
21
+ end
22
+
23
+ private
24
+
25
+ # The line ranges of the bodies of methods with an error in them.
26
+ def unparsed_bodies
27
+ # An error at the end of the file, like a missing "end", is on the line after its last.
28
+ @error_lines.filter_map { |line| body_around([line, @lines.count].min - 1) }.uniq
29
+ end
30
+
31
+ # The body of the method an error's in: from the line after its "def" to the line before its lined up "end".
32
+ def body_around(error_index)
33
+ def_index = error_index.downto(0).find { |index| @lines[index].match?(DEF_PATTERN) }
34
+ return unless def_index
35
+
36
+ indent = @lines[def_index][DEF_PATTERN, 1]
37
+ end_index = ((def_index + 1)...@lines.count).find { |index| @lines[index].match?(/\A#{indent}end\b/) }
38
+ return unless end_index && error_index.between?(def_index, end_index)
39
+
40
+ (def_index + 1)...end_index
41
+ end
42
+ end
43
+ end
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lowkey
4
- VERSION = '0.7.3'
4
+ VERSION = '0.7.4'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lowkey
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.3
4
+ version: 0.7.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi
@@ -40,6 +40,7 @@ files:
40
40
  - lib/interfaces/releasable_node.rb
41
41
  - lib/lowkey.rb
42
42
  - lib/maps/parent_map.rb
43
+ - lib/models/masked_source.rb
43
44
  - lib/models/source.rb
44
45
  - lib/proxies/body_proxy.rb
45
46
  - lib/proxies/class_proxy.rb