Finalized destructuring support in loose parser; fixed startNodeAt.

This commit is contained in:
Ingvar Stepanyan 2015-01-27 15:53:16 +02:00
parent 6660a21b79
commit e4a97ab877
2 changed files with 27 additions and 22 deletions

View File

@ -235,13 +235,14 @@
if (options.locations) {
node = new Node(pos[0]);
node.loc = new SourceLocation(pos[1]);
pos = pos[0];
} else {
node = new Node(pos);
}
if (options.directSourceFile)
node.sourceFile = options.directSourceFile;
if (options.ranges)
node.range = [pos[0], 0];
node.range = [pos, 0];
return node;
}
@ -365,7 +366,7 @@
}
var init = parseExpression(true);
if (token.type === tt._in || isContextual("of")) {
return parseForIn(node, checkLVal(init));
return parseForIn(node, toAssignable(init));
}
return parseFor(node, init);
@ -432,7 +433,7 @@
var clause = startNode();
next();
expect(tt.parenL);
clause.param = parseIdent();
clause.param = toAssignable(parseExprAtom());
expect(tt.parenR);
clause.guard = null;
clause.body = parseBlock();
@ -857,11 +858,12 @@
if (curIndent + 1 < indent) { indent = curIndent; line = curLineStart; }
while (!closes(tt.braceR, indent, line)) {
if (isClass && semicolon()) continue;
var prop = startNode(), isGenerator;
var prop = startNode(), isGenerator, start;
if (options.ecmaVersion >= 6) {
if (isClass) {
prop['static'] = false;
} else {
start = storeCurrentPos();
prop.method = false;
prop.shorthand = false;
}
@ -901,7 +903,19 @@
prop.value = parseMethod(isGenerator);
} else {
prop.kind = "init";
prop.value = options.ecmaVersion >= 6 ? prop.key : dummyIdent();
if (options.ecmaVersion >= 6) {
if (eat(tt.eq)) {
var assign = startNodeAt(start);
assign.operator = "=";
assign.left = prop.key;
assign.right = parseMaybeAssign();
prop.value = finishNode(assign, "AssignmentExpression");
} else {
prop.value = prop.key;
}
} else {
prop.value = dummyIdent();
}
prop.shorthand = true;
}

View File

@ -14395,8 +14395,7 @@ test("var {propName: localVar = defaultValue} = obj", {
}, {
ecmaVersion: 6,
ranges: true,
locations: true,
loose: false
locations: true
});
test("var {propName = defaultValue} = obj", {
@ -14451,8 +14450,7 @@ test("var {propName = defaultValue} = obj", {
}, {
ecmaVersion: 6,
ranges: true,
locations: true,
loose: false
locations: true
});
test("var [localVar = defaultValue] = obj", {
@ -14494,8 +14492,7 @@ test("var [localVar = defaultValue] = obj", {
}, {
ecmaVersion: 6,
ranges: true,
locations: true,
loose: false
locations: true
});
test("({x = 0} = obj)", {
@ -14549,8 +14546,7 @@ test("({x = 0} = obj)", {
}]
}, {
ecmaVersion: 6,
ranges: true,
loose: false
ranges: true
});
test("({x = 0}) => x", {
@ -14606,8 +14602,7 @@ test("({x = 0}) => x", {
}]
}, {
ecmaVersion: 6,
ranges: true,
loose: false
ranges: true
});
test("[a, {b: {c = 1}}] = arr", {
@ -14689,8 +14684,7 @@ test("[a, {b: {c = 1}}] = arr", {
}]
}, {
ecmaVersion: 6,
ranges: true,
loose: false
ranges: true
});
test("for ({x = 0} in arr);", {
@ -14743,8 +14737,7 @@ test("for ({x = 0} in arr);", {
}]
}, {
ecmaVersion: 6,
ranges: true,
loose: false
ranges: true
});
testFail("obj = {x = 0}", "Unexpected token (1:9)", {ecmaVersion: 6});
@ -14796,14 +14789,12 @@ test("try {} catch ({message}) {}", {
body: []
}
},
guardedHandlers: [],
finalizer: null
}]
}, {
ecmaVersion: 6,
ranges: true,
locations: true,
loose: false
locations: true
});
// https://github.com/marijnh/acorn/issues/192