2016-07-24 23:37:37 -04:00
|
|
|
|
# Error Formatting
|
|
|
|
|
# ----------------
|
2013-02-25 23:30:23 -05:00
|
|
|
|
|
|
|
|
|
# Ensure that errors of different kinds (lexer, parser and compiler) are shown
|
|
|
|
|
# in a consistent way.
|
|
|
|
|
|
|
|
|
|
assertErrorFormat = (code, expectedErrorFormat) ->
|
2013-03-21 02:11:31 -04:00
|
|
|
|
throws (-> CoffeeScript.run code), (err) ->
|
2013-07-31 07:27:49 -04:00
|
|
|
|
err.colorful = no
|
|
|
|
|
eq expectedErrorFormat, "#{err}"
|
2013-02-25 23:30:23 -05:00
|
|
|
|
yes
|
|
|
|
|
|
2016-07-24 23:37:37 -04:00
|
|
|
|
test "lexer errors formatting", ->
|
2013-02-25 23:30:23 -05:00
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
normalObject = {}
|
|
|
|
|
insideOutObject = }{
|
|
|
|
|
''',
|
|
|
|
|
'''
|
2013-07-31 07:27:49 -04:00
|
|
|
|
[stdin]:2:19: error: unmatched }
|
2013-02-25 23:30:23 -05:00
|
|
|
|
insideOutObject = }{
|
|
|
|
|
^
|
|
|
|
|
'''
|
|
|
|
|
|
2016-07-24 23:37:37 -04:00
|
|
|
|
test "parser error formatting", ->
|
2013-02-25 23:30:23 -05:00
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
foo in bar or in baz
|
|
|
|
|
''',
|
|
|
|
|
'''
|
2014-01-21 21:44:50 -05:00
|
|
|
|
[stdin]:1:15: error: unexpected in
|
2013-02-25 23:30:23 -05:00
|
|
|
|
foo in bar or in baz
|
|
|
|
|
^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "compiler error formatting", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
evil = (foo, eval, bar) ->
|
|
|
|
|
''',
|
|
|
|
|
'''
|
2016-03-05 14:59:39 -05:00
|
|
|
|
[stdin]:1:14: error: 'eval' can't be assigned
|
2013-02-25 23:30:23 -05:00
|
|
|
|
evil = (foo, eval, bar) ->
|
|
|
|
|
^^^^
|
2013-03-21 02:11:31 -04:00
|
|
|
|
'''
|
|
|
|
|
|
2014-01-27 11:55:20 -05:00
|
|
|
|
if require?
|
2017-01-22 16:20:18 -05:00
|
|
|
|
os = require 'os'
|
2014-01-27 11:55:20 -05:00
|
|
|
|
fs = require 'fs'
|
|
|
|
|
path = require 'path'
|
2013-03-21 02:11:31 -04:00
|
|
|
|
|
2017-01-22 16:20:18 -05:00
|
|
|
|
test "patchStackTrace line patching", ->
|
|
|
|
|
err = new Error 'error'
|
|
|
|
|
ok err.stack.match /test[\/\\]error_messages\.coffee:\d+:\d+\b/
|
|
|
|
|
|
|
|
|
|
test "patchStackTrace stack prelude consistent with V8", ->
|
|
|
|
|
err = new Error
|
|
|
|
|
ok err.stack.match /^Error\n/ # Notice no colon when no message.
|
|
|
|
|
|
|
|
|
|
err = new Error 'error'
|
|
|
|
|
ok err.stack.match /^Error: error\n/
|
|
|
|
|
|
2014-01-27 11:55:20 -05:00
|
|
|
|
test "#2849: compilation error in a require()d file", ->
|
|
|
|
|
# Create a temporary file to require().
|
2017-02-18 02:47:02 -05:00
|
|
|
|
tempFile = path.join os.tmpdir(), 'syntax-error.coffee'
|
|
|
|
|
ok not fs.existsSync tempFile
|
|
|
|
|
fs.writeFileSync tempFile, 'foo in bar or in baz'
|
2014-02-07 07:01:01 -05:00
|
|
|
|
|
2014-01-27 11:55:20 -05:00
|
|
|
|
try
|
2017-02-18 02:47:02 -05:00
|
|
|
|
assertErrorFormat """
|
2017-10-03 01:19:32 -04:00
|
|
|
|
require '#{tempFile.replace /\\/g, '\\\\'}'
|
2017-02-18 02:47:02 -05:00
|
|
|
|
""",
|
2014-01-27 11:55:20 -05:00
|
|
|
|
"""
|
2017-02-18 02:47:02 -05:00
|
|
|
|
#{fs.realpathSync tempFile}:1:15: error: unexpected in
|
2014-01-27 11:55:20 -05:00
|
|
|
|
foo in bar or in baz
|
|
|
|
|
^^
|
|
|
|
|
"""
|
|
|
|
|
finally
|
2017-02-18 02:47:02 -05:00
|
|
|
|
fs.unlinkSync tempFile
|
2014-01-21 21:44:50 -05:00
|
|
|
|
|
2017-08-23 09:50:46 -04:00
|
|
|
|
test "#3890: Error.prepareStackTrace doesn't throw an error if a compiled file is deleted", ->
|
2017-01-22 16:20:18 -05:00
|
|
|
|
# Adapted from https://github.com/atom/coffee-cash/blob/master/spec/coffee-cash-spec.coffee
|
|
|
|
|
filePath = path.join os.tmpdir(), 'PrepareStackTraceTestFile.coffee'
|
|
|
|
|
fs.writeFileSync filePath, "module.exports = -> throw new Error('hello world')"
|
|
|
|
|
throwsAnError = require filePath
|
|
|
|
|
fs.unlinkSync filePath
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
throwsAnError()
|
|
|
|
|
catch error
|
|
|
|
|
|
|
|
|
|
eq error.message, 'hello world'
|
|
|
|
|
doesNotThrow(-> error.stack)
|
|
|
|
|
notEqual error.stack.toString().indexOf(filePath), -1
|
|
|
|
|
|
2017-08-23 09:50:46 -04:00
|
|
|
|
test "#4418: stack traces for compiled files reference the correct line number", ->
|
2017-09-01 15:26:47 -04:00
|
|
|
|
# The browser is already compiling other anonymous scripts (the tests)
|
|
|
|
|
# which will conflict.
|
|
|
|
|
return if global.testingBrowser
|
2017-01-22 16:20:18 -05:00
|
|
|
|
filePath = path.join os.tmpdir(), 'StackTraceLineNumberTestFile.coffee'
|
|
|
|
|
fileContents = """
|
|
|
|
|
testCompiledFileStackTraceLineNumber = ->
|
|
|
|
|
# `a` on the next line is undefined and should throw a ReferenceError
|
|
|
|
|
console.log a if true
|
|
|
|
|
|
|
|
|
|
do testCompiledFileStackTraceLineNumber
|
|
|
|
|
"""
|
|
|
|
|
fs.writeFileSync filePath, fileContents
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
require filePath
|
|
|
|
|
catch error
|
|
|
|
|
fs.unlinkSync filePath
|
|
|
|
|
|
|
|
|
|
# Make sure the line number reported is line 3 (the original Coffee source)
|
|
|
|
|
# and not line 6 (the generated JavaScript).
|
|
|
|
|
eq /StackTraceLineNumberTestFile.coffee:(\d)/.exec(error.stack.toString())[1], '3'
|
|
|
|
|
|
|
|
|
|
|
2017-08-23 09:50:46 -04:00
|
|
|
|
test "#4418: stack traces for compiled strings reference the correct line number", ->
|
2017-09-01 15:26:47 -04:00
|
|
|
|
# The browser is already compiling other anonymous scripts (the tests)
|
|
|
|
|
# which will conflict.
|
|
|
|
|
return if global.testingBrowser
|
2017-01-22 16:20:18 -05:00
|
|
|
|
try
|
2017-08-23 09:50:46 -04:00
|
|
|
|
CoffeeScript.run '''
|
2017-01-22 16:20:18 -05:00
|
|
|
|
testCompiledStringStackTraceLineNumber = ->
|
|
|
|
|
# `a` on the next line is undefined and should throw a ReferenceError
|
|
|
|
|
console.log a if true
|
|
|
|
|
|
|
|
|
|
do testCompiledStringStackTraceLineNumber
|
2017-08-23 09:50:46 -04:00
|
|
|
|
'''
|
2017-01-22 16:20:18 -05:00
|
|
|
|
catch error
|
|
|
|
|
|
|
|
|
|
# Make sure the line number reported is line 3 (the original Coffee source)
|
|
|
|
|
# and not line 6 (the generated JavaScript).
|
2017-02-20 18:59:31 -05:00
|
|
|
|
eq /testCompiledStringStackTraceLineNumber.*:(\d):/.exec(error.stack.toString())[1], '3'
|
2017-01-22 16:20:18 -05:00
|
|
|
|
|
2014-02-07 07:01:01 -05:00
|
|
|
|
|
2017-08-23 09:50:46 -04:00
|
|
|
|
test "#4558: compiling a string inside a script doesn’t screw up stack trace line number", ->
|
2017-09-01 15:26:47 -04:00
|
|
|
|
# The browser is already compiling other anonymous scripts (the tests)
|
|
|
|
|
# which will conflict.
|
|
|
|
|
return if global.testingBrowser
|
2017-08-23 09:50:46 -04:00
|
|
|
|
try
|
|
|
|
|
CoffeeScript.run '''
|
|
|
|
|
testCompilingInsideAScriptDoesntScrewUpStackTraceLineNumber = ->
|
|
|
|
|
if require?
|
|
|
|
|
CoffeeScript = require './lib/coffeescript'
|
|
|
|
|
CoffeeScript.compile ''
|
|
|
|
|
throw new Error 'Some Error'
|
|
|
|
|
|
|
|
|
|
do testCompilingInsideAScriptDoesntScrewUpStackTraceLineNumber
|
|
|
|
|
'''
|
|
|
|
|
catch error
|
|
|
|
|
|
|
|
|
|
# Make sure the line number reported is line 5 (the original Coffee source)
|
|
|
|
|
# and not line 10 (the generated JavaScript).
|
|
|
|
|
eq /testCompilingInsideAScriptDoesntScrewUpStackTraceLineNumber.*:(\d):/.exec(error.stack.toString())[1], '5'
|
|
|
|
|
|
2014-01-21 21:44:50 -05:00
|
|
|
|
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:
|
|
|
|
|
^
|
|
|
|
|
'''
|
2015-01-12 14:40:59 -05:00
|
|
|
|
assertErrorFormat 'a +', '''
|
|
|
|
|
[stdin]:1:4: error: unexpected end of input
|
|
|
|
|
a +
|
|
|
|
|
^
|
|
|
|
|
'''
|
Fix #3597: Allow interpolations in object keys
The following is now allowed:
o =
a: 1
b: 2
"#{'c'}": 3
"#{'d'}": 4
e: 5
"#{'f'}": 6
g: 7
It compiles to:
o = (
obj = {
a: 1,
b: 2
},
obj["" + 'c'] = 3,
obj["" + 'd'] = 4,
obj.e = 5,
obj["" + 'f'] = 6,
obj.g = 7,
obj
);
- Closes #3039. Empty interpolations in object keys are now _supposed_ to be
allowed.
- Closes #1131. No need to improve error messages for attempted key
interpolation anymore.
- Implementing this required fixing the following bug: `("" + a): 1` used to
error out on the colon, saying "unexpected colon". But really, it is the
attempted object key that is unexpected. Now the error is on the opening
parenthesis instead.
- However, the above fix broke some error message tests for regexes. The easiest
way to fix this was to make a seemingly unrelated change: The error messages
for unexpected identifiers, numbers, strings and regexes now say for example
'unexpected string' instead of 'unexpected """some #{really long} string"""'.
In other words, the tag _name_ is used instead of the tag _value_.
This was way easier to implement, and is more helpful to the user. Using the
tag value is good for operators, reserved words and the like, but not for
tokens which can contain any text. For example, 'unexpected identifier' is
better than 'unexpected expected' (if a variable called 'expected' was used
erraneously).
- While writing tests for the above point I found a few minor bugs with string
locations which have been fixed.
2015-02-07 14:16:59 -05:00
|
|
|
|
# Unexpected key in implicit object (an implicit object itself is _not_
|
|
|
|
|
# unexpected here)
|
2014-01-21 21:44:50 -05:00
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
for i in [1]:
|
|
|
|
|
1
|
|
|
|
|
''', '''
|
2017-12-09 01:42:47 -05:00
|
|
|
|
[stdin]:2:4: error: unexpected end of input
|
|
|
|
|
1
|
|
|
|
|
^
|
2014-01-21 21:44:50 -05:00
|
|
|
|
'''
|
2015-02-03 14:42:50 -05:00
|
|
|
|
# Unexpected regex
|
|
|
|
|
assertErrorFormat '{/a/i: val}', '''
|
Fix #3597: Allow interpolations in object keys
The following is now allowed:
o =
a: 1
b: 2
"#{'c'}": 3
"#{'d'}": 4
e: 5
"#{'f'}": 6
g: 7
It compiles to:
o = (
obj = {
a: 1,
b: 2
},
obj["" + 'c'] = 3,
obj["" + 'd'] = 4,
obj.e = 5,
obj["" + 'f'] = 6,
obj.g = 7,
obj
);
- Closes #3039. Empty interpolations in object keys are now _supposed_ to be
allowed.
- Closes #1131. No need to improve error messages for attempted key
interpolation anymore.
- Implementing this required fixing the following bug: `("" + a): 1` used to
error out on the colon, saying "unexpected colon". But really, it is the
attempted object key that is unexpected. Now the error is on the opening
parenthesis instead.
- However, the above fix broke some error message tests for regexes. The easiest
way to fix this was to make a seemingly unrelated change: The error messages
for unexpected identifiers, numbers, strings and regexes now say for example
'unexpected string' instead of 'unexpected """some #{really long} string"""'.
In other words, the tag _name_ is used instead of the tag _value_.
This was way easier to implement, and is more helpful to the user. Using the
tag value is good for operators, reserved words and the like, but not for
tokens which can contain any text. For example, 'unexpected identifier' is
better than 'unexpected expected' (if a variable called 'expected' was used
erraneously).
- While writing tests for the above point I found a few minor bugs with string
locations which have been fixed.
2015-02-07 14:16:59 -05:00
|
|
|
|
[stdin]:1:2: error: unexpected regex
|
2015-02-03 14:42:50 -05:00
|
|
|
|
{/a/i: val}
|
|
|
|
|
^^^^
|
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '{///a///i: val}', '''
|
Fix #3597: Allow interpolations in object keys
The following is now allowed:
o =
a: 1
b: 2
"#{'c'}": 3
"#{'d'}": 4
e: 5
"#{'f'}": 6
g: 7
It compiles to:
o = (
obj = {
a: 1,
b: 2
},
obj["" + 'c'] = 3,
obj["" + 'd'] = 4,
obj.e = 5,
obj["" + 'f'] = 6,
obj.g = 7,
obj
);
- Closes #3039. Empty interpolations in object keys are now _supposed_ to be
allowed.
- Closes #1131. No need to improve error messages for attempted key
interpolation anymore.
- Implementing this required fixing the following bug: `("" + a): 1` used to
error out on the colon, saying "unexpected colon". But really, it is the
attempted object key that is unexpected. Now the error is on the opening
parenthesis instead.
- However, the above fix broke some error message tests for regexes. The easiest
way to fix this was to make a seemingly unrelated change: The error messages
for unexpected identifiers, numbers, strings and regexes now say for example
'unexpected string' instead of 'unexpected """some #{really long} string"""'.
In other words, the tag _name_ is used instead of the tag _value_.
This was way easier to implement, and is more helpful to the user. Using the
tag value is good for operators, reserved words and the like, but not for
tokens which can contain any text. For example, 'unexpected identifier' is
better than 'unexpected expected' (if a variable called 'expected' was used
erraneously).
- While writing tests for the above point I found a few minor bugs with string
locations which have been fixed.
2015-02-07 14:16:59 -05:00
|
|
|
|
[stdin]:1:2: error: unexpected regex
|
2015-02-03 14:42:50 -05:00
|
|
|
|
{///a///i: val}
|
|
|
|
|
^^^^^^^^
|
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '{///#{a}///i: val}', '''
|
Fix #3597: Allow interpolations in object keys
The following is now allowed:
o =
a: 1
b: 2
"#{'c'}": 3
"#{'d'}": 4
e: 5
"#{'f'}": 6
g: 7
It compiles to:
o = (
obj = {
a: 1,
b: 2
},
obj["" + 'c'] = 3,
obj["" + 'd'] = 4,
obj.e = 5,
obj["" + 'f'] = 6,
obj.g = 7,
obj
);
- Closes #3039. Empty interpolations in object keys are now _supposed_ to be
allowed.
- Closes #1131. No need to improve error messages for attempted key
interpolation anymore.
- Implementing this required fixing the following bug: `("" + a): 1` used to
error out on the colon, saying "unexpected colon". But really, it is the
attempted object key that is unexpected. Now the error is on the opening
parenthesis instead.
- However, the above fix broke some error message tests for regexes. The easiest
way to fix this was to make a seemingly unrelated change: The error messages
for unexpected identifiers, numbers, strings and regexes now say for example
'unexpected string' instead of 'unexpected """some #{really long} string"""'.
In other words, the tag _name_ is used instead of the tag _value_.
This was way easier to implement, and is more helpful to the user. Using the
tag value is good for operators, reserved words and the like, but not for
tokens which can contain any text. For example, 'unexpected identifier' is
better than 'unexpected expected' (if a variable called 'expected' was used
erraneously).
- While writing tests for the above point I found a few minor bugs with string
locations which have been fixed.
2015-02-07 14:16:59 -05:00
|
|
|
|
[stdin]:1:2: error: unexpected regex
|
2015-02-03 14:42:50 -05:00
|
|
|
|
{///#{a}///i: val}
|
|
|
|
|
^^^^^^^^^^^
|
|
|
|
|
'''
|
Fix #3597: Allow interpolations in object keys
The following is now allowed:
o =
a: 1
b: 2
"#{'c'}": 3
"#{'d'}": 4
e: 5
"#{'f'}": 6
g: 7
It compiles to:
o = (
obj = {
a: 1,
b: 2
},
obj["" + 'c'] = 3,
obj["" + 'd'] = 4,
obj.e = 5,
obj["" + 'f'] = 6,
obj.g = 7,
obj
);
- Closes #3039. Empty interpolations in object keys are now _supposed_ to be
allowed.
- Closes #1131. No need to improve error messages for attempted key
interpolation anymore.
- Implementing this required fixing the following bug: `("" + a): 1` used to
error out on the colon, saying "unexpected colon". But really, it is the
attempted object key that is unexpected. Now the error is on the opening
parenthesis instead.
- However, the above fix broke some error message tests for regexes. The easiest
way to fix this was to make a seemingly unrelated change: The error messages
for unexpected identifiers, numbers, strings and regexes now say for example
'unexpected string' instead of 'unexpected """some #{really long} string"""'.
In other words, the tag _name_ is used instead of the tag _value_.
This was way easier to implement, and is more helpful to the user. Using the
tag value is good for operators, reserved words and the like, but not for
tokens which can contain any text. For example, 'unexpected identifier' is
better than 'unexpected expected' (if a variable called 'expected' was used
erraneously).
- While writing tests for the above point I found a few minor bugs with string
locations which have been fixed.
2015-02-07 14:16:59 -05:00
|
|
|
|
# Unexpected string
|
Support import and export of ES2015 modules (#4300)
This pull request adds support for ES2015 modules, by recognizing `import` and `export` statements. The following syntaxes are supported, based on the MDN [import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) and [export](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export) pages:
```js
import "module-name"
import defaultMember from "module-name"
import * as name from "module-name"
import { } from "module-name"
import { member } from "module-name"
import { member as alias } from "module-name"
import { member1, member2 as alias2, … } from "module-name"
import defaultMember, * as name from "module-name"
import defaultMember, { … } from "module-name"
export default expression
export class name
export { }
export { name }
export { name as exportedName }
export { name as default }
export { name1, name2 as exportedName2, name3 as default, … }
export * from "module-name"
export { … } from "module-name"
```
As a subsitute for ECMAScript’s `export var name = …` and `export function name {}`, CoffeeScript also supports:
```js
export name = …
```
CoffeeScript also supports optional commas within `{ … }`.
This PR converts the supported `import` and `export` statements into ES2015 `import` and `export` statements; it **does not resolve the modules**. So any CoffeeScript with `import` or `export` statements will be output as ES2015, and will need to be transpiled by another tool such as Babel before it can be used in a browser. We will need to add a warning to the documentation explaining this.
This should be fully backwards-compatible, as `import` and `export` were previously reserved keywords. No flags are used.
There are extensive tests included, though because no current JavaScript runtime supports `import` or `export`, the tests compare strings of what the compiled CoffeeScript output is against what the expected ES2015 should be. I also conducted two more elaborate tests:
* I forked the [ember-piqu](https://github.com/pauc/piqu-ember) project, which was an Ember CLI app that used ember-cli-coffeescript and [ember-cli-coffees6](https://github.com/alexspeller/ember-cli-coffees6) (which adds “support” for `import`/`export` by wrapping such statements in backticks before passing the result to the CoffeeScript compiler). I removed `ember-cli-coffees6` and replaced the CoffeeScript compiler used in the build chain with this code, and the app built without errors. [Demo here.](https://github.com/GeoffreyBooth/coffeescript-modules-test-piqu)
* I also forked the [CoffeeScript version of Meteor’s Todos example app](https://github.com/meteor/todos/tree/coffeescript), and replaced all of its `require` statements with the `import` and `export` statements from the original ES2015 version of the app on its `master` branch. I then updated the `coffeescript` Meteor package in the app to use this new code, and again the app builds without errors. [Demo here.](https://github.com/GeoffreyBooth/coffeescript-modules-test-meteor-todos)
The discussion history for this work started [here](https://github.com/jashkenas/coffeescript/pull/4160) and continued [here](https://github.com/GeoffreyBooth/coffeescript/pull/2). @lydell provided guidance, and @JimPanic and @rattrayalex contributed essential code.
2016-09-14 14:46:05 -04:00
|
|
|
|
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}"
|
|
|
|
|
^^^^^^^^^^^^^^^^
|
|
|
|
|
'''
|
|
|
|
|
|
Fix #3597: Allow interpolations in object keys
The following is now allowed:
o =
a: 1
b: 2
"#{'c'}": 3
"#{'d'}": 4
e: 5
"#{'f'}": 6
g: 7
It compiles to:
o = (
obj = {
a: 1,
b: 2
},
obj["" + 'c'] = 3,
obj["" + 'd'] = 4,
obj.e = 5,
obj["" + 'f'] = 6,
obj.g = 7,
obj
);
- Closes #3039. Empty interpolations in object keys are now _supposed_ to be
allowed.
- Closes #1131. No need to improve error messages for attempted key
interpolation anymore.
- Implementing this required fixing the following bug: `("" + a): 1` used to
error out on the colon, saying "unexpected colon". But really, it is the
attempted object key that is unexpected. Now the error is on the opening
parenthesis instead.
- However, the above fix broke some error message tests for regexes. The easiest
way to fix this was to make a seemingly unrelated change: The error messages
for unexpected identifiers, numbers, strings and regexes now say for example
'unexpected string' instead of 'unexpected """some #{really long} string"""'.
In other words, the tag _name_ is used instead of the tag _value_.
This was way easier to implement, and is more helpful to the user. Using the
tag value is good for operators, reserved words and the like, but not for
tokens which can contain any text. For example, 'unexpected identifier' is
better than 'unexpected expected' (if a variable called 'expected' was used
erraneously).
- While writing tests for the above point I found a few minor bugs with string
locations which have been fixed.
2015-02-07 14:16:59 -05:00
|
|
|
|
# Unexpected number
|
|
|
|
|
assertErrorFormat '"a"0x00Af2', '''
|
|
|
|
|
[stdin]:1:4: error: unexpected number
|
|
|
|
|
"a"0x00Af2
|
|
|
|
|
^^^^^^^
|
|
|
|
|
'''
|
2014-01-26 00:25:13 -05:00
|
|
|
|
|
2015-01-12 14:10:54 -05:00
|
|
|
|
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.}"
|
|
|
|
|
^
|
|
|
|
|
'''
|
|
|
|
|
|
2014-01-26 00:25:13 -05:00
|
|
|
|
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
|
|
|
|
|
^^
|
|
|
|
|
'''
|
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
|
|
|
|
|
|
|
|
|
test "unclosed strings", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
'
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:1: error: missing '
|
|
|
|
|
'
|
|
|
|
|
^
|
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
"
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:1: error: missing "
|
|
|
|
|
"
|
|
|
|
|
^
|
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat """
|
|
|
|
|
'''
|
|
|
|
|
""", """
|
|
|
|
|
[stdin]:1:1: error: missing '''
|
|
|
|
|
'''
|
2015-02-06 04:52:02 -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
|
|
|
|
"""
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
"""
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:1: error: missing """
|
|
|
|
|
"""
|
2015-02-06 04:52:02 -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
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
"#{"
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:4: error: missing "
|
|
|
|
|
"#{"
|
|
|
|
|
^
|
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
"""#{"
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:6: error: missing "
|
|
|
|
|
"""#{"
|
|
|
|
|
^
|
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
"#{"""
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:4: error: missing """
|
|
|
|
|
"#{"""
|
2015-02-06 04:52:02 -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
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
"""#{"""
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:6: error: missing """
|
|
|
|
|
"""#{"""
|
2015-02-06 04:52:02 -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
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
///#{"""
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:6: error: missing """
|
|
|
|
|
///#{"""
|
2015-02-06 04:52:02 -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
|
|
|
|
'''
|
|
|
|
|
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\\"""
|
2015-02-06 04:52:02 -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
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "unclosed heregexes", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
///
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:1: error: missing ///
|
|
|
|
|
///
|
2015-02-06 04:52:02 -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
|
|
|
|
'''
|
|
|
|
|
# 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\\///
|
2015-02-06 04:52:02 -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
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "unexpected token after string", ->
|
|
|
|
|
# Parsing error.
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
'foo'bar
|
|
|
|
|
''', '''
|
Fix #3597: Allow interpolations in object keys
The following is now allowed:
o =
a: 1
b: 2
"#{'c'}": 3
"#{'d'}": 4
e: 5
"#{'f'}": 6
g: 7
It compiles to:
o = (
obj = {
a: 1,
b: 2
},
obj["" + 'c'] = 3,
obj["" + 'd'] = 4,
obj.e = 5,
obj["" + 'f'] = 6,
obj.g = 7,
obj
);
- Closes #3039. Empty interpolations in object keys are now _supposed_ to be
allowed.
- Closes #1131. No need to improve error messages for attempted key
interpolation anymore.
- Implementing this required fixing the following bug: `("" + a): 1` used to
error out on the colon, saying "unexpected colon". But really, it is the
attempted object key that is unexpected. Now the error is on the opening
parenthesis instead.
- However, the above fix broke some error message tests for regexes. The easiest
way to fix this was to make a seemingly unrelated change: The error messages
for unexpected identifiers, numbers, strings and regexes now say for example
'unexpected string' instead of 'unexpected """some #{really long} string"""'.
In other words, the tag _name_ is used instead of the tag _value_.
This was way easier to implement, and is more helpful to the user. Using the
tag value is good for operators, reserved words and the like, but not for
tokens which can contain any text. For example, 'unexpected identifier' is
better than 'unexpected expected' (if a variable called 'expected' was used
erraneously).
- While writing tests for the above point I found a few minor bugs with string
locations which have been fixed.
2015-02-07 14:16:59 -05:00
|
|
|
|
[stdin]:1:6: error: unexpected identifier
|
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
|
|
|
|
'foo'bar
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
"foo"bar
|
|
|
|
|
''', '''
|
Fix #3597: Allow interpolations in object keys
The following is now allowed:
o =
a: 1
b: 2
"#{'c'}": 3
"#{'d'}": 4
e: 5
"#{'f'}": 6
g: 7
It compiles to:
o = (
obj = {
a: 1,
b: 2
},
obj["" + 'c'] = 3,
obj["" + 'd'] = 4,
obj.e = 5,
obj["" + 'f'] = 6,
obj.g = 7,
obj
);
- Closes #3039. Empty interpolations in object keys are now _supposed_ to be
allowed.
- Closes #1131. No need to improve error messages for attempted key
interpolation anymore.
- Implementing this required fixing the following bug: `("" + a): 1` used to
error out on the colon, saying "unexpected colon". But really, it is the
attempted object key that is unexpected. Now the error is on the opening
parenthesis instead.
- However, the above fix broke some error message tests for regexes. The easiest
way to fix this was to make a seemingly unrelated change: The error messages
for unexpected identifiers, numbers, strings and regexes now say for example
'unexpected string' instead of 'unexpected """some #{really long} string"""'.
In other words, the tag _name_ is used instead of the tag _value_.
This was way easier to implement, and is more helpful to the user. Using the
tag value is good for operators, reserved words and the like, but not for
tokens which can contain any text. For example, 'unexpected identifier' is
better than 'unexpected expected' (if a variable called 'expected' was used
erraneously).
- While writing tests for the above point I found a few minor bugs with string
locations which have been fixed.
2015-02-07 14:16:59 -05:00
|
|
|
|
[stdin]:1:6: error: unexpected identifier
|
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
|
|
|
|
"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 '''
|
Fix #3597: Allow interpolations in object keys
The following is now allowed:
o =
a: 1
b: 2
"#{'c'}": 3
"#{'d'}": 4
e: 5
"#{'f'}": 6
g: 7
It compiles to:
o = (
obj = {
a: 1,
b: 2
},
obj["" + 'c'] = 3,
obj["" + 'd'] = 4,
obj.e = 5,
obj["" + 'f'] = 6,
obj.g = 7,
obj
);
- Closes #3039. Empty interpolations in object keys are now _supposed_ to be
allowed.
- Closes #1131. No need to improve error messages for attempted key
interpolation anymore.
- Implementing this required fixing the following bug: `("" + a): 1` used to
error out on the colon, saying "unexpected colon". But really, it is the
attempted object key that is unexpected. Now the error is on the opening
parenthesis instead.
- However, the above fix broke some error message tests for regexes. The easiest
way to fix this was to make a seemingly unrelated change: The error messages
for unexpected identifiers, numbers, strings and regexes now say for example
'unexpected string' instead of 'unexpected """some #{really long} string"""'.
In other words, the tag _name_ is used instead of the tag _value_.
This was way easier to implement, and is more helpful to the user. Using the
tag value is good for operators, reserved words and the like, but not for
tokens which can contain any text. For example, 'unexpected identifier' is
better than 'unexpected expected' (if a variable called 'expected' was used
erraneously).
- While writing tests for the above point I found a few minor bugs with string
locations which have been fixed.
2015-02-07 14:16:59 -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
|
|
|
|
''', '''
|
Fix #3597: Allow interpolations in object keys
The following is now allowed:
o =
a: 1
b: 2
"#{'c'}": 3
"#{'d'}": 4
e: 5
"#{'f'}": 6
g: 7
It compiles to:
o = (
obj = {
a: 1,
b: 2
},
obj["" + 'c'] = 3,
obj["" + 'd'] = 4,
obj.e = 5,
obj["" + 'f'] = 6,
obj.g = 7,
obj
);
- Closes #3039. Empty interpolations in object keys are now _supposed_ to be
allowed.
- Closes #1131. No need to improve error messages for attempted key
interpolation anymore.
- Implementing this required fixing the following bug: `("" + a): 1` used to
error out on the colon, saying "unexpected colon". But really, it is the
attempted object key that is unexpected. Now the error is on the opening
parenthesis instead.
- However, the above fix broke some error message tests for regexes. The easiest
way to fix this was to make a seemingly unrelated change: The error messages
for unexpected identifiers, numbers, strings and regexes now say for example
'unexpected string' instead of 'unexpected """some #{really long} string"""'.
In other words, the tag _name_ is used instead of the tag _value_.
This was way easier to implement, and is more helpful to the user. Using the
tag value is good for operators, reserved words and the like, but not for
tokens which can contain any text. For example, 'unexpected identifier' is
better than 'unexpected expected' (if a variable called 'expected' was used
erraneously).
- While writing tests for the above point I found a few minor bugs with string
locations which have been fixed.
2015-02-07 14:16:59 -05:00
|
|
|
|
[stdin]:1:5: error: unexpected *
|
|
|
|
|
"#{ * }"
|
|
|
|
|
^
|
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
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "octal escapes", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
"a\\0\\tb\\\\\\07c"
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:10: error: octal escape sequences are not allowed \\07
|
|
|
|
|
"a\\0\\tb\\\\\\07c"
|
2015-02-06 04:52:02 -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
|
|
|
|
'''
|
2015-02-05 11:23:03 -05:00
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
"a
|
|
|
|
|
#{b} \\1"
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:2:8: error: octal escape sequences are not allowed \\1
|
|
|
|
|
#{b} \\1"
|
2015-02-06 04:52:02 -05:00
|
|
|
|
^\^
|
2015-02-05 11:23:03 -05:00
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
/a\\0\\tb\\\\\\07c/
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:10: error: octal escape sequences are not allowed \\07
|
|
|
|
|
/a\\0\\tb\\\\\\07c/
|
2015-02-06 04:52:02 -05:00
|
|
|
|
\ \ \ \ ^\^^
|
2015-02-05 11:23:03 -05:00
|
|
|
|
'''
|
2017-04-06 19:39:13 -04:00
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
/a\\1\\tb\\\\\\07c/
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:10: error: octal escape sequences are not allowed \\07
|
|
|
|
|
/a\\1\\tb\\\\\\07c/
|
|
|
|
|
\ \ \ \ ^\^^
|
|
|
|
|
'''
|
2015-02-05 11:23:03 -05:00
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
///a
|
|
|
|
|
#{b} \\01///
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:2:8: error: octal escape sequences are not allowed \\01
|
|
|
|
|
#{b} \\01///
|
2015-02-06 04:52:02 -05:00
|
|
|
|
^\^^
|
2015-02-05 11:23:03 -05:00
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "#3795: invalid escapes", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
"a\\0\\tb\\\\\\x7g"
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:10: error: invalid escape sequence \\x7g
|
|
|
|
|
"a\\0\\tb\\\\\\x7g"
|
2015-02-06 04:52:02 -05:00
|
|
|
|
\ \ \ \ ^\^^^
|
2015-02-05 11:23:03 -05:00
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
"a
|
|
|
|
|
#{b} \\uA02
|
|
|
|
|
c"
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:2:8: error: invalid escape sequence \\uA02
|
|
|
|
|
#{b} \\uA02
|
2015-02-06 04:52:02 -05:00
|
|
|
|
^\^^^^
|
2015-02-05 11:23:03 -05:00
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
/a\\u002space/
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:3: error: invalid escape sequence \\u002s
|
|
|
|
|
/a\\u002space/
|
2015-02-06 04:52:02 -05:00
|
|
|
|
^\^^^^^
|
2015-02-05 11:23:03 -05:00
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
///a \\u002 0 space///
|
|
|
|
|
''', '''
|
2016-12-21 03:24:05 -05:00
|
|
|
|
[stdin]:1:6: error: invalid escape sequence \\u002 \n\
|
2015-02-05 11:23:03 -05:00
|
|
|
|
///a \\u002 0 space///
|
2015-02-06 04:52:02 -05:00
|
|
|
|
^\^^^^^
|
2015-02-05 11:23:03 -05:00
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
///a
|
|
|
|
|
#{b} \\x0
|
|
|
|
|
c///
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:2:8: error: invalid escape sequence \\x0
|
|
|
|
|
#{b} \\x0
|
2015-02-06 04:52:02 -05:00
|
|
|
|
^\^^
|
2015-02-05 11:23:03 -05:00
|
|
|
|
'''
|
2015-02-12 13:26:41 -05:00
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
/ab\\u/
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:4: error: invalid escape sequence \\u
|
|
|
|
|
/ab\\u/
|
|
|
|
|
^\^
|
|
|
|
|
'''
|
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
|
|
|
|
|
|
|
|
|
test "illegal herecomment", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
###
|
|
|
|
|
Regex: /a*/g
|
|
|
|
|
###
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:2:12: error: block comments cannot contain */
|
|
|
|
|
Regex: /a*/g
|
2015-02-06 04:52:02 -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
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
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
|
2015-02-06 04:52:02 -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
|
|
|
|
'''
|
|
|
|
|
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
|
2015-02-06 04:52:02 -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
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
/a/g_
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:4: error: invalid regular expression flags g_
|
|
|
|
|
/a/g_
|
2015-02-06 04:52:02 -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
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
///a///ii
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:8: error: invalid regular expression flags ii
|
|
|
|
|
///a///ii
|
2015-02-06 04:52:02 -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
|
|
|
|
'''
|
|
|
|
|
doesNotThrow -> CoffeeScript.compile '/a/ymgi'
|
2015-01-03 18:28:23 -05:00
|
|
|
|
|
|
|
|
|
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}"
|
|
|
|
|
^
|
|
|
|
|
'''
|
2015-01-09 19:48:00 -05:00
|
|
|
|
|
|
|
|
|
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}"" """}
|
|
|
|
|
^
|
|
|
|
|
'''
|
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
|
|
|
|
|
|
|
|
|
test "duplicate function arguments", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
(foo, bar, foo) ->
|
|
|
|
|
''', '''
|
2016-09-25 02:23:24 -04:00
|
|
|
|
[stdin]:1:12: error: multiple parameters named 'foo'
|
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
|
|
|
|
(foo, bar, foo) ->
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
(@foo, bar, @foo) ->
|
|
|
|
|
''', '''
|
2016-09-25 02:23:24 -04:00
|
|
|
|
[stdin]:1:13: error: multiple parameters named '@foo'
|
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
|
|
|
|
(@foo, bar, @foo) ->
|
|
|
|
|
^^^^
|
|
|
|
|
'''
|
2015-02-06 04:52:02 -05:00
|
|
|
|
|
|
|
|
|
test "reserved words", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
case
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:1: error: reserved word 'case'
|
|
|
|
|
case
|
|
|
|
|
^^^^
|
|
|
|
|
'''
|
2016-03-05 14:59:39 -05:00
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
case = 1
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:1: error: reserved word 'case'
|
|
|
|
|
case = 1
|
|
|
|
|
^^^^
|
|
|
|
|
'''
|
2015-02-06 04:52:02 -05:00
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
for = 1
|
|
|
|
|
''', '''
|
2016-03-05 14:59:39 -05:00
|
|
|
|
[stdin]:1:1: error: keyword 'for' can't be assigned
|
2015-02-06 04:52:02 -05:00
|
|
|
|
for = 1
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
2016-03-05 14:59:39 -05:00
|
|
|
|
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
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
2015-05-01 08:33:11 -04:00
|
|
|
|
# #2306: Show unaliased name in error messages.
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
on = 1
|
|
|
|
|
''', '''
|
2016-03-05 14:59:39 -05:00
|
|
|
|
[stdin]:1:1: error: keyword 'on' can't be assigned
|
2015-05-01 08:33:11 -04:00
|
|
|
|
on = 1
|
|
|
|
|
^^
|
|
|
|
|
'''
|
2015-02-06 04:52:02 -05:00
|
|
|
|
|
2016-03-05 14:59:39 -05:00
|
|
|
|
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
|
|
|
|
|
^^^^^^^^^
|
|
|
|
|
'''
|
|
|
|
|
|
2015-02-06 04:52:02 -05:00
|
|
|
|
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
|
|
|
|
|
^^^
|
Fix #3597: Allow interpolations in object keys
The following is now allowed:
o =
a: 1
b: 2
"#{'c'}": 3
"#{'d'}": 4
e: 5
"#{'f'}": 6
g: 7
It compiles to:
o = (
obj = {
a: 1,
b: 2
},
obj["" + 'c'] = 3,
obj["" + 'd'] = 4,
obj.e = 5,
obj["" + 'f'] = 6,
obj.g = 7,
obj
);
- Closes #3039. Empty interpolations in object keys are now _supposed_ to be
allowed.
- Closes #1131. No need to improve error messages for attempted key
interpolation anymore.
- Implementing this required fixing the following bug: `("" + a): 1` used to
error out on the colon, saying "unexpected colon". But really, it is the
attempted object key that is unexpected. Now the error is on the opening
parenthesis instead.
- However, the above fix broke some error message tests for regexes. The easiest
way to fix this was to make a seemingly unrelated change: The error messages
for unexpected identifiers, numbers, strings and regexes now say for example
'unexpected string' instead of 'unexpected """some #{really long} string"""'.
In other words, the tag _name_ is used instead of the tag _value_.
This was way easier to implement, and is more helpful to the user. Using the
tag value is good for operators, reserved words and the like, but not for
tokens which can contain any text. For example, 'unexpected identifier' is
better than 'unexpected expected' (if a variable called 'expected' was used
erraneously).
- While writing tests for the above point I found a few minor bugs with string
locations which have been fixed.
2015-02-07 14:16:59 -05:00
|
|
|
|
'''
|
|
|
|
|
|
[CS2] Destructuring object spreads (#4493)
* Don’t confuse the syntax highlighter
* Comment Assign::compilePatternMatch a bit
* Assignment expressions in conditionals are a bad practice
* Rename `wrapInBraces` to `wrapInParentheses`, to set the stage for future `wrapInBraces` that uses `{` and `wrapInBrackets` that uses `[`
* Correct comment
* object destructuring
* Allow custom position of the rest element.
* Output simple array destructuring assignments to ES2015
* Output simple object destructured assignments to ES2015
* Compile shorthand object properties to ES2015 shorthand properties
This dramatically improves the appearance of destructured imports.
* Don’t confuse the syntax highlighter
* Comment Assign::compilePatternMatch a bit
* Assignment expressions in conditionals are a bad practice
* Rename `wrapInBraces` to `wrapInParentheses`, to set the stage for future `wrapInBraces` that uses `{` and `wrapInBrackets` that uses `[`
* object destructuring
* Allow custom position of the rest element.
* rest element in object destructuring
* rest element in object destructuring
* fix string interpolation
* merging
* fixing splats in object literal
* Rest element in parameter destructuring
* merging with CS2
* merged with CS2
* Add support for the object spread initializer. https://github.com/tc39/proposal-object-rest-spread/blob/master/Spread.md
* Fix misspellings, trailing whitespace, other minor details
* merging with beta2
* refactor object spread properties
* small fix
* - Fixed object spread function parameters.
- Clean up "Assign" and moved all logic for object rest properties in single method (compileObjectDestruct).
- Add helper function "objectWithoutKeys" to the "UTILITIES" for use with object rest properties,
e.g. {a, b, r...} = obj => {a, b} = obj, r = objectWithoutKeys(...)
- Clean up "Obj" and moved all logic for object spread properties in single method (compileSpread).
- Clean up "Code".
- Add method "hasSplat" to "Obj" and "Value" for checking if Obj contains the splat.
- Enable placing spread syntax triple dots on either right or left, per #85 (https://github.com/coffeescript6/discuss/issues/85)
* Fixed typos
* Remove unused code
* Removed dots (e.g. splat) on the left side from the grammar
* Initial release for deep spread properties, e.g. obj2 = {obj.b..., a: 1} or {obj[b][c]..., d: 7}
Tests need to be prepared!
* 1. Object literal spread properties
Object literals:
- obj = { {b:{c:{d:1}}}..., a:1 }
Parenthetical:
- obj = { ( body ), a:1 }
- obj = { ( body )..., a:1 }
Invocation:
- obj = { ( (args) -> ... )(params), a:1 }
- obj = { ( (args) -> ... )(params)..., a:1 }
- obj = { foo(), a:1 }
- obj = { foo()..., a:1 }
2. Refactor, cleanup & other optimizations.
* Merged with 2.0
* Cleanup
* Some more cleanup.
* Fixed error with freeVariable and object destructuring.
* Fixed errors with object spread properties.
* Improvements, fixed errors.
* Minor improvement.
* Minor improvements.
* Typo.
* Remove unnecessary whitespace.
* Remove unnecessary whitespace.
* Changed few "assertErrorFormat" tests since parentheses are now allowed in the Obj.
* Whitespace cleanup
* Comments cleanup
* fix destructured obj param declarations
* refine fix; add test
* Refactor function args ({a, b...})
* Additional tests for object destructuring in function argument.
* Minor improvement for object destructuring variable declaration.
* refactor function args ({a, b...}) and ({a, b...} = {}); Obj And Param cleanup
* fix comment
* Fix object destructuring variable declaration.
* more tests with default values
* fix typo
* Fixed default values in object destructuring.
* small fix
* Babel’s tests for object rest spread
* Style: spaces after colons in object declarations
* Cleanup comments
* Simplify Babel tests
* Fix comments
* Fix destructuring with splats in multiple objects
* Add test for default values in detsructuring assignment with splats
* Handle default values when assigning to object splats
* Rewrite traverseRest to fix handling of dynamic keys
* Fix double parens around destructuring with splats
* Update compileObjectDestruct comments
* Improve formatting of top-level destructures with splats and tidy parens
* Added a bigger destructuring-with-defaults test and fixed a bug
* Refactor destructuring grammar to allow additional forms
* Add a missing case to ObjSpreadExpr
* These tests shouldn’t run in the browser
* Fix test.html
2017-06-30 01:57:42 -04:00
|
|
|
|
|
2017-12-09 01:42:47 -05:00
|
|
|
|
test "unexpected object keys", ->
|
Fix #3597: Allow interpolations in object keys
The following is now allowed:
o =
a: 1
b: 2
"#{'c'}": 3
"#{'d'}": 4
e: 5
"#{'f'}": 6
g: 7
It compiles to:
o = (
obj = {
a: 1,
b: 2
},
obj["" + 'c'] = 3,
obj["" + 'd'] = 4,
obj.e = 5,
obj["" + 'f'] = 6,
obj.g = 7,
obj
);
- Closes #3039. Empty interpolations in object keys are now _supposed_ to be
allowed.
- Closes #1131. No need to improve error messages for attempted key
interpolation anymore.
- Implementing this required fixing the following bug: `("" + a): 1` used to
error out on the colon, saying "unexpected colon". But really, it is the
attempted object key that is unexpected. Now the error is on the opening
parenthesis instead.
- However, the above fix broke some error message tests for regexes. The easiest
way to fix this was to make a seemingly unrelated change: The error messages
for unexpected identifiers, numbers, strings and regexes now say for example
'unexpected string' instead of 'unexpected """some #{really long} string"""'.
In other words, the tag _name_ is used instead of the tag _value_.
This was way easier to implement, and is more helpful to the user. Using the
tag value is good for operators, reserved words and the like, but not for
tokens which can contain any text. For example, 'unexpected identifier' is
better than 'unexpected expected' (if a variable called 'expected' was used
erraneously).
- While writing tests for the above point I found a few minor bugs with string
locations which have been fixed.
2015-02-07 14:16:59 -05:00
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
{(a + "b")}
|
|
|
|
|
''', '''
|
[CS2] Destructuring object spreads (#4493)
* Don’t confuse the syntax highlighter
* Comment Assign::compilePatternMatch a bit
* Assignment expressions in conditionals are a bad practice
* Rename `wrapInBraces` to `wrapInParentheses`, to set the stage for future `wrapInBraces` that uses `{` and `wrapInBrackets` that uses `[`
* Correct comment
* object destructuring
* Allow custom position of the rest element.
* Output simple array destructuring assignments to ES2015
* Output simple object destructured assignments to ES2015
* Compile shorthand object properties to ES2015 shorthand properties
This dramatically improves the appearance of destructured imports.
* Don’t confuse the syntax highlighter
* Comment Assign::compilePatternMatch a bit
* Assignment expressions in conditionals are a bad practice
* Rename `wrapInBraces` to `wrapInParentheses`, to set the stage for future `wrapInBraces` that uses `{` and `wrapInBrackets` that uses `[`
* object destructuring
* Allow custom position of the rest element.
* rest element in object destructuring
* rest element in object destructuring
* fix string interpolation
* merging
* fixing splats in object literal
* Rest element in parameter destructuring
* merging with CS2
* merged with CS2
* Add support for the object spread initializer. https://github.com/tc39/proposal-object-rest-spread/blob/master/Spread.md
* Fix misspellings, trailing whitespace, other minor details
* merging with beta2
* refactor object spread properties
* small fix
* - Fixed object spread function parameters.
- Clean up "Assign" and moved all logic for object rest properties in single method (compileObjectDestruct).
- Add helper function "objectWithoutKeys" to the "UTILITIES" for use with object rest properties,
e.g. {a, b, r...} = obj => {a, b} = obj, r = objectWithoutKeys(...)
- Clean up "Obj" and moved all logic for object spread properties in single method (compileSpread).
- Clean up "Code".
- Add method "hasSplat" to "Obj" and "Value" for checking if Obj contains the splat.
- Enable placing spread syntax triple dots on either right or left, per #85 (https://github.com/coffeescript6/discuss/issues/85)
* Fixed typos
* Remove unused code
* Removed dots (e.g. splat) on the left side from the grammar
* Initial release for deep spread properties, e.g. obj2 = {obj.b..., a: 1} or {obj[b][c]..., d: 7}
Tests need to be prepared!
* 1. Object literal spread properties
Object literals:
- obj = { {b:{c:{d:1}}}..., a:1 }
Parenthetical:
- obj = { ( body ), a:1 }
- obj = { ( body )..., a:1 }
Invocation:
- obj = { ( (args) -> ... )(params), a:1 }
- obj = { ( (args) -> ... )(params)..., a:1 }
- obj = { foo(), a:1 }
- obj = { foo()..., a:1 }
2. Refactor, cleanup & other optimizations.
* Merged with 2.0
* Cleanup
* Some more cleanup.
* Fixed error with freeVariable and object destructuring.
* Fixed errors with object spread properties.
* Improvements, fixed errors.
* Minor improvement.
* Minor improvements.
* Typo.
* Remove unnecessary whitespace.
* Remove unnecessary whitespace.
* Changed few "assertErrorFormat" tests since parentheses are now allowed in the Obj.
* Whitespace cleanup
* Comments cleanup
* fix destructured obj param declarations
* refine fix; add test
* Refactor function args ({a, b...})
* Additional tests for object destructuring in function argument.
* Minor improvement for object destructuring variable declaration.
* refactor function args ({a, b...}) and ({a, b...} = {}); Obj And Param cleanup
* fix comment
* Fix object destructuring variable declaration.
* more tests with default values
* fix typo
* Fixed default values in object destructuring.
* small fix
* Babel’s tests for object rest spread
* Style: spaces after colons in object declarations
* Cleanup comments
* Simplify Babel tests
* Fix comments
* Fix destructuring with splats in multiple objects
* Add test for default values in detsructuring assignment with splats
* Handle default values when assigning to object splats
* Rewrite traverseRest to fix handling of dynamic keys
* Fix double parens around destructuring with splats
* Update compileObjectDestruct comments
* Improve formatting of top-level destructures with splats and tidy parens
* Added a bigger destructuring-with-defaults test and fixed a bug
* Refactor destructuring grammar to allow additional forms
* Add a missing case to ObjSpreadExpr
* These tests shouldn’t run in the browser
* Fix test.html
2017-06-30 01:57:42 -04:00
|
|
|
|
[stdin]:1:11: error: unexpected }
|
Fix #3597: Allow interpolations in object keys
The following is now allowed:
o =
a: 1
b: 2
"#{'c'}": 3
"#{'d'}": 4
e: 5
"#{'f'}": 6
g: 7
It compiles to:
o = (
obj = {
a: 1,
b: 2
},
obj["" + 'c'] = 3,
obj["" + 'd'] = 4,
obj.e = 5,
obj["" + 'f'] = 6,
obj.g = 7,
obj
);
- Closes #3039. Empty interpolations in object keys are now _supposed_ to be
allowed.
- Closes #1131. No need to improve error messages for attempted key
interpolation anymore.
- Implementing this required fixing the following bug: `("" + a): 1` used to
error out on the colon, saying "unexpected colon". But really, it is the
attempted object key that is unexpected. Now the error is on the opening
parenthesis instead.
- However, the above fix broke some error message tests for regexes. The easiest
way to fix this was to make a seemingly unrelated change: The error messages
for unexpected identifiers, numbers, strings and regexes now say for example
'unexpected string' instead of 'unexpected """some #{really long} string"""'.
In other words, the tag _name_ is used instead of the tag _value_.
This was way easier to implement, and is more helpful to the user. Using the
tag value is good for operators, reserved words and the like, but not for
tokens which can contain any text. For example, 'unexpected identifier' is
better than 'unexpected expected' (if a variable called 'expected' was used
erraneously).
- While writing tests for the above point I found a few minor bugs with string
locations which have been fixed.
2015-02-07 14:16:59 -05:00
|
|
|
|
{(a + "b")}
|
[CS2] Destructuring object spreads (#4493)
* Don’t confuse the syntax highlighter
* Comment Assign::compilePatternMatch a bit
* Assignment expressions in conditionals are a bad practice
* Rename `wrapInBraces` to `wrapInParentheses`, to set the stage for future `wrapInBraces` that uses `{` and `wrapInBrackets` that uses `[`
* Correct comment
* object destructuring
* Allow custom position of the rest element.
* Output simple array destructuring assignments to ES2015
* Output simple object destructured assignments to ES2015
* Compile shorthand object properties to ES2015 shorthand properties
This dramatically improves the appearance of destructured imports.
* Don’t confuse the syntax highlighter
* Comment Assign::compilePatternMatch a bit
* Assignment expressions in conditionals are a bad practice
* Rename `wrapInBraces` to `wrapInParentheses`, to set the stage for future `wrapInBraces` that uses `{` and `wrapInBrackets` that uses `[`
* object destructuring
* Allow custom position of the rest element.
* rest element in object destructuring
* rest element in object destructuring
* fix string interpolation
* merging
* fixing splats in object literal
* Rest element in parameter destructuring
* merging with CS2
* merged with CS2
* Add support for the object spread initializer. https://github.com/tc39/proposal-object-rest-spread/blob/master/Spread.md
* Fix misspellings, trailing whitespace, other minor details
* merging with beta2
* refactor object spread properties
* small fix
* - Fixed object spread function parameters.
- Clean up "Assign" and moved all logic for object rest properties in single method (compileObjectDestruct).
- Add helper function "objectWithoutKeys" to the "UTILITIES" for use with object rest properties,
e.g. {a, b, r...} = obj => {a, b} = obj, r = objectWithoutKeys(...)
- Clean up "Obj" and moved all logic for object spread properties in single method (compileSpread).
- Clean up "Code".
- Add method "hasSplat" to "Obj" and "Value" for checking if Obj contains the splat.
- Enable placing spread syntax triple dots on either right or left, per #85 (https://github.com/coffeescript6/discuss/issues/85)
* Fixed typos
* Remove unused code
* Removed dots (e.g. splat) on the left side from the grammar
* Initial release for deep spread properties, e.g. obj2 = {obj.b..., a: 1} or {obj[b][c]..., d: 7}
Tests need to be prepared!
* 1. Object literal spread properties
Object literals:
- obj = { {b:{c:{d:1}}}..., a:1 }
Parenthetical:
- obj = { ( body ), a:1 }
- obj = { ( body )..., a:1 }
Invocation:
- obj = { ( (args) -> ... )(params), a:1 }
- obj = { ( (args) -> ... )(params)..., a:1 }
- obj = { foo(), a:1 }
- obj = { foo()..., a:1 }
2. Refactor, cleanup & other optimizations.
* Merged with 2.0
* Cleanup
* Some more cleanup.
* Fixed error with freeVariable and object destructuring.
* Fixed errors with object spread properties.
* Improvements, fixed errors.
* Minor improvement.
* Minor improvements.
* Typo.
* Remove unnecessary whitespace.
* Remove unnecessary whitespace.
* Changed few "assertErrorFormat" tests since parentheses are now allowed in the Obj.
* Whitespace cleanup
* Comments cleanup
* fix destructured obj param declarations
* refine fix; add test
* Refactor function args ({a, b...})
* Additional tests for object destructuring in function argument.
* Minor improvement for object destructuring variable declaration.
* refactor function args ({a, b...}) and ({a, b...} = {}); Obj And Param cleanup
* fix comment
* Fix object destructuring variable declaration.
* more tests with default values
* fix typo
* Fixed default values in object destructuring.
* small fix
* Babel’s tests for object rest spread
* Style: spaces after colons in object declarations
* Cleanup comments
* Simplify Babel tests
* Fix comments
* Fix destructuring with splats in multiple objects
* Add test for default values in detsructuring assignment with splats
* Handle default values when assigning to object splats
* Rewrite traverseRest to fix handling of dynamic keys
* Fix double parens around destructuring with splats
* Update compileObjectDestruct comments
* Improve formatting of top-level destructures with splats and tidy parens
* Added a bigger destructuring-with-defaults test and fixed a bug
* Refactor destructuring grammar to allow additional forms
* Add a missing case to ObjSpreadExpr
* These tests shouldn’t run in the browser
* Fix test.html
2017-06-30 01:57:42 -04:00
|
|
|
|
^
|
Fix #3597: Allow interpolations in object keys
The following is now allowed:
o =
a: 1
b: 2
"#{'c'}": 3
"#{'d'}": 4
e: 5
"#{'f'}": 6
g: 7
It compiles to:
o = (
obj = {
a: 1,
b: 2
},
obj["" + 'c'] = 3,
obj["" + 'd'] = 4,
obj.e = 5,
obj["" + 'f'] = 6,
obj.g = 7,
obj
);
- Closes #3039. Empty interpolations in object keys are now _supposed_ to be
allowed.
- Closes #1131. No need to improve error messages for attempted key
interpolation anymore.
- Implementing this required fixing the following bug: `("" + a): 1` used to
error out on the colon, saying "unexpected colon". But really, it is the
attempted object key that is unexpected. Now the error is on the opening
parenthesis instead.
- However, the above fix broke some error message tests for regexes. The easiest
way to fix this was to make a seemingly unrelated change: The error messages
for unexpected identifiers, numbers, strings and regexes now say for example
'unexpected string' instead of 'unexpected """some #{really long} string"""'.
In other words, the tag _name_ is used instead of the tag _value_.
This was way easier to implement, and is more helpful to the user. Using the
tag value is good for operators, reserved words and the like, but not for
tokens which can contain any text. For example, 'unexpected identifier' is
better than 'unexpected expected' (if a variable called 'expected' was used
erraneously).
- While writing tests for the above point I found a few minor bugs with string
locations which have been fixed.
2015-02-07 14:16:59 -05:00
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
{(a + "b"): 1}
|
|
|
|
|
''', '''
|
[CS2] Destructuring object spreads (#4493)
* Don’t confuse the syntax highlighter
* Comment Assign::compilePatternMatch a bit
* Assignment expressions in conditionals are a bad practice
* Rename `wrapInBraces` to `wrapInParentheses`, to set the stage for future `wrapInBraces` that uses `{` and `wrapInBrackets` that uses `[`
* Correct comment
* object destructuring
* Allow custom position of the rest element.
* Output simple array destructuring assignments to ES2015
* Output simple object destructured assignments to ES2015
* Compile shorthand object properties to ES2015 shorthand properties
This dramatically improves the appearance of destructured imports.
* Don’t confuse the syntax highlighter
* Comment Assign::compilePatternMatch a bit
* Assignment expressions in conditionals are a bad practice
* Rename `wrapInBraces` to `wrapInParentheses`, to set the stage for future `wrapInBraces` that uses `{` and `wrapInBrackets` that uses `[`
* object destructuring
* Allow custom position of the rest element.
* rest element in object destructuring
* rest element in object destructuring
* fix string interpolation
* merging
* fixing splats in object literal
* Rest element in parameter destructuring
* merging with CS2
* merged with CS2
* Add support for the object spread initializer. https://github.com/tc39/proposal-object-rest-spread/blob/master/Spread.md
* Fix misspellings, trailing whitespace, other minor details
* merging with beta2
* refactor object spread properties
* small fix
* - Fixed object spread function parameters.
- Clean up "Assign" and moved all logic for object rest properties in single method (compileObjectDestruct).
- Add helper function "objectWithoutKeys" to the "UTILITIES" for use with object rest properties,
e.g. {a, b, r...} = obj => {a, b} = obj, r = objectWithoutKeys(...)
- Clean up "Obj" and moved all logic for object spread properties in single method (compileSpread).
- Clean up "Code".
- Add method "hasSplat" to "Obj" and "Value" for checking if Obj contains the splat.
- Enable placing spread syntax triple dots on either right or left, per #85 (https://github.com/coffeescript6/discuss/issues/85)
* Fixed typos
* Remove unused code
* Removed dots (e.g. splat) on the left side from the grammar
* Initial release for deep spread properties, e.g. obj2 = {obj.b..., a: 1} or {obj[b][c]..., d: 7}
Tests need to be prepared!
* 1. Object literal spread properties
Object literals:
- obj = { {b:{c:{d:1}}}..., a:1 }
Parenthetical:
- obj = { ( body ), a:1 }
- obj = { ( body )..., a:1 }
Invocation:
- obj = { ( (args) -> ... )(params), a:1 }
- obj = { ( (args) -> ... )(params)..., a:1 }
- obj = { foo(), a:1 }
- obj = { foo()..., a:1 }
2. Refactor, cleanup & other optimizations.
* Merged with 2.0
* Cleanup
* Some more cleanup.
* Fixed error with freeVariable and object destructuring.
* Fixed errors with object spread properties.
* Improvements, fixed errors.
* Minor improvement.
* Minor improvements.
* Typo.
* Remove unnecessary whitespace.
* Remove unnecessary whitespace.
* Changed few "assertErrorFormat" tests since parentheses are now allowed in the Obj.
* Whitespace cleanup
* Comments cleanup
* fix destructured obj param declarations
* refine fix; add test
* Refactor function args ({a, b...})
* Additional tests for object destructuring in function argument.
* Minor improvement for object destructuring variable declaration.
* refactor function args ({a, b...}) and ({a, b...} = {}); Obj And Param cleanup
* fix comment
* Fix object destructuring variable declaration.
* more tests with default values
* fix typo
* Fixed default values in object destructuring.
* small fix
* Babel’s tests for object rest spread
* Style: spaces after colons in object declarations
* Cleanup comments
* Simplify Babel tests
* Fix comments
* Fix destructuring with splats in multiple objects
* Add test for default values in detsructuring assignment with splats
* Handle default values when assigning to object splats
* Rewrite traverseRest to fix handling of dynamic keys
* Fix double parens around destructuring with splats
* Update compileObjectDestruct comments
* Improve formatting of top-level destructures with splats and tidy parens
* Added a bigger destructuring-with-defaults test and fixed a bug
* Refactor destructuring grammar to allow additional forms
* Add a missing case to ObjSpreadExpr
* These tests shouldn’t run in the browser
* Fix test.html
2017-06-30 01:57:42 -04:00
|
|
|
|
[stdin]:1:11: error: unexpected :
|
Fix #3597: Allow interpolations in object keys
The following is now allowed:
o =
a: 1
b: 2
"#{'c'}": 3
"#{'d'}": 4
e: 5
"#{'f'}": 6
g: 7
It compiles to:
o = (
obj = {
a: 1,
b: 2
},
obj["" + 'c'] = 3,
obj["" + 'd'] = 4,
obj.e = 5,
obj["" + 'f'] = 6,
obj.g = 7,
obj
);
- Closes #3039. Empty interpolations in object keys are now _supposed_ to be
allowed.
- Closes #1131. No need to improve error messages for attempted key
interpolation anymore.
- Implementing this required fixing the following bug: `("" + a): 1` used to
error out on the colon, saying "unexpected colon". But really, it is the
attempted object key that is unexpected. Now the error is on the opening
parenthesis instead.
- However, the above fix broke some error message tests for regexes. The easiest
way to fix this was to make a seemingly unrelated change: The error messages
for unexpected identifiers, numbers, strings and regexes now say for example
'unexpected string' instead of 'unexpected """some #{really long} string"""'.
In other words, the tag _name_ is used instead of the tag _value_.
This was way easier to implement, and is more helpful to the user. Using the
tag value is good for operators, reserved words and the like, but not for
tokens which can contain any text. For example, 'unexpected identifier' is
better than 'unexpected expected' (if a variable called 'expected' was used
erraneously).
- While writing tests for the above point I found a few minor bugs with string
locations which have been fixed.
2015-02-07 14:16:59 -05:00
|
|
|
|
{(a + "b"): 1}
|
[CS2] Destructuring object spreads (#4493)
* Don’t confuse the syntax highlighter
* Comment Assign::compilePatternMatch a bit
* Assignment expressions in conditionals are a bad practice
* Rename `wrapInBraces` to `wrapInParentheses`, to set the stage for future `wrapInBraces` that uses `{` and `wrapInBrackets` that uses `[`
* Correct comment
* object destructuring
* Allow custom position of the rest element.
* Output simple array destructuring assignments to ES2015
* Output simple object destructured assignments to ES2015
* Compile shorthand object properties to ES2015 shorthand properties
This dramatically improves the appearance of destructured imports.
* Don’t confuse the syntax highlighter
* Comment Assign::compilePatternMatch a bit
* Assignment expressions in conditionals are a bad practice
* Rename `wrapInBraces` to `wrapInParentheses`, to set the stage for future `wrapInBraces` that uses `{` and `wrapInBrackets` that uses `[`
* object destructuring
* Allow custom position of the rest element.
* rest element in object destructuring
* rest element in object destructuring
* fix string interpolation
* merging
* fixing splats in object literal
* Rest element in parameter destructuring
* merging with CS2
* merged with CS2
* Add support for the object spread initializer. https://github.com/tc39/proposal-object-rest-spread/blob/master/Spread.md
* Fix misspellings, trailing whitespace, other minor details
* merging with beta2
* refactor object spread properties
* small fix
* - Fixed object spread function parameters.
- Clean up "Assign" and moved all logic for object rest properties in single method (compileObjectDestruct).
- Add helper function "objectWithoutKeys" to the "UTILITIES" for use with object rest properties,
e.g. {a, b, r...} = obj => {a, b} = obj, r = objectWithoutKeys(...)
- Clean up "Obj" and moved all logic for object spread properties in single method (compileSpread).
- Clean up "Code".
- Add method "hasSplat" to "Obj" and "Value" for checking if Obj contains the splat.
- Enable placing spread syntax triple dots on either right or left, per #85 (https://github.com/coffeescript6/discuss/issues/85)
* Fixed typos
* Remove unused code
* Removed dots (e.g. splat) on the left side from the grammar
* Initial release for deep spread properties, e.g. obj2 = {obj.b..., a: 1} or {obj[b][c]..., d: 7}
Tests need to be prepared!
* 1. Object literal spread properties
Object literals:
- obj = { {b:{c:{d:1}}}..., a:1 }
Parenthetical:
- obj = { ( body ), a:1 }
- obj = { ( body )..., a:1 }
Invocation:
- obj = { ( (args) -> ... )(params), a:1 }
- obj = { ( (args) -> ... )(params)..., a:1 }
- obj = { foo(), a:1 }
- obj = { foo()..., a:1 }
2. Refactor, cleanup & other optimizations.
* Merged with 2.0
* Cleanup
* Some more cleanup.
* Fixed error with freeVariable and object destructuring.
* Fixed errors with object spread properties.
* Improvements, fixed errors.
* Minor improvement.
* Minor improvements.
* Typo.
* Remove unnecessary whitespace.
* Remove unnecessary whitespace.
* Changed few "assertErrorFormat" tests since parentheses are now allowed in the Obj.
* Whitespace cleanup
* Comments cleanup
* fix destructured obj param declarations
* refine fix; add test
* Refactor function args ({a, b...})
* Additional tests for object destructuring in function argument.
* Minor improvement for object destructuring variable declaration.
* refactor function args ({a, b...}) and ({a, b...} = {}); Obj And Param cleanup
* fix comment
* Fix object destructuring variable declaration.
* more tests with default values
* fix typo
* Fixed default values in object destructuring.
* small fix
* Babel’s tests for object rest spread
* Style: spaces after colons in object declarations
* Cleanup comments
* Simplify Babel tests
* Fix comments
* Fix destructuring with splats in multiple objects
* Add test for default values in detsructuring assignment with splats
* Handle default values when assigning to object splats
* Rewrite traverseRest to fix handling of dynamic keys
* Fix double parens around destructuring with splats
* Update compileObjectDestruct comments
* Improve formatting of top-level destructures with splats and tidy parens
* Added a bigger destructuring-with-defaults test and fixed a bug
* Refactor destructuring grammar to allow additional forms
* Add a missing case to ObjSpreadExpr
* These tests shouldn’t run in the browser
* Fix test.html
2017-06-30 01:57:42 -04:00
|
|
|
|
^
|
Fix #3597: Allow interpolations in object keys
The following is now allowed:
o =
a: 1
b: 2
"#{'c'}": 3
"#{'d'}": 4
e: 5
"#{'f'}": 6
g: 7
It compiles to:
o = (
obj = {
a: 1,
b: 2
},
obj["" + 'c'] = 3,
obj["" + 'd'] = 4,
obj.e = 5,
obj["" + 'f'] = 6,
obj.g = 7,
obj
);
- Closes #3039. Empty interpolations in object keys are now _supposed_ to be
allowed.
- Closes #1131. No need to improve error messages for attempted key
interpolation anymore.
- Implementing this required fixing the following bug: `("" + a): 1` used to
error out on the colon, saying "unexpected colon". But really, it is the
attempted object key that is unexpected. Now the error is on the opening
parenthesis instead.
- However, the above fix broke some error message tests for regexes. The easiest
way to fix this was to make a seemingly unrelated change: The error messages
for unexpected identifiers, numbers, strings and regexes now say for example
'unexpected string' instead of 'unexpected """some #{really long} string"""'.
In other words, the tag _name_ is used instead of the tag _value_.
This was way easier to implement, and is more helpful to the user. Using the
tag value is good for operators, reserved words and the like, but not for
tokens which can contain any text. For example, 'unexpected identifier' is
better than 'unexpected expected' (if a variable called 'expected' was used
erraneously).
- While writing tests for the above point I found a few minor bugs with string
locations which have been fixed.
2015-02-07 14:16:59 -05:00
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
(a + "b"): 1
|
|
|
|
|
''', '''
|
[CS2] Destructuring object spreads (#4493)
* Don’t confuse the syntax highlighter
* Comment Assign::compilePatternMatch a bit
* Assignment expressions in conditionals are a bad practice
* Rename `wrapInBraces` to `wrapInParentheses`, to set the stage for future `wrapInBraces` that uses `{` and `wrapInBrackets` that uses `[`
* Correct comment
* object destructuring
* Allow custom position of the rest element.
* Output simple array destructuring assignments to ES2015
* Output simple object destructured assignments to ES2015
* Compile shorthand object properties to ES2015 shorthand properties
This dramatically improves the appearance of destructured imports.
* Don’t confuse the syntax highlighter
* Comment Assign::compilePatternMatch a bit
* Assignment expressions in conditionals are a bad practice
* Rename `wrapInBraces` to `wrapInParentheses`, to set the stage for future `wrapInBraces` that uses `{` and `wrapInBrackets` that uses `[`
* object destructuring
* Allow custom position of the rest element.
* rest element in object destructuring
* rest element in object destructuring
* fix string interpolation
* merging
* fixing splats in object literal
* Rest element in parameter destructuring
* merging with CS2
* merged with CS2
* Add support for the object spread initializer. https://github.com/tc39/proposal-object-rest-spread/blob/master/Spread.md
* Fix misspellings, trailing whitespace, other minor details
* merging with beta2
* refactor object spread properties
* small fix
* - Fixed object spread function parameters.
- Clean up "Assign" and moved all logic for object rest properties in single method (compileObjectDestruct).
- Add helper function "objectWithoutKeys" to the "UTILITIES" for use with object rest properties,
e.g. {a, b, r...} = obj => {a, b} = obj, r = objectWithoutKeys(...)
- Clean up "Obj" and moved all logic for object spread properties in single method (compileSpread).
- Clean up "Code".
- Add method "hasSplat" to "Obj" and "Value" for checking if Obj contains the splat.
- Enable placing spread syntax triple dots on either right or left, per #85 (https://github.com/coffeescript6/discuss/issues/85)
* Fixed typos
* Remove unused code
* Removed dots (e.g. splat) on the left side from the grammar
* Initial release for deep spread properties, e.g. obj2 = {obj.b..., a: 1} or {obj[b][c]..., d: 7}
Tests need to be prepared!
* 1. Object literal spread properties
Object literals:
- obj = { {b:{c:{d:1}}}..., a:1 }
Parenthetical:
- obj = { ( body ), a:1 }
- obj = { ( body )..., a:1 }
Invocation:
- obj = { ( (args) -> ... )(params), a:1 }
- obj = { ( (args) -> ... )(params)..., a:1 }
- obj = { foo(), a:1 }
- obj = { foo()..., a:1 }
2. Refactor, cleanup & other optimizations.
* Merged with 2.0
* Cleanup
* Some more cleanup.
* Fixed error with freeVariable and object destructuring.
* Fixed errors with object spread properties.
* Improvements, fixed errors.
* Minor improvement.
* Minor improvements.
* Typo.
* Remove unnecessary whitespace.
* Remove unnecessary whitespace.
* Changed few "assertErrorFormat" tests since parentheses are now allowed in the Obj.
* Whitespace cleanup
* Comments cleanup
* fix destructured obj param declarations
* refine fix; add test
* Refactor function args ({a, b...})
* Additional tests for object destructuring in function argument.
* Minor improvement for object destructuring variable declaration.
* refactor function args ({a, b...}) and ({a, b...} = {}); Obj And Param cleanup
* fix comment
* Fix object destructuring variable declaration.
* more tests with default values
* fix typo
* Fixed default values in object destructuring.
* small fix
* Babel’s tests for object rest spread
* Style: spaces after colons in object declarations
* Cleanup comments
* Simplify Babel tests
* Fix comments
* Fix destructuring with splats in multiple objects
* Add test for default values in detsructuring assignment with splats
* Handle default values when assigning to object splats
* Rewrite traverseRest to fix handling of dynamic keys
* Fix double parens around destructuring with splats
* Update compileObjectDestruct comments
* Improve formatting of top-level destructures with splats and tidy parens
* Added a bigger destructuring-with-defaults test and fixed a bug
* Refactor destructuring grammar to allow additional forms
* Add a missing case to ObjSpreadExpr
* These tests shouldn’t run in the browser
* Fix test.html
2017-06-30 01:57:42 -04:00
|
|
|
|
[stdin]:1:10: error: unexpected :
|
Fix #3597: Allow interpolations in object keys
The following is now allowed:
o =
a: 1
b: 2
"#{'c'}": 3
"#{'d'}": 4
e: 5
"#{'f'}": 6
g: 7
It compiles to:
o = (
obj = {
a: 1,
b: 2
},
obj["" + 'c'] = 3,
obj["" + 'd'] = 4,
obj.e = 5,
obj["" + 'f'] = 6,
obj.g = 7,
obj
);
- Closes #3039. Empty interpolations in object keys are now _supposed_ to be
allowed.
- Closes #1131. No need to improve error messages for attempted key
interpolation anymore.
- Implementing this required fixing the following bug: `("" + a): 1` used to
error out on the colon, saying "unexpected colon". But really, it is the
attempted object key that is unexpected. Now the error is on the opening
parenthesis instead.
- However, the above fix broke some error message tests for regexes. The easiest
way to fix this was to make a seemingly unrelated change: The error messages
for unexpected identifiers, numbers, strings and regexes now say for example
'unexpected string' instead of 'unexpected """some #{really long} string"""'.
In other words, the tag _name_ is used instead of the tag _value_.
This was way easier to implement, and is more helpful to the user. Using the
tag value is good for operators, reserved words and the like, but not for
tokens which can contain any text. For example, 'unexpected identifier' is
better than 'unexpected expected' (if a variable called 'expected' was used
erraneously).
- While writing tests for the above point I found a few minor bugs with string
locations which have been fixed.
2015-02-07 14:16:59 -05:00
|
|
|
|
(a + "b"): 1
|
[CS2] Destructuring object spreads (#4493)
* Don’t confuse the syntax highlighter
* Comment Assign::compilePatternMatch a bit
* Assignment expressions in conditionals are a bad practice
* Rename `wrapInBraces` to `wrapInParentheses`, to set the stage for future `wrapInBraces` that uses `{` and `wrapInBrackets` that uses `[`
* Correct comment
* object destructuring
* Allow custom position of the rest element.
* Output simple array destructuring assignments to ES2015
* Output simple object destructured assignments to ES2015
* Compile shorthand object properties to ES2015 shorthand properties
This dramatically improves the appearance of destructured imports.
* Don’t confuse the syntax highlighter
* Comment Assign::compilePatternMatch a bit
* Assignment expressions in conditionals are a bad practice
* Rename `wrapInBraces` to `wrapInParentheses`, to set the stage for future `wrapInBraces` that uses `{` and `wrapInBrackets` that uses `[`
* object destructuring
* Allow custom position of the rest element.
* rest element in object destructuring
* rest element in object destructuring
* fix string interpolation
* merging
* fixing splats in object literal
* Rest element in parameter destructuring
* merging with CS2
* merged with CS2
* Add support for the object spread initializer. https://github.com/tc39/proposal-object-rest-spread/blob/master/Spread.md
* Fix misspellings, trailing whitespace, other minor details
* merging with beta2
* refactor object spread properties
* small fix
* - Fixed object spread function parameters.
- Clean up "Assign" and moved all logic for object rest properties in single method (compileObjectDestruct).
- Add helper function "objectWithoutKeys" to the "UTILITIES" for use with object rest properties,
e.g. {a, b, r...} = obj => {a, b} = obj, r = objectWithoutKeys(...)
- Clean up "Obj" and moved all logic for object spread properties in single method (compileSpread).
- Clean up "Code".
- Add method "hasSplat" to "Obj" and "Value" for checking if Obj contains the splat.
- Enable placing spread syntax triple dots on either right or left, per #85 (https://github.com/coffeescript6/discuss/issues/85)
* Fixed typos
* Remove unused code
* Removed dots (e.g. splat) on the left side from the grammar
* Initial release for deep spread properties, e.g. obj2 = {obj.b..., a: 1} or {obj[b][c]..., d: 7}
Tests need to be prepared!
* 1. Object literal spread properties
Object literals:
- obj = { {b:{c:{d:1}}}..., a:1 }
Parenthetical:
- obj = { ( body ), a:1 }
- obj = { ( body )..., a:1 }
Invocation:
- obj = { ( (args) -> ... )(params), a:1 }
- obj = { ( (args) -> ... )(params)..., a:1 }
- obj = { foo(), a:1 }
- obj = { foo()..., a:1 }
2. Refactor, cleanup & other optimizations.
* Merged with 2.0
* Cleanup
* Some more cleanup.
* Fixed error with freeVariable and object destructuring.
* Fixed errors with object spread properties.
* Improvements, fixed errors.
* Minor improvement.
* Minor improvements.
* Typo.
* Remove unnecessary whitespace.
* Remove unnecessary whitespace.
* Changed few "assertErrorFormat" tests since parentheses are now allowed in the Obj.
* Whitespace cleanup
* Comments cleanup
* fix destructured obj param declarations
* refine fix; add test
* Refactor function args ({a, b...})
* Additional tests for object destructuring in function argument.
* Minor improvement for object destructuring variable declaration.
* refactor function args ({a, b...}) and ({a, b...} = {}); Obj And Param cleanup
* fix comment
* Fix object destructuring variable declaration.
* more tests with default values
* fix typo
* Fixed default values in object destructuring.
* small fix
* Babel’s tests for object rest spread
* Style: spaces after colons in object declarations
* Cleanup comments
* Simplify Babel tests
* Fix comments
* Fix destructuring with splats in multiple objects
* Add test for default values in detsructuring assignment with splats
* Handle default values when assigning to object splats
* Rewrite traverseRest to fix handling of dynamic keys
* Fix double parens around destructuring with splats
* Update compileObjectDestruct comments
* Improve formatting of top-level destructures with splats and tidy parens
* Added a bigger destructuring-with-defaults test and fixed a bug
* Refactor destructuring grammar to allow additional forms
* Add a missing case to ObjSpreadExpr
* These tests shouldn’t run in the browser
* Fix test.html
2017-06-30 01:57:42 -04:00
|
|
|
|
^
|
Fix #3597: Allow interpolations in object keys
The following is now allowed:
o =
a: 1
b: 2
"#{'c'}": 3
"#{'d'}": 4
e: 5
"#{'f'}": 6
g: 7
It compiles to:
o = (
obj = {
a: 1,
b: 2
},
obj["" + 'c'] = 3,
obj["" + 'd'] = 4,
obj.e = 5,
obj["" + 'f'] = 6,
obj.g = 7,
obj
);
- Closes #3039. Empty interpolations in object keys are now _supposed_ to be
allowed.
- Closes #1131. No need to improve error messages for attempted key
interpolation anymore.
- Implementing this required fixing the following bug: `("" + a): 1` used to
error out on the colon, saying "unexpected colon". But really, it is the
attempted object key that is unexpected. Now the error is on the opening
parenthesis instead.
- However, the above fix broke some error message tests for regexes. The easiest
way to fix this was to make a seemingly unrelated change: The error messages
for unexpected identifiers, numbers, strings and regexes now say for example
'unexpected string' instead of 'unexpected """some #{really long} string"""'.
In other words, the tag _name_ is used instead of the tag _value_.
This was way easier to implement, and is more helpful to the user. Using the
tag value is good for operators, reserved words and the like, but not for
tokens which can contain any text. For example, 'unexpected identifier' is
better than 'unexpected expected' (if a variable called 'expected' was used
erraneously).
- While writing tests for the above point I found a few minor bugs with string
locations which have been fixed.
2015-02-07 14:16:59 -05:00
|
|
|
|
'''
|
2015-05-01 05:58:37 -04:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
^^
|
|
|
|
|
'''
|
2015-08-22 15:39:26 -04:00
|
|
|
|
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
|
|
|
|
|
^
|
|
|
|
|
'''
|
2015-08-25 13:19:21 -04:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
2015-08-28 17:11:47 -04:00
|
|
|
|
|
|
|
|
|
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) ->
|
|
|
|
|
^
|
|
|
|
|
'''
|
2015-09-16 01:51:17 -04:00
|
|
|
|
|
2015-10-22 11:26:11 -04:00
|
|
|
|
test "#4130: unassignable in destructured param", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
fun = ({
|
|
|
|
|
@param : null
|
|
|
|
|
}) ->
|
|
|
|
|
console.log "Oh hello!"
|
|
|
|
|
''', '''
|
2016-03-05 14:59:39 -05:00
|
|
|
|
[stdin]:2:12: error: keyword 'null' can't be assigned
|
2015-10-22 11:26:11 -04:00
|
|
|
|
@param : null
|
|
|
|
|
^^^^
|
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
({a: null}) ->
|
|
|
|
|
''', '''
|
2016-03-05 14:59:39 -05:00
|
|
|
|
[stdin]:1:6: error: keyword 'null' can't be assigned
|
2015-10-22 11:26:11 -04:00
|
|
|
|
({a: null}) ->
|
|
|
|
|
^^^^
|
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
({a: 1}) ->
|
|
|
|
|
''', '''
|
2016-03-05 14:59:39 -05:00
|
|
|
|
[stdin]:1:6: error: '1' can't be assigned
|
2015-10-22 11:26:11 -04:00
|
|
|
|
({a: 1}) ->
|
|
|
|
|
^
|
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
({1}) ->
|
|
|
|
|
''', '''
|
2016-03-05 14:59:39 -05:00
|
|
|
|
[stdin]:1:3: error: '1' can't be assigned
|
2015-10-22 11:26:11 -04:00
|
|
|
|
({1}) ->
|
|
|
|
|
^
|
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
({a: true = 1}) ->
|
|
|
|
|
''', '''
|
2016-03-05 14:59:39 -05:00
|
|
|
|
[stdin]:1:6: error: keyword 'true' can't be assigned
|
2015-10-22 11:26:11 -04:00
|
|
|
|
({a: true = 1}) ->
|
|
|
|
|
^^^^
|
|
|
|
|
'''
|
|
|
|
|
|
2015-09-16 01:51:17 -04:00
|
|
|
|
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)
|
|
|
|
|
^^^^^^^^^^^^
|
|
|
|
|
'''
|
2016-03-05 14:59:39 -05:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
^
|
|
|
|
|
'''
|
Support import and export of ES2015 modules (#4300)
This pull request adds support for ES2015 modules, by recognizing `import` and `export` statements. The following syntaxes are supported, based on the MDN [import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) and [export](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export) pages:
```js
import "module-name"
import defaultMember from "module-name"
import * as name from "module-name"
import { } from "module-name"
import { member } from "module-name"
import { member as alias } from "module-name"
import { member1, member2 as alias2, … } from "module-name"
import defaultMember, * as name from "module-name"
import defaultMember, { … } from "module-name"
export default expression
export class name
export { }
export { name }
export { name as exportedName }
export { name as default }
export { name1, name2 as exportedName2, name3 as default, … }
export * from "module-name"
export { … } from "module-name"
```
As a subsitute for ECMAScript’s `export var name = …` and `export function name {}`, CoffeeScript also supports:
```js
export name = …
```
CoffeeScript also supports optional commas within `{ … }`.
This PR converts the supported `import` and `export` statements into ES2015 `import` and `export` statements; it **does not resolve the modules**. So any CoffeeScript with `import` or `export` statements will be output as ES2015, and will need to be transpiled by another tool such as Babel before it can be used in a browser. We will need to add a warning to the documentation explaining this.
This should be fully backwards-compatible, as `import` and `export` were previously reserved keywords. No flags are used.
There are extensive tests included, though because no current JavaScript runtime supports `import` or `export`, the tests compare strings of what the compiled CoffeeScript output is against what the expected ES2015 should be. I also conducted two more elaborate tests:
* I forked the [ember-piqu](https://github.com/pauc/piqu-ember) project, which was an Ember CLI app that used ember-cli-coffeescript and [ember-cli-coffees6](https://github.com/alexspeller/ember-cli-coffees6) (which adds “support” for `import`/`export` by wrapping such statements in backticks before passing the result to the CoffeeScript compiler). I removed `ember-cli-coffees6` and replaced the CoffeeScript compiler used in the build chain with this code, and the app built without errors. [Demo here.](https://github.com/GeoffreyBooth/coffeescript-modules-test-piqu)
* I also forked the [CoffeeScript version of Meteor’s Todos example app](https://github.com/meteor/todos/tree/coffeescript), and replaced all of its `require` statements with the `import` and `export` statements from the original ES2015 version of the app on its `master` branch. I then updated the `coffeescript` Meteor package in the app to use this new code, and again the app builds without errors. [Demo here.](https://github.com/GeoffreyBooth/coffeescript-modules-test-meteor-todos)
The discussion history for this work started [here](https://github.com/jashkenas/coffeescript/pull/4160) and continued [here](https://github.com/GeoffreyBooth/coffeescript/pull/2). @lydell provided guidance, and @JimPanic and @rattrayalex contributed essential code.
2016-09-14 14:46:05 -04:00
|
|
|
|
|
|
|
|
|
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!'
|
|
|
|
|
''', '''
|
2016-10-16 00:32:06 -04:00
|
|
|
|
[stdin]:1:8: error: anonymous classes cannot be exported
|
|
|
|
|
export class
|
|
|
|
|
^^^^^
|
Support import and export of ES2015 modules (#4300)
This pull request adds support for ES2015 modules, by recognizing `import` and `export` statements. The following syntaxes are supported, based on the MDN [import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) and [export](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export) pages:
```js
import "module-name"
import defaultMember from "module-name"
import * as name from "module-name"
import { } from "module-name"
import { member } from "module-name"
import { member as alias } from "module-name"
import { member1, member2 as alias2, … } from "module-name"
import defaultMember, * as name from "module-name"
import defaultMember, { … } from "module-name"
export default expression
export class name
export { }
export { name }
export { name as exportedName }
export { name as default }
export { name1, name2 as exportedName2, name3 as default, … }
export * from "module-name"
export { … } from "module-name"
```
As a subsitute for ECMAScript’s `export var name = …` and `export function name {}`, CoffeeScript also supports:
```js
export name = …
```
CoffeeScript also supports optional commas within `{ … }`.
This PR converts the supported `import` and `export` statements into ES2015 `import` and `export` statements; it **does not resolve the modules**. So any CoffeeScript with `import` or `export` statements will be output as ES2015, and will need to be transpiled by another tool such as Babel before it can be used in a browser. We will need to add a warning to the documentation explaining this.
This should be fully backwards-compatible, as `import` and `export` were previously reserved keywords. No flags are used.
There are extensive tests included, though because no current JavaScript runtime supports `import` or `export`, the tests compare strings of what the compiled CoffeeScript output is against what the expected ES2015 should be. I also conducted two more elaborate tests:
* I forked the [ember-piqu](https://github.com/pauc/piqu-ember) project, which was an Ember CLI app that used ember-cli-coffeescript and [ember-cli-coffees6](https://github.com/alexspeller/ember-cli-coffees6) (which adds “support” for `import`/`export` by wrapping such statements in backticks before passing the result to the CoffeeScript compiler). I removed `ember-cli-coffees6` and replaced the CoffeeScript compiler used in the build chain with this code, and the app built without errors. [Demo here.](https://github.com/GeoffreyBooth/coffeescript-modules-test-piqu)
* I also forked the [CoffeeScript version of Meteor’s Todos example app](https://github.com/meteor/todos/tree/coffeescript), and replaced all of its `require` statements with the `import` and `export` statements from the original ES2015 version of the app on its `master` branch. I then updated the `coffeescript` Meteor package in the app to use this new code, and again the app builds without errors. [Demo here.](https://github.com/GeoffreyBooth/coffeescript-modules-test-meteor-todos)
The discussion history for this work started [here](https://github.com/jashkenas/coffeescript/pull/4160) and continued [here](https://github.com/GeoffreyBooth/coffeescript/pull/2). @lydell provided guidance, and @JimPanic and @rattrayalex contributed essential code.
2016-09-14 14:46:05 -04:00
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
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'
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
[CS2] Output ES2015 arrow functions, default parameters, rest parameters (#4311)
* Eliminate wrapper around “bound” (arrow) functions; output `=>` for such functions
* Remove irrelevant (and breaking) tests
* Minor cleanup
* When a function parameter is a splat (i.e., it uses the ES2015 rest parameter syntax) output that parameter as ES2015
* Rearrange function parameters when one of the parameters is a splat and isn’t the last parameter (very WIP)
* Handle params like `@param`, adding assignment expressions for them when they appear; ensure splat parameter is last
* Add parameter names (not a text like `'\nValue IdentifierLiteral: a'`) to the scope, so that parameters can’t be deleted; move body-related lines together; more explanation of what’s going on
* For parameters with a default value, correctly add the parameter name to the function scope
* Handle expansions in function parameters: when an expansion is found, set the parameters to only be the original parameters left of the expansion, then an `...args` parameter; and in the function body define variables for the parameters to the right of the expansion, including setting default values
* Handle splat parameters the same way we handle expansions: if a splat parameter is found, it becomes the last parameter in the function definition, and all following parameters get declared in the function body. Fix the splat/rest parameter values after the post-splat parameters have been extracted from it. Clean up `Code.compileNode` so that we loop through the parameters only once, and we create all expressions using calls like `new IdentifierLiteral` rather than `@makeCode`.
* Fix parameter name when a parameter is a splat attached to `this` (e.g. `@param...`)
* Rather than assigning post-splat parameters based on index, use slice; passes test “Functions with splats being called with too few arguments”
* Dial back our w00t indentation
* Better parsing of parameter names (WIP)
* Refactor processing of splat/expansion parameters
* Fix assignment of default parameters for parameters that come after a splat
* Better check for whether a param is attached to `this`
* More understandable variable names
* For parameters after a splat or expansion, assign them similar to the 1.x destructuring method of using `arguments`, except only concern ourselves with the post-splat parameters instead of all parameters; and use the splat/expansion parameter name, since `arguments` in ES fat arrow functions refers to the parent function’s `arguments` rather than the fat arrow function’s arguments/parameters
* Don’t add unnamed parameters (like `[]` as a parameter) to the function scope
* Disallow multiple splat/expansion parameters in function definitions; disallow lone expansion parameters
* Fix `this` params not getting assigned if the parameter is after a splat parameter
* Allow names of function parameters attached to `this` to be reserved words
* Always add a statement to the function body defining a variable with its default value, if it has one, if the variable `== null`; this covers the case when ES doesn’t apply the default value when `null` is passed in as a value, but CoffeeScript expects `null` and `undefined` to act interchangeably
* Aftermath of having both `undefined` and `null` trigger the use of default values for parameters with default values
* More careful parsing of destructured parameters
* Fall back to processing destructured parameters in the function body, to account for `this` or default values within destructured objects
* Clean up comments
* Restore new bare function test, minus the arrow function part of it
* Test that bound/arrow functions aren’t overwriting the `arguments` object, which should refer to the parent scope’s `arguments` (like `this`)
* Follow ES2015 spec for parameter default values: `null` gets assigned as as `null`, not the default value
* Mimic ES default parameters behavior for parameters after a splat or expansion parameter
* Bound functions cannot be generators: remove no-longer-relevant test, add check to throw error if `yield` appears inside a bound (arrow) function
* Error for bound generator functions should underline the `yield`
2016-10-26 01:26:13 -04:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
^^^^^^^^^^
|
|
|
|
|
'''
|
2016-10-26 11:59:43 -04:00
|
|
|
|
|
2017-11-21 20:14:32 -05:00
|
|
|
|
test "#4790: bound functions cannot be generators, even when we’re creating IIFEs", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
=>
|
|
|
|
|
for x in []
|
|
|
|
|
for y in []
|
|
|
|
|
yield z
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:4:7: error: yield cannot occur inside bound (fat arrow) functions
|
|
|
|
|
yield z
|
|
|
|
|
^^^^^^^
|
|
|
|
|
'''
|
|
|
|
|
|
2016-10-26 11:59:43 -04:00
|
|
|
|
test "CoffeeScript keywords cannot be used as unaliased names in import lists", ->
|
2016-10-26 08:37:19 -04:00
|
|
|
|
assertErrorFormat """
|
|
|
|
|
import { unless, baz as bar } from 'lib'
|
|
|
|
|
bar.barMethod()
|
|
|
|
|
""", '''
|
|
|
|
|
[stdin]:1:10: error: unexpected unless
|
|
|
|
|
import { unless, baz as bar } from 'lib'
|
|
|
|
|
^^^^^^
|
|
|
|
|
'''
|
|
|
|
|
|
2016-10-26 11:59:43 -04:00
|
|
|
|
test "CoffeeScript keywords cannot be used as local names in import list aliases", ->
|
2016-10-26 08:37:19 -04:00
|
|
|
|
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'
|
|
|
|
|
^^^^^^
|
|
|
|
|
'''
|
2016-11-02 11:51:26 -04:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
^^^^^^^
|
|
|
|
|
'''
|
|
|
|
|
|
2016-11-11 01:51:39 -05:00
|
|
|
|
test "cannot have `await` outside a function", ->
|
2016-11-02 11:51:26 -04:00
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
await 1
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:1: error: await can only occur inside functions
|
|
|
|
|
await 1
|
|
|
|
|
^^^^^^^
|
|
|
|
|
'''
|
2016-11-11 01:51:39 -05:00
|
|
|
|
|
2016-11-08 02:40:01 -05:00
|
|
|
|
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]
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
2016-11-18 13:25:03 -05:00
|
|
|
|
|
|
|
|
|
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}"""
|
|
|
|
|
^
|
|
|
|
|
'''
|
[CS2] Compile class constructors to ES2015 classes (#4354)
* 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.
2017-01-13 00:55:30 -05:00
|
|
|
|
|
|
|
|
|
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", ->
|
[CS2] Compile all super calls to ES2015 super (#4424)
* Compile all super calls to ES2015 super
This breaks using `super` in non-methods, meaning several tests are
failing. Self-compilation still works.
* Use bound functions for IIFEs containing `super`
`super` can only be called directly in a method, or in an arrow
function.
* Fix handling of `class @A extends A`
This behaviour worked 'for free' when the parent reference was being
cached by the executable class body wrapper. There now needs to be
special handling in place to check if the parent name matches the class
name, and if so to cache the parent reference.
* Fix tests broken by compiling ES2015 `super`
* Disallow bare super
This removes syntax support for 'bare' super calls, e.g.:
class B extends A
constructor: -> super
`super` must now always be followed with arguments like a regular
function call. This also removes the capability of implicitly forwarding
arguments. The above can be equivalently be written as:
class B extends A
constructor: -> super arguments...
* Support super with accessors
`super` with following accessor(s) is now compiled to ES2015
equivalents. In particular, expressions such as `super.name`,
`super[name]`, and also `super.name.prop` are all now valid, and can be
used as expected as calls (i.e. `super.name()`) or in expressions (i.e.
`if super.name? ...`).
`super` without accessors is compiled to a constructor super call in a
constructor, and otherwise, as before, to a super call to the method of
the same name, i.e.
speak: -> super()
...is equivalent to
speak: -> super.speak()
A neat side-effect of the changes is that existential calls now work
properly with super, meaning `super?()` will only call if the super
property exists (and is a function). This is not valid for super in
constructors.
* Prevent calling `super` methods with `new`
This fixes a bug in the previous super handling whereby using the `new`
operator with a `super` call would silently drop the `new`. This is now
an explicit compiler error, as it is invalid JS at runtime.
* Clean up some old super handling code
This was mostly code for tracking the source classes and variables for
methods, which were needed to build the old lookups on `__super__`.
* Add TODO to improve bare super parse error
* Add some TODOs to improve some of the class tests
2017-02-04 15:03:17 -05:00
|
|
|
|
assertErrorFormat 'class then constructor: -> super()', '''
|
[CS2] Compile class constructors to ES2015 classes (#4354)
* 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.
2017-01-13 00:55:30 -05:00
|
|
|
|
[stdin]:1:28: error: 'super' is only allowed in derived class constructors
|
[CS2] Compile all super calls to ES2015 super (#4424)
* Compile all super calls to ES2015 super
This breaks using `super` in non-methods, meaning several tests are
failing. Self-compilation still works.
* Use bound functions for IIFEs containing `super`
`super` can only be called directly in a method, or in an arrow
function.
* Fix handling of `class @A extends A`
This behaviour worked 'for free' when the parent reference was being
cached by the executable class body wrapper. There now needs to be
special handling in place to check if the parent name matches the class
name, and if so to cache the parent reference.
* Fix tests broken by compiling ES2015 `super`
* Disallow bare super
This removes syntax support for 'bare' super calls, e.g.:
class B extends A
constructor: -> super
`super` must now always be followed with arguments like a regular
function call. This also removes the capability of implicitly forwarding
arguments. The above can be equivalently be written as:
class B extends A
constructor: -> super arguments...
* Support super with accessors
`super` with following accessor(s) is now compiled to ES2015
equivalents. In particular, expressions such as `super.name`,
`super[name]`, and also `super.name.prop` are all now valid, and can be
used as expected as calls (i.e. `super.name()`) or in expressions (i.e.
`if super.name? ...`).
`super` without accessors is compiled to a constructor super call in a
constructor, and otherwise, as before, to a super call to the method of
the same name, i.e.
speak: -> super()
...is equivalent to
speak: -> super.speak()
A neat side-effect of the changes is that existential calls now work
properly with super, meaning `super?()` will only call if the super
property exists (and is a function). This is not valid for super in
constructors.
* Prevent calling `super` methods with `new`
This fixes a bug in the previous super handling whereby using the `new`
operator with a `super` call would silently drop the `new`. This is now
an explicit compiler error, as it is invalid JS at runtime.
* Clean up some old super handling code
This was mostly code for tracking the source classes and variables for
methods, which were needed to build the old lookups on `__super__`.
* Add TODO to improve bare super parse error
* Add some TODOs to improve some of the class tests
2017-02-04 15:03:17 -05:00
|
|
|
|
class then constructor: -> super()
|
|
|
|
|
^^^^^^^
|
[CS2] Compile class constructors to ES2015 classes (#4354)
* 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.
2017-01-13 00:55:30 -05:00
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
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) ->
|
|
|
|
|
^^
|
|
|
|
|
'''
|
|
|
|
|
|
2017-10-26 01:15:05 -04:00
|
|
|
|
test "derived constructors can't call super with @params", ->
|
|
|
|
|
assertErrorFormat 'class extends A then constructor: (@a) -> super(@a)', '''
|
|
|
|
|
[stdin]:1:49: error: Can't call super with @params in derived class constructors
|
|
|
|
|
class extends A then constructor: (@a) -> super(@a)
|
|
|
|
|
^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "derived constructors can't call super with buried @params", ->
|
|
|
|
|
assertErrorFormat 'class extends A then constructor: (@a) -> super((=> @a)())', '''
|
|
|
|
|
[stdin]:1:53: error: Can't call super with @params in derived class constructors
|
|
|
|
|
class extends A then constructor: (@a) -> super((=> @a)())
|
|
|
|
|
^^
|
|
|
|
|
'''
|
|
|
|
|
|
[CS2] Compile class constructors to ES2015 classes (#4354)
* 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.
2017-01-13 00:55:30 -05:00
|
|
|
|
test "'super' is not allowed in constructor parameter defaults", ->
|
[CS2] Compile all super calls to ES2015 super (#4424)
* Compile all super calls to ES2015 super
This breaks using `super` in non-methods, meaning several tests are
failing. Self-compilation still works.
* Use bound functions for IIFEs containing `super`
`super` can only be called directly in a method, or in an arrow
function.
* Fix handling of `class @A extends A`
This behaviour worked 'for free' when the parent reference was being
cached by the executable class body wrapper. There now needs to be
special handling in place to check if the parent name matches the class
name, and if so to cache the parent reference.
* Fix tests broken by compiling ES2015 `super`
* Disallow bare super
This removes syntax support for 'bare' super calls, e.g.:
class B extends A
constructor: -> super
`super` must now always be followed with arguments like a regular
function call. This also removes the capability of implicitly forwarding
arguments. The above can be equivalently be written as:
class B extends A
constructor: -> super arguments...
* Support super with accessors
`super` with following accessor(s) is now compiled to ES2015
equivalents. In particular, expressions such as `super.name`,
`super[name]`, and also `super.name.prop` are all now valid, and can be
used as expected as calls (i.e. `super.name()`) or in expressions (i.e.
`if super.name? ...`).
`super` without accessors is compiled to a constructor super call in a
constructor, and otherwise, as before, to a super call to the method of
the same name, i.e.
speak: -> super()
...is equivalent to
speak: -> super.speak()
A neat side-effect of the changes is that existential calls now work
properly with super, meaning `super?()` will only call if the super
property exists (and is a function). This is not valid for super in
constructors.
* Prevent calling `super` methods with `new`
This fixes a bug in the previous super handling whereby using the `new`
operator with a `super` call would silently drop the `new`. This is now
an explicit compiler error, as it is invalid JS at runtime.
* Clean up some old super handling code
This was mostly code for tracking the source classes and variables for
methods, which were needed to build the old lookups on `__super__`.
* Add TODO to improve bare super parse error
* Add some TODOs to improve some of the class tests
2017-02-04 15:03:17 -05:00
|
|
|
|
assertErrorFormat 'class extends A then constructor: (a = super()) ->', '''
|
[CS2] Compile class constructors to ES2015 classes (#4354)
* 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.
2017-01-13 00:55:30 -05:00
|
|
|
|
[stdin]:1:40: error: 'super' is not allowed in constructor parameter defaults
|
[CS2] Compile all super calls to ES2015 super (#4424)
* Compile all super calls to ES2015 super
This breaks using `super` in non-methods, meaning several tests are
failing. Self-compilation still works.
* Use bound functions for IIFEs containing `super`
`super` can only be called directly in a method, or in an arrow
function.
* Fix handling of `class @A extends A`
This behaviour worked 'for free' when the parent reference was being
cached by the executable class body wrapper. There now needs to be
special handling in place to check if the parent name matches the class
name, and if so to cache the parent reference.
* Fix tests broken by compiling ES2015 `super`
* Disallow bare super
This removes syntax support for 'bare' super calls, e.g.:
class B extends A
constructor: -> super
`super` must now always be followed with arguments like a regular
function call. This also removes the capability of implicitly forwarding
arguments. The above can be equivalently be written as:
class B extends A
constructor: -> super arguments...
* Support super with accessors
`super` with following accessor(s) is now compiled to ES2015
equivalents. In particular, expressions such as `super.name`,
`super[name]`, and also `super.name.prop` are all now valid, and can be
used as expected as calls (i.e. `super.name()`) or in expressions (i.e.
`if super.name? ...`).
`super` without accessors is compiled to a constructor super call in a
constructor, and otherwise, as before, to a super call to the method of
the same name, i.e.
speak: -> super()
...is equivalent to
speak: -> super.speak()
A neat side-effect of the changes is that existential calls now work
properly with super, meaning `super?()` will only call if the super
property exists (and is a function). This is not valid for super in
constructors.
* Prevent calling `super` methods with `new`
This fixes a bug in the previous super handling whereby using the `new`
operator with a `super` call would silently drop the `new`. This is now
an explicit compiler error, as it is invalid JS at runtime.
* Clean up some old super handling code
This was mostly code for tracking the source classes and variables for
methods, which were needed to build the old lookups on `__super__`.
* Add TODO to improve bare super parse error
* Add some TODOs to improve some of the class tests
2017-02-04 15:03:17 -05:00
|
|
|
|
class extends A then constructor: (a = super()) ->
|
|
|
|
|
^^^^^^^
|
[CS2] Compile class constructors to ES2015 classes (#4354)
* 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.
2017-01-13 00:55:30 -05:00
|
|
|
|
'''
|
2017-01-22 19:32:38 -05:00
|
|
|
|
|
2016-12-29 13:55:33 -05:00
|
|
|
|
test "can't use pattern matches for loop indices", ->
|
|
|
|
|
assertErrorFormat 'a for b, {c} in d', '''
|
|
|
|
|
[stdin]:1:10: error: index cannot be a pattern matching expression
|
|
|
|
|
a for b, {c} in d
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
[CS2] Compile all super calls to ES2015 super (#4424)
* Compile all super calls to ES2015 super
This breaks using `super` in non-methods, meaning several tests are
failing. Self-compilation still works.
* Use bound functions for IIFEs containing `super`
`super` can only be called directly in a method, or in an arrow
function.
* Fix handling of `class @A extends A`
This behaviour worked 'for free' when the parent reference was being
cached by the executable class body wrapper. There now needs to be
special handling in place to check if the parent name matches the class
name, and if so to cache the parent reference.
* Fix tests broken by compiling ES2015 `super`
* Disallow bare super
This removes syntax support for 'bare' super calls, e.g.:
class B extends A
constructor: -> super
`super` must now always be followed with arguments like a regular
function call. This also removes the capability of implicitly forwarding
arguments. The above can be equivalently be written as:
class B extends A
constructor: -> super arguments...
* Support super with accessors
`super` with following accessor(s) is now compiled to ES2015
equivalents. In particular, expressions such as `super.name`,
`super[name]`, and also `super.name.prop` are all now valid, and can be
used as expected as calls (i.e. `super.name()`) or in expressions (i.e.
`if super.name? ...`).
`super` without accessors is compiled to a constructor super call in a
constructor, and otherwise, as before, to a super call to the method of
the same name, i.e.
speak: -> super()
...is equivalent to
speak: -> super.speak()
A neat side-effect of the changes is that existential calls now work
properly with super, meaning `super?()` will only call if the super
property exists (and is a function). This is not valid for super in
constructors.
* Prevent calling `super` methods with `new`
This fixes a bug in the previous super handling whereby using the `new`
operator with a `super` call would silently drop the `new`. This is now
an explicit compiler error, as it is invalid JS at runtime.
* Clean up some old super handling code
This was mostly code for tracking the source classes and variables for
methods, which were needed to build the old lookups on `__super__`.
* Add TODO to improve bare super parse error
* Add some TODOs to improve some of the class tests
2017-02-04 15:03:17 -05:00
|
|
|
|
|
|
|
|
|
test "bare 'super' is no longer allowed", ->
|
|
|
|
|
# TODO Improve this error message (it should at least be 'unexpected super')
|
|
|
|
|
assertErrorFormat 'class extends A then constructor: -> super', '''
|
|
|
|
|
[stdin]:1:35: error: unexpected ->
|
|
|
|
|
class extends A then constructor: -> super
|
|
|
|
|
^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "soaked 'super' in constructor", ->
|
|
|
|
|
assertErrorFormat 'class extends A then constructor: -> super?()', '''
|
|
|
|
|
[stdin]:1:38: error: Unsupported reference to 'super'
|
|
|
|
|
class extends A then constructor: -> super?()
|
|
|
|
|
^^^^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "new with 'super'", ->
|
|
|
|
|
assertErrorFormat 'class extends A then foo: -> new super()', '''
|
|
|
|
|
[stdin]:1:34: error: Unsupported reference to 'super'
|
|
|
|
|
class extends A then foo: -> new super()
|
|
|
|
|
^^^^^
|
|
|
|
|
'''
|
2017-04-09 00:59:09 -04:00
|
|
|
|
|
|
|
|
|
test "getter keyword in object", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
obj =
|
|
|
|
|
get foo: ->
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:2:3: error: 'get' cannot be used as a keyword, or as a function call without parentheses
|
|
|
|
|
get foo: ->
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "setter keyword in object", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
obj =
|
|
|
|
|
set foo: ->
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:2:3: error: 'set' cannot be used as a keyword, or as a function call without parentheses
|
|
|
|
|
set foo: ->
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "getter keyword in inline implicit object", ->
|
|
|
|
|
assertErrorFormat 'obj = get foo: ->', '''
|
|
|
|
|
[stdin]:1:7: error: 'get' cannot be used as a keyword, or as a function call without parentheses
|
|
|
|
|
obj = get foo: ->
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "setter keyword in inline implicit object", ->
|
|
|
|
|
assertErrorFormat 'obj = set foo: ->', '''
|
|
|
|
|
[stdin]:1:7: error: 'set' cannot be used as a keyword, or as a function call without parentheses
|
|
|
|
|
obj = set foo: ->
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "getter keyword in inline explicit object", ->
|
|
|
|
|
assertErrorFormat 'obj = {get foo: ->}', '''
|
|
|
|
|
[stdin]:1:8: error: 'get' cannot be used as a keyword, or as a function call without parentheses
|
|
|
|
|
obj = {get foo: ->}
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "setter keyword in inline explicit object", ->
|
|
|
|
|
assertErrorFormat 'obj = {set foo: ->}', '''
|
|
|
|
|
[stdin]:1:8: error: 'set' cannot be used as a keyword, or as a function call without parentheses
|
|
|
|
|
obj = {set foo: ->}
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "getter keyword in function", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
f = ->
|
|
|
|
|
get foo: ->
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:2:3: error: 'get' cannot be used as a keyword, or as a function call without parentheses
|
|
|
|
|
get foo: ->
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "setter keyword in function", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
f = ->
|
|
|
|
|
set foo: ->
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:2:3: error: 'set' cannot be used as a keyword, or as a function call without parentheses
|
|
|
|
|
set foo: ->
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "getter keyword in inline function", ->
|
|
|
|
|
assertErrorFormat 'f = -> get foo: ->', '''
|
|
|
|
|
[stdin]:1:8: error: 'get' cannot be used as a keyword, or as a function call without parentheses
|
|
|
|
|
f = -> get foo: ->
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "setter keyword in inline function", ->
|
|
|
|
|
assertErrorFormat 'f = -> set foo: ->', '''
|
|
|
|
|
[stdin]:1:8: error: 'set' cannot be used as a keyword, or as a function call without parentheses
|
|
|
|
|
f = -> set foo: ->
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "getter keyword in class", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
class A
|
|
|
|
|
get foo: ->
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:2:3: error: 'get' cannot be used as a keyword, or as a function call without parentheses
|
|
|
|
|
get foo: ->
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "setter keyword in class", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
class A
|
|
|
|
|
set foo: ->
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:2:3: error: 'set' cannot be used as a keyword, or as a function call without parentheses
|
|
|
|
|
set foo: ->
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "getter keyword in inline class", ->
|
|
|
|
|
assertErrorFormat 'class A then get foo: ->', '''
|
|
|
|
|
[stdin]:1:14: error: 'get' cannot be used as a keyword, or as a function call without parentheses
|
|
|
|
|
class A then get foo: ->
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "setter keyword in inline class", ->
|
|
|
|
|
assertErrorFormat 'class A then set foo: ->', '''
|
|
|
|
|
[stdin]:1:14: error: 'set' cannot be used as a keyword, or as a function call without parentheses
|
|
|
|
|
class A then set foo: ->
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "getter keyword before static method", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
class A
|
|
|
|
|
get @foo = ->
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:2:3: error: 'get' cannot be used as a keyword, or as a function call without parentheses
|
|
|
|
|
get @foo = ->
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "setter keyword before static method", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
class A
|
|
|
|
|
set @foo = ->
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:2:3: error: 'set' cannot be used as a keyword, or as a function call without parentheses
|
|
|
|
|
set @foo = ->
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
2017-04-20 15:41:28 -04:00
|
|
|
|
|
2017-04-20 02:03:06 -04:00
|
|
|
|
test "#4248: Unicode code point escapes", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
"a
|
|
|
|
|
#{b} \\u{G02}
|
|
|
|
|
c"
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:2:8: error: invalid escape sequence \\u{G02}
|
|
|
|
|
#{b} \\u{G02}
|
|
|
|
|
^\^^^^^^
|
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
/a\\u{}b/
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:3: error: invalid escape sequence \\u{}
|
|
|
|
|
/a\\u{}b/
|
|
|
|
|
^\^^^
|
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
///a \\u{01abc///
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:6: error: invalid escape sequence \\u{01abc
|
|
|
|
|
///a \\u{01abc///
|
|
|
|
|
^\^^^^^^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
/\\u{123} \\u{110000}/
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:10: error: unicode code point escapes greater than \\u{10ffff} are not allowed
|
|
|
|
|
/\\u{123} \\u{110000}/
|
|
|
|
|
\ ^\^^^^^^^^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
///abc\\\\\\u{123456}///u
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:9: error: unicode code point escapes greater than \\u{10ffff} are not allowed
|
|
|
|
|
///abc\\\\\\u{123456}///u
|
|
|
|
|
\ \^\^^^^^^^^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
"""
|
|
|
|
|
\\u{123}
|
|
|
|
|
a
|
|
|
|
|
\\u{00110000}
|
|
|
|
|
#{ 'b' }
|
|
|
|
|
"""
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:4:5: error: unicode code point escapes greater than \\u{10ffff} are not allowed
|
|
|
|
|
\\u{00110000}
|
|
|
|
|
^\^^^^^^^^^^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
'\\u{a}\\u{1111110000}'
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:7: error: unicode code point escapes greater than \\u{10ffff} are not allowed
|
|
|
|
|
'\\u{a}\\u{1111110000}'
|
|
|
|
|
\ ^\^^^^^^^^^^^^^
|
|
|
|
|
'''
|
2017-06-07 02:33:46 -04:00
|
|
|
|
|
|
|
|
|
test "CSX error: non-matching tag names", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
<div><span></div></span>
|
|
|
|
|
''',
|
|
|
|
|
'''
|
|
|
|
|
[stdin]:1:7: error: expected corresponding CSX closing tag for span
|
|
|
|
|
<div><span></div></span>
|
|
|
|
|
^^^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "CSX error: bare expressions not allowed", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
<div x=3 />
|
|
|
|
|
''',
|
|
|
|
|
'''
|
[CS2] Comments (#4572)
* Make `addLocationDataFn` more DRY
* Style fixes
* Provide access to full parser inside our custom function running in parser.js; rename the function to lay the groundwork for adding data aside from location data
* Fix style.
* Fix style.
* Label test comments
* Update grammar to remove comment tokens; update DSL to call new helper function that preserves comments through parsing
* New implementation of compiling block comments: the lexer pulls them out of the token stream, attaching them as a property to a token; the rewriter moves the attachment around so it lives on a token that is destined to make it through to compilation (and in a good placement); and the nodes render the block comment. All tests but one pass (commented out).
* If a comment follows a class declaration, move the comment inside the class body
* Style
* Improve indentation of multiline comments
* Fix indentation for block comments, at least in the cases covered by the one failing test
* Don’t reverse the order of unshifted comments
* Simplify rewriter’s handling of comments, generalizing the special case
* Expand the list of tokens we need to avoid for passing comments through the parser; get some literal tokens to have nodes created for them so that the comments pass through
* Improve comments; fix multiline flag
* Prepare HereComments for processing line comments
* Line comments, first draft: the tests pass, but the line comments aren’t indented and sometimes trail previous lines when they shouldn’t; updated compiler output in following commit
* Updated compiler, now with line comments
* `process` doesn’t exist in the browser, so we should check for its existence first
* Update parser output
* Test that proves #4290 is fixed
* Indent line comments, first pass
* Compiled output with indented line comments
* Comments that start a new line shouldn’t trail; don’t skip comments attached to generated tokens; stop looking for indentation once we hit a newline
* Revised output
* Cleanup
* Split “multiline” line comment tokens, shifting them forward or back as appropriate
* Fix comments in module specifiers
* Abstract attaching comments to a node
* Line comments in interpolated strings
* Line comments can’t be multiline anymore
* Improve handling of blank lines and indentation of following comments that start a new line (i.e. don’t trail)
* Make comments compilation more object-oriented
* Remove lots of dead code that we don’t need anymore because a comment is never a node, only a fragment
* Improve eqJS helper
* Fix #4290 definitively, with improved output for arrays with interspersed block comments
* Add support for line comments output interspersed within arrays
* Fix mistake, don’t lose the variable we’re working on
* Remove redundant replacements
* Check for indentation only from the start of the string
* Indentations in generated JS are always multiples of two spaces (never tabs) so just look for 2+ spaces
* Update package versions; run Babel twice, once for each preset, temporarily until a Babili bug is fixed that prevents it from running with the env preset
* Don’t rely on `fragment.type`, which can break when the compiler is minified
* Updated generated docs and browser compiler
* Output block comments after function arguments
* Comments appear above scope `var` declarations; better tracking of generated `JS` tokens created only to shepherd comments through to the output
* Create new FuncGlyph node, to hold comments we want to output near the function parameters
* Block comments between `)` and `->`/`=>` get output between `)` and `{`.
* Fix indentation of comments that are the first line inside a bare mode block
* Updated output
* Full Flow example
* Updated browser compiler
* Abstract and organize comment fragment generation code; store more properties on the comment fragment objects; make `throw` behave like `return`
* Abstract token insertion code
* Add missing locationData to STRING_START token, giving it the locationData of the overall StringWithInterpolations token so that comments attached to STRING_START end up on the StringWithInterpolations node
* Allow `SUPER` tokens to carry comments
* Rescue comments from `Existence` nodes and `If` nodes’ conditions
* Rescue comments after `\` line continuation tokens
* Updated compiled output
* Updated browser compiler
* Output block comments in the same `compileFragments` method as line comments, except for inline block comments
* Comments before splice
* Updated browser compiler
* Track compiledComments as a property of Base, to ensure that it’s not a global variable
* Docs: split up the Usage section
* Docs for type annotations via Flow; updated docs output
* Update regular comments documentation
* Updated browser compiler
* Comments before soak
* Comments before static methods, and probably before `@variable =` (this) assignments generally
* Comments before ‘if exists?’, refactor comment before ‘if this.var’ to be more precise, improve helper methods
* Comments before a method that contains ‘super()’ should output above the method property, not above the ‘super.method()’ call
* Fix missing comments before `if not` (i.e. before a UNARY token)
* Fix comments before ‘for’; add test for comment before assignment if (fixed in earlier commit)
* Comments within heregexes
* Updated browser compiler
* Update description to reflect what’s now happening in compileCommentFragments
* Preserve blank lines between line comments; output “whitespace-only” line comments as blank lines, rather than `//` following by whitespace
* Better future-proof comments tests
* Comments before object destructuring; abstract method for setting comments aside before compilation
* Handle more cases of comments before or after `for` loop declaration lines
* Fix indentation of comments preceding `for` loops
* Fix comment before splat function parameter
* Catch another RegexWithInterpolations comment edge case
* Updated browser compiler
* Change heregex example to one that’s more readable; update output
* Remove a few last references to the defunct HERECOMMENT token
* Abstract location hash creation into a function
* Improved clarity per code review notes
* Updated browser compiler
2017-08-02 22:34:34 -04:00
|
|
|
|
[stdin]:1:8: error: expected wrapped or quoted JSX attribute
|
2017-06-07 02:33:46 -04:00
|
|
|
|
<div x=3 />
|
|
|
|
|
^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "CSX error: unescaped opening tag angle bracket disallowed", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
<Person><<</Person>
|
|
|
|
|
''',
|
|
|
|
|
'''
|
|
|
|
|
[stdin]:1:9: error: unexpected <<
|
|
|
|
|
<Person><<</Person>
|
|
|
|
|
^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "CSX error: ambiguous tag-like expression", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
x = a <b > c
|
|
|
|
|
''',
|
|
|
|
|
'''
|
|
|
|
|
[stdin]:1:10: error: missing </
|
|
|
|
|
x = a <b > c
|
|
|
|
|
^
|
|
|
|
|
'''
|
2017-06-14 18:11:53 -04:00
|
|
|
|
|
2017-08-03 00:00:39 -04:00
|
|
|
|
test 'CSX error: invalid attributes', ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
<div a="b" {props} />
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:12: error: Unexpected token. Allowed CSX attributes are: id="val", src={source}, {props...} or attribute.
|
|
|
|
|
<div a="b" {props} />
|
|
|
|
|
^^^^^^^
|
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
<div a={b} {a:{b}} />
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:12: error: Unexpected token. Allowed CSX attributes are: id="val", src={source}, {props...} or attribute.
|
|
|
|
|
<div a={b} {a:{b}} />
|
|
|
|
|
^^^^^^^
|
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
<div {"#{a}"} />
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:6: error: Unexpected token. Allowed CSX attributes are: id="val", src={source}, {props...} or attribute.
|
|
|
|
|
<div {"#{a}"} />
|
|
|
|
|
^^^^^^^^
|
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
<div props... />
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:11: error: Unexpected token. Allowed CSX attributes are: id="val", src={source}, {props...} or attribute.
|
|
|
|
|
<div props... />
|
|
|
|
|
^^^
|
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
<div {a:"b", props..., c:d()} />
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:6: error: Unexpected token. Allowed CSX attributes are: id="val", src={source}, {props...} or attribute.
|
|
|
|
|
<div {a:"b", props..., c:d()} />
|
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
'''
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
<div {props..., a, b} />
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:6: error: Unexpected token. Allowed CSX attributes are: id="val", src={source}, {props...} or attribute.
|
|
|
|
|
<div {props..., a, b} />
|
|
|
|
|
^^^^^^^^^^^^^^^^
|
|
|
|
|
'''
|
|
|
|
|
|
2017-06-14 18:11:53 -04:00
|
|
|
|
test 'Bound method called as callback before binding throws runtime error', ->
|
|
|
|
|
class Base
|
|
|
|
|
constructor: ->
|
|
|
|
|
f = @derivedBound
|
|
|
|
|
try
|
|
|
|
|
f()
|
|
|
|
|
ok no
|
|
|
|
|
catch e
|
|
|
|
|
eq e.message, 'Bound instance method accessed before binding'
|
|
|
|
|
|
|
|
|
|
class Derived extends Base
|
|
|
|
|
derivedBound: =>
|
|
|
|
|
ok no
|
|
|
|
|
d = new Derived
|
2017-06-29 21:39:05 -04:00
|
|
|
|
|
2017-06-29 22:00:01 -04:00
|
|
|
|
test "#3845/#3446: chain after function glyph (but not inline)", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
a -> .b
|
|
|
|
|
''',
|
|
|
|
|
'''
|
|
|
|
|
[stdin]:1:6: error: unexpected .
|
|
|
|
|
a -> .b
|
|
|
|
|
^
|
|
|
|
|
'''
|
|
|
|
|
|
2017-06-29 21:39:05 -04:00
|
|
|
|
test "#3906: error for unusual indentation", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
a
|
|
|
|
|
c
|
|
|
|
|
.d
|
|
|
|
|
|
|
|
|
|
e(
|
|
|
|
|
f)
|
|
|
|
|
|
|
|
|
|
g
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:2:1: error: unexpected indentation
|
|
|
|
|
c
|
|
|
|
|
^^
|
|
|
|
|
'''
|
2017-07-03 23:52:49 -04:00
|
|
|
|
|
|
|
|
|
test "#4283: error message for implicit call", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
(a, b c) ->
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:5: error: unexpected implicit function call
|
|
|
|
|
(a, b c) ->
|
|
|
|
|
^
|
|
|
|
|
'''
|
2017-07-09 13:28:50 -04:00
|
|
|
|
|
|
|
|
|
test "#3199: error message for call indented non-object", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
fn = ->
|
|
|
|
|
fn
|
|
|
|
|
1
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:3:1: error: unexpected indentation
|
|
|
|
|
1
|
|
|
|
|
^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "#3199: error message for call indented comprehension", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
fn = ->
|
|
|
|
|
fn
|
|
|
|
|
x for x in [1, 2, 3]
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:3:1: error: unexpected indentation
|
|
|
|
|
x for x in [1, 2, 3]
|
|
|
|
|
^^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "#3199: error message for return indented non-object", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
return
|
|
|
|
|
1
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:2:3: error: unexpected number
|
|
|
|
|
1
|
|
|
|
|
^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "#3199: error message for return indented comprehension", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
return
|
|
|
|
|
x for x in [1, 2, 3]
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:2:3: error: unexpected identifier
|
|
|
|
|
x for x in [1, 2, 3]
|
|
|
|
|
^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "#3199: error message for throw indented non-object", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
throw
|
|
|
|
|
1
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:2:3: error: unexpected number
|
|
|
|
|
1
|
|
|
|
|
^
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
test "#3199: error message for throw indented comprehension", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
throw
|
|
|
|
|
x for x in [1, 2, 3]
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:2:3: error: unexpected identifier
|
|
|
|
|
x for x in [1, 2, 3]
|
|
|
|
|
^
|
|
|
|
|
'''
|
2017-09-01 10:09:36 -04:00
|
|
|
|
|
|
|
|
|
test "#3098: suppressed newline should be unsuppressed by semicolon", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
a = ; 5
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:5: error: unexpected ;
|
|
|
|
|
a = ; 5
|
|
|
|
|
^
|
|
|
|
|
'''
|
2017-12-03 14:55:26 -05:00
|
|
|
|
|
|
|
|
|
test "#4811: '///' inside a heregex comment does not close the heregex", ->
|
|
|
|
|
assertErrorFormat '''
|
|
|
|
|
/// .* # comment ///
|
|
|
|
|
''', '''
|
|
|
|
|
[stdin]:1:1: error: missing ///
|
|
|
|
|
/// .* # comment ///
|
|
|
|
|
^^^
|
|
|
|
|
'''
|