Class: Opal::Rewriters::JsReservedWords
- Defined in:
- 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. homura patch: added
prototypebecause every JS class constructor is a function object whose.prototypeslot controlsnew; a Ruby class instance variable named@prototypewould otherwise overwrite that slot and break allocate/new. Sinatra::Base#reset! sets@prototype = nilat class-body time, which corrupts the class. /#{REGEXP_START}(?:constructor|displayName|prototype|__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
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
Instance Method Summary collapse
- #fix_ivar_name(name) ⇒ Object
- #fix_var_name(name) ⇒ Object
- #on_argument(node) ⇒ Object
- #on_ivar(node) ⇒ Object
- #on_ivasgn(node) ⇒ Object
- #on_lvar(node) ⇒ Object
- #on_lvasgn(node) ⇒ Object
-
#on_restarg(node) ⇒ Object
(also: #on_kwrestarg)
Restarg and kwrestarg are special cases because they may have no name def m(, *); end.
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
44 45 46 |
# File 'lib/opal/rewriters/js_reserved_words.rb', line 44 def self.valid_ivar_name?(name) !(PROTO_SPECIAL_PROPS =~ name || PROTO_SPECIAL_METHODS =~ name) end |
.valid_name?(name) ⇒ Boolean
36 37 38 39 40 41 42 |
# File 'lib/opal/rewriters/js_reserved_words.rb', line 36 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
52 53 54 |
# File 'lib/opal/rewriters/js_reserved_words.rb', line 52 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
48 49 50 |
# File 'lib/opal/rewriters/js_reserved_words.rb', line 48 def fix_var_name(name) self.class.valid_name?(name) ? name : "#{name}$".to_sym end |
#on_argument(node) ⇒ Object
109 110 111 112 113 114 115 116 |
# File 'lib/opal/rewriters/js_reserved_words.rb', line 109 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
75 76 77 78 79 |
# File 'lib/opal/rewriters/js_reserved_words.rb', line 75 def on_ivar(node) name, _ = *node node = node.updated(nil, [fix_ivar_name(name)]) super(node) end |
#on_ivasgn(node) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/opal/rewriters/js_reserved_words.rb', line 81 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
56 57 58 59 60 |
# File 'lib/opal/rewriters/js_reserved_words.rb', line 56 def on_lvar(node) name, _ = *node node = node.updated(nil, [fix_var_name(name)]) super(node) end |
#on_lvasgn(node) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/opal/rewriters/js_reserved_words.rb', line 62 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
97 98 99 100 101 102 103 104 105 |
# File 'lib/opal/rewriters/js_reserved_words.rb', line 97 def on_restarg(node) name, _ = *node if name node = node.updated(nil, [fix_var_name(name)], meta: { arg_name: name }) end node end |