2012-01-09 13:06:18 -05:00
|
|
|
# Strict Early Errors
|
|
|
|
# -------------------
|
|
|
|
|
|
|
|
# The following are prohibited under ES5's `strict` mode
|
|
|
|
# * `Octal Integer Literals`
|
|
|
|
# * `Octal Escape Sequences`
|
|
|
|
# * duplicate property definitions in `Object Literal`s
|
|
|
|
# * duplicate formal parameter
|
|
|
|
# * `delete` operand is a variable
|
|
|
|
# * `delete` operand is a parameter
|
|
|
|
# * `delete` operand is `undefined`
|
|
|
|
# * `Future Reserved Word`s as identifiers: implements, interface, let, package, private, protected, public, static, yield
|
|
|
|
# * `eval` or `arguments` as `catch` identifier
|
|
|
|
# * `eval` or `arguments` as formal parameter
|
|
|
|
# * `eval` or `arguments` as function declaration identifier
|
|
|
|
# * `eval` or `arguments` as LHS of assignment
|
|
|
|
# * `eval` or `arguments` as the operand of a post/pre-fix inc/dec-rement expression
|
|
|
|
|
|
|
|
# helper to assert that code complies with strict prohibitions
|
|
|
|
strict = (code, msg) ->
|
2012-04-20 18:29:40 -04:00
|
|
|
throws (-> CoffeeScript.compile code), null, msg ? code
|
2012-01-09 13:06:18 -05:00
|
|
|
strictOk = (code, msg) ->
|
2012-04-20 18:29:40 -04:00
|
|
|
doesNotThrow (-> CoffeeScript.compile code), msg ? code
|
2012-01-09 13:06:18 -05:00
|
|
|
|
|
|
|
|
2012-01-11 18:04:14 -05:00
|
|
|
test "octal integer literals prohibited", ->
|
2012-01-09 13:06:18 -05:00
|
|
|
strict '01'
|
|
|
|
strict '07777'
|
2012-01-12 15:32:14 -05:00
|
|
|
# decimals with a leading '0' are also prohibited
|
2012-02-03 19:30:32 -05:00
|
|
|
strict '09'
|
2012-01-12 15:32:14 -05:00
|
|
|
strict '079'
|
2012-01-09 13:06:18 -05:00
|
|
|
strictOk '`01`'
|
|
|
|
|
2012-01-11 18:04:14 -05:00
|
|
|
test "octal escape sequences prohibited", ->
|
2012-04-20 18:29:40 -04:00
|
|
|
strict '"\\1"'
|
2012-01-09 12:50:51 -05:00
|
|
|
strict '"\\7"'
|
2012-04-20 18:29:40 -04:00
|
|
|
strict '"\\001"'
|
2012-01-09 12:50:51 -05:00
|
|
|
strict '"\\777"'
|
2012-04-20 18:29:40 -04:00
|
|
|
strict '"_\\1"'
|
|
|
|
strict '"\\1_"'
|
|
|
|
strict '"_\\1_"'
|
|
|
|
strict '"\\\\\\1"'
|
|
|
|
strictOk '"\\0"'
|
|
|
|
eq "\x00", "\0"
|
|
|
|
strictOk '"\\08"'
|
|
|
|
eq "\x008", "\08"
|
|
|
|
strictOk '"\\0\\8"'
|
|
|
|
eq "\x008", "\0\8"
|
2012-01-09 12:50:51 -05:00
|
|
|
strictOk '"\\8"'
|
2012-04-20 18:29:40 -04:00
|
|
|
eq "8", "\8"
|
|
|
|
strictOk '"\\\\1"'
|
|
|
|
eq "\\" + "1", "\\1"
|
|
|
|
strictOk '"\\\\\\\\1"'
|
|
|
|
eq "\\\\" + "1", "\\\\1"
|
|
|
|
strictOk "`'\\1'`"
|
|
|
|
eq "\\" + "1", `"\\1"`
|
2012-02-03 19:30:32 -05:00
|
|
|
|
Refactor interpolation (and string and regex) handling in lexer
- Fix #3394: Unclosed single-quoted strings (both regular ones and heredocs)
used to pass through the lexer, causing a parsing error later, while
double-quoted strings caused an error already in the lexing phase. Now both
single and double-quoted unclosed strings error out in the lexer (which is the
more logical option) with consistent error messages. This also fixes the last
comment by @satyr in #3301.
- Similar to the above, unclosed heregexes also used to pass through the lexer
and not error until in the parsing phase, which resulted in confusing error
messages. This has been fixed, too.
- Fix #3348, by adding passing tests.
- Fix #3529: If a string starts with an interpolation, an empty string is no
longer emitted before the interpolation (unless it is needed to coerce the
interpolation into a string).
- Block comments cannot contain `*/`. Now the error message also shows exactly
where the offending `*/`. This improvement might seem unrelated, but I had to
touch that code anyway to refactor string and regex related code, and the
change was very trivial. Moreover, it's consistent with the next two points.
- Regexes cannot start with `*`. Now the error message also shows exactly where
the offending `*` is. (It might actually not be exatly at the start in
heregexes.) It is a very minor improvement, but it was trivial to add.
- Octal escapes in strings are forbidden in CoffeeScript (just like in
JavaScript strict mode). However, this used to be the case only for regular
strings. Now they are also forbidden in heredocs. Moreover, the errors now
point at the offending octal escape.
- Invalid regex flags are no longer allowed. This includes repeated modifiers
and unknown ones. Moreover, invalid modifiers do not stop a heregex from
being matched, which results in better error messages.
- Fix #3621: `///a#{1}///` compiles to `RegExp("a" + 1)`. So does
`RegExp("a#{1}")`. Still, those two code snippets used to generate different
tokens, which is a bit weird, but more importantly causes problems for
coffeelint (see clutchski/coffeelint#340). This required lots of tests in
test/location.coffee to be updated. Note that some updates to those tests are
unrelated to this point; some have been updated to be more consistent (I
discovered this because the refactored code happened to be seemingly more
correct).
- Regular regex literals used to erraneously allow newlines to be escaped,
causing invalid JavaScript output. This has been fixed.
- Heregexes may now be completely empty (`//////`), instead of erroring out with
a confusing message.
- Fix #2388: Heredocs and heregexes used to be lexed simply, which meant that
you couldn't nest a heredoc within a heredoc (double-quoted, that is) or a
heregex inside a heregex.
- Fix #2321: If you used division inside interpolation and then a slash later in
the string containing that interpolation, the division slash and the latter
slash was erraneously matched as a regex. This has been fixed.
- Indentation inside interpolations in heredocs no longer affect how much
indentation is removed from each line of the heredoc (which is more
intuitive).
- Whitespace is now correctly trimmed from the start and end of strings in a few
edge cases.
- Last but not least, the lexing of interpolated strings now seems to be more
efficient. For a regular double-quoted string, we used to use a custom
function to find the end of it (taking interpolations and interpolations
within interpolations etc. into account). Then we used to re-find the
interpolations and recursively lex their contents. In effect, the same string
was processed twice, or even more in the case of deeper nesting of
interpolations. Now the same string is processed just once.
- Code duplication between regular strings, heredocs, regular regexes and
heregexes has been reduced.
- The above two points should result in more easily read code, too.
2015-01-03 17:40:43 -05:00
|
|
|
# Also test other string types.
|
|
|
|
strict "'\\\\\\1'"
|
|
|
|
eq "\x008", '\08'
|
|
|
|
eq "\\\\" + "1", '\\\\1'
|
|
|
|
strict "'''\\\\\\1'''"
|
|
|
|
eq "\x008", '''\08'''
|
|
|
|
eq "\\\\" + "1", '''\\\\1'''
|
|
|
|
strict '"""\\\\\\1"""'
|
|
|
|
eq "\x008", """\08"""
|
|
|
|
eq "\\\\" + "1", """\\\\1"""
|
|
|
|
|
2012-04-23 20:41:56 -04:00
|
|
|
test "duplicate formal parameters are prohibited", ->
|
2012-01-09 13:06:18 -05:00
|
|
|
nonce = {}
|
|
|
|
# a Param can be an Identifier, ThisProperty( @-param ), Array, or Object
|
|
|
|
# a Param can also be a splat (...) or an assignment (param=value)
|
|
|
|
# the following function expressions should throw errors
|
|
|
|
strict '(_,_)->', 'param, param'
|
|
|
|
strict '(_,_...)->', 'param, param...'
|
|
|
|
strict '(_,_ = true)->', 'param, param='
|
|
|
|
strict '(@_,@_)->', 'two @params'
|
Fix #1500, #1574, #3318: Name generated vars uniquely
Any variables generated by CoffeeScript are now made sure to be named to
something not present in the source code being compiled. This way you can no
longer interfere with them, either on purpose or by mistake. (#1500, #1574)
For example, `({a}, _arg) ->` now compiles correctly. (#1574)
As opposed to the somewhat complex implementations discussed in #1500, this
commit takes a very simple approach by saving all used variables names using a
single pass over the token stream. Any generated variables are then made sure
not to exist in that list.
`(@a) -> a` used to be equivalent to `(@a) -> @a`, but now throws a runtime
`ReferenceError` instead (unless `a` exists in an upper scope of course). (#3318)
`(@a) ->` used to compile to `(function(a) { this.a = a; })`. Now it compiles to
`(function(_at_a) { this.a = _at_a; })`. (But you cannot access `_at_a` either,
of course.)
Because of the above, `(@a, a) ->` is now valid; `@a` and `a` are not duplicate
parameters.
Duplicate this-parameters with a reserved word, such as `(@case, @case) ->`,
used to compile but now throws, just like regular duplicate parameters.
2015-01-10 17:04:30 -05:00
|
|
|
strict '(@case,@case)->', 'two @reserved'
|
2012-01-09 13:06:18 -05:00
|
|
|
strict '(_,{_})->', 'param, {param}'
|
2015-08-22 15:39:26 -04:00
|
|
|
strict '(_,{_=true})->', 'param, {param=}'
|
2012-01-09 13:06:18 -05:00
|
|
|
strict '({_,_})->', '{param, param}'
|
2015-08-22 15:39:26 -04:00
|
|
|
strict '({_=true,_})->', '{param=, param}'
|
2012-01-09 13:06:18 -05:00
|
|
|
strict '(_,[_])->', 'param, [param]'
|
2015-08-22 15:39:26 -04:00
|
|
|
strict '(_,[_=true])->', 'param, [param=]'
|
2012-01-09 13:06:18 -05:00
|
|
|
strict '([_,_])->', '[param, param]'
|
2015-08-22 15:39:26 -04:00
|
|
|
strict '([_=true,_])->', '[param=, param]'
|
2012-01-09 13:06:18 -05:00
|
|
|
strict '(_,[_]=true)->', 'param, [param]='
|
2015-08-22 15:39:26 -04:00
|
|
|
strict '(_,[_=true]=true)->', 'param, [param=]='
|
2012-01-09 13:06:18 -05:00
|
|
|
strict '(_,[@_,{_}])->', 'param, [@param, {param}]'
|
|
|
|
strict '(_,[_,{@_}])->', 'param, [param, {@param}]'
|
2015-08-22 15:39:26 -04:00
|
|
|
strict '(_,[_,{@_=true}])->', 'param, [param, {@param=}]'
|
2012-01-09 13:06:18 -05:00
|
|
|
strict '(_,[_,{_}])->', 'param, [param, {param}]'
|
|
|
|
strict '(_,[_,{__}])->', 'param, [param, {param2}]'
|
|
|
|
strict '(_,[__,{_}])->', 'param, [param2, {param}]'
|
|
|
|
strict '(__,[_,{_}])->', 'param, [param2, {param2}]'
|
2012-04-23 20:41:56 -04:00
|
|
|
strict '({0:a,1:a})->', '{0:param,1:param}'
|
2015-09-27 08:59:37 -04:00
|
|
|
strict '(a=b=true,a)->', 'param=assignment, param'
|
|
|
|
strict '({a=b=true},a)->', '{param=assignment}, param'
|
2012-01-09 13:06:18 -05:00
|
|
|
# the following function expressions should **not** throw errors
|
Fix #1500, #1574, #3318: Name generated vars uniquely
Any variables generated by CoffeeScript are now made sure to be named to
something not present in the source code being compiled. This way you can no
longer interfere with them, either on purpose or by mistake. (#1500, #1574)
For example, `({a}, _arg) ->` now compiles correctly. (#1574)
As opposed to the somewhat complex implementations discussed in #1500, this
commit takes a very simple approach by saving all used variables names using a
single pass over the token stream. Any generated variables are then made sure
not to exist in that list.
`(@a) -> a` used to be equivalent to `(@a) -> @a`, but now throws a runtime
`ReferenceError` instead (unless `a` exists in an upper scope of course). (#3318)
`(@a) ->` used to compile to `(function(a) { this.a = a; })`. Now it compiles to
`(function(_at_a) { this.a = _at_a; })`. (But you cannot access `_at_a` either,
of course.)
Because of the above, `(@a, a) ->` is now valid; `@a` and `a` are not duplicate
parameters.
Duplicate this-parameters with a reserved word, such as `(@case, @case) ->`,
used to compile but now throws, just like regular duplicate parameters.
2015-01-10 17:04:30 -05:00
|
|
|
strictOk '(_,@_)->'
|
|
|
|
strictOk '(@_,_...)->'
|
|
|
|
strictOk '(_,@_ = true)->'
|
|
|
|
strictOk '(@_,{_})->'
|
|
|
|
strictOk '({_,@_})->'
|
2015-08-22 15:39:26 -04:00
|
|
|
strictOk '({_,@_ = true})->'
|
Fix #1500, #1574, #3318: Name generated vars uniquely
Any variables generated by CoffeeScript are now made sure to be named to
something not present in the source code being compiled. This way you can no
longer interfere with them, either on purpose or by mistake. (#1500, #1574)
For example, `({a}, _arg) ->` now compiles correctly. (#1574)
As opposed to the somewhat complex implementations discussed in #1500, this
commit takes a very simple approach by saving all used variables names using a
single pass over the token stream. Any generated variables are then made sure
not to exist in that list.
`(@a) -> a` used to be equivalent to `(@a) -> @a`, but now throws a runtime
`ReferenceError` instead (unless `a` exists in an upper scope of course). (#3318)
`(@a) ->` used to compile to `(function(a) { this.a = a; })`. Now it compiles to
`(function(_at_a) { this.a = _at_a; })`. (But you cannot access `_at_a` either,
of course.)
Because of the above, `(@a, a) ->` is now valid; `@a` and `a` are not duplicate
parameters.
Duplicate this-parameters with a reserved word, such as `(@case, @case) ->`,
used to compile but now throws, just like regular duplicate parameters.
2015-01-10 17:04:30 -05:00
|
|
|
strictOk '([_,@_])->'
|
2015-08-22 15:39:26 -04:00
|
|
|
strictOk '([_,@_ = true])->'
|
2012-01-09 13:06:18 -05:00
|
|
|
strictOk '({},_arg)->'
|
|
|
|
strictOk '({},{})->'
|
|
|
|
strictOk '([]...,_arg)->'
|
|
|
|
strictOk '({}...,_arg)->'
|
|
|
|
strictOk '({}...,[],_arg)->'
|
|
|
|
strictOk '([]...,{},_arg)->'
|
|
|
|
strictOk '(@case,_case)->'
|
|
|
|
strictOk '(@case,_case...)->'
|
|
|
|
strictOk '(@case...,_case)->'
|
|
|
|
strictOk '(_case,@case)->'
|
|
|
|
strictOk '(_case,@case...)->'
|
2015-08-28 17:11:47 -04:00
|
|
|
strictOk '({a:a})->'
|
|
|
|
strictOk '({a:a,a:b})->'
|
2012-01-09 13:06:18 -05:00
|
|
|
|
|
|
|
test "`delete` operand restrictions", ->
|
|
|
|
strict 'a = 1; delete a'
|
|
|
|
strictOk 'delete a' #noop
|
|
|
|
strict '(a) -> delete a'
|
|
|
|
strict '(a...) -> delete a'
|
|
|
|
strict '(a = 1) -> delete a'
|
|
|
|
strict '([a]) -> delete a'
|
|
|
|
strict '({a}) -> delete a'
|
|
|
|
|
2012-01-09 13:02:15 -05:00
|
|
|
test "`Future Reserved Word`s, `eval` and `arguments` restrictions", ->
|
2012-01-09 13:06:18 -05:00
|
|
|
|
2012-01-09 13:02:15 -05:00
|
|
|
access = (keyword, check = strict) ->
|
|
|
|
check "#{keyword}.a = 1"
|
|
|
|
check "#{keyword}[0] = 1"
|
|
|
|
assign = (keyword, check = strict) ->
|
|
|
|
check "#{keyword} = 1"
|
|
|
|
check "#{keyword} += 1"
|
|
|
|
check "#{keyword} -= 1"
|
|
|
|
check "#{keyword} *= 1"
|
|
|
|
check "#{keyword} /= 1"
|
|
|
|
check "#{keyword} ?= 1"
|
|
|
|
check "{keyword}++"
|
|
|
|
check "++{keyword}"
|
|
|
|
check "{keyword}--"
|
|
|
|
check "--{keyword}"
|
2012-01-19 11:33:43 -05:00
|
|
|
destruct = (keyword, check = strict) ->
|
|
|
|
check "{#{keyword}}"
|
|
|
|
check "o = {#{keyword}}"
|
2012-02-03 19:30:32 -05:00
|
|
|
invoke = (keyword, check = strict) ->
|
2012-01-09 13:02:15 -05:00
|
|
|
check "#{keyword} yes"
|
|
|
|
check "do #{keyword}"
|
|
|
|
fnDecl = (keyword, check = strict) ->
|
|
|
|
check "class #{keyword}"
|
|
|
|
param = (keyword, check = strict) ->
|
|
|
|
check "(#{keyword}) ->"
|
|
|
|
check "({#{keyword}}) ->"
|
|
|
|
prop = (keyword, check = strict) ->
|
|
|
|
check "a.#{keyword} = 1"
|
2012-02-03 19:30:32 -05:00
|
|
|
tryCatch = (keyword, check = strict) ->
|
2012-01-09 13:02:15 -05:00
|
|
|
check "try new Error catch #{keyword}"
|
2012-01-09 13:06:18 -05:00
|
|
|
|
2013-11-29 23:58:43 -05:00
|
|
|
future = 'implements interface let package private protected public static'.split ' '
|
2012-01-09 13:02:15 -05:00
|
|
|
for keyword in future
|
|
|
|
access keyword
|
|
|
|
assign keyword
|
2012-01-19 11:33:43 -05:00
|
|
|
destruct keyword
|
2012-01-09 13:02:15 -05:00
|
|
|
invoke keyword
|
|
|
|
fnDecl keyword
|
|
|
|
param keyword
|
|
|
|
prop keyword, strictOk
|
|
|
|
tryCatch keyword
|
2012-02-03 19:30:32 -05:00
|
|
|
|
2012-01-09 13:02:15 -05:00
|
|
|
for keyword in ['eval', 'arguments']
|
|
|
|
access keyword, strictOk
|
|
|
|
assign keyword
|
2012-01-19 11:33:43 -05:00
|
|
|
destruct keyword, strictOk
|
2012-01-09 13:02:15 -05:00
|
|
|
invoke keyword, strictOk
|
|
|
|
fnDecl keyword
|
|
|
|
param keyword
|
|
|
|
prop keyword, strictOk
|
|
|
|
tryCatch keyword
|