lowkey 0.7.1 → 0.7.3

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: 5990301db8e248a6bc99c209512593b48d6e97c34070e8f9389b69648be06c2f
4
- data.tar.gz: 4d7ce17a69589f2103b03f446d583bfc128089fe4224c9f4fa91aaa87ac75fb6
3
+ metadata.gz: a3f67a40b7a6bac54260d413736dd90bc33a2c6f0bda31f80827236675d6ed05
4
+ data.tar.gz: 86a57542a05bd71806a9b1cb0e97cb1b399df974c5044cca6a03b86801be03ff
5
5
  SHA512:
6
- metadata.gz: 9feb9cc6d8e19325ada9fa42d043c3bcade1d3c6004f98c7c8febd74768c1dd84a822820edbfc82308409595322fb9c1d6c2c26d864c98431a535599cd23e826
7
- data.tar.gz: 10328e823b9ff040080a0671794887207e9458173fba13db61552755b88c3237cf9376950b961df17cf2ac3b1e774494f2cf00c8c81b025dfe73b20e4bcfc812
6
+ metadata.gz: ab5e11078fda54b8c9b7b47865b8a5881dc3f603ea30adb3d599ca1d2a93c88d594e83c05976d07cf73369d7ddea810e4c94e5c891f201f252f72abe0b7935eb
7
+ data.tar.gz: fb743d0414d28efd6cb7564322871cb6f08c802bed158020e41deb4b6bfd646c34652cb8d09c7c9a72feebfd90daa65d6892cf3f518b380d39a4050c7e19e9f1
@@ -20,7 +20,11 @@ module Lowkey
20
20
  arguments_node = method_call_node.compact_child_nodes.first
21
21
  next unless arguments_node.is_a?(Prism::ArgumentsNode)
22
22
 
23
- pattern = arguments_node.arguments.first.content
23
+ # A route's pattern is a string or regex, so calls like Net::HTTP.get(uri) aren't routes.
24
+ pattern_node = arguments_node.arguments.first
25
+ next unless pattern_node.respond_to?(:content)
26
+
27
+ pattern = pattern_node.content
24
28
  route = "#{method_call_node.name.upcase} #{pattern}"
25
29
  name = route
26
30
  source = SourceFactory.method_call_source(method_call_node:, arguments_node:, file_path:, lines:)
data/lib/lowkey.rb CHANGED
@@ -1,15 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'prism'
4
-
5
- require_relative 'adapters/adapter_loader'
6
- require_relative 'factories/proxy_factory'
7
- require_relative 'maps/parent_map'
8
- require_relative 'visitors/visitor'
9
- require_relative 'proxies/file_proxy'
10
3
  require_relative 'queries/value_parser'
11
4
 
12
5
  module Lowkey
6
+ # Loaded when first used, so reading values, like Antlers' templates do, loads only ValueParser.
7
+ { AdapterLoader: 'adapters/adapter_loader', SinatraAdapter: 'adapters/sinatra_adapter',
8
+ ProxyFactory: 'factories/proxy_factory', SourceFactory: 'factories/source_factory', Adapter: 'interfaces/adapter',
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',
11
+ ClassProxy: 'proxies/class_proxy', FileProxy: 'proxies/file_proxy', MethodCallProxy: 'proxies/method_call_proxy',
12
+ MethodProxy: 'proxies/method_proxy', ModuleProxy: 'proxies/module_proxy', ParamProxy: 'proxies/param_proxy',
13
+ ReturnProxy: 'proxies/return_proxy', Query: 'queries/query' }.each { |name, path| autoload(name, File.join(__dir__, path)) }
14
+
13
15
  NAMESPACE_PATTERN = /\A[A-Z]\w*(::[A-Z]\w*)*\z/
14
16
 
15
17
  class << self
@@ -25,6 +27,7 @@ module Lowkey
25
27
  end
26
28
 
27
29
  def load(file_path, cache: true, release_nodes: config.release_nodes)
30
+ load_prism
28
31
  root_node = Prism.parse(File.read(file_path), filepath: file_path).value
29
32
  file_proxy = ProxyFactory.file_proxy(root_node:, file_path:)
30
33
 
@@ -43,6 +46,15 @@ module Lowkey
43
46
  file_proxy
44
47
  end
45
48
 
49
+ # Prism takes a while to load, so it's loaded when a file or value is first parsed rather than with Lowkey.
50
+ def load_prism
51
+ return if @prism_loaded
52
+
53
+ require 'prism'
54
+ require_relative 'visitors/visitor' # A Prism::Visitor.
55
+ @prism_loaded = true
56
+ end
57
+
46
58
  def kept
47
59
  @kept ||= Set.new
48
60
  end
data/lib/queries/query.rb CHANGED
@@ -1,12 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'prism'
4
-
5
3
  module Lowkey
6
4
  module Query
7
- # Prism 2 renamed MissingNode to ErrorRecoveryNode.
8
- MISSING_NODE = defined?(Prism::ErrorRecoveryNode) ? Prism::ErrorRecoveryNode : Prism::MissingNode
9
-
10
5
  # TODO: Make name a keypath.
11
6
  def query(node:, namespace:, name:) # rubocop:disable Lint/UnusedMethodArgument
12
7
  node.breadth_first_search do |n|
@@ -33,7 +28,7 @@ module Lowkey
33
28
 
34
29
  def resolve_namespace_path(node:, parent_map:)
35
30
  # When inside RBX, Prism thinks we're defining a class because of "<".
36
- return nil if node.respond_to?(:constant_path) && node.constant_path.is_a?(MISSING_NODE)
31
+ return nil if node.respond_to?(:constant_path) && node.constant_path.is_a?(missing_node)
37
32
  return '' if (parent = parent_map[node]).nil?
38
33
 
39
34
  path = namespace_path(node: parent, parent_map:)
@@ -42,5 +37,10 @@ module Lowkey
42
37
  name = node.constant_path.name.to_s
43
38
  (path.empty? ? name : "#{path}::#{name}").freeze
44
39
  end
40
+
41
+ # Prism 2 renamed MissingNode to ErrorRecoveryNode. A method so Prism isn't needed until a file is parsed.
42
+ def missing_node
43
+ defined?(Prism::ErrorRecoveryNode) ? Prism::ErrorRecoveryNode : Prism::MissingNode
44
+ end
45
45
  end
46
46
  end
@@ -1,30 +1,103 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'prism'
4
-
5
3
  module Lowkey
6
4
  # Reads Ruby values like nil, true, 'text', 3, -1.5 or :symbol from Prism nodes or text, without evaluating any code.
7
5
  # Ignores potentially malicious values like variables, method calls or interpolated strings.
8
6
  module ValueParser
9
- VALUE_NODES = [
10
- Prism::NilNode,
11
- Prism::TrueNode,
12
- Prism::FalseNode,
13
- Prism::IntegerNode,
14
- Prism::FloatNode,
15
- Prism::StringNode,
16
- Prism::SymbolNode
17
- ].freeze
18
-
19
- VALUE_KEYWORDS = %w[nil true false].freeze
7
+ KEYWORDS = { 'nil' => nil, 'true' => true, 'false' => false }.freeze
8
+ VALUE_KEYWORDS = KEYWORDS.keys.freeze
20
9
  VALUE_STARTS = ["'", '"', ':', '-', *'0'..'9'].freeze
21
10
 
11
+ NUMBER = /\A-?\d/
12
+ FLOAT = /\A-?(?:0|[1-9][\d_]*)(?:\.\d[\d_]*)?(?:[eE][-+]?\d+)?\z/
13
+ SINGLE_QUOTED = /\A'((?:[^'\\]|\\.)*)'\z/m
14
+ DOUBLE_QUOTED = /\A"((?:[^"\\]|\\.)*)"\z/m
15
+ # #{}, #@ and #$ interpolate, unless escaped.
16
+ INTERPOLATION = /(?<!\\)(?:\\\\)*\#[{@$]/
17
+ # A double-quoted string's escapes: Unicode, hex, octal, then a character.
18
+ ESCAPE = /\\(u\{[\h ]+\}|u\h{4}|x\h{1,2}|[0-7]{1,3}|.)/m
19
+ ESCAPES = { 'n' => "\n", 't' => "\t", 'r' => "\r", 's' => ' ', 'e' => "\e", 'a' => "\a", 'b' => "\b", 'f' => "\f",
20
+ 'v' => "\v", "\n" => '' }.freeze
21
+ SYMBOL = %r{\A:([A-Za-z_]\w*[?!=]?|\[\]=?|\*\*|[-+]@|<=>|===?|=~|![=~]?|<[<=]?|>[>=]?|[-+*/%&|^~])\z}
22
+
22
23
  module_function
23
24
 
24
- # Returns the AST node's value or yields a block.
25
+ # Returns the value, or yields a block when the input isn't a value. It's read as Ruby reads it, without Prism,
26
+ # which only parses files, so reading a value never loads it.
25
27
  def parse(input)
26
28
  return yield(input) unless possible_value?(input)
27
29
 
30
+ text = input.strip
31
+ return KEYWORDS[text] if KEYWORDS.key?(text)
32
+
33
+ found = literal(text)
34
+ return yield(input) if found.empty?
35
+
36
+ found.first
37
+ end
38
+
39
+ # The literal's value in an array, or an empty one when it isn't one: a number, a string or a symbol.
40
+ def literal(text)
41
+ return number(text) if text.match?(NUMBER)
42
+ return [single_quoted(text)].compact if text.start_with?("'")
43
+ return [double_quoted(text)].compact if text.start_with?('"')
44
+
45
+ symbol(text)
46
+ end
47
+
48
+ # Integers as Ruby writes them, like 1_000, 0x1F or 007, then floats, like 1.5e-3.
49
+ def number(text)
50
+ [Integer(text)]
51
+ rescue ArgumentError
52
+ float(text)
53
+ end
54
+
55
+ def float(text)
56
+ return [] unless text.match?(FLOAT)
57
+
58
+ [Float(text)]
59
+ rescue ArgumentError
60
+ []
61
+ end
62
+
63
+ # Only \\ and \' are escapes in single quotes.
64
+ def single_quoted(text)
65
+ text[SINGLE_QUOTED, 1]&.gsub(/\\([\\'])/, '\1')
66
+ end
67
+
68
+ # With its escapes, like \t, \u00e9 or \101, and nil for an interpolation, or a control escape, like \C-a.
69
+ def double_quoted(text)
70
+ body = text[DOUBLE_QUOTED, 1]
71
+ return if body.nil? || body.match?(INTERPOLATION) || body.match?(/\\[cCM]/)
72
+
73
+ body.gsub(ESCAPE) { escaped(Regexp.last_match(1)) }
74
+ end
75
+
76
+ def escaped(escape)
77
+ return unicode(escape[1..]) if escape.match?(/\Au./)
78
+ return escape[1..].hex.chr if escape.match?(/\Ax./)
79
+ return escape.oct.chr if escape.match?(/\A[0-7]/)
80
+
81
+ ESCAPES.fetch(escape, escape)
82
+ end
83
+
84
+ # Like 00e9, or {1F600 1F601}, as UTF-8.
85
+ def unicode(codes)
86
+ codes.delete('{}').split.map { it.hex.chr(Encoding::UTF_8) }.join
87
+ end
88
+
89
+ def symbol(text)
90
+ return [text[SYMBOL, 1].to_sym] if text.match?(SYMBOL)
91
+
92
+ quoted = double_quoted(text.delete_prefix(':')) if text.start_with?(':"')
93
+ return [quoted.to_sym] if quoted
94
+
95
+ []
96
+ end
97
+
98
+ # Prism's reading of the text, which parse reads as, for checking it against.
99
+ def prism_parse(input)
100
+ Lowkey.load_prism
28
101
  result = Prism.parse(input)
29
102
  statements = result.value.statements.body
30
103
  return yield(input) unless result.errors.empty? && statements.one? && value?(statements.first)
@@ -38,7 +111,20 @@ module Lowkey
38
111
  end
39
112
 
40
113
  def value?(node)
41
- VALUE_NODES.include?(node.class)
114
+ value_nodes.include?(node.class)
115
+ end
116
+
117
+ # Prism's value nodes, once Prism has loaded.
118
+ def value_nodes
119
+ @value_nodes ||= [
120
+ Prism::NilNode,
121
+ Prism::TrueNode,
122
+ Prism::FalseNode,
123
+ Prism::IntegerNode,
124
+ Prism::FloatNode,
125
+ Prism::StringNode,
126
+ Prism::SymbolNode
127
+ ].freeze
42
128
  end
43
129
 
44
130
  # Returns the AST node's value or yields a block.
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.1'
4
+ VERSION = '0.7.3'
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.1
4
+ version: 0.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi
@@ -76,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
78
  requirements: []
79
- rubygems_version: 4.0.6
79
+ rubygems_version: 4.0.20
80
80
  specification_version: 4
81
81
  summary: An API for accessing PRISM metadata easily and efficiently
82
82
  test_files: []