Skip to content

Commit 423f54d

Browse files
committed
1547 use strict: eval and arguments use restricted
1 parent 696d873 commit 423f54d

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/lexer.coffee

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ exports.Lexer = class Lexer
106106
@tokens.pop()
107107
id = '!' + id
108108

109-
if id in ['eval', 'arguments'].concat JS_FORBIDDEN
109+
if id in JS_FORBIDDEN
110110
if forcedIdentifier
111111
tag = 'IDENTIFIER'
112112
id = new String id
@@ -577,11 +577,14 @@ RESERVED = [
577577
'private', 'protected', 'public', 'static', 'yield'
578578
]
579579

580+
STRICT_PROSCRIBED = ['arguments', 'eval']
581+
580582
# The superset of both JavaScript keywords and reserved words, none of which may
581583
# be used as identifiers or properties.
582-
JS_FORBIDDEN = JS_KEYWORDS.concat RESERVED
584+
JS_FORBIDDEN = JS_KEYWORDS.concat(RESERVED).concat(STRICT_PROSCRIBED)
583585

584-
exports.RESERVED = RESERVED.concat(JS_KEYWORDS).concat(COFFEE_KEYWORDS)
586+
exports.RESERVED = RESERVED.concat(JS_KEYWORDS).concat(COFFEE_KEYWORDS).concat(STRICT_PROSCRIBED)
587+
exports.STRICT_PROSCRIBED = STRICT_PROSCRIBED
585588

586589
# Token matching regexes.
587590
IDENTIFIER = /// ^

src/nodes.coffee

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# the syntax tree into a string of JavaScript code, call `compile()` on the root.
55

66
{Scope} = require './scope'
7-
{RESERVED} = require './lexer'
7+
{RESERVED, STRICT_PROSCRIBED} = require './lexer'
88

99
# Import the helpers we plan to use.
1010
{compact, flatten, extend, merge, del, starts, ends, last} = require './helpers'
@@ -329,7 +329,7 @@ exports.Literal = class Literal extends Base
329329
if o.level >= LEVEL_ACCESS then '(void 0)' else 'void 0'
330330
else if @value is 'this'
331331
if o.scope.method?.bound then o.scope.method.context else @value
332-
else if @value.reserved and "#{@value}" not in ['eval', 'arguments']
332+
else if @value.reserved
333333
"\"#{@value}\""
334334
else
335335
@value
@@ -861,6 +861,8 @@ exports.Class = class Class extends Base
861861
tail instanceof Access and tail.name.value
862862
else
863863
@variable.base.value
864+
if decl in STRICT_PROSCRIBED
865+
throw SyntaxError 'variable name may not be eval or arguments'
864866
decl and= IDENTIFIER.test(decl) and decl
865867

866868
# For all `this`-references and bound functions in the class definition,
@@ -1042,7 +1044,7 @@ exports.Assign = class Assign extends Base
10421044
acc = IDENTIFIER.test idx.unwrap().value or 0
10431045
value = new Value value
10441046
value.properties.push new (if acc then Access else Index) idx
1045-
if obj.unwrap().value in ['arguments','eval'].concat RESERVED
1047+
if obj.unwrap().value in RESERVED
10461048
throw new SyntaxError "assignment to a reserved word: #{obj.compile o} = #{value.compile o}"
10471049
return new Assign(obj, value, null, param: @param).compile o, LEVEL_TOP
10481050
vvar = value.compile o, LEVEL_LIST
@@ -1087,7 +1089,7 @@ exports.Assign = class Assign extends Base
10871089
else
10881090
acc = isObject and IDENTIFIER.test idx.unwrap().value or 0
10891091
val = new Value new Literal(vvar), [new (if acc then Access else Index) idx]
1090-
if name? and name in ['arguments','eval'].concat RESERVED
1092+
if name? and name in RESERVED
10911093
throw new SyntaxError "assignment to a reserved word: #{obj.compile o} = #{val.compile o}"
10921094
assigns.push new Assign(obj, val, null, param: @param, subpattern: yes).compile o, LEVEL_LIST
10931095
assigns.push vvar unless top or @subpattern
@@ -1210,6 +1212,8 @@ exports.Code = class Code extends Base
12101212
# as well as be a splat, gathering up a group of parameters into an array.
12111213
exports.Param = class Param extends Base
12121214
constructor: (@name, @value, @splat) ->
1215+
if @name.unwrapAll().value in STRICT_PROSCRIBED
1216+
throw SyntaxError 'parameter name eval or arguments is not allowed'
12131217

12141218
children: ['name', 'value']
12151219

@@ -1545,6 +1549,8 @@ exports.Try = class Try extends Base
15451549
tryPart = @attempt.compile o, LEVEL_TOP
15461550

15471551
catchPart = if @recovery
1552+
if @error.value in STRICT_PROSCRIBED
1553+
throw SyntaxError "catch variable may not be eval or arguments"
15481554
o.scope.add @error.value, 'param' unless o.scope.check @error.value
15491555
" catch#{errorPart}{\n#{ @recovery.compile o, LEVEL_TOP }\n#{@tab}}"
15501556
else unless @ensure or @recovery

0 commit comments

Comments
 (0)