mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
We no longer need to wrap in quotes JavaScript reserved words used as properties (#4527)
This commit is contained in:
parent
5651b8b14b
commit
7e35c2c3da
9 changed files with 41 additions and 54 deletions
|
@ -9,7 +9,7 @@
|
|||
|
||||
compile = CoffeeScript.compile;
|
||||
|
||||
CoffeeScript["eval"] = function(code, options = {}) {
|
||||
CoffeeScript.eval = function(code, options = {}) {
|
||||
if (options.bare == null) {
|
||||
options.bare = true;
|
||||
}
|
||||
|
|
|
@ -58,7 +58,7 @@
|
|||
e = error;
|
||||
return fatalError(`${e}`);
|
||||
}
|
||||
ref = options["arguments"];
|
||||
ref = options.arguments;
|
||||
results = [];
|
||||
for (i = 0, len = ref.length; i < len; i++) {
|
||||
arg = ref[i];
|
||||
|
|
|
@ -167,7 +167,7 @@
|
|||
return mainModule._compile(code, mainModule.filename);
|
||||
};
|
||||
|
||||
exports["eval"] = function(code, options = {}) {
|
||||
exports.eval = function(code, options = {}) {
|
||||
var Module, _module, _require, createContext, i, isContext, js, k, len, o, r, ref, ref1, ref2, ref3, sandbox, v;
|
||||
if (!(code = code.trim())) {
|
||||
return;
|
||||
|
|
|
@ -74,13 +74,13 @@
|
|||
if (opts.stdio) {
|
||||
return compileStdio();
|
||||
}
|
||||
if (opts["eval"]) {
|
||||
return compileScript(null, opts["arguments"][0]);
|
||||
if (opts.eval) {
|
||||
return compileScript(null, opts.arguments[0]);
|
||||
}
|
||||
if (!opts["arguments"].length) {
|
||||
if (!opts.arguments.length) {
|
||||
return require('./repl').start(replCliOpts);
|
||||
}
|
||||
literals = opts.run ? opts["arguments"].splice(1) : [];
|
||||
literals = opts.run ? opts.arguments.splice(1) : [];
|
||||
process.argv = process.argv.slice(0, 2).concat(literals);
|
||||
process.argv[0] = 'coffee';
|
||||
if (opts.output) {
|
||||
|
@ -90,7 +90,7 @@
|
|||
opts.join = path.resolve(opts.join);
|
||||
console.error('\nThe --join option is deprecated and will be removed in a future version.\n\nIf for some reason it\'s necessary to share local variables between files,\nreplace...\n\n $ coffee --compile --join bundle.js -- a.coffee b.coffee c.coffee\n\nwith...\n\n $ cat a.coffee b.coffee c.coffee | coffee --compile --stdio > bundle.js\n');
|
||||
}
|
||||
ref = opts["arguments"];
|
||||
ref = opts.arguments;
|
||||
results = [];
|
||||
for (i = 0, len = ref.length; i < len; i++) {
|
||||
source = ref[i];
|
||||
|
@ -212,7 +212,7 @@
|
|||
} else if (o.run) {
|
||||
CoffeeScript.register();
|
||||
if (opts.prelude) {
|
||||
CoffeeScript["eval"](opts.prelude, t.options);
|
||||
CoffeeScript.eval(opts.prelude, t.options);
|
||||
}
|
||||
return CoffeeScript.run(t.input, t.options);
|
||||
} else if (o.join && t.file !== o.join) {
|
||||
|
@ -536,7 +536,7 @@
|
|||
o = opts = optionParser.parse(process.argv.slice(2));
|
||||
o.compile || (o.compile = !!o.output);
|
||||
o.run = !(o.compile || o.print || o.map);
|
||||
return o.print = !!(o.print || (o["eval"] || o.stdio && o.compile));
|
||||
return o.print = !!(o.print || (o.eval || o.stdio && o.compile));
|
||||
};
|
||||
|
||||
compileOptions = function(filename, base) {
|
||||
|
|
|
@ -926,7 +926,7 @@
|
|||
|
||||
looksStatic(className) {
|
||||
var ref1;
|
||||
return (this["this"] || this.base instanceof ThisLiteral || this.base.value === className) && this.properties.length === 1 && ((ref1 = this.properties[0].name) != null ? ref1.value : void 0) !== 'prototype';
|
||||
return (this.this || this.base instanceof ThisLiteral || this.base.value === className) && this.properties.length === 1 && ((ref1 = this.properties[0].name) != null ? ref1.value : void 0) !== 'prototype';
|
||||
}
|
||||
|
||||
unwrap() {
|
||||
|
@ -1289,15 +1289,11 @@
|
|||
}
|
||||
|
||||
compileToFragments(o) {
|
||||
var name, node, ref1;
|
||||
var name, node;
|
||||
name = this.name.compileToFragments(o);
|
||||
node = this.name.unwrap();
|
||||
if (node instanceof PropertyName) {
|
||||
if (ref1 = node.value, indexOf.call(JS_FORBIDDEN, ref1) >= 0) {
|
||||
return [this.makeCode('["'), ...name, this.makeCode('"]')];
|
||||
} else {
|
||||
return [this.makeCode('.'), ...name];
|
||||
}
|
||||
return [this.makeCode('.'), ...name];
|
||||
} else {
|
||||
return [this.makeCode('['), ...name, this.makeCode(']')];
|
||||
}
|
||||
|
@ -1529,7 +1525,7 @@
|
|||
indent = isCompact || prop instanceof Comment ? '' : idt;
|
||||
key = prop instanceof Assign && prop.context === 'object' ? prop.variable : prop instanceof Assign ? (!this.lhs ? prop.operatorToken.error(`unexpected ${prop.operatorToken.value}`) : void 0, prop.variable) : !(prop instanceof Comment) ? prop : void 0;
|
||||
if (key instanceof Value && key.hasProperties()) {
|
||||
if (prop.context === 'object' || !key["this"]) {
|
||||
if (prop.context === 'object' || !key.this) {
|
||||
key.error('invalid object key');
|
||||
}
|
||||
key = key.properties[0].name;
|
||||
|
@ -1965,7 +1961,7 @@
|
|||
class ExecutableClassBody extends Base {
|
||||
constructor(_class, body1 = new Block) {
|
||||
super();
|
||||
this["class"] = _class;
|
||||
this.class = _class;
|
||||
this.body = body1;
|
||||
}
|
||||
|
||||
|
@ -1977,7 +1973,7 @@
|
|||
if (argumentsNode = this.body.contains(isLiteralArguments)) {
|
||||
argumentsNode.error("Class bodies shouldn't reference arguments");
|
||||
}
|
||||
this.name = (ref1 = this["class"].name) != null ? ref1 : this.defaultClassVariableName;
|
||||
this.name = (ref1 = this.class.name) != null ? ref1 : this.defaultClassVariableName;
|
||||
directives = this.walkBody();
|
||||
this.setContext();
|
||||
ident = new IdentifierLiteral(this.name);
|
||||
|
@ -1987,23 +1983,23 @@
|
|||
klass = new Parens(new Call(wrapper, args));
|
||||
this.body.spaced = true;
|
||||
o.classScope = wrapper.makeScope(o.scope);
|
||||
if (this["class"].hasNameClash) {
|
||||
if (this.class.hasNameClash) {
|
||||
parent = new IdentifierLiteral(o.classScope.freeVariable('superClass'));
|
||||
wrapper.params.push(new Param(parent));
|
||||
args.push(this["class"].parent);
|
||||
this["class"].parent = parent;
|
||||
args.push(this.class.parent);
|
||||
this.class.parent = parent;
|
||||
}
|
||||
if (this.externalCtor) {
|
||||
externalCtor = new IdentifierLiteral(o.classScope.freeVariable('ctor', {
|
||||
reserve: false
|
||||
}));
|
||||
this["class"].externalCtor = externalCtor;
|
||||
this.class.externalCtor = externalCtor;
|
||||
this.externalCtor.variable.base = externalCtor;
|
||||
}
|
||||
if (this.name !== this["class"].name) {
|
||||
this.body.expressions.unshift(new Assign(new IdentifierLiteral(this.name), this["class"]));
|
||||
if (this.name !== this.class.name) {
|
||||
this.body.expressions.unshift(new Assign(new IdentifierLiteral(this.name), this.class));
|
||||
} else {
|
||||
this.body.expressions.unshift(this["class"]);
|
||||
this.body.expressions.unshift(this.class);
|
||||
}
|
||||
this.body.expressions.unshift(...directives);
|
||||
this.body.push(ident);
|
||||
|
@ -2076,7 +2072,7 @@
|
|||
base.error('constructors must be defined at the top level of a class body');
|
||||
}
|
||||
assign = this.externalCtor = new Assign(new Value, value);
|
||||
} else if (!assign.variable["this"]) {
|
||||
} else if (!assign.variable.this) {
|
||||
name = new (base.shouldCache() ? Index : Access)(base);
|
||||
prototype = new Access(new PropertyName('prototype'));
|
||||
variable = new Value(new ThisLiteral(), [prototype, name]);
|
||||
|
@ -2360,7 +2356,7 @@
|
|||
}
|
||||
|
||||
compileNode(o) {
|
||||
var answer, compiledName, isValue, j, name, properties, prototype, ref1, ref2, ref3, ref4, ref5, ref6, val, varBase;
|
||||
var answer, compiledName, isValue, j, name, properties, prototype, ref1, ref2, ref3, ref4, ref5, val, varBase;
|
||||
isValue = this.variable instanceof Value;
|
||||
if (isValue) {
|
||||
this.variable.param = this.param;
|
||||
|
@ -2418,9 +2414,6 @@
|
|||
if (this.variable.shouldCache()) {
|
||||
compiledName.unshift(this.makeCode('['));
|
||||
compiledName.push(this.makeCode(']'));
|
||||
} else if (ref6 = fragmentsToText(compiledName), indexOf.call(JS_FORBIDDEN, ref6) >= 0) {
|
||||
compiledName.unshift(this.makeCode('"'));
|
||||
compiledName.push(this.makeCode('"'));
|
||||
}
|
||||
return compiledName.concat(this.makeCode(": "), val);
|
||||
}
|
||||
|
@ -2469,7 +2462,7 @@
|
|||
defaultValue = obj.value;
|
||||
obj = obj.variable;
|
||||
}
|
||||
idx = isObject ? obj["this"] ? obj.properties[0].name : new PropertyName(obj.unwrap().value) : new NumberLiteral(0);
|
||||
idx = isObject ? obj.this ? obj.properties[0].name : new PropertyName(obj.unwrap().value) : new NumberLiteral(0);
|
||||
}
|
||||
acc = idx.unwrap() instanceof PropertyName;
|
||||
value = new Value(value);
|
||||
|
@ -2550,7 +2543,7 @@
|
|||
defaultValue = obj.value;
|
||||
obj = obj.variable;
|
||||
}
|
||||
idx = isObject ? obj["this"] ? obj.properties[0].name : new PropertyName(obj.unwrap().value) : new Literal(expandedIdx || idx);
|
||||
idx = isObject ? obj.this ? obj.properties[0].name : new PropertyName(obj.unwrap().value) : new Literal(expandedIdx || idx);
|
||||
}
|
||||
name = obj.unwrap().value;
|
||||
acc = idx.unwrap() instanceof PropertyName;
|
||||
|
@ -2725,7 +2718,7 @@
|
|||
node.error(`multiple parameters named '${name}'`);
|
||||
}
|
||||
paramNames.push(name);
|
||||
if (node["this"]) {
|
||||
if (node.this) {
|
||||
name = node.properties[0].name.value;
|
||||
if (indexOf.call(JS_FORBIDDEN, name) >= 0) {
|
||||
name = `_${name}`;
|
||||
|
@ -3006,7 +2999,7 @@
|
|||
return this.reference;
|
||||
}
|
||||
node = this.name;
|
||||
if (node["this"]) {
|
||||
if (node.this) {
|
||||
name = node.properties[0].name.value;
|
||||
if (indexOf.call(JS_FORBIDDEN, name) >= 0) {
|
||||
name = `_${name}`;
|
||||
|
@ -3052,7 +3045,7 @@
|
|||
} else if (obj instanceof Value) {
|
||||
if (obj.isArray() || obj.isObject()) {
|
||||
this.eachName(iterator, obj.base);
|
||||
} else if (obj["this"]) {
|
||||
} else if (obj.this) {
|
||||
atParam(obj);
|
||||
} else {
|
||||
iterator(obj.base.value, obj.base, this);
|
||||
|
@ -3072,7 +3065,7 @@
|
|||
var key;
|
||||
if (parent instanceof Obj) {
|
||||
key = node;
|
||||
if (node["this"]) {
|
||||
if (node.this) {
|
||||
key = node.properties[0].name;
|
||||
}
|
||||
return new Assign(new Value(key), newNode, 'object');
|
||||
|
@ -3235,10 +3228,10 @@
|
|||
return Op.prototype.generateDo(first);
|
||||
}
|
||||
if (op === 'new') {
|
||||
if (first instanceof Call && !first["do"] && !first.isNew) {
|
||||
if (first instanceof Call && !first.do && !first.isNew) {
|
||||
return first.newInstance();
|
||||
}
|
||||
if (first instanceof Code && first.bound || first["do"]) {
|
||||
if (first instanceof Code && first.bound || first.do) {
|
||||
first = new Parens(first);
|
||||
}
|
||||
}
|
||||
|
@ -3331,7 +3324,7 @@
|
|||
}
|
||||
}
|
||||
call = new Call(exp, passedParams);
|
||||
call["do"] = true;
|
||||
call.do = true;
|
||||
return call;
|
||||
}
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
parse(args) {
|
||||
var arg, i, isOption, j, k, len, len1, matchedRule, options, originalArgs, pos, ref, rule, seenNonOptionArg, skippingArgument, value;
|
||||
options = {
|
||||
"arguments": []
|
||||
arguments: []
|
||||
};
|
||||
skippingArgument = false;
|
||||
originalArgs = args;
|
||||
|
@ -26,11 +26,11 @@
|
|||
}
|
||||
if (arg === '--') {
|
||||
pos = originalArgs.indexOf('--');
|
||||
options["arguments"] = options["arguments"].concat(originalArgs.slice(pos + 1));
|
||||
options.arguments = options.arguments.concat(originalArgs.slice(pos + 1));
|
||||
break;
|
||||
}
|
||||
isOption = !!(arg.match(LONG_FLAG) || arg.match(SHORT_FLAG));
|
||||
seenNonOptionArg = options["arguments"].length > 0;
|
||||
seenNonOptionArg = options.arguments.length > 0;
|
||||
if (!seenNonOptionArg) {
|
||||
matchedRule = false;
|
||||
ref = this.rules;
|
||||
|
@ -52,7 +52,7 @@
|
|||
}
|
||||
}
|
||||
if (seenNonOptionArg || !isOption) {
|
||||
options["arguments"].push(arg);
|
||||
options.arguments.push(arg);
|
||||
}
|
||||
}
|
||||
return options;
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
}
|
||||
})(),
|
||||
historyMaxInputSize: 10240,
|
||||
"eval": function(input, context, filename, cb) {
|
||||
eval: function(input, context, filename, cb) {
|
||||
var Assign, Block, Literal, Value, ast, err, js, referencedVars, token, tokens;
|
||||
input = input.replace(/\uFF00/g, '\n');
|
||||
input = input.replace(/^\(([\s\S]*)\n\)$/m, '$1');
|
||||
|
|
|
@ -951,10 +951,7 @@ exports.Access = class Access extends Base
|
|||
name = @name.compileToFragments o
|
||||
node = @name.unwrap()
|
||||
if node instanceof PropertyName
|
||||
if node.value in JS_FORBIDDEN
|
||||
[@makeCode('["'), name..., @makeCode('"]')]
|
||||
else
|
||||
[@makeCode('.'), name...]
|
||||
[@makeCode('.'), name...]
|
||||
else
|
||||
[@makeCode('['), name..., @makeCode(']')]
|
||||
|
||||
|
@ -1781,9 +1778,6 @@ exports.Assign = class Assign extends Base
|
|||
if @variable.shouldCache()
|
||||
compiledName.unshift @makeCode '['
|
||||
compiledName.push @makeCode ']'
|
||||
else if fragmentsToText(compiledName) in JS_FORBIDDEN
|
||||
compiledName.unshift @makeCode '"'
|
||||
compiledName.push @makeCode '"'
|
||||
return compiledName.concat @makeCode(": "), val
|
||||
|
||||
answer = compiledName.concat @makeCode(" #{ @context or '=' } "), val
|
||||
|
|
|
@ -770,7 +770,7 @@ test "#4451: `default` in an export statement is only treated as a keyword when
|
|||
input = "export default { default: 1 }"
|
||||
output = """
|
||||
export default {
|
||||
"default": 1
|
||||
default: 1
|
||||
};
|
||||
"""
|
||||
eq toJS(input), output
|
||||
|
|
Loading…
Add table
Reference in a new issue