Class: Opal::Rewriters::JsReservedWords

Inherits:
Base
  • Object
show all
Defined in:
opal/lib/opal/rewriters/js_reserved_words.rb

Constant Summary collapse

ES51_RESERVED_WORD =

Reserved javascript keywords - we cannot create variables with the same name (ref: http://stackoverflow.com/a/9337272/601782)

/#{REGEXP_START}(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)#{REGEXP_END}/.freeze
ES3_RESERVED_WORD_EXCLUSIVE =

ES3 reserved words that aren’t ES5.1 reserved words

/#{REGEXP_START}(?:int|byte|char|goto|long|final|float|short|double|native|throws|boolean|abstract|volatile|transient|synchronized)#{REGEXP_END}/.freeze
PROTO_SPECIAL_PROPS =

Prototype special properties.

/#{REGEXP_START}(?:constructor|displayName|__proto__|__parent__|__noSuchMethod__|__count__)#{REGEXP_END}/.freeze
PROTO_SPECIAL_METHODS =

Prototype special methods.

/#{REGEXP_START}(?:hasOwnProperty|valueOf)#{REGEXP_END}/.freeze
IMMUTABLE_PROPS =

Immutable properties of the global object

/#{REGEXP_START}(?:NaN|Infinity|undefined)#{REGEXP_END}/.freeze
BASIC_IDENTIFIER_RULES =

Doesn't take in account utf8

/#{REGEXP_START}[$_a-z][$_a-z\d]*#{REGEXP_END}/i.freeze
RESERVED_FUNCTION_NAMES =

Defining a local function like Array may break everything

/#{REGEXP_START}(?:Array)#{REGEXP_END}/.freeze

Constants inherited from Base

Base::DUMMY_LOCATION

Instance Attribute Summary

Attributes inherited from Base

#current_node

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#append_to_body, #begin_with_stmts, #dynamic!, #error, #on_top, #prepend_to_body, #process, s, #s, #stmts_of

Class Method Details

.valid_ivar_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'opal/lib/opal/rewriters/js_reserved_words.rb', line 39

def self.valid_ivar_name?(name)
  !(PROTO_SPECIAL_PROPS =~ name || PROTO_SPECIAL_METHODS =~ name)
end

.valid_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
# File 'opal/lib/opal/rewriters/js_reserved_words.rb', line 31

def self.valid_name?(name)
  BASIC_IDENTIFIER_RULES =~ name && !(
    ES51_RESERVED_WORD =~ name ||
    ES3_RESERVED_WORD_EXCLUSIVE =~ name ||
    IMMUTABLE_PROPS =~ name
  )
end

Instance Method Details

#fix_ivar_name(name) ⇒ Object



47
48
49
# File 'opal/lib/opal/rewriters/js_reserved_words.rb', line 47

def fix_ivar_name(name)
  self.class.valid_ivar_name?(name.to_s[1..-1]) ? name : "#{name}$".to_sym
end

#fix_var_name(name) ⇒ Object



43
44
45
# File 'opal/lib/opal/rewriters/js_reserved_words.rb', line 43

def fix_var_name(name)
  self.class.valid_name?(name) ? name : "#{name}$".to_sym
end

#on_argument(node) ⇒ Object



104
105
106
107
108
109
110
111
# File 'opal/lib/opal/rewriters/js_reserved_words.rb', line 104

def on_argument(node)
  node = super(node)
  name, value = *node
  fixed_name = fix_var_name(name)
  new_children = value ? [fixed_name, value] : [fixed_name]

  node.updated(nil, new_children, meta: { arg_name: name })
end

#on_ivar(node) ⇒ Object



70
71
72
73
74
# File 'opal/lib/opal/rewriters/js_reserved_words.rb', line 70

def on_ivar(node)
  name, _ = *node
  node = node.updated(nil, [fix_ivar_name(name)])
  super(node)
end

#on_ivasgn(node) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'opal/lib/opal/rewriters/js_reserved_words.rb', line 76

def on_ivasgn(node)
  name, value = *node

  node =
    if value
      node.updated(nil, [fix_ivar_name(name), value])
    else
      node.updated(nil, [fix_ivar_name(name)])
    end

  super(node)
end

#on_lvar(node) ⇒ Object



51
52
53
54
55
# File 'opal/lib/opal/rewriters/js_reserved_words.rb', line 51

def on_lvar(node)
  name, _ = *node
  node = node.updated(nil, [fix_var_name(name)])
  super(node)
end

#on_lvasgn(node) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'opal/lib/opal/rewriters/js_reserved_words.rb', line 57

def on_lvasgn(node)
  name, value = *node

  node =
    if value
      node.updated(nil, [fix_var_name(name), value])
    else
      node.updated(nil, [fix_var_name(name)])
    end

  super(node)
end

#on_restarg(node) ⇒ Object Also known as: on_kwrestarg

Restarg and kwrestarg are special cases because they may have no name def m(, *); end



92
93
94
95
96
97
98
99
100
# File 'opal/lib/opal/rewriters/js_reserved_words.rb', line 92

def on_restarg(node)
  name, _ = *node

  if name
    node = node.updated(nil, [fix_var_name(name)], meta: { arg_name: name })
  end

  node
end