actionview_precompiler 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 48e7feef8ebfac2d8a2d18188dcfe98ce8e156e84567a8c632ebd713facb9a6e
4
- data.tar.gz: 4b5efa2f1ce1a221bbf1f1729ec4548a0dc6250a4086a5e5dc1e1ef485cd59aa
3
+ metadata.gz: 58e32739b081a1318cdd69ea8bb88286f7921c1028e3081738ecda419d68b25f
4
+ data.tar.gz: 6309ed0e14d1261ea9f184c8f0522d6cf38b1c3a045a09537f01b8a13841bcae
5
5
  SHA512:
6
- metadata.gz: 84eccaa58fb87a5a9dd06183e9f2fda66e258eaeaf57e0abbe27a4e8eed691e7cf1981a3d0ab4e55dbe6e9df841ea7f75ab924938ab8458ac8c7a2068ffa8c8f
7
- data.tar.gz: bc95591c0489a70a3c1de6975c3ab0deb7890ee7d0ce4be018b4283098d3dabcb65eddaca299e50220673dbdb425d795ce20fabec178a5f20a6a6021038766fe
6
+ metadata.gz: f4476e8391491ca650bb1dbf3f946160c631a506a9abe7f20760e07ba6e1c94229e443579d7961b17301a6487a819d0c942f1c0261040b6110db45a6d3fda0ca
7
+ data.tar.gz: d7abd504555763b20cfbbd9afec57a67a1caf032fb1a4e3f571ca9d12f8fc58e72570c2437e42f26ec7e3f6d746cc383a35a90b38dc0758f4a0a3f919a37023f
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "syntax_tree"
3
+ require "prism"
4
4
 
5
5
  module ActionviewPrecompiler
6
- module SyntaxTreeASTParser
6
+ module PrismASTParser
7
7
  # This error is raised whenever an assumption we made wasn't met by the AST.
8
8
  class CompilationError < StandardError
9
9
  end
@@ -30,75 +30,79 @@ module ActionviewPrecompiler
30
30
  end
31
31
 
32
32
  def call?
33
- node.is_a?(SyntaxTree::Call)
33
+ node.is_a?(Prism::CallNode)
34
34
  end
35
35
 
36
36
  def hash?
37
- node.is_a?(SyntaxTree::HashLiteral) || node.is_a?(SyntaxTree::BareAssocHash)
37
+ node.is_a?(Prism::HashNode) || node.is_a?(Prism::KeywordHashNode)
38
38
  end
39
39
 
40
40
  def string?
41
- node.is_a?(SyntaxTree::StringLiteral)
41
+ node.is_a?(Prism::StringNode)
42
42
  end
43
43
 
44
44
  def symbol?
45
- node.is_a?(SyntaxTree::Label) || node.is_a?(SyntaxTree::SymbolLiteral)
45
+ node.is_a?(Prism::SymbolNode)
46
46
  end
47
47
 
48
48
  def variable_reference?
49
- node.is_a?(SyntaxTree::VarRef)
49
+ case node.type
50
+ when :class_variable_read_node, :instance_variable_read_node,
51
+ :global_variable_read_node, :local_variable_read_node
52
+ true
53
+ else
54
+ false
55
+ end
50
56
  end
51
57
 
52
58
  def vcall?
53
- node.is_a?(SyntaxTree::VCall)
59
+ node.is_a?(Prism::CallNode) && node.variable_call?
60
+ end
61
+
62
+ def call_method_name
63
+ node.message
64
+ end
65
+
66
+ def variable_name
67
+ case node.type
68
+ when :class_variable_read_node, :instance_variable_read_node,
69
+ :global_variable_read_node, :local_variable_read_node
70
+ node.name.name
71
+ when :call_node
72
+ node.message
73
+ end
54
74
  end
55
75
 
56
76
  # Converts the node into a hash where the keys and values are nodes. This
57
77
  # will raise an error if the hash doesn't match the format we expect or
58
78
  # if the hash contains any splats.
59
79
  def to_hash
60
- case node
61
- in SyntaxTree::HashLiteral[assocs:]
62
- in SyntaxTree::BareAssocHash[assocs:]
63
- else
64
- raise CompilationError, "Unexpected node type: #{node.class.name}"
65
- end
66
-
67
- assocs.to_h do |assoc|
68
- case assoc
69
- in SyntaxTree::Assoc[key:, value:]
70
- [RenderNode.new(key), RenderNode.new(value)]
71
- else
72
- raise CompilationError, "Unexpected node type: #{node.class.name}"
80
+ if hash?
81
+ if node.elements.all? { |assoc| assoc.is_a?(Prism::AssocNode) && assoc.key.is_a?(Prism::SymbolNode) }
82
+ node.elements.to_h { |assoc| [RenderNode.new(assoc.key), RenderNode.new(assoc.value)] }
73
83
  end
84
+ else
85
+ raise CompilationError, "Unexpected node type: #{node.inspect}"
74
86
  end
75
87
  end
76
88
 
77
89
  # Converts the node into a string value. Only handles plain string
78
90
  # content, and will raise an error if the node contains interpolation.
79
91
  def to_string
80
- case node
81
- in SyntaxTree::StringLiteral[parts: [SyntaxTree::TStringContent[value:]]]
82
- value
83
- in SyntaxTree::StringLiteral[parts:]
84
- raise CompilationError, "Unexpected string parts type: #{parts.inspect}"
92
+ if string?
93
+ node.unescaped
85
94
  else
86
- raise CompilationError, "Unexpected node type: #{node.class.name}"
95
+ raise CompilationError, "Unexpected node type: #{node.inspect}"
87
96
  end
88
97
  end
89
98
 
90
99
  # Converts the node into a symbol value. Only handles labels and plain
91
100
  # symbols, and will raise an error if the node contains interpolation.
92
101
  def to_symbol
93
- case node
94
- in SyntaxTree::Label[value:]
95
- value.chomp(":").to_sym
96
- in SyntaxTree::SymbolLiteral[value: SyntaxTree::Ident[value:]]
97
- value.to_sym
98
- in SyntaxTree::SymbolLiteral[value:]
99
- raise CompilationError, "Unexpected symbol value type: #{value.inspect}"
102
+ if symbol?
103
+ node.unescaped.to_sym
100
104
  else
101
- raise CompilationError, "Unexpected node type: #{node.class.name}"
105
+ raise CompilationError, "Unexpected node type: #{node.inspect}"
102
106
  end
103
107
  end
104
108
  end
@@ -106,7 +110,7 @@ module ActionviewPrecompiler
106
110
  # This visitor is responsible for visiting the parsed tree and extracting
107
111
  # out the render calls. After visiting the tree, the #render_calls method
108
112
  # will return the hash expected by the #parse_render_nodes method.
109
- class RenderVisitor < SyntaxTree::Visitor
113
+ class RenderVisitor < Prism::Visitor
110
114
  MESSAGE = /\A(render|render_to_string|layout)\z/
111
115
 
112
116
  attr_reader :render_calls
@@ -115,29 +119,18 @@ module ActionviewPrecompiler
115
119
  @render_calls = Hash.new { |hash, key| hash[key] = [] }
116
120
  end
117
121
 
118
- visit_method def visit_command(node)
119
- case node
120
- in SyntaxTree::Command[
121
- message: SyntaxTree::Ident[value: MESSAGE],
122
- arguments: SyntaxTree::Args[parts:]
123
- ]
124
- argument_nodes = parts.map { |part| RenderNode.new(part) }
125
- render_calls[$1.to_sym] << RenderCall.new(argument_nodes)
126
- else
127
- end
128
-
129
- super
130
- end
131
-
132
- visit_method def visit_fcall(node)
133
- case node
134
- in SyntaxTree::FCall[
135
- value: SyntaxTree::Ident[value: MESSAGE],
136
- arguments: SyntaxTree::ArgParen[arguments: SyntaxTree::Args[parts:]]
137
- ]
138
- argument_nodes = parts.map { |part| RenderNode.new(part) }
139
- render_calls[$1.to_sym] << RenderCall.new(argument_nodes)
140
- else
122
+ def visit_call_node(node)
123
+ if node.name.match?(MESSAGE) && !node.receiver && node.arguments
124
+ args =
125
+ node.arguments.arguments.map do |arg|
126
+ if arg.is_a?(Prism::ParenthesesNode) && arg.body && arg.body.body.length == 1
127
+ RenderNode.new(arg.body.body.first)
128
+ else
129
+ RenderNode.new(arg)
130
+ end
131
+ end
132
+
133
+ render_calls[node.name.to_sym] << RenderCall.new(args)
141
134
  end
142
135
 
143
136
  super
@@ -149,10 +142,14 @@ module ActionviewPrecompiler
149
142
  # values are arrays of call objects.
150
143
  def self.parse_render_nodes(code)
151
144
  visitor = RenderVisitor.new
152
- SyntaxTree.parse(code).accept(visitor)
153
- visitor.render_calls
154
- rescue SyntaxTree::Parser::ParseError
155
- raise CompilationError, "Unable to parse the template"
145
+ result = Prism.parse(code)
146
+
147
+ if result.success?
148
+ result.value.accept(visitor)
149
+ visitor.render_calls
150
+ else
151
+ raise CompilationError, "Unable to parse the template"
152
+ end
156
153
  end
157
154
  end
158
155
  end
@@ -184,7 +184,11 @@ module ActionviewPrecompiler
184
184
  end
185
185
 
186
186
  def on_paren(content)
187
- content
187
+ if (content.size == 1) && (content.is_a?(Array))
188
+ content.first
189
+ else
190
+ content
191
+ end
188
192
  end
189
193
  end
190
194
 
@@ -74,7 +74,7 @@ module ActionviewPrecompiler
74
74
  end
75
75
 
76
76
  def symbol?
77
- type == :LIT && Symbol === children[0]
77
+ (type == :SYM) || (type == :LIT && Symbol === children[0])
78
78
  end
79
79
 
80
80
  def to_hash
@@ -1,7 +1,13 @@
1
1
  module ActionviewPrecompiler
2
2
  parser = ENV["PRECOMPILER_PARSER"]
3
- parser ||= "jruby" if RUBY_ENGINE == 'jruby'
4
- parser ||= "rubyvm_ast" if RUBY_ENGINE == 'ruby'
3
+
4
+ begin
5
+ require "prism"
6
+ parser ||= "prism"
7
+ rescue LoadError
8
+ parser ||= "jruby" if RUBY_ENGINE == 'jruby'
9
+ parser ||= "rubyvm_ast" if RUBY_ENGINE == 'ruby'
10
+ end
5
11
 
6
12
  case parser
7
13
  when "rubyvm_ast"
@@ -10,9 +16,9 @@ module ActionviewPrecompiler
10
16
  when "jruby"
11
17
  require "actionview_precompiler/ast_parser/jruby"
12
18
  ASTParser = JRubyASTParser
13
- when "syntax_tree"
14
- require "actionview_precompiler/ast_parser/syntax_tree"
15
- ASTParser = SyntaxTreeASTParser
19
+ when "prism"
20
+ require "actionview_precompiler/ast_parser/prism"
21
+ ASTParser = PrismASTParser
16
22
  else
17
23
  require "actionview_precompiler/ast_parser/ripper"
18
24
  ASTParser = RipperASTParser
@@ -128,6 +128,7 @@ module ActionviewPrecompiler
128
128
  dependency = node.variable_name
129
129
  elsif node.call?
130
130
  dependency = node.call_method_name
131
+ return unless dependency.is_a? String
131
132
  else
132
133
  return
133
134
  end
@@ -29,6 +29,7 @@ module ActionviewPrecompiler
29
29
  src = File.read(@filename)
30
30
  if src.include?("render")
31
31
  compiled_source = @handler.call(FakeTemplate.new, File.read(@filename))
32
+ compiled_source = "def _template; #{compiled_source}; end"
32
33
  RenderParser.new(compiled_source).render_calls
33
34
  else
34
35
  []
@@ -1,3 +1,3 @@
1
1
  module ActionviewPrecompiler
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: actionview_precompiler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Hawthorn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-24 00:00:00.000000000 Z
11
+ date: 2024-02-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionview
@@ -77,9 +77,9 @@ files:
77
77
  - lib/actionview_precompiler.rb
78
78
  - lib/actionview_precompiler/ast_parser.rb
79
79
  - lib/actionview_precompiler/ast_parser/jruby.rb
80
+ - lib/actionview_precompiler/ast_parser/prism.rb
80
81
  - lib/actionview_precompiler/ast_parser/ripper.rb
81
82
  - lib/actionview_precompiler/ast_parser/ruby26.rb
82
- - lib/actionview_precompiler/ast_parser/syntax_tree.rb
83
83
  - lib/actionview_precompiler/controller_parser.rb
84
84
  - lib/actionview_precompiler/controller_scanner.rb
85
85
  - lib/actionview_precompiler/helper_parser.rb
@@ -111,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
111
  - !ruby/object:Gem::Version
112
112
  version: '0'
113
113
  requirements: []
114
- rubygems_version: 3.4.10
114
+ rubygems_version: 3.5.3
115
115
  signing_key:
116
116
  specification_version: 4
117
117
  summary: Precompiles ActionView templates