Class: Opal::Nodes::RegexpNode

Inherits:
Base
  • Object
show all
Defined in:
lib/opal/nodes/literal.rb

Constant Summary collapse

SUPPORTED_FLAGS =
/[gimuy]/.freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#compiler, #sexp, #type

Attributes included from Closure::NodeSupport

#closure

Instance Method Summary collapse

Methods inherited from Base

#add_gvar, #add_ivar, #add_local, #add_temp, children, #children, #class_variable_owner, #class_variable_owner_nesting_level, #comments, #compile_to_fragments, #error, #expr, #expr?, #expr_or_empty, #expr_or_nil, #fragment, handle, handlers, #has_rescue_else?, #helper, #in_ensure, #in_ensure?, #in_resbody, #in_resbody?, #in_rescue, #in_while?, #process, #push, #recv, #recv?, #s, #scope, #source_location, #stmt, #stmt?, #top_scope, truthy_optimize?, #unshift, #while_loop, #with_temp, #wrap

Methods included from Closure::NodeSupport

#closure_is?, #compile_catcher, #generate_thrower, #generate_thrower_without_catcher, #in_closure, #pop_closure, #push_closure, #select_closure, #thrower

Methods included from Helpers

#current_indent, #empty_line, #indent, #js_truthy, #js_truthy_optimize, #line, #mid_to_jsid, #property, #valid_name?

Constructor Details

#initializeRegexpNode

Returns a new instance of RegexpNode.



125
126
127
128
# File 'lib/opal/nodes/literal.rb', line 125

def initialize(*)
  super
  extract_flags_and_value
end

Instance Attribute Details

#flagsObject

Returns the value of attribute flags.



120
121
122
# File 'lib/opal/nodes/literal.rb', line 120

def flags
  @flags
end

#valueObject

Returns the value of attribute value.



120
121
122
# File 'lib/opal/nodes/literal.rb', line 120

def value
  @value
end

Instance Method Details

#compileObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/opal/nodes/literal.rb', line 130

def compile
  flags.select! do |flag|
    if SUPPORTED_FLAGS =~ flag
      true
    else
      compiler.warning "Skipping the '#{flag}' Regexp flag as it's not widely supported by JavaScript vendors.", @sexp.line
      false
    end
  end

  if value.type == :str
    compile_static_regexp
  else
    compile_dynamic_regexp
  end
end

#compile_dynamic_regexpObject



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/opal/nodes/literal.rb', line 147

def compile_dynamic_regexp
  helper :regexp

  push '$regexp(['
  value.children.each_with_index do |v, index|
    push ', ' unless index.zero?
    push expr(v)
  end
  push ']'
  push ", '#{flags.join}'" if flags.any?
  push ")"
end

#compile_static_regexpObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/opal/nodes/literal.rb', line 168

def compile_static_regexp
  value = self.value.children[0]
  # homura patch: upstream normalises `\A` to `^` (via the parser gem)
  # but leaves `\Z` / `\z` as the literal two-character escape, which
  # JavaScript regex engines then treat as the literal letter `Z` / `z`
  # at the end. Ruby semantics map `\z` (strict end) and `\Z` (end or
  # just before trailing newline) to JavaScript's `$`. Mustermann's
  # `/\A...\Z/` patterns depend on this — without it, every Sinatra
  # route fails to match.
  value = normalize_ruby_regex_anchors(value)
  case value
  when ''
    push('/(?:)/')
  when /\(\?[(<>#]|[*+?]\+/
    # Safari/WebKit will not execute javascript code if it contains a lookbehind literal RegExp
    # and they fail with "Syntax Error". This tricks their parser by disguising the literal RegExp
    # as string for the dynamic $regexp helper. Safari/Webkit will still fail to execute the RegExp,
    # but at least they will parse and run everything else.
    #
    # Also, let's compile a couple of more patterns into $regexp calls, as there are many syntax
    # errors in RubySpec when ran in reverse, while there shouldn't be (they should be catchable
    # errors) - at least since Node 17.
    static_as_dynamic(value)
  else
    push "#{Regexp.new(value).inspect}#{flags.join}"
  end
end

#extract_flags_and_valueObject



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/opal/nodes/literal.rb', line 206

def extract_flags_and_value
  *values, flags_sexp = *children
  self.flags = flags_sexp.children.map(&:to_s)

  self.value = if values.empty?
                 # empty regexp, we can process it inline
                 s(:str, '')
               elsif single_line?(values)
                 # simple plain regexp, we can put it inline
                 values[0]
               else
                 s(:dstr, *values)
               end

  # trimming when //x provided
  # required by parser gem, but JS doesn't support 'x' flag
  if flags.include?('x')
    parts = value.children.map do |part|
      if part.is_a?(::Opal::AST::Node) && part.type == :str
        trimmed_value = part.children[0].gsub(/\A\s*\#.*/, '').gsub(/\s/, '')
        s(:str, trimmed_value)
      else
        part
      end
    end

    self.value = value.updated(nil, parts)
    flags.delete('x')
  end

  if value.type == :str
    # Replacing \A -> ^, \z -> $, required for the parser gem.
    # homura patch: also rewrite \Z (end-or-trailing-newline) to $,
    # matching Ruby semantics closely enough for real-world regexes
    # (Mustermann's route patterns in particular).
    self.value = s(:str, value.children[0].gsub('\A', '^').gsub('\z', '$').gsub('\Z', '$'))
  elsif value.type == :dstr
    # homura patch: interpolated regex literals (`/\A#{inner}\Z/`,
    # produced heavily by Mustermann's RegexpBased#initialize) are
    # compiled via `compile_dynamic_regexp`, which concatenates the
    # children verbatim into a JavaScript `new RegExp(...)` call.
    # Without this normalisation the literal two-character `\A`/`\Z`
    # sequences reach V8, which does not know those Ruby anchors and
    # silently interprets them as the letters `A`/`Z`. Every Sinatra
    # route pattern then fails to match its own path.
    parts = value.children.map do |part|
      if part.is_a?(::Opal::AST::Node) && part.type == :str
        s(:str, part.children[0].gsub('\A', '^').gsub('\z', '$').gsub('\Z', '$'))
      else
        part
      end
    end
    self.value = value.updated(nil, parts)
  end
end

#normalize_ruby_regex_anchors(str) ⇒ Object

homura patch: replace Ruby's \Z / \z end-of-string anchors with JavaScript's $. Keep the match spec-compatible for the common "end of input" case Mustermann relies on.



163
164
165
166
# File 'lib/opal/nodes/literal.rb', line 163

def normalize_ruby_regex_anchors(str)
  return str unless str.is_a?(String)
  str.gsub(/(?<!\\)((?:\\\\)*)\\([Zz])/) { "#{$1}$" }
end

#raw_valueObject



262
263
264
# File 'lib/opal/nodes/literal.rb', line 262

def raw_value
  self.value = @sexp.loc.expression.source
end

#static_as_dynamic(value) ⇒ Object



196
197
198
199
200
201
202
203
204
# File 'lib/opal/nodes/literal.rb', line 196

def static_as_dynamic(value)
  helper :regexp

  push '$regexp(["'
  push value.gsub('\\', '\\\\\\\\')
  push '"]'
  push ", '#{flags.join}'" if flags.any?
  push ")"
end