mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00

* Compile classes to ES2015 classes Rather than compiling classes to named functions with prototype and class assignments, they are now compiled to ES2015 class declarations. Backwards compatibility has been maintained by compiling ES2015- incompatible properties as prototype or class assignments. `super` continues to be compiled as before. Where possible, classes will be compiled "bare", without an enclosing IIFE. This is possible when the class contains only ES2015 compatible expressions (methods and static methods), and has no parent (this last constraint is a result of the legacy `super` compilation, and could be removed once ES2015 `super` is being used). Classes are still assigned to variables to maintain compatibility for assigned class expressions. There are a few changes to existing functionality that could break backwards compatibility: - Derived constructors that deliberately don't call `super` are no longer possible. ES2015 derived classes can't use `this` unless the parent constructor has been called, so it's now called implicitly when not present. - As a consequence of the above, derived constructors with @ parameters or bound methods and explicit `super` calls are not allowed. The implicit `super` must be used in these cases. * Add tests to verify class interoperability with ES * Refactor class nodes to separate executable body logic Logic has been redistributed amongst the class nodes so that: - `Class` contains the logic necessary to compile an ES class declaration. - `ExecutableClassBody` contains the logic necessary to compile CS' class extensions that require an executable class body. `Class` still necessarily contains logic to determine whether an expression is valid in an ES class initializer or not. If any invalid expressions are found then `Class` will wrap itself in an `ExecutableClassBody` when compiling. * Rename `Code#static` to `Code#isStatic` This naming is more consistent with other `Code` flags. * Output anonymous classes when possible Anonymous classes can be output when: - The class has no parent. The current super compilation needs a class variable to reference. This condition will go away when ES2015 super is in use. - The class contains no bound static methods. Bound static methods have their context set to the class name. * Throw errors at compile time for async or generator constructors * Improve handling of anonymous classes Anonymous classes are now always anonymous. If a name is required (e.g. for bound static methods or derived classes) then the class is compiled in an `ExecutableClassBody` which will give the anonymous class a stable reference. * Add a `replaceInContext` method to `Node` `replaceInContext` will traverse children looking for a node for which `match` returns true. Once found, the matching node will be replaced by the result of calling `replacement`. * Separate `this` assignments from function parameters This change has been made to simplify two future changes: 1. Outputting `@`-param assignments after a `super` call. In this case it is necessary that non-`@` parameters are available before `super` is called, so destructuring has to happen before `this` assignment. 2. Compiling destructured assignment to ES6 In this case also destructuring has to happen before `this`, as destructuring can happen in the arguments list, but `this` assignment can not. A bonus side-effect is that default values for `@` params are now output as ES6 default parameters, e.g. (@a = 1) -> becomes function a (a = 1) { this.a = a; } * Change `super` handling in class constructors Inside an ES derived constructor (a constructor for a class that extends another class), it is impossible to access `this` until `super` has been called. This conflicts with CoffeeScript's `@`-param and bound method features, which compile to `this` references at the top of a function body. For example: class B extends A constructor: (@param) -> super method: => This would compile to something like: class B extends A { constructor (param) { this.param = param; this.method = bind(this.method, this); super(...arguments); } } This would break in an ES-compliant runtime as there are `this` references before the call to `super`. Before this commit we were dealing with this by injecting an implicit `super` call into derived constructors that do not already have an explicit `super` call. Furthermore, we would disallow explicit `super` calls in derived constructors that used bound methods or `@`-params, meaning the above example would need to be rewritten as: class B extends A constructor: (@param) -> method: => This would result in a call to `super(...arguments)` being generated as the first expression in `B#constructor`. Whilst this approach seems to work pretty well, and is arguably more convenient than having to manually call `super` when you don't particularly care about the arguments, it does introduce some 'magic' and separation from ES, and would likely be a pain point in a project that made use of significant constructor overriding. This commit introduces a mechanism through which `super` in constructors is 'expanded' to include any generated `this` assignments, whilst retaining the same semantics of a super call. The first example above now compiles to something like: class B extends A { constructor (param) { var ref ref = super(...arguments), this.param = param, this.method = bind(this.method, this), ref; } } * Improve `super` handling in constructors Rather than functions expanding their `super` calls, the `SuperCall` node can now be given a list of `thisAssignments` to apply when it is compiled. This allows us to use the normal compiler machinery to determine whether the `super` result needs to be cached, whether it appears inline or not, etc. * Fix anonymous classes at the top level Anonymous classes in ES are only valid within expressions. If an anonymous class is at the top level it will now be wrapped in parenthses to force it into an expression. * Re-add Parens wrapper around executable class bodies This was lost in the refactoring, but it necessary to ensure `new class ...` works as expected when there's an executable body. * Throw compiler errors for badly configured derived constructors Rather than letting them become runtime errors, the following checks are now performed when compiling a derived constructor: - The constructor **must** include a call to `super`. - The constructor **must not** reference `this` in the function body before `super` has been called. * Add some tests exercising new class behaviour - async methods in classes - `this` access after `super` in extended classes - constructor super in arrow functions - constructor functions can't be async - constructor functions can't be generators - derived constructors must call super - derived constructors can't reference `this` before calling super - generator methods in classes - 'new' target * Improve constructor `super` errors Add a check for `super` in non-extended class constructors, and explicitly mention derived constructors in the "can't reference this before super" error. * Fix compilation of multiple `super` paths in derived constructors `super` can only be called once, but it can be called conditionally from multiple locations. The chosen fix is to add the `this` assignments to every super call. * Additional class tests, added as a separate file to simplify testing and merging. Some methods are commented out because they currently throw and I'm not sure how to test for compilation errors like those. There is also one test which I deliberately left without passing, `super` in an external prototype override. This test should 'pass' but is really a variation on the failing `super only allowed in an instance method` tests above it. * Changes to the tests. Found bug in super in prototype method. fixed. * Added failing test back in, dealing with bound functions in external prototype overrides. * Located a bug in the compiler relating to assertions and escaped ES6 classes. * Move tests from classes-additional.coffee into classes.coffee; comment out console.log * Cleaned up tests and made changes based on feedback. Test at the end still has issues, but it's commented out for now. * Make HoistTarget.expand recursive It's possible that a hoisted node may itself contain hoisted nodes (e.g. a class method inside a class method). For this to work the hoisted fragments need to be expanded recursively. * Uncomment final test in classes.coffee The test case now compiles, however another issue is affecting the test due to the error for `this` before `super` triggering based on source order rather than execution order. These have been commented out for now. * Fixed last test TODOs in test/classes.coffee Turns out an expression like `this.foo = super()` won't run in JS as it attempts to lookup `this` before evaluating `super` (i.e. throws "this is not defined"). * Added more tests for compatability checks, statics, prototypes and ES6 expectations. Cleaned test "nested classes with super". * Changes to reflect feedback and to comment out issues that will be addressed seperately. * Clean up test/classes.coffee - Trim trailing whitespace. - Rephrase a condition to be more idiomatic. * Remove check for `super` in derived constructors In order to be usable at runtime, an extended ES class must call `super` OR return an alternative object. This check prevented the latter case, and checking for an alternative return can't be completed statically without control flow analysis. * Disallow 'super' in constructor parameter defaults There are many edge cases when combining 'super' in parameter defaults with @-parameters and bound functions (and potentially property initializers in the future). Rather than attempting to resolve these edge cases, 'super' is now explicitly disallowed in constructor parameter defaults. * Disallow @-params in derived constructors without 'super' @-parameters can't be assigned unless 'super' is called.
1249 lines
26 KiB
CoffeeScript
1249 lines
26 KiB
CoffeeScript
# Error Formatting
|
|
# ----------------
|
|
|
|
# Ensure that errors of different kinds (lexer, parser and compiler) are shown
|
|
# in a consistent way.
|
|
|
|
assertErrorFormat = (code, expectedErrorFormat) ->
|
|
throws (-> CoffeeScript.run code), (err) ->
|
|
err.colorful = no
|
|
eq expectedErrorFormat, "#{err}"
|
|
yes
|
|
|
|
test "lexer errors formatting", ->
|
|
assertErrorFormat '''
|
|
normalObject = {}
|
|
insideOutObject = }{
|
|
''',
|
|
'''
|
|
[stdin]:2:19: error: unmatched }
|
|
insideOutObject = }{
|
|
^
|
|
'''
|
|
|
|
test "parser error formatting", ->
|
|
assertErrorFormat '''
|
|
foo in bar or in baz
|
|
''',
|
|
'''
|
|
[stdin]:1:15: error: unexpected in
|
|
foo in bar or in baz
|
|
^^
|
|
'''
|
|
|
|
test "compiler error formatting", ->
|
|
assertErrorFormat '''
|
|
evil = (foo, eval, bar) ->
|
|
''',
|
|
'''
|
|
[stdin]:1:14: error: 'eval' can't be assigned
|
|
evil = (foo, eval, bar) ->
|
|
^^^^
|
|
'''
|
|
|
|
if require?
|
|
fs = require 'fs'
|
|
path = require 'path'
|
|
|
|
test "#2849: compilation error in a require()d file", ->
|
|
# Create a temporary file to require().
|
|
ok not fs.existsSync 'test/syntax-error.coffee'
|
|
fs.writeFileSync 'test/syntax-error.coffee', 'foo in bar or in baz'
|
|
|
|
try
|
|
assertErrorFormat '''
|
|
require './test/syntax-error'
|
|
''',
|
|
"""
|
|
#{path.join __dirname, 'syntax-error.coffee'}:1:15: error: unexpected in
|
|
foo in bar or in baz
|
|
^^
|
|
"""
|
|
finally
|
|
fs.unlinkSync 'test/syntax-error.coffee'
|
|
|
|
|
|
test "#1096: unexpected generated tokens", ->
|
|
# Implicit ends
|
|
assertErrorFormat 'a:, b', '''
|
|
[stdin]:1:3: error: unexpected ,
|
|
a:, b
|
|
^
|
|
'''
|
|
# Explicit ends
|
|
assertErrorFormat '(a:)', '''
|
|
[stdin]:1:4: error: unexpected )
|
|
(a:)
|
|
^
|
|
'''
|
|
# Unexpected end of file
|
|
assertErrorFormat 'a:', '''
|
|
[stdin]:1:3: error: unexpected end of input
|
|
a:
|
|
^
|
|
'''
|
|
assertErrorFormat 'a +', '''
|
|
[stdin]:1:4: error: unexpected end of input
|
|
a +
|
|
^
|
|
'''
|
|
# Unexpected key in implicit object (an implicit object itself is _not_
|
|
# unexpected here)
|
|
assertErrorFormat '''
|
|
for i in [1]:
|
|
1
|
|
''', '''
|
|
[stdin]:1:10: error: unexpected [
|
|
for i in [1]:
|
|
^
|
|
'''
|
|
# Unexpected regex
|
|
assertErrorFormat '{/a/i: val}', '''
|
|
[stdin]:1:2: error: unexpected regex
|
|
{/a/i: val}
|
|
^^^^
|
|
'''
|
|
assertErrorFormat '{///a///i: val}', '''
|
|
[stdin]:1:2: error: unexpected regex
|
|
{///a///i: val}
|
|
^^^^^^^^
|
|
'''
|
|
assertErrorFormat '{///#{a}///i: val}', '''
|
|
[stdin]:1:2: error: unexpected regex
|
|
{///#{a}///i: val}
|
|
^^^^^^^^^^^
|
|
'''
|
|
# Unexpected string
|
|
assertErrorFormat 'import foo from "lib-#{version}"', '''
|
|
[stdin]:1:17: error: the name of the module to be imported from must be an uninterpolated string
|
|
import foo from "lib-#{version}"
|
|
^^^^^^^^^^^^^^^^
|
|
'''
|
|
|
|
# Unexpected number
|
|
assertErrorFormat '"a"0x00Af2', '''
|
|
[stdin]:1:4: error: unexpected number
|
|
"a"0x00Af2
|
|
^^^^^^^
|
|
'''
|
|
|
|
test "#1316: unexpected end of interpolation", ->
|
|
assertErrorFormat '''
|
|
"#{+}"
|
|
''', '''
|
|
[stdin]:1:5: error: unexpected end of interpolation
|
|
"#{+}"
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
"#{++}"
|
|
''', '''
|
|
[stdin]:1:6: error: unexpected end of interpolation
|
|
"#{++}"
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
"#{-}"
|
|
''', '''
|
|
[stdin]:1:5: error: unexpected end of interpolation
|
|
"#{-}"
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
"#{--}"
|
|
''', '''
|
|
[stdin]:1:6: error: unexpected end of interpolation
|
|
"#{--}"
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
"#{~}"
|
|
''', '''
|
|
[stdin]:1:5: error: unexpected end of interpolation
|
|
"#{~}"
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
"#{!}"
|
|
''', '''
|
|
[stdin]:1:5: error: unexpected end of interpolation
|
|
"#{!}"
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
"#{not}"
|
|
''', '''
|
|
[stdin]:1:7: error: unexpected end of interpolation
|
|
"#{not}"
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
"#{5) + (4}_"
|
|
''', '''
|
|
[stdin]:1:5: error: unmatched )
|
|
"#{5) + (4}_"
|
|
^
|
|
'''
|
|
# #2918
|
|
assertErrorFormat '''
|
|
"#{foo.}"
|
|
''', '''
|
|
[stdin]:1:8: error: unexpected end of interpolation
|
|
"#{foo.}"
|
|
^
|
|
'''
|
|
|
|
test "#3325: implicit indentation errors", ->
|
|
assertErrorFormat '''
|
|
i for i in a then i
|
|
''', '''
|
|
[stdin]:1:14: error: unexpected then
|
|
i for i in a then i
|
|
^^^^
|
|
'''
|
|
|
|
test "explicit indentation errors", ->
|
|
assertErrorFormat '''
|
|
a = b
|
|
c
|
|
''', '''
|
|
[stdin]:2:1: error: unexpected indentation
|
|
c
|
|
^^
|
|
'''
|
|
|
|
test "unclosed strings", ->
|
|
assertErrorFormat '''
|
|
'
|
|
''', '''
|
|
[stdin]:1:1: error: missing '
|
|
'
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
"
|
|
''', '''
|
|
[stdin]:1:1: error: missing "
|
|
"
|
|
^
|
|
'''
|
|
assertErrorFormat """
|
|
'''
|
|
""", """
|
|
[stdin]:1:1: error: missing '''
|
|
'''
|
|
^^^
|
|
"""
|
|
assertErrorFormat '''
|
|
"""
|
|
''', '''
|
|
[stdin]:1:1: error: missing """
|
|
"""
|
|
^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
"#{"
|
|
''', '''
|
|
[stdin]:1:4: error: missing "
|
|
"#{"
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
"""#{"
|
|
''', '''
|
|
[stdin]:1:6: error: missing "
|
|
"""#{"
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
"#{"""
|
|
''', '''
|
|
[stdin]:1:4: error: missing """
|
|
"#{"""
|
|
^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
"""#{"""
|
|
''', '''
|
|
[stdin]:1:6: error: missing """
|
|
"""#{"""
|
|
^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
///#{"""
|
|
''', '''
|
|
[stdin]:1:6: error: missing """
|
|
///#{"""
|
|
^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
"a
|
|
#{foo """
|
|
bar
|
|
#{ +'12 }
|
|
baz
|
|
"""} b"
|
|
''', '''
|
|
[stdin]:4:11: error: missing '
|
|
#{ +'12 }
|
|
^
|
|
'''
|
|
# https://github.com/jashkenas/coffeescript/issues/3301#issuecomment-31735168
|
|
assertErrorFormat '''
|
|
# Note the double escaping; this would be `"""a\"""` real code.
|
|
"""a\\"""
|
|
''', '''
|
|
[stdin]:2:1: error: missing """
|
|
"""a\\"""
|
|
^^^
|
|
'''
|
|
|
|
test "unclosed heregexes", ->
|
|
assertErrorFormat '''
|
|
///
|
|
''', '''
|
|
[stdin]:1:1: error: missing ///
|
|
///
|
|
^^^
|
|
'''
|
|
# https://github.com/jashkenas/coffeescript/issues/3301#issuecomment-31735168
|
|
assertErrorFormat '''
|
|
# Note the double escaping; this would be `///a\///` real code.
|
|
///a\\///
|
|
''', '''
|
|
[stdin]:2:1: error: missing ///
|
|
///a\\///
|
|
^^^
|
|
'''
|
|
|
|
test "unexpected token after string", ->
|
|
# Parsing error.
|
|
assertErrorFormat '''
|
|
'foo'bar
|
|
''', '''
|
|
[stdin]:1:6: error: unexpected identifier
|
|
'foo'bar
|
|
^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
"foo"bar
|
|
''', '''
|
|
[stdin]:1:6: error: unexpected identifier
|
|
"foo"bar
|
|
^^^
|
|
'''
|
|
# Lexing error.
|
|
assertErrorFormat '''
|
|
'foo'bar'
|
|
''', '''
|
|
[stdin]:1:9: error: missing '
|
|
'foo'bar'
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
"foo"bar"
|
|
''', '''
|
|
[stdin]:1:9: error: missing "
|
|
"foo"bar"
|
|
^
|
|
'''
|
|
|
|
test "#3348: Location data is wrong in interpolations with leading whitespace", ->
|
|
assertErrorFormat '''
|
|
"#{ * }"
|
|
''', '''
|
|
[stdin]:1:5: error: unexpected *
|
|
"#{ * }"
|
|
^
|
|
'''
|
|
|
|
test "octal escapes", ->
|
|
assertErrorFormat '''
|
|
"a\\0\\tb\\\\\\07c"
|
|
''', '''
|
|
[stdin]:1:10: error: octal escape sequences are not allowed \\07
|
|
"a\\0\\tb\\\\\\07c"
|
|
\ \ \ \ ^\^^
|
|
'''
|
|
assertErrorFormat '''
|
|
"a
|
|
#{b} \\1"
|
|
''', '''
|
|
[stdin]:2:8: error: octal escape sequences are not allowed \\1
|
|
#{b} \\1"
|
|
^\^
|
|
'''
|
|
assertErrorFormat '''
|
|
/a\\0\\tb\\\\\\07c/
|
|
''', '''
|
|
[stdin]:1:10: error: octal escape sequences are not allowed \\07
|
|
/a\\0\\tb\\\\\\07c/
|
|
\ \ \ \ ^\^^
|
|
'''
|
|
assertErrorFormat '''
|
|
///a
|
|
#{b} \\01///
|
|
''', '''
|
|
[stdin]:2:8: error: octal escape sequences are not allowed \\01
|
|
#{b} \\01///
|
|
^\^^
|
|
'''
|
|
|
|
test "#3795: invalid escapes", ->
|
|
assertErrorFormat '''
|
|
"a\\0\\tb\\\\\\x7g"
|
|
''', '''
|
|
[stdin]:1:10: error: invalid escape sequence \\x7g
|
|
"a\\0\\tb\\\\\\x7g"
|
|
\ \ \ \ ^\^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
"a
|
|
#{b} \\uA02
|
|
c"
|
|
''', '''
|
|
[stdin]:2:8: error: invalid escape sequence \\uA02
|
|
#{b} \\uA02
|
|
^\^^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
/a\\u002space/
|
|
''', '''
|
|
[stdin]:1:3: error: invalid escape sequence \\u002s
|
|
/a\\u002space/
|
|
^\^^^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
///a \\u002 0 space///
|
|
''', '''
|
|
[stdin]:1:6: error: invalid escape sequence \\u002 \n\
|
|
///a \\u002 0 space///
|
|
^\^^^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
///a
|
|
#{b} \\x0
|
|
c///
|
|
''', '''
|
|
[stdin]:2:8: error: invalid escape sequence \\x0
|
|
#{b} \\x0
|
|
^\^^
|
|
'''
|
|
assertErrorFormat '''
|
|
/ab\\u/
|
|
''', '''
|
|
[stdin]:1:4: error: invalid escape sequence \\u
|
|
/ab\\u/
|
|
^\^
|
|
'''
|
|
|
|
test "illegal herecomment", ->
|
|
assertErrorFormat '''
|
|
###
|
|
Regex: /a*/g
|
|
###
|
|
''', '''
|
|
[stdin]:2:12: error: block comments cannot contain */
|
|
Regex: /a*/g
|
|
^^
|
|
'''
|
|
|
|
test "#1724: regular expressions beginning with *", ->
|
|
assertErrorFormat '''
|
|
/* foo/
|
|
''', '''
|
|
[stdin]:1:2: error: regular expressions cannot begin with *
|
|
/* foo/
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
///
|
|
* foo
|
|
///
|
|
''', '''
|
|
[stdin]:2:3: error: regular expressions cannot begin with *
|
|
* foo
|
|
^
|
|
'''
|
|
|
|
test "invalid regex flags", ->
|
|
assertErrorFormat '''
|
|
/a/ii
|
|
''', '''
|
|
[stdin]:1:4: error: invalid regular expression flags ii
|
|
/a/ii
|
|
^^
|
|
'''
|
|
assertErrorFormat '''
|
|
/a/G
|
|
''', '''
|
|
[stdin]:1:4: error: invalid regular expression flags G
|
|
/a/G
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
/a/gimi
|
|
''', '''
|
|
[stdin]:1:4: error: invalid regular expression flags gimi
|
|
/a/gimi
|
|
^^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
/a/g_
|
|
''', '''
|
|
[stdin]:1:4: error: invalid regular expression flags g_
|
|
/a/g_
|
|
^^
|
|
'''
|
|
assertErrorFormat '''
|
|
///a///ii
|
|
''', '''
|
|
[stdin]:1:8: error: invalid regular expression flags ii
|
|
///a///ii
|
|
^^
|
|
'''
|
|
doesNotThrow -> CoffeeScript.compile '/a/ymgi'
|
|
|
|
test "missing `)`, `}`, `]`", ->
|
|
assertErrorFormat '''
|
|
(
|
|
''', '''
|
|
[stdin]:1:1: error: missing )
|
|
(
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
{
|
|
''', '''
|
|
[stdin]:1:1: error: missing }
|
|
{
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
[
|
|
''', '''
|
|
[stdin]:1:1: error: missing ]
|
|
[
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
obj = {a: [1, (2+
|
|
''', '''
|
|
[stdin]:1:15: error: missing )
|
|
obj = {a: [1, (2+
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
"#{
|
|
''', '''
|
|
[stdin]:1:3: error: missing }
|
|
"#{
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
"""
|
|
foo#{ bar "#{1}"
|
|
''', '''
|
|
[stdin]:2:7: error: missing }
|
|
foo#{ bar "#{1}"
|
|
^
|
|
'''
|
|
|
|
test "unclosed regexes", ->
|
|
assertErrorFormat '''
|
|
/
|
|
''', '''
|
|
[stdin]:1:1: error: missing / (unclosed regex)
|
|
/
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
# Note the double escaping; this would be `/a\/` real code.
|
|
/a\\/
|
|
''', '''
|
|
[stdin]:2:1: error: missing / (unclosed regex)
|
|
/a\\/
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
/// ^
|
|
a #{""" ""#{if /[/].test "|" then 1 else 0}"" """}
|
|
///
|
|
''', '''
|
|
[stdin]:2:18: error: missing / (unclosed regex)
|
|
a #{""" ""#{if /[/].test "|" then 1 else 0}"" """}
|
|
^
|
|
'''
|
|
|
|
test "duplicate function arguments", ->
|
|
assertErrorFormat '''
|
|
(foo, bar, foo) ->
|
|
''', '''
|
|
[stdin]:1:12: error: multiple parameters named 'foo'
|
|
(foo, bar, foo) ->
|
|
^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
(@foo, bar, @foo) ->
|
|
''', '''
|
|
[stdin]:1:13: error: multiple parameters named '@foo'
|
|
(@foo, bar, @foo) ->
|
|
^^^^
|
|
'''
|
|
|
|
test "reserved words", ->
|
|
assertErrorFormat '''
|
|
case
|
|
''', '''
|
|
[stdin]:1:1: error: reserved word 'case'
|
|
case
|
|
^^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
case = 1
|
|
''', '''
|
|
[stdin]:1:1: error: reserved word 'case'
|
|
case = 1
|
|
^^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
for = 1
|
|
''', '''
|
|
[stdin]:1:1: error: keyword 'for' can't be assigned
|
|
for = 1
|
|
^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
unless = 1
|
|
''', '''
|
|
[stdin]:1:1: error: keyword 'unless' can't be assigned
|
|
unless = 1
|
|
^^^^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
for += 1
|
|
''', '''
|
|
[stdin]:1:1: error: keyword 'for' can't be assigned
|
|
for += 1
|
|
^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
for &&= 1
|
|
''', '''
|
|
[stdin]:1:1: error: keyword 'for' can't be assigned
|
|
for &&= 1
|
|
^^^
|
|
'''
|
|
# Make sure token look-behind doesn't go out of range.
|
|
assertErrorFormat '''
|
|
&&= 1
|
|
''', '''
|
|
[stdin]:1:1: error: unexpected &&=
|
|
&&= 1
|
|
^^^
|
|
'''
|
|
# #2306: Show unaliased name in error messages.
|
|
assertErrorFormat '''
|
|
on = 1
|
|
''', '''
|
|
[stdin]:1:1: error: keyword 'on' can't be assigned
|
|
on = 1
|
|
^^
|
|
'''
|
|
|
|
test "strict mode errors", ->
|
|
assertErrorFormat '''
|
|
eval = 1
|
|
''', '''
|
|
[stdin]:1:1: error: 'eval' can't be assigned
|
|
eval = 1
|
|
^^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
class eval
|
|
''', '''
|
|
[stdin]:1:7: error: 'eval' can't be assigned
|
|
class eval
|
|
^^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
arguments++
|
|
''', '''
|
|
[stdin]:1:1: error: 'arguments' can't be assigned
|
|
arguments++
|
|
^^^^^^^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
--arguments
|
|
''', '''
|
|
[stdin]:1:3: error: 'arguments' can't be assigned
|
|
--arguments
|
|
^^^^^^^^^
|
|
'''
|
|
|
|
test "invalid numbers", ->
|
|
assertErrorFormat '''
|
|
0X0
|
|
''', '''
|
|
[stdin]:1:2: error: radix prefix in '0X0' must be lowercase
|
|
0X0
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
10E0
|
|
''', '''
|
|
[stdin]:1:3: error: exponential notation in '10E0' must be indicated with a lowercase 'e'
|
|
10E0
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
018
|
|
''', '''
|
|
[stdin]:1:1: error: decimal literal '018' must not be prefixed with '0'
|
|
018
|
|
^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
010
|
|
''', '''
|
|
[stdin]:1:1: error: octal literal '010' must be prefixed with '0o'
|
|
010
|
|
^^^
|
|
'''
|
|
|
|
test "unexpected object keys", ->
|
|
assertErrorFormat '''
|
|
{[[]]}
|
|
''', '''
|
|
[stdin]:1:2: error: unexpected [
|
|
{[[]]}
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
{[[]]: 1}
|
|
''', '''
|
|
[stdin]:1:2: error: unexpected [
|
|
{[[]]: 1}
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
[[]]: 1
|
|
''', '''
|
|
[stdin]:1:1: error: unexpected [
|
|
[[]]: 1
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
{(a + "b")}
|
|
''', '''
|
|
[stdin]:1:2: error: unexpected (
|
|
{(a + "b")}
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
{(a + "b"): 1}
|
|
''', '''
|
|
[stdin]:1:2: error: unexpected (
|
|
{(a + "b"): 1}
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
(a + "b"): 1
|
|
''', '''
|
|
[stdin]:1:1: error: unexpected (
|
|
(a + "b"): 1
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
a: 1, [[]]: 2
|
|
''', '''
|
|
[stdin]:1:7: error: unexpected [
|
|
a: 1, [[]]: 2
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
{a: 1, [[]]: 2}
|
|
''', '''
|
|
[stdin]:1:8: error: unexpected [
|
|
{a: 1, [[]]: 2}
|
|
^
|
|
'''
|
|
|
|
test "invalid object keys", ->
|
|
assertErrorFormat '''
|
|
@a: 1
|
|
''', '''
|
|
[stdin]:1:1: error: invalid object key
|
|
@a: 1
|
|
^^
|
|
'''
|
|
assertErrorFormat '''
|
|
f
|
|
@a: 1
|
|
''', '''
|
|
[stdin]:2:3: error: invalid object key
|
|
@a: 1
|
|
^^
|
|
'''
|
|
assertErrorFormat '''
|
|
{a=2}
|
|
''', '''
|
|
[stdin]:1:3: error: unexpected =
|
|
{a=2}
|
|
^
|
|
'''
|
|
|
|
test "invalid destructuring default target", ->
|
|
assertErrorFormat '''
|
|
{'a' = 2} = obj
|
|
''', '''
|
|
[stdin]:1:6: error: unexpected =
|
|
{'a' = 2} = obj
|
|
^
|
|
'''
|
|
|
|
test "#4070: lone expansion", ->
|
|
assertErrorFormat '''
|
|
[...] = a
|
|
''', '''
|
|
[stdin]:1:2: error: Destructuring assignment has no target
|
|
[...] = a
|
|
^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
[ ..., ] = a
|
|
''', '''
|
|
[stdin]:1:3: error: Destructuring assignment has no target
|
|
[ ..., ] = a
|
|
^^^
|
|
'''
|
|
|
|
test "#3926: implicit object in parameter list", ->
|
|
assertErrorFormat '''
|
|
(a: b) ->
|
|
''', '''
|
|
[stdin]:1:3: error: unexpected :
|
|
(a: b) ->
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
(one, two, {three, four: five}, key: value) ->
|
|
''', '''
|
|
[stdin]:1:36: error: unexpected :
|
|
(one, two, {three, four: five}, key: value) ->
|
|
^
|
|
'''
|
|
|
|
test "#4130: unassignable in destructured param", ->
|
|
assertErrorFormat '''
|
|
fun = ({
|
|
@param : null
|
|
}) ->
|
|
console.log "Oh hello!"
|
|
''', '''
|
|
[stdin]:2:12: error: keyword 'null' can't be assigned
|
|
@param : null
|
|
^^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
({a: null}) ->
|
|
''', '''
|
|
[stdin]:1:6: error: keyword 'null' can't be assigned
|
|
({a: null}) ->
|
|
^^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
({a: 1}) ->
|
|
''', '''
|
|
[stdin]:1:6: error: '1' can't be assigned
|
|
({a: 1}) ->
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
({1}) ->
|
|
''', '''
|
|
[stdin]:1:3: error: '1' can't be assigned
|
|
({1}) ->
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
({a: true = 1}) ->
|
|
''', '''
|
|
[stdin]:1:6: error: keyword 'true' can't be assigned
|
|
({a: true = 1}) ->
|
|
^^^^
|
|
'''
|
|
|
|
test "`yield` outside of a function", ->
|
|
assertErrorFormat '''
|
|
yield 1
|
|
''', '''
|
|
[stdin]:1:1: error: yield can only occur inside functions
|
|
yield 1
|
|
^^^^^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
yield return
|
|
''', '''
|
|
[stdin]:1:1: error: yield can only occur inside functions
|
|
yield return
|
|
^^^^^^^^^^^^
|
|
'''
|
|
|
|
test "#4097: `yield return` as an expression", ->
|
|
assertErrorFormat '''
|
|
-> (yield return)
|
|
''', '''
|
|
[stdin]:1:5: error: cannot use a pure statement in an expression
|
|
-> (yield return)
|
|
^^^^^^^^^^^^
|
|
'''
|
|
|
|
test "`&&=` and `||=` with a space in-between", ->
|
|
assertErrorFormat '''
|
|
a = 0
|
|
a && = 1
|
|
''', '''
|
|
[stdin]:2:6: error: unexpected =
|
|
a && = 1
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
a = 0
|
|
a and = 1
|
|
''', '''
|
|
[stdin]:2:7: error: unexpected =
|
|
a and = 1
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
a = 0
|
|
a || = 1
|
|
''', '''
|
|
[stdin]:2:6: error: unexpected =
|
|
a || = 1
|
|
^
|
|
'''
|
|
assertErrorFormat '''
|
|
a = 0
|
|
a or = 1
|
|
''', '''
|
|
[stdin]:2:6: error: unexpected =
|
|
a or = 1
|
|
^
|
|
'''
|
|
|
|
test "anonymous functions cannot be exported", ->
|
|
assertErrorFormat '''
|
|
export ->
|
|
console.log 'hello, world!'
|
|
''', '''
|
|
[stdin]:1:8: error: unexpected ->
|
|
export ->
|
|
^^
|
|
'''
|
|
|
|
test "anonymous classes cannot be exported", ->
|
|
assertErrorFormat '''
|
|
export class
|
|
constructor: ->
|
|
console.log 'hello, world!'
|
|
''', '''
|
|
[stdin]:1:8: error: anonymous classes cannot be exported
|
|
export class
|
|
^^^^^
|
|
'''
|
|
|
|
test "unless enclosed by curly braces, only * can be aliased", ->
|
|
assertErrorFormat '''
|
|
import foo as bar from 'lib'
|
|
''', '''
|
|
[stdin]:1:12: error: unexpected as
|
|
import foo as bar from 'lib'
|
|
^^
|
|
'''
|
|
|
|
test "unwrapped imports must follow constrained syntax", ->
|
|
assertErrorFormat '''
|
|
import foo, bar from 'lib'
|
|
''', '''
|
|
[stdin]:1:13: error: unexpected identifier
|
|
import foo, bar from 'lib'
|
|
^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
import foo, bar, baz from 'lib'
|
|
''', '''
|
|
[stdin]:1:13: error: unexpected identifier
|
|
import foo, bar, baz from 'lib'
|
|
^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
import foo, bar as baz from 'lib'
|
|
''', '''
|
|
[stdin]:1:13: error: unexpected identifier
|
|
import foo, bar as baz from 'lib'
|
|
^^^
|
|
'''
|
|
|
|
test "cannot export * without a module to export from", ->
|
|
assertErrorFormat '''
|
|
export *
|
|
''', '''
|
|
[stdin]:1:9: error: unexpected end of input
|
|
export *
|
|
^
|
|
'''
|
|
|
|
test "imports and exports must be top-level", ->
|
|
assertErrorFormat '''
|
|
if foo
|
|
import { bar } from 'lib'
|
|
''', '''
|
|
[stdin]:2:3: error: import statements must be at top-level scope
|
|
import { bar } from 'lib'
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
foo = ->
|
|
export { bar }
|
|
''', '''
|
|
[stdin]:2:3: error: export statements must be at top-level scope
|
|
export { bar }
|
|
^^^^^^^^^^^^^^
|
|
'''
|
|
|
|
test "cannot import the same member more than once", ->
|
|
assertErrorFormat '''
|
|
import { foo, foo } from 'lib'
|
|
''', '''
|
|
[stdin]:1:15: error: 'foo' has already been declared
|
|
import { foo, foo } from 'lib'
|
|
^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
import { foo, bar, foo } from 'lib'
|
|
''', '''
|
|
[stdin]:1:20: error: 'foo' has already been declared
|
|
import { foo, bar, foo } from 'lib'
|
|
^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
import { foo, bar as foo } from 'lib'
|
|
''', '''
|
|
[stdin]:1:15: error: 'foo' has already been declared
|
|
import { foo, bar as foo } from 'lib'
|
|
^^^^^^^^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
import foo, { foo } from 'lib'
|
|
''', '''
|
|
[stdin]:1:15: error: 'foo' has already been declared
|
|
import foo, { foo } from 'lib'
|
|
^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
import foo, { bar as foo } from 'lib'
|
|
''', '''
|
|
[stdin]:1:15: error: 'foo' has already been declared
|
|
import foo, { bar as foo } from 'lib'
|
|
^^^^^^^^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
import foo from 'libA'
|
|
import foo from 'libB'
|
|
''', '''
|
|
[stdin]:2:8: error: 'foo' has already been declared
|
|
import foo from 'libB'
|
|
^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
import * as foo from 'libA'
|
|
import { foo } from 'libB'
|
|
''', '''
|
|
[stdin]:2:10: error: 'foo' has already been declared
|
|
import { foo } from 'libB'
|
|
^^^
|
|
'''
|
|
|
|
test "imported members cannot be reassigned", ->
|
|
assertErrorFormat '''
|
|
import { foo } from 'lib'
|
|
foo = 'bar'
|
|
''', '''
|
|
[stdin]:2:1: error: 'foo' is read-only
|
|
foo = 'bar'
|
|
^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
import { foo } from 'lib'
|
|
export default foo = 'bar'
|
|
''', '''
|
|
[stdin]:2:16: error: 'foo' is read-only
|
|
export default foo = 'bar'
|
|
^^^
|
|
'''
|
|
assertErrorFormat '''
|
|
import { foo } from 'lib'
|
|
export foo = 'bar'
|
|
''', '''
|
|
[stdin]:2:8: error: 'foo' is read-only
|
|
export foo = 'bar'
|
|
^^^
|
|
'''
|
|
|
|
test "bound functions cannot be generators", ->
|
|
assertErrorFormat 'f = => yield this', '''
|
|
[stdin]:1:8: error: yield cannot occur inside bound (fat arrow) functions
|
|
f = => yield this
|
|
^^^^^^^^^^
|
|
'''
|
|
|
|
test "CoffeeScript keywords cannot be used as unaliased names in import lists", ->
|
|
assertErrorFormat """
|
|
import { unless, baz as bar } from 'lib'
|
|
bar.barMethod()
|
|
""", '''
|
|
[stdin]:1:10: error: unexpected unless
|
|
import { unless, baz as bar } from 'lib'
|
|
^^^^^^
|
|
'''
|
|
|
|
test "CoffeeScript keywords cannot be used as local names in import list aliases", ->
|
|
assertErrorFormat """
|
|
import { bar as unless, baz as bar } from 'lib'
|
|
bar.barMethod()
|
|
""", '''
|
|
[stdin]:1:17: error: unexpected unless
|
|
import { bar as unless, baz as bar } from 'lib'
|
|
^^^^^^
|
|
'''
|
|
|
|
test "function cannot contain both `await` and `yield`", ->
|
|
assertErrorFormat '''
|
|
f = () ->
|
|
yield 5
|
|
await a
|
|
''', '''
|
|
[stdin]:3:3: error: function can't contain both yield and await
|
|
await a
|
|
^^^^^^^
|
|
'''
|
|
|
|
test "function cannot contain both `await` and `yield from`", ->
|
|
assertErrorFormat '''
|
|
f = () ->
|
|
yield from a
|
|
await b
|
|
''', '''
|
|
[stdin]:3:3: error: function can't contain both yield and await
|
|
await b
|
|
^^^^^^^
|
|
'''
|
|
|
|
test "cannot have `await` outside a function", ->
|
|
assertErrorFormat '''
|
|
await 1
|
|
''', '''
|
|
[stdin]:1:1: error: await can only occur inside functions
|
|
await 1
|
|
^^^^^^^
|
|
'''
|
|
|
|
test "indexes are not supported in for-from loops", ->
|
|
assertErrorFormat "x for x, i from [1, 2, 3]", '''
|
|
[stdin]:1:10: error: cannot use index with for-from
|
|
x for x, i from [1, 2, 3]
|
|
^
|
|
'''
|
|
|
|
test "own is not supported in for-from loops", ->
|
|
assertErrorFormat "x for own x from [1, 2, 3]", '''
|
|
[stdin]:1:7: error: cannot use own with for-from
|
|
x for own x from [1, 2, 3]
|
|
^^^
|
|
'''
|
|
|
|
test "tagged template literals must be called by an identifier", ->
|
|
assertErrorFormat "1''", '''
|
|
[stdin]:1:1: error: literal is not a function
|
|
1''
|
|
^
|
|
'''
|
|
assertErrorFormat '1""', '''
|
|
[stdin]:1:1: error: literal is not a function
|
|
1""
|
|
^
|
|
'''
|
|
assertErrorFormat "1'b'", '''
|
|
[stdin]:1:1: error: literal is not a function
|
|
1'b'
|
|
^
|
|
'''
|
|
assertErrorFormat '1"b"', '''
|
|
[stdin]:1:1: error: literal is not a function
|
|
1"b"
|
|
^
|
|
'''
|
|
assertErrorFormat "1'''b'''", """
|
|
[stdin]:1:1: error: literal is not a function
|
|
1'''b'''
|
|
^
|
|
"""
|
|
assertErrorFormat '1"""b"""', '''
|
|
[stdin]:1:1: error: literal is not a function
|
|
1"""b"""
|
|
^
|
|
'''
|
|
assertErrorFormat '1"#{b}"', '''
|
|
[stdin]:1:1: error: literal is not a function
|
|
1"#{b}"
|
|
^
|
|
'''
|
|
assertErrorFormat '1"""#{b}"""', '''
|
|
[stdin]:1:1: error: literal is not a function
|
|
1"""#{b}"""
|
|
^
|
|
'''
|
|
|
|
test "constructor functions can't be async", ->
|
|
assertErrorFormat 'class then constructor: -> await x', '''
|
|
[stdin]:1:12: error: Class constructor may not be async
|
|
class then constructor: -> await x
|
|
^^^^^^^^^^^
|
|
'''
|
|
|
|
test "constructor functions can't be generators", ->
|
|
assertErrorFormat 'class then constructor: -> yield', '''
|
|
[stdin]:1:12: error: Class constructor may not be a generator
|
|
class then constructor: -> yield
|
|
^^^^^^^^^^^
|
|
'''
|
|
|
|
test "non-derived constructors can't call super", ->
|
|
assertErrorFormat 'class then constructor: -> super', '''
|
|
[stdin]:1:28: error: 'super' is only allowed in derived class constructors
|
|
class then constructor: -> super
|
|
^^^^^
|
|
'''
|
|
|
|
test "derived constructors can't reference `this` before calling super", ->
|
|
assertErrorFormat 'class extends A then constructor: -> @', '''
|
|
[stdin]:1:38: error: Can't reference 'this' before calling super in derived class constructors
|
|
class extends A then constructor: -> @
|
|
^
|
|
'''
|
|
|
|
test "derived constructors can't use @params without calling super", ->
|
|
assertErrorFormat 'class extends A then constructor: (@a) ->', '''
|
|
[stdin]:1:36: error: Can't use @params in derived class constructors without calling super
|
|
class extends A then constructor: (@a) ->
|
|
^^
|
|
'''
|
|
|
|
test "'super' is not allowed in constructor parameter defaults", ->
|
|
assertErrorFormat 'class extends A then constructor: (a = super) ->', '''
|
|
[stdin]:1:40: error: 'super' is not allowed in constructor parameter defaults
|
|
class extends A then constructor: (a = super) ->
|
|
^^^^^
|
|
'''
|