Class: Opal::Rewriters::JsReservedWords

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

Constant Summary

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}/
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}/
PROTO_SPECIAL_PROPS =

Prototype special properties.

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

Prototype special methods.

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

Immutable properties of the global object

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

Doesn't take in account utf8

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

Defining a local function like Array may break everything

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#append_to_body, #prepend_to_body, #s, s

Class Method Details

.valid_ivar_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.valid_ivar_name?(name)
  not (PROTO_SPECIAL_PROPS =~ name or PROTO_SPECIAL_METHODS =~ name)
end

.valid_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.valid_name?(name)
  BASIC_IDENTIFIER_RULES =~ name and not(
    ES51_RESERVED_WORD          =~ name or
    ES3_RESERVED_WORD_EXCLUSIVE =~ name or
    IMMUTABLE_PROPS             =~ name
  )
end

Instance Method Details

#fix_ivar_name(name) ⇒ Object



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

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



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

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

#on_argument(node) ⇒ Object



98
99
100
101
102
103
104
105
# File 'opal/lib/opal/rewriters/js_reserved_words.rb', line 98

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



68
69
70
71
72
# File 'opal/lib/opal/rewriters/js_reserved_words.rb', line 68

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

#on_ivasgn(node) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'opal/lib/opal/rewriters/js_reserved_words.rb', line 74

def on_ivasgn(node)
  name, value = *node

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

#on_lvar(node) ⇒ Object



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

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

#on_lvasgn(node) ⇒ Object



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

def on_lvasgn(node)
  name, value = *node

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

  super(node)
end

#on_restarg(node) ⇒ Object

Restarg is a special case because it may have no name def m(*); end



88
89
90
91
92
93
94
95
96
# File 'opal/lib/opal/rewriters/js_reserved_words.rb', line 88

def on_restarg(node)
  name, _ = *node

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

  node
end