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 +4 -4
- data/lib/actionview_precompiler/ast_parser/{syntax_tree.rb → prism.rb} +60 -63
- data/lib/actionview_precompiler/ast_parser/ripper.rb +5 -1
- data/lib/actionview_precompiler/ast_parser/ruby26.rb +1 -1
- data/lib/actionview_precompiler/ast_parser.rb +11 -5
- data/lib/actionview_precompiler/render_parser.rb +1 -0
- data/lib/actionview_precompiler/template_parser.rb +1 -0
- data/lib/actionview_precompiler/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58e32739b081a1318cdd69ea8bb88286f7921c1028e3081738ecda419d68b25f
|
4
|
+
data.tar.gz: 6309ed0e14d1261ea9f184c8f0522d6cf38b1c3a045a09537f01b8a13841bcae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f4476e8391491ca650bb1dbf3f946160c631a506a9abe7f20760e07ba6e1c94229e443579d7961b17301a6487a819d0c942f1c0261040b6110db45a6d3fda0ca
|
7
|
+
data.tar.gz: d7abd504555763b20cfbbd9afec57a67a1caf032fb1a4e3f571ca9d12f8fc58e72570c2437e42f26ec7e3f6d746cc383a35a90b38dc0758f4a0a3f919a37023f
|
@@ -1,9 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "
|
3
|
+
require "prism"
|
4
4
|
|
5
5
|
module ActionviewPrecompiler
|
6
|
-
module
|
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?(
|
33
|
+
node.is_a?(Prism::CallNode)
|
34
34
|
end
|
35
35
|
|
36
36
|
def hash?
|
37
|
-
node.is_a?(
|
37
|
+
node.is_a?(Prism::HashNode) || node.is_a?(Prism::KeywordHashNode)
|
38
38
|
end
|
39
39
|
|
40
40
|
def string?
|
41
|
-
node.is_a?(
|
41
|
+
node.is_a?(Prism::StringNode)
|
42
42
|
end
|
43
43
|
|
44
44
|
def symbol?
|
45
|
-
node.is_a?(
|
45
|
+
node.is_a?(Prism::SymbolNode)
|
46
46
|
end
|
47
47
|
|
48
48
|
def variable_reference?
|
49
|
-
node.
|
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?(
|
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
|
-
|
61
|
-
|
62
|
-
|
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
|
-
|
81
|
-
|
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.
|
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
|
-
|
94
|
-
|
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.
|
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 <
|
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
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
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
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
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
|
@@ -1,7 +1,13 @@
|
|
1
1
|
module ActionviewPrecompiler
|
2
2
|
parser = ENV["PRECOMPILER_PARSER"]
|
3
|
-
|
4
|
-
|
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 "
|
14
|
-
require "actionview_precompiler/ast_parser/
|
15
|
-
ASTParser =
|
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
|
@@ -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
|
[]
|
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.
|
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:
|
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.
|
114
|
+
rubygems_version: 3.5.3
|
115
115
|
signing_key:
|
116
116
|
specification_version: 4
|
117
117
|
summary: Precompiles ActionView templates
|