2.0.0-beta3 (#4594)
* 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
* Fix docs scroll position getting screwed up by CodeMirror initialization
* Breaking change documentation about => (fixes #4593)
* Spread/rest syntax documentation
* Documentation about bound class methods
* 2.0.0-beta3 changelog
* Add note about ‘lib’
* Fix accidentally converting this to tabs
* Bump version to 2.0.0-beta3
* Update annotated source and test.html
2017-06-30 12:58:05 -04:00
|
|
|
// Generated by CoffeeScript 2.0.0-beta3
|
2010-07-24 11:31:43 -04:00
|
|
|
(function() {
|
2017-07-24 15:47:14 -04:00
|
|
|
var CoffeeScript, addHistory, addMultilineHandler, fs, getCommandId, merge, nodeREPL, path, replDefaults, runInContext, sawSIGINT, updateSyntaxError, vm;
|
2013-03-25 01:44:35 -04:00
|
|
|
|
|
|
|
fs = require('fs');
|
|
|
|
|
|
|
|
path = require('path');
|
2012-01-03 14:56:17 -05:00
|
|
|
|
2013-01-15 00:17:48 -05:00
|
|
|
vm = require('vm');
|
2012-01-03 14:56:17 -05:00
|
|
|
|
2013-01-16 02:31:40 -05:00
|
|
|
nodeREPL = require('repl');
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2017-05-14 00:18:05 -04:00
|
|
|
CoffeeScript = require('./');
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2017-04-06 13:06:45 -04:00
|
|
|
({merge, updateSyntaxError} = require('./helpers'));
|
2011-09-18 18:16:39 -04:00
|
|
|
|
2017-07-24 15:47:14 -04:00
|
|
|
sawSIGINT = false;
|
|
|
|
|
2013-01-15 00:17:48 -05:00
|
|
|
replDefaults = {
|
2013-01-16 02:31:40 -05:00
|
|
|
prompt: 'coffee> ',
|
2017-04-20 15:41:28 -04:00
|
|
|
historyFile: (function() {
|
|
|
|
var historyPath;
|
|
|
|
historyPath = process.env.XDG_CACHE_HOME || process.env.HOME;
|
|
|
|
if (historyPath) {
|
|
|
|
return path.join(historyPath, '.coffee_history');
|
|
|
|
}
|
|
|
|
})(),
|
2013-03-25 23:50:56 -04:00
|
|
|
historyMaxInputSize: 10240,
|
2017-07-24 15:47:14 -04:00
|
|
|
eval: async function(input, context, filename, cb) {
|
|
|
|
var Assign, Block, Call, Code, Literal, Value, ast, err, isAsync, js, referencedVars, result, token, tokens;
|
2013-01-26 03:07:56 -05:00
|
|
|
input = input.replace(/\uFF00/g, '\n');
|
2013-03-03 20:08:27 -05:00
|
|
|
input = input.replace(/^\(([\s\S]*)\n\)$/m, '$1');
|
2016-12-15 23:50:54 -05:00
|
|
|
input = input.replace(/^\s*try\s*{([\s\S]*)}\s*catch.*$/m, '$1');
|
2017-07-24 15:47:14 -04:00
|
|
|
({Block, Assign, Value, Literal, Call, Code} = require('./nodes'));
|
2012-01-03 18:21:26 -05:00
|
|
|
try {
|
2015-01-26 11:18:35 -05:00
|
|
|
tokens = CoffeeScript.tokens(input);
|
|
|
|
referencedVars = (function() {
|
2015-01-30 14:33:03 -05:00
|
|
|
var i, len, results;
|
|
|
|
results = [];
|
|
|
|
for (i = 0, len = tokens.length; i < len; i++) {
|
|
|
|
token = tokens[i];
|
2016-03-05 11:41:15 -05:00
|
|
|
if (token[0] === 'IDENTIFIER') {
|
2015-01-30 14:33:03 -05:00
|
|
|
results.push(token[1]);
|
2015-01-26 11:18:35 -05:00
|
|
|
}
|
|
|
|
}
|
2015-01-30 14:33:03 -05:00
|
|
|
return results;
|
2015-01-26 11:18:35 -05:00
|
|
|
})();
|
|
|
|
ast = CoffeeScript.nodes(tokens);
|
2017-06-07 14:37:25 -04:00
|
|
|
ast = new Block([new Assign(new Value(new Literal('__')), ast, '=')]);
|
2017-07-24 15:47:14 -04:00
|
|
|
ast = new Code([], ast);
|
|
|
|
isAsync = ast.isAsync;
|
|
|
|
ast = new Block([new Call(ast)]);
|
2013-03-03 20:08:27 -05:00
|
|
|
js = ast.compile({
|
2013-03-11 18:52:18 -04:00
|
|
|
bare: true,
|
2015-01-26 11:18:35 -05:00
|
|
|
locals: Object.keys(context),
|
2017-07-24 15:47:14 -04:00
|
|
|
referencedVars,
|
|
|
|
sharedScope: true
|
2013-01-15 00:17:48 -05:00
|
|
|
});
|
2017-07-24 15:47:14 -04:00
|
|
|
result = runInContext(js, context, filename);
|
|
|
|
if (isAsync) {
|
|
|
|
result = (await result);
|
|
|
|
if (!sawSIGINT) {
|
|
|
|
cb(null, result);
|
|
|
|
}
|
|
|
|
return sawSIGINT = false;
|
|
|
|
} else {
|
|
|
|
return cb(null, result);
|
|
|
|
}
|
2015-08-16 16:34:22 -04:00
|
|
|
} catch (error) {
|
|
|
|
err = error;
|
2013-08-02 00:52:36 -04:00
|
|
|
updateSyntaxError(err, input);
|
2013-07-31 07:27:49 -04:00
|
|
|
return cb(err);
|
2012-05-16 15:50:34 -04:00
|
|
|
}
|
2012-01-03 18:21:26 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-19 10:54:08 -05:00
|
|
|
runInContext = function(js, context, filename) {
|
|
|
|
if (context === global) {
|
|
|
|
return vm.runInThisContext(js, filename);
|
|
|
|
} else {
|
|
|
|
return vm.runInContext(js, context, filename);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-16 02:31:40 -05:00
|
|
|
addMultilineHandler = function(repl) {
|
2017-04-06 13:06:45 -04:00
|
|
|
var inputStream, multiline, nodeLineListener, origPrompt, outputStream, ref, rli;
|
|
|
|
({rli, inputStream, outputStream} = repl);
|
|
|
|
origPrompt = (ref = repl._prompt) != null ? ref : repl.prompt;
|
2013-01-16 02:31:40 -05:00
|
|
|
multiline = {
|
|
|
|
enabled: false,
|
2014-04-19 04:18:21 -04:00
|
|
|
initialPrompt: origPrompt.replace(/^[^> ]*/, function(x) {
|
2013-01-21 11:27:52 -05:00
|
|
|
return x.replace(/./g, '-');
|
|
|
|
}),
|
2014-04-19 04:18:21 -04:00
|
|
|
prompt: origPrompt.replace(/^[^> ]*>?/, function(x) {
|
2013-01-21 11:27:52 -05:00
|
|
|
return x.replace(/./g, '.');
|
|
|
|
}),
|
2013-01-16 02:31:40 -05:00
|
|
|
buffer: ''
|
|
|
|
};
|
|
|
|
nodeLineListener = rli.listeners('line')[0];
|
|
|
|
rli.removeListener('line', nodeLineListener);
|
|
|
|
rli.on('line', function(cmd) {
|
2013-01-21 01:43:08 -05:00
|
|
|
if (multiline.enabled) {
|
2016-11-28 09:05:51 -05:00
|
|
|
multiline.buffer += `${cmd}\n`;
|
2013-01-21 01:43:08 -05:00
|
|
|
rli.setPrompt(multiline.prompt);
|
|
|
|
rli.prompt(true);
|
2013-01-16 02:31:40 -05:00
|
|
|
} else {
|
2014-04-19 04:18:21 -04:00
|
|
|
rli.setPrompt(origPrompt);
|
2013-01-21 01:43:08 -05:00
|
|
|
nodeLineListener(cmd);
|
2013-01-16 02:31:40 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return inputStream.on('keypress', function(char, key) {
|
|
|
|
if (!(key && key.ctrl && !key.meta && !key.shift && key.name === 'v')) {
|
|
|
|
return;
|
|
|
|
}
|
2013-01-21 01:43:08 -05:00
|
|
|
if (multiline.enabled) {
|
2013-01-16 02:31:40 -05:00
|
|
|
if (!multiline.buffer.match(/\n/)) {
|
2013-01-21 01:43:08 -05:00
|
|
|
multiline.enabled = !multiline.enabled;
|
2014-04-19 04:18:21 -04:00
|
|
|
rli.setPrompt(origPrompt);
|
2013-01-16 02:31:40 -05:00
|
|
|
rli.prompt(true);
|
|
|
|
return;
|
|
|
|
}
|
2013-01-26 03:07:56 -05:00
|
|
|
if ((rli.line != null) && !rli.line.match(/^\s*$/)) {
|
2013-01-21 01:43:08 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
multiline.enabled = !multiline.enabled;
|
|
|
|
rli.line = '';
|
|
|
|
rli.cursor = 0;
|
|
|
|
rli.output.cursorTo(0);
|
|
|
|
rli.output.clearLine(1);
|
2013-01-21 11:27:52 -05:00
|
|
|
multiline.buffer = multiline.buffer.replace(/\n/g, '\uFF00');
|
2013-01-16 02:31:40 -05:00
|
|
|
rli.emit('line', multiline.buffer);
|
2013-01-21 01:43:08 -05:00
|
|
|
multiline.buffer = '';
|
2013-01-16 02:31:40 -05:00
|
|
|
} else {
|
2013-01-21 01:43:08 -05:00
|
|
|
multiline.enabled = !multiline.enabled;
|
|
|
|
rli.setPrompt(multiline.initialPrompt);
|
|
|
|
rli.prompt(true);
|
2013-01-16 02:31:40 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-03-25 23:50:56 -04:00
|
|
|
addHistory = function(repl, filename, maxSize) {
|
2014-06-03 05:29:03 -04:00
|
|
|
var buffer, fd, lastLine, readFd, size, stat;
|
2013-04-07 01:10:08 -04:00
|
|
|
lastLine = null;
|
2013-03-25 01:44:35 -04:00
|
|
|
try {
|
|
|
|
stat = fs.statSync(filename);
|
2013-03-25 23:50:56 -04:00
|
|
|
size = Math.min(maxSize, stat.size);
|
2013-03-25 12:49:59 -04:00
|
|
|
readFd = fs.openSync(filename, 'r');
|
2016-10-21 12:56:25 -04:00
|
|
|
buffer = Buffer.alloc(size);
|
2013-03-25 12:49:59 -04:00
|
|
|
fs.readSync(readFd, buffer, 0, size, stat.size - size);
|
2016-10-19 00:49:15 -04:00
|
|
|
fs.closeSync(readFd);
|
2013-03-25 01:44:35 -04:00
|
|
|
repl.rli.history = buffer.toString().split('\n').reverse();
|
2013-04-07 01:10:08 -04:00
|
|
|
if (stat.size > maxSize) {
|
2013-03-25 23:50:56 -04:00
|
|
|
repl.rli.history.pop();
|
|
|
|
}
|
|
|
|
if (repl.rli.history[0] === '') {
|
|
|
|
repl.rli.history.shift();
|
|
|
|
}
|
2013-03-25 01:44:35 -04:00
|
|
|
repl.rli.historyIndex = -1;
|
2013-04-07 01:10:08 -04:00
|
|
|
lastLine = repl.rli.history[0];
|
2015-11-02 02:05:35 -05:00
|
|
|
} catch (error) {}
|
2013-03-25 01:44:35 -04:00
|
|
|
fd = fs.openSync(filename, 'a');
|
|
|
|
repl.rli.addListener('line', function(code) {
|
2016-04-14 12:24:21 -04:00
|
|
|
if (code && code.length && code !== '.history' && code !== '.exit' && lastLine !== code) {
|
2016-11-28 09:05:51 -05:00
|
|
|
fs.writeSync(fd, `${code}\n`);
|
2013-04-07 14:28:59 -04:00
|
|
|
return lastLine = code;
|
2013-03-25 01:44:35 -04:00
|
|
|
}
|
|
|
|
});
|
2017-07-24 15:47:14 -04:00
|
|
|
repl.on('SIGINT', function() {
|
|
|
|
return sawSIGINT = true;
|
|
|
|
});
|
2015-04-22 02:26:44 -04:00
|
|
|
repl.on('exit', function() {
|
2016-10-19 00:49:15 -04:00
|
|
|
return fs.closeSync(fd);
|
2013-03-25 01:44:35 -04:00
|
|
|
});
|
2014-06-03 16:50:25 -04:00
|
|
|
return repl.commands[getCommandId(repl, 'history')] = {
|
2013-03-25 01:44:35 -04:00
|
|
|
help: 'Show command history',
|
|
|
|
action: function() {
|
2016-11-28 09:05:51 -05:00
|
|
|
repl.outputStream.write(`${repl.rli.history.slice(0).reverse().join('\n')}\n`);
|
2013-03-25 01:44:35 -04:00
|
|
|
return repl.displayPrompt();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-06-03 05:29:03 -04:00
|
|
|
getCommandId = function(repl, commandName) {
|
|
|
|
var commandsHaveLeadingDot;
|
2014-06-03 16:50:25 -04:00
|
|
|
commandsHaveLeadingDot = repl.commands['.help'] != null;
|
2014-06-03 05:29:03 -04:00
|
|
|
if (commandsHaveLeadingDot) {
|
2016-11-28 09:05:51 -05:00
|
|
|
return `.${commandName}`;
|
2014-06-03 05:29:03 -04:00
|
|
|
} else {
|
2014-06-03 16:50:25 -04:00
|
|
|
return commandName;
|
2014-06-03 05:29:03 -04:00
|
|
|
}
|
2014-06-02 07:24:57 -04:00
|
|
|
};
|
|
|
|
|
2013-01-15 00:17:48 -05:00
|
|
|
module.exports = {
|
[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
|
|
|
start: function(opts = {}) {
|
2017-04-06 13:06:45 -04:00
|
|
|
var build, major, minor, repl;
|
|
|
|
[major, minor, build] = process.versions.node.split('.').map(function(n) {
|
2017-04-17 15:49:30 -04:00
|
|
|
return parseInt(n, 10);
|
2017-04-06 13:06:45 -04:00
|
|
|
});
|
2016-10-21 12:56:25 -04:00
|
|
|
if (major < 6) {
|
|
|
|
console.warn("Node 6+ required for CoffeeScript REPL");
|
2013-03-12 19:06:38 -04:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2013-12-08 15:21:18 -05:00
|
|
|
CoffeeScript.register();
|
2013-10-20 15:53:08 -04:00
|
|
|
process.argv = ['coffee'].concat(process.argv.slice(2));
|
2013-01-16 02:31:40 -05:00
|
|
|
opts = merge(replDefaults, opts);
|
|
|
|
repl = nodeREPL.start(opts);
|
2015-02-19 10:54:08 -05:00
|
|
|
if (opts.prelude) {
|
|
|
|
runInContext(opts.prelude, repl.context, 'prelude');
|
|
|
|
}
|
2013-01-21 01:43:08 -05:00
|
|
|
repl.on('exit', function() {
|
2015-04-22 02:11:56 -04:00
|
|
|
if (!repl.rli.closed) {
|
|
|
|
return repl.outputStream.write('\n');
|
|
|
|
}
|
2013-01-21 01:43:08 -05:00
|
|
|
});
|
2013-01-16 02:31:40 -05:00
|
|
|
addMultilineHandler(repl);
|
2013-03-25 11:46:40 -04:00
|
|
|
if (opts.historyFile) {
|
2013-03-25 23:50:56 -04:00
|
|
|
addHistory(repl, opts.historyFile, opts.historyMaxInputSize);
|
2013-03-25 11:46:40 -04:00
|
|
|
}
|
2014-06-03 16:50:25 -04:00
|
|
|
repl.commands[getCommandId(repl, 'load')].help = 'Load code from a file into this REPL session';
|
2013-01-16 02:31:40 -05:00
|
|
|
return repl;
|
2012-01-03 18:21:26 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-09-21 03:53:58 -04:00
|
|
|
}).call(this);
|