Fixes #1055 -- disallow invalid keys in object literals ... but allow them where the implicit object is being used as a class defn' or a destructuring

This commit is contained in:
Jeremy Ashkenas 2013-03-05 21:10:56 +13:00
parent fbe07f1fce
commit 355754ed20
3 changed files with 16 additions and 8 deletions

View File

@ -1231,7 +1231,7 @@
for (_i = 0, _len = props.length; _i < _len; _i++) {
node = props[_i];
if (node instanceof Value) {
throw new Error('cannot have an implicit value in an implicit object');
throw new SyntaxError('cannot have an implicit value in an implicit object');
}
}
}
@ -1242,6 +1242,9 @@
prop = props[i];
join = i === props.length - 1 ? '' : prop === lastNoncom || prop instanceof Comment ? '\n' : ',\n';
indent = prop instanceof Comment ? '' : idt;
if (prop instanceof Assign && prop.variable instanceof Value && prop.variable.hasProperties()) {
throw new SyntaxError('Invalid object key');
}
if (prop instanceof Value && prop["this"]) {
prop = new Assign(prop.properties[0].name, prop, 'object');
}
@ -1419,10 +1422,10 @@
func = assign.value;
if (base.value === 'constructor') {
if (this.ctor) {
throw new Error('cannot define more than one constructor in a class');
throw new SyntaxError('cannot define more than one constructor in a class');
}
if (func.bound) {
throw new Error('cannot define a constructor as a bound function');
throw new SyntaxError('cannot define a constructor as a bound function');
}
if (func instanceof Code) {
assign = this.ctor = func;
@ -1729,7 +1732,7 @@
var left, right, _ref4;
_ref4 = this.variable.cacheReference(o), left = _ref4[0], right = _ref4[1];
if (!left.properties.length && left.base instanceof Literal && left.base.value !== "this" && !o.scope.check(left.base.value)) {
throw new Error("the variable \"" + left.base.value + "\" can't be assigned with " + this.context + " because it has not been defined.");
throw new SyntaxError("the variable \"" + left.base.value + "\" can't be assigned with " + this.context + " because it has not been defined.");
}
if (__indexOf.call(this.context, "?") >= 0) {
o.isExistentialEquals = true;

View File

@ -882,7 +882,7 @@ exports.Obj = class Obj extends Base
return [@makeCode(if @front then '({})' else '{}')] unless props.length
if @generated
for node in props when node instanceof Value
throw new Error 'cannot have an implicit value in an implicit object'
throw new SyntaxError 'cannot have an implicit value in an implicit object'
idt = o.indent += TAB
lastNoncom = @lastNonComment @properties
answer = []
@ -894,6 +894,8 @@ exports.Obj = class Obj extends Base
else
',\n'
indent = if prop instanceof Comment then '' else idt
if prop instanceof Assign and prop.variable instanceof Value and prop.variable.hasProperties()
throw new SyntaxError 'Invalid object key'
if prop instanceof Value and prop.this
prop = new Assign prop.properties[0].name, prop, 'object'
if prop not instanceof Comment
@ -1002,9 +1004,9 @@ exports.Class = class Class extends Base
func = assign.value
if base.value is 'constructor'
if @ctor
throw new Error 'cannot define more than one constructor in a class'
throw new SyntaxError 'cannot define more than one constructor in a class'
if func.bound
throw new Error 'cannot define a constructor as a bound function'
throw new SyntaxError 'cannot define a constructor as a bound function'
if func instanceof Code
assign = @ctor = func
else
@ -1237,7 +1239,7 @@ exports.Assign = class Assign extends Base
# Disallow conditional assignment of undefined variables.
if not left.properties.length and left.base instanceof Literal and
left.base.value != "this" and not o.scope.check left.base.value
throw new Error "the variable \"#{left.base.value}\" can't be assigned with #{@context} because it has not been defined."
throw new SyntaxError "the variable \"#{left.base.value}\" can't be assigned with #{@context} because it has not been defined."
if "?" in @context then o.isExistentialEquals = true
new Op(@context[...-1], left, new Assign(right, @value, '=') ).compileToFragments o

View File

@ -70,3 +70,6 @@ test "#1106: __proto__ compilation", ->
test "reference named hasOwnProperty", ->
CoffeeScript.compile 'hasOwnProperty = 0; a = 1'
test "#1055", ->
cantCompile "@key: value"