2016-12-16 05:28:24 +00:00
|
|
|
// Generated by CoffeeScript 1.12.2
|
2010-07-24 15:31:43 +00:00
|
|
|
(function() {
|
2016-12-16 05:28:24 +00:00
|
|
|
var Lexer, SourceMap, base64encode, compile, ext, fn1, fs, helpers, i, len, lexer, packageJson, parser, path, ref, vm, withPrettyErrors,
|
2016-09-14 19:45:06 +00:00
|
|
|
hasProp = {}.hasOwnProperty;
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-10-24 18:19:47 +00:00
|
|
|
fs = require('fs');
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2013-03-18 03:46:54 +00:00
|
|
|
vm = require('vm');
|
|
|
|
|
2010-09-21 09:19:49 +00:00
|
|
|
path = require('path');
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2012-09-25 23:01:16 +00:00
|
|
|
Lexer = require('./lexer').Lexer;
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-09-28 20:47:12 +00:00
|
|
|
parser = require('./parser').parser;
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2013-02-28 02:54:17 +00:00
|
|
|
helpers = require('./helpers');
|
2013-02-28 20:51:29 +00:00
|
|
|
|
2013-03-18 11:23:05 +00:00
|
|
|
SourceMap = require('./sourcemap');
|
2012-09-25 23:01:16 +00:00
|
|
|
|
2016-12-16 04:52:31 +00:00
|
|
|
packageJson = require('../../package.json');
|
|
|
|
|
|
|
|
exports.VERSION = packageJson.version;
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2013-10-20 14:08:13 +00:00
|
|
|
exports.FILE_EXTENSIONS = ['.coffee', '.litcoffee', '.coffee.md'];
|
2013-07-30 04:06:41 +00:00
|
|
|
|
2013-03-04 21:40:39 +00:00
|
|
|
exports.helpers = helpers;
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2016-01-31 18:48:40 +00:00
|
|
|
base64encode = function(src) {
|
|
|
|
switch (false) {
|
|
|
|
case typeof Buffer !== 'function':
|
|
|
|
return new Buffer(src).toString('base64');
|
|
|
|
case typeof btoa !== 'function':
|
2016-11-28 07:05:49 +00:00
|
|
|
return btoa(encodeURIComponent(src).replace(/%([0-9A-F]{2})/g, function(match, p1) {
|
|
|
|
return String.fromCharCode('0x' + p1);
|
|
|
|
}));
|
2016-01-31 18:48:40 +00:00
|
|
|
default:
|
|
|
|
throw new Error('Unable to base64 encode inline sourcemap.');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-31 11:27:49 +00:00
|
|
|
withPrettyErrors = function(fn) {
|
|
|
|
return function(code, options) {
|
2015-09-13 10:27:07 +00:00
|
|
|
var err;
|
2013-07-31 11:27:49 +00:00
|
|
|
if (options == null) {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return fn.call(this, code, options);
|
2015-08-16 20:34:22 +00:00
|
|
|
} catch (error) {
|
|
|
|
err = error;
|
2015-05-01 11:43:04 +00:00
|
|
|
if (typeof code !== 'string') {
|
|
|
|
throw err;
|
|
|
|
}
|
2013-08-02 04:52:36 +00:00
|
|
|
throw helpers.updateSyntaxError(err, code, options.filename);
|
2013-07-31 11:27:49 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.compile = compile = withPrettyErrors(function(code, options) {
|
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 18:46:05 +00:00
|
|
|
var currentColumn, currentLine, encoded, extend, fragment, fragments, generateSourceMap, header, i, j, js, len, len1, map, merge, newLines, ref, ref1, sourceMapDataURI, sourceURL, token, tokens, v3SourceMap;
|
2013-10-20 19:21:06 +00:00
|
|
|
merge = helpers.merge, extend = helpers.extend;
|
|
|
|
options = extend({}, options);
|
2016-01-31 18:48:40 +00:00
|
|
|
generateSourceMap = options.sourceMap || options.inlineMap;
|
|
|
|
if (generateSourceMap) {
|
2013-03-18 11:23:05 +00:00
|
|
|
map = new SourceMap;
|
2013-03-05 02:37:36 +00:00
|
|
|
}
|
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 22:04:30 +00:00
|
|
|
tokens = lexer.tokenize(code, options);
|
|
|
|
options.referencedVars = (function() {
|
2015-01-30 19:33:03 +00:00
|
|
|
var i, len, results;
|
|
|
|
results = [];
|
|
|
|
for (i = 0, len = tokens.length; i < len; i++) {
|
|
|
|
token = tokens[i];
|
2016-03-05 16:41:15 +00:00
|
|
|
if (token[0] === 'IDENTIFIER') {
|
2015-01-30 19:33:03 +00:00
|
|
|
results.push(token[1]);
|
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 22:04:30 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-30 19:33:03 +00:00
|
|
|
return results;
|
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 22:04:30 +00:00
|
|
|
})();
|
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 18:46:05 +00:00
|
|
|
if (!((options.bare != null) && options.bare === true)) {
|
|
|
|
for (i = 0, len = tokens.length; i < len; i++) {
|
|
|
|
token = tokens[i];
|
|
|
|
if ((ref = token[0]) === 'IMPORT' || ref === 'EXPORT') {
|
|
|
|
options.bare = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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 22:04:30 +00:00
|
|
|
fragments = parser.parse(tokens).compileToFragments(options);
|
2013-03-05 02:37:36 +00:00
|
|
|
currentLine = 0;
|
2013-03-25 17:56:24 +00:00
|
|
|
if (options.header) {
|
|
|
|
currentLine += 1;
|
|
|
|
}
|
|
|
|
if (options.shiftLine) {
|
2013-03-05 02:37:36 +00:00
|
|
|
currentLine += 1;
|
|
|
|
}
|
|
|
|
currentColumn = 0;
|
|
|
|
js = "";
|
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 18:46:05 +00:00
|
|
|
for (j = 0, len1 = fragments.length; j < len1; j++) {
|
|
|
|
fragment = fragments[j];
|
2016-01-31 18:48:40 +00:00
|
|
|
if (generateSourceMap) {
|
2015-05-13 15:50:02 +00:00
|
|
|
if (fragment.locationData && !/^[;\s]*$/.test(fragment.code)) {
|
2013-03-18 11:23:05 +00:00
|
|
|
map.add([fragment.locationData.first_line, fragment.locationData.first_column], [currentLine, currentColumn], {
|
2013-03-05 02:37:36 +00:00
|
|
|
noReplace: true
|
|
|
|
});
|
2013-02-28 20:51:29 +00:00
|
|
|
}
|
2013-03-05 02:37:36 +00:00
|
|
|
newLines = helpers.count(fragment.code, "\n");
|
|
|
|
currentLine += newLines;
|
2013-08-06 20:25:23 +00:00
|
|
|
if (newLines) {
|
|
|
|
currentColumn = fragment.code.length - (fragment.code.lastIndexOf("\n") + 1);
|
|
|
|
} else {
|
|
|
|
currentColumn += fragment.code.length;
|
|
|
|
}
|
2013-02-28 20:51:29 +00:00
|
|
|
}
|
2013-03-05 02:37:36 +00:00
|
|
|
js += fragment.code;
|
2010-03-08 03:08:24 +00:00
|
|
|
}
|
2013-02-25 22:20:37 +00:00
|
|
|
if (options.header) {
|
|
|
|
header = "Generated by CoffeeScript " + this.VERSION;
|
|
|
|
js = "// " + header + "\n" + js;
|
2010-03-08 03:08:24 +00:00
|
|
|
}
|
2016-01-31 18:48:40 +00:00
|
|
|
if (generateSourceMap) {
|
|
|
|
v3SourceMap = map.generate(options, code);
|
|
|
|
}
|
|
|
|
if (options.inlineMap) {
|
Improve inline source maps generation
- Inline source maps are now shorter by not using pretty-printed JSON.
- `.register()`ed files are now given more information in their inline source
maps: The name and contents of the source file.
- Some code cleanup.
If you decode the inline source map generated (when using `.register()`) for a
file test.coffee with the contents `console.log "it works!"`, here is the
output:
Before:
{
"version": 3,
"file": "",
"sourceRoot": "",
"sources": [
""
],
"names": [],
"mappings": "AAAA;EAAA,OAAO,CAAC,GAAR,CAAY,eAAZ;AAAA"
}
After:
{"version":3,"file":"","sourceRoot":"","sources":["test.coffee"],"names":[],"mappings":"AAAA;EAAA,OAAO,CAAC,GAAR,CAAY,WAAZ;AAAA","sourcesContent":["console.log \"it works!\"\n"]}
Related: #4214.
2016-03-06 13:30:47 +00:00
|
|
|
encoded = base64encode(JSON.stringify(v3SourceMap));
|
|
|
|
sourceMapDataURI = "//# sourceMappingURL=data:application/json;base64," + encoded;
|
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 18:46:05 +00:00
|
|
|
sourceURL = "//# sourceURL=" + ((ref1 = options.filename) != null ? ref1 : 'coffeescript');
|
2016-01-31 18:48:40 +00:00
|
|
|
js = js + "\n" + sourceMapDataURI + "\n" + sourceURL;
|
|
|
|
}
|
2013-03-04 21:19:21 +00:00
|
|
|
if (options.sourceMap) {
|
Improve inline source maps generation
- Inline source maps are now shorter by not using pretty-printed JSON.
- `.register()`ed files are now given more information in their inline source
maps: The name and contents of the source file.
- Some code cleanup.
If you decode the inline source map generated (when using `.register()`) for a
file test.coffee with the contents `console.log "it works!"`, here is the
output:
Before:
{
"version": 3,
"file": "",
"sourceRoot": "",
"sources": [
""
],
"names": [],
"mappings": "AAAA;EAAA,OAAO,CAAC,GAAR,CAAY,eAAZ;AAAA"
}
After:
{"version":3,"file":"","sourceRoot":"","sources":["test.coffee"],"names":[],"mappings":"AAAA;EAAA,OAAO,CAAC,GAAR,CAAY,WAAZ;AAAA","sourcesContent":["console.log \"it works!\"\n"]}
Related: #4214.
2016-03-06 13:30:47 +00:00
|
|
|
return {
|
|
|
|
js: js,
|
|
|
|
sourceMap: map,
|
|
|
|
v3SourceMap: JSON.stringify(v3SourceMap, null, 2)
|
2013-03-01 16:34:39 +00:00
|
|
|
};
|
2013-03-04 14:45:25 +00:00
|
|
|
} else {
|
|
|
|
return js;
|
2013-02-28 20:51:29 +00:00
|
|
|
}
|
2013-07-31 11:27:49 +00:00
|
|
|
});
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2013-07-31 11:27:49 +00:00
|
|
|
exports.tokens = withPrettyErrors(function(code, options) {
|
2010-10-08 19:27:05 +00:00
|
|
|
return lexer.tokenize(code, options);
|
2013-07-31 11:27:49 +00:00
|
|
|
});
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2013-07-31 11:27:49 +00:00
|
|
|
exports.nodes = withPrettyErrors(function(source, options) {
|
2010-11-17 16:34:23 +00:00
|
|
|
if (typeof source === 'string') {
|
2010-11-20 21:25:22 +00:00
|
|
|
return parser.parse(lexer.tokenize(source, options));
|
2010-11-17 16:34:23 +00:00
|
|
|
} else {
|
|
|
|
return parser.parse(source);
|
|
|
|
}
|
2013-07-31 11:27:49 +00:00
|
|
|
});
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-09-04 10:39:01 +00:00
|
|
|
exports.run = function(code, options) {
|
2015-01-30 19:33:03 +00:00
|
|
|
var answer, dir, mainModule, ref;
|
2012-04-10 18:57:45 +00:00
|
|
|
if (options == null) {
|
|
|
|
options = {};
|
|
|
|
}
|
2011-05-03 19:53:10 +00:00
|
|
|
mainModule = require.main;
|
|
|
|
mainModule.filename = process.argv[1] = options.filename ? fs.realpathSync(options.filename) : '.';
|
2011-05-03 20:10:30 +00:00
|
|
|
mainModule.moduleCache && (mainModule.moduleCache = {});
|
2014-01-29 18:23:19 +00:00
|
|
|
dir = options.filename ? path.dirname(fs.realpathSync(options.filename)) : fs.realpathSync('.');
|
2013-10-20 20:50:13 +00:00
|
|
|
mainModule.paths = require('module')._nodeModulePaths(dir);
|
2013-02-28 02:54:17 +00:00
|
|
|
if (!helpers.isCoffee(mainModule.filename) || require.extensions) {
|
2013-03-12 06:26:51 +00:00
|
|
|
answer = compile(code, options);
|
2015-01-30 19:33:03 +00:00
|
|
|
code = (ref = answer.js) != null ? ref : answer;
|
2010-11-09 04:07:51 +00:00
|
|
|
}
|
2013-07-30 04:06:41 +00:00
|
|
|
return mainModule._compile(code, mainModule.filename);
|
2010-09-04 10:39:01 +00:00
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2012-01-11 23:04:14 +00:00
|
|
|
exports["eval"] = function(code, options) {
|
2015-01-30 19:33:03 +00:00
|
|
|
var Module, _module, _require, createContext, i, isContext, js, k, len, o, r, ref, ref1, ref2, ref3, sandbox, v;
|
2012-04-10 18:57:45 +00:00
|
|
|
if (options == null) {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
if (!(code = code.trim())) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-30 19:33:03 +00:00
|
|
|
createContext = (ref = vm.Script.createContext) != null ? ref : vm.createContext;
|
|
|
|
isContext = (ref1 = vm.isContext) != null ? ref1 : function(ctx) {
|
2015-01-05 20:40:04 +00:00
|
|
|
return options.sandbox instanceof createContext().constructor;
|
|
|
|
};
|
|
|
|
if (createContext) {
|
2011-08-05 03:17:23 +00:00
|
|
|
if (options.sandbox != null) {
|
2015-01-05 20:40:04 +00:00
|
|
|
if (isContext(options.sandbox)) {
|
2011-08-05 03:17:23 +00:00
|
|
|
sandbox = options.sandbox;
|
|
|
|
} else {
|
2015-01-05 20:40:04 +00:00
|
|
|
sandbox = createContext();
|
2015-01-30 19:33:03 +00:00
|
|
|
ref2 = options.sandbox;
|
|
|
|
for (k in ref2) {
|
|
|
|
if (!hasProp.call(ref2, k)) continue;
|
|
|
|
v = ref2[k];
|
2011-08-05 03:17:23 +00:00
|
|
|
sandbox[k] = v;
|
|
|
|
}
|
2011-05-01 05:45:14 +00:00
|
|
|
}
|
2011-10-24 02:45:32 +00:00
|
|
|
sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox;
|
|
|
|
} else {
|
|
|
|
sandbox = global;
|
2011-04-29 17:59:59 +00:00
|
|
|
}
|
2011-08-05 03:17:23 +00:00
|
|
|
sandbox.__filename = options.filename || 'eval';
|
|
|
|
sandbox.__dirname = path.dirname(sandbox.__filename);
|
2011-10-24 02:45:32 +00:00
|
|
|
if (!(sandbox !== global || sandbox.module || sandbox.require)) {
|
2011-08-05 03:17:23 +00:00
|
|
|
Module = require('module');
|
|
|
|
sandbox.module = _module = new Module(options.modulename || 'eval');
|
|
|
|
sandbox.require = _require = function(path) {
|
2011-10-24 02:45:32 +00:00
|
|
|
return Module._load(path, _module, true);
|
2011-08-05 03:17:23 +00:00
|
|
|
};
|
|
|
|
_module.filename = sandbox.__filename;
|
2015-01-30 19:33:03 +00:00
|
|
|
ref3 = Object.getOwnPropertyNames(require);
|
|
|
|
for (i = 0, len = ref3.length; i < len; i++) {
|
|
|
|
r = ref3[i];
|
2015-06-22 12:36:29 +00:00
|
|
|
if (r !== 'paths' && r !== 'arguments' && r !== 'caller') {
|
2012-04-10 18:57:45 +00:00
|
|
|
_require[r] = require[r];
|
|
|
|
}
|
2011-08-05 03:17:23 +00:00
|
|
|
}
|
|
|
|
_require.paths = _module.paths = Module._nodeModulePaths(process.cwd());
|
|
|
|
_require.resolve = function(request) {
|
|
|
|
return Module._resolveFilename(request, _module);
|
|
|
|
};
|
2011-07-06 07:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-25 07:53:51 +00:00
|
|
|
o = {};
|
|
|
|
for (k in options) {
|
2015-01-30 19:33:03 +00:00
|
|
|
if (!hasProp.call(options, k)) continue;
|
2011-05-25 07:53:51 +00:00
|
|
|
v = options[k];
|
|
|
|
o[k] = v;
|
|
|
|
}
|
|
|
|
o.bare = true;
|
2011-07-06 20:31:48 +00:00
|
|
|
js = compile(code, o);
|
2011-10-24 02:45:32 +00:00
|
|
|
if (sandbox === global) {
|
|
|
|
return vm.runInThisContext(js);
|
2011-08-05 03:17:23 +00:00
|
|
|
} else {
|
2011-10-24 02:45:32 +00:00
|
|
|
return vm.runInContext(js, sandbox);
|
2011-08-05 03:17:23 +00:00
|
|
|
}
|
2010-09-21 05:36:23 +00:00
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2013-12-08 20:21:18 +00:00
|
|
|
exports.register = function() {
|
|
|
|
return require('./register');
|
|
|
|
};
|
|
|
|
|
2014-02-23 02:40:19 +00:00
|
|
|
if (require.extensions) {
|
2015-01-30 19:33:03 +00:00
|
|
|
ref = this.FILE_EXTENSIONS;
|
2016-01-07 06:21:05 +00:00
|
|
|
fn1 = function(ext) {
|
|
|
|
var base;
|
|
|
|
return (base = require.extensions)[ext] != null ? base[ext] : base[ext] = function() {
|
|
|
|
throw new Error("Use CoffeeScript.register() or require the coffee-script/register module to require " + ext + " files.");
|
|
|
|
};
|
|
|
|
};
|
2015-01-30 19:33:03 +00:00
|
|
|
for (i = 0, len = ref.length; i < len; i++) {
|
|
|
|
ext = ref[i];
|
2016-01-07 06:21:05 +00:00
|
|
|
fn1(ext);
|
2014-02-23 02:40:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-31 18:48:40 +00:00
|
|
|
exports._compileFile = function(filename, sourceMap, inlineMap) {
|
2015-09-13 10:27:07 +00:00
|
|
|
var answer, err, raw, stripped;
|
2013-10-20 14:08:13 +00:00
|
|
|
if (sourceMap == null) {
|
|
|
|
sourceMap = false;
|
|
|
|
}
|
2016-01-31 18:48:40 +00:00
|
|
|
if (inlineMap == null) {
|
|
|
|
inlineMap = false;
|
|
|
|
}
|
2013-03-18 03:46:54 +00:00
|
|
|
raw = fs.readFileSync(filename, 'utf8');
|
|
|
|
stripped = raw.charCodeAt(0) === 0xFEFF ? raw.substring(1) : raw;
|
2013-06-09 05:54:34 +00:00
|
|
|
try {
|
|
|
|
answer = compile(stripped, {
|
|
|
|
filename: filename,
|
2013-07-30 04:06:41 +00:00
|
|
|
sourceMap: sourceMap,
|
2016-01-31 18:48:40 +00:00
|
|
|
inlineMap: inlineMap,
|
Improve inline source maps generation
- Inline source maps are now shorter by not using pretty-printed JSON.
- `.register()`ed files are now given more information in their inline source
maps: The name and contents of the source file.
- Some code cleanup.
If you decode the inline source map generated (when using `.register()`) for a
file test.coffee with the contents `console.log "it works!"`, here is the
output:
Before:
{
"version": 3,
"file": "",
"sourceRoot": "",
"sources": [
""
],
"names": [],
"mappings": "AAAA;EAAA,OAAO,CAAC,GAAR,CAAY,eAAZ;AAAA"
}
After:
{"version":3,"file":"","sourceRoot":"","sources":["test.coffee"],"names":[],"mappings":"AAAA;EAAA,OAAO,CAAC,GAAR,CAAY,WAAZ;AAAA","sourcesContent":["console.log \"it works!\"\n"]}
Related: #4214.
2016-03-06 13:30:47 +00:00
|
|
|
sourceFiles: [filename],
|
2013-06-09 05:54:34 +00:00
|
|
|
literate: helpers.isLiterate(filename)
|
|
|
|
});
|
2015-08-16 20:34:22 +00:00
|
|
|
} catch (error) {
|
|
|
|
err = error;
|
2013-08-02 04:52:36 +00:00
|
|
|
throw helpers.updateSyntaxError(err, stripped, filename);
|
2013-06-09 05:54:34 +00:00
|
|
|
}
|
2013-07-30 04:06:41 +00:00
|
|
|
return answer;
|
|
|
|
};
|
|
|
|
|
2010-09-25 08:39:19 +00:00
|
|
|
lexer = new Lexer;
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-02-11 06:57:33 +00:00
|
|
|
parser.lexer = {
|
2010-05-15 03:40:04 +00:00
|
|
|
lex: function() {
|
2012-11-21 21:57:30 +00:00
|
|
|
var tag, token;
|
2015-01-15 16:47:07 +00:00
|
|
|
token = parser.tokens[this.pos++];
|
2013-01-14 20:20:35 +00:00
|
|
|
if (token) {
|
|
|
|
tag = token[0], this.yytext = token[1], this.yylloc = token[2];
|
2015-01-15 16:47:07 +00:00
|
|
|
parser.errorToken = token.origin || token;
|
2013-01-14 20:20:35 +00:00
|
|
|
this.yylineno = this.yylloc.first_line;
|
|
|
|
} else {
|
|
|
|
tag = '';
|
|
|
|
}
|
2010-11-02 04:05:06 +00:00
|
|
|
return tag;
|
2010-02-11 06:57:33 +00:00
|
|
|
},
|
2015-01-15 16:47:07 +00:00
|
|
|
setInput: function(tokens) {
|
|
|
|
parser.tokens = tokens;
|
2010-10-20 22:19:08 +00:00
|
|
|
return this.pos = 0;
|
2010-02-11 06:57:33 +00:00
|
|
|
},
|
2010-05-15 03:40:04 +00:00
|
|
|
upcomingInput: function() {
|
2010-02-11 06:57:33 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-09-21 07:50:32 +00:00
|
|
|
parser.yy = require('./nodes');
|
2011-12-14 15:39:20 +00:00
|
|
|
|
2015-01-30 19:33:03 +00:00
|
|
|
parser.yy.parseError = function(message, arg) {
|
2015-01-15 16:47:07 +00:00
|
|
|
var errorLoc, errorTag, errorText, errorToken, token, tokens;
|
2015-01-30 19:33:03 +00:00
|
|
|
token = arg.token;
|
2015-01-15 16:47:07 +00:00
|
|
|
errorToken = parser.errorToken, tokens = parser.tokens;
|
2014-01-26 05:25:13 +00:00
|
|
|
errorTag = errorToken[0], errorText = errorToken[1], errorLoc = errorToken[2];
|
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 19:16:59 +00:00
|
|
|
errorText = (function() {
|
|
|
|
switch (false) {
|
|
|
|
case errorToken !== tokens[tokens.length - 1]:
|
|
|
|
return 'end of input';
|
|
|
|
case errorTag !== 'INDENT' && errorTag !== 'OUTDENT':
|
|
|
|
return 'indentation';
|
Refactor `Literal` into several subtypes
Previously, the parser created `Literal` nodes for many things. This resulted in
information loss. Instead of being able to check the node type, we had to use
regexes to tell the different types of `Literal`s apart. That was a bit like
parsing literals twice: Once in the lexer, and once (or more) in the compiler.
It also caused problems, such as `` `this` `` and `this` being indistinguishable
(fixes #2009).
Instead returning `new Literal` in the grammar, subtypes of it are now returned
instead, such as `NumberLiteral`, `StringLiteral` and `IdentifierLiteral`. `new
Literal` by itself is only used to represent code chunks that fit no category.
(While mentioning `NumberLiteral`, there's also `InfinityLiteral` now, which is
a subtype of `NumberLiteral`.)
`StringWithInterpolations` has been added as a subtype of `Parens`, and
`RegexWithInterpolations` as a subtype of `Call`. This makes it easier for other
programs to make use of CoffeeScript's "AST" (nodes). For example, it is now
possible to distinguish between `"a #{b} c"` and `"a " + b + " c"`. Fixes #4192.
`SuperCall` has been added as a subtype of `Call`.
Note, though, that some information is still lost, especially in the lexer. For
example, there is no way to distinguish a heredoc from a regular string, or a
heregex without interpolations from a regular regex. Binary and octal number
literals are indistinguishable from hexadecimal literals.
After the new subtypes were added, they were taken advantage of, removing most
regexes in nodes.coffee. `SIMPLENUM` (which matches non-hex integers) had to be
kept, though, because such numbers need special handling in JavaScript (for
example in `1..toString()`).
An especially nice hack to get rid of was using `new String()` for the token
value for reserved identifiers (to be able to set a property on them which could
survive through the parser). Now it's a good old regular string.
In range literals, slices, splices and for loop steps when number literals
are involved, CoffeeScript can do some optimizations, such as precomputing the
value of, say, `5 - 3` (outputting `2` instead of `5 - 3` literally). As a side
bonus, this now also works with hexadecimal number literals, such as `0x02`.
Finally, this also improves the output of `coffee --nodes`:
# Before:
$ bin/coffee -ne 'while true
"#{a}"
break'
Block
While
Value
Bool
Block
Value
Parens
Block
Op +
Value """"
Value
Parens
Block
Value "a" "break"
# After:
$ bin/coffee -ne 'while true
"#{a}"
break'
Block
While
Value BooleanLiteral: true
Block
Value
StringWithInterpolations
Block
Op +
Value StringLiteral: ""
Value
Parens
Block
Value IdentifierLiteral: a
StatementLiteral: break
2016-01-31 19:24:31 +00:00
|
|
|
case errorTag !== 'IDENTIFIER' && errorTag !== 'NUMBER' && errorTag !== 'INFINITY' && errorTag !== 'STRING' && errorTag !== 'STRING_START' && errorTag !== 'REGEX' && errorTag !== 'REGEX_START':
|
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 19:16:59 +00:00
|
|
|
return errorTag.replace(/_START$/, '').toLowerCase();
|
|
|
|
default:
|
|
|
|
return helpers.nameWhitespaceCharacter(errorText);
|
|
|
|
}
|
|
|
|
})();
|
2014-01-22 02:44:50 +00:00
|
|
|
return helpers.throwSyntaxError("unexpected " + errorText, errorLoc);
|
2013-02-25 18:09:42 +00:00
|
|
|
};
|
|
|
|
|
2010-09-21 07:53:58 +00:00
|
|
|
}).call(this);
|