Fix locations for AssignmentPatterns.

This commit is contained in:
Ingvar Stepanyan 2015-01-27 15:10:25 +02:00
parent 5d96bbd781
commit 3d5964ceed
2 changed files with 68 additions and 10 deletions

View File

@ -1591,9 +1591,10 @@
// Parses assignment pattern around given atom if possible. // Parses assignment pattern around given atom if possible.
function parseMaybeDefault(startPos, left) { function parseMaybeDefault(startPos, left) {
startPos = startPos || storeCurrentPos();
left = left || parseAssignableAtom(); left = left || parseAssignableAtom();
if (!eat(_eq)) return left; if (!eat(_eq)) return left;
var node = startPos ? startNodeAt(startPos) : startNode(); var node = startNodeAt(startPos);
node.operator = "="; node.operator = "=";
node.left = left; node.left = left;
node.right = parseMaybeAssign(); node.right = parseMaybeAssign();
@ -2437,15 +2438,16 @@
if (options.ecmaVersion >= 6) { if (options.ecmaVersion >= 6) {
prop.method = false; prop.method = false;
prop.shorthand = false; prop.shorthand = false;
if (isPattern) { if (isPattern || refShorthandDefaultPos) {
start = storeCurrentPos(); start = storeCurrentPos();
} else { }
if (!isPattern) {
isGenerator = eat(_star); isGenerator = eat(_star);
} }
} }
parsePropertyName(prop); parsePropertyName(prop);
if (eat(_colon)) { if (eat(_colon)) {
prop.value = isPattern ? parseMaybeDefault(start) : parseMaybeAssign(false, refShorthandDefaultPos); prop.value = isPattern ? parseMaybeDefault() : parseMaybeAssign(false, refShorthandDefaultPos);
prop.kind = "init"; prop.kind = "init";
} else if (options.ecmaVersion >= 6 && tokType === _parenL) { } else if (options.ecmaVersion >= 6 && tokType === _parenL) {
if (isPattern) unexpected(); if (isPattern) unexpected();

View File

@ -1806,6 +1806,10 @@ test("(x=1) => x * x", {
start: {line: 1, column: 3}, start: {line: 1, column: 3},
end: {line: 1, column: 4} end: {line: 1, column: 4}
} }
},
loc: {
start: {line: 1, column: 1},
end: {line: 1, column: 4}
} }
}], }],
body: { body: {
@ -2073,6 +2077,10 @@ test("(eval = 10) => 42", {
start: {line: 1, column: 8}, start: {line: 1, column: 8},
end: {line: 1, column: 10} end: {line: 1, column: 10}
} }
},
loc: {
start: {line: 1, column: 1},
end: {line: 1, column: 10}
} }
}], }],
body: { body: {
@ -2140,6 +2148,10 @@ test("(eval, a = 10) => 42", {
start: {line: 1, column: 11}, start: {line: 1, column: 11},
end: {line: 1, column: 13} end: {line: 1, column: 13}
} }
},
loc: {
start: {line: 1, column: 7},
end: {line: 1, column: 13}
} }
} }
], ],
@ -9589,6 +9601,10 @@ test("function f([x] = [1]) {}", {
start: {line: 1, column: 17}, start: {line: 1, column: 17},
end: {line: 1, column: 20} end: {line: 1, column: 20}
} }
},
loc: {
start: {line: 1, column: 11},
end: {line: 1, column: 20}
} }
}], }],
body: { body: {
@ -9698,6 +9714,10 @@ test("function f({x} = {x: 10}) {}", {
start: {line: 1, column: 17}, start: {line: 1, column: 17},
end: {line: 1, column: 24} end: {line: 1, column: 24}
} }
},
loc: {
start: {line: 1, column: 11},
end: {line: 1, column: 24}
} }
}], }],
body: { body: {
@ -9813,6 +9833,10 @@ test("f = function({x} = {x: 10}) {}", {
start: {line: 1, column: 19}, start: {line: 1, column: 19},
end: {line: 1, column: 26} end: {line: 1, column: 26}
} }
},
loc: {
start: {line: 1, column: 13},
end: {line: 1, column: 26}
} }
}], }],
body: { body: {
@ -9939,6 +9963,10 @@ test("({f: function({x} = {x: 10}) {}})", {
start: {line: 1, column: 20}, start: {line: 1, column: 20},
end: {line: 1, column: 27} end: {line: 1, column: 27}
} }
},
loc: {
start: {line: 1, column: 14},
end: {line: 1, column: 27}
} }
}], }],
body: { body: {
@ -10074,6 +10102,10 @@ test("({f({x} = {x: 10}) {}})", {
start: {line: 1, column: 10}, start: {line: 1, column: 10},
end: {line: 1, column: 17} end: {line: 1, column: 17}
} }
},
loc: {
start: {line: 1, column: 4},
end: {line: 1, column: 17}
} }
}], }],
body: { body: {
@ -10213,6 +10245,10 @@ test("(class {f({x} = {x: 10}) {}})", {
start: {line: 1, column: 16}, start: {line: 1, column: 16},
end: {line: 1, column: 23} end: {line: 1, column: 23}
} }
},
loc: {
start: {line: 1, column: 10},
end: {line: 1, column: 23}
} }
}], }],
body: { body: {
@ -10339,6 +10375,10 @@ test("(({x} = {x: 10}) => {})", {
start: {line: 1, column: 8}, start: {line: 1, column: 8},
end: {line: 1, column: 15} end: {line: 1, column: 15}
} }
},
loc: {
start: {line: 1, column: 2},
end: {line: 1, column: 15}
} }
}], }],
body: { body: {
@ -10407,6 +10447,10 @@ test("x = function(y = 1) {}", {
start: {line: 1, column: 17}, start: {line: 1, column: 17},
end: {line: 1, column: 18} end: {line: 1, column: 18}
} }
},
loc: {
start: {line: 1, column: 13},
end: {line: 1, column: 18}
} }
}], }],
body: { body: {
@ -10474,6 +10518,10 @@ test("function f(a = 1) {}", {
start: {line: 1, column: 15}, start: {line: 1, column: 15},
end: {line: 1, column: 16} end: {line: 1, column: 16}
} }
},
loc: {
start: {line: 1, column: 11},
end: {line: 1, column: 16}
} }
}], }],
body: { body: {
@ -10549,6 +10597,10 @@ test("x = { f: function(a=1) {} }", {
start: {line: 1, column: 20}, start: {line: 1, column: 20},
end: {line: 1, column: 21} end: {line: 1, column: 21}
} }
},
loc: {
start: {line: 1, column: 18},
end: {line: 1, column: 21}
} }
}], }],
body: { body: {
@ -10648,6 +10700,10 @@ test("x = { f(a=1) {} }", {
start: {line: 1, column: 10}, start: {line: 1, column: 10},
end: {line: 1, column: 11} end: {line: 1, column: 11}
} }
},
loc: {
start: {line: 1, column: 8},
end: {line: 1, column: 11}
} }
}], }],
body: { body: {
@ -14379,7 +14435,7 @@ test("var {propName: localVar = defaultValue} = obj", {
}, },
value: { value: {
type: "AssignmentPattern", type: "AssignmentPattern",
range: [5, 38], range: [15, 38],
operator: "=", operator: "=",
left: { left: {
type: "Identifier", type: "Identifier",
@ -14480,7 +14536,7 @@ test("var [localVar = defaultValue] = obj", {
range: [4, 29], range: [4, 29],
elements: [{ elements: [{
type: "AssignmentPattern", type: "AssignmentPattern",
range: [16, 28], range: [5, 28],
operator: "=", operator: "=",
left: { left: {
type: "Identifier", type: "Identifier",
@ -14536,7 +14592,7 @@ test("({x = 0} = obj)", {
kind: "init", kind: "init",
value: { value: {
type: "AssignmentPattern", type: "AssignmentPattern",
range: [6, 7], range: [2, 7],
operator: "=", operator: "=",
left: { left: {
type: "Identifier", type: "Identifier",
@ -14593,7 +14649,7 @@ test("({x = 0}) => x", {
kind: "init", kind: "init",
value: { value: {
type: "AssignmentPattern", type: "AssignmentPattern",
range: [6, 7], range: [2, 7],
operator: "=", operator: "=",
left: { left: {
type: "Identifier", type: "Identifier",
@ -14671,7 +14727,7 @@ test("[a, {b: {c = 1}}] = arr", {
kind: "init", kind: "init",
value: { value: {
type: "AssignmentPattern", type: "AssignmentPattern",
range: [13, 14], range: [9, 14],
operator: "=", operator: "=",
left: { left: {
type: "Identifier", type: "Identifier",
@ -14727,7 +14783,7 @@ test("for ({x = 0} in arr);", {
kind: "init", kind: "init",
value: { value: {
type: "AssignmentPattern", type: "AssignmentPattern",
range: [10, 11], range: [6, 11],
operator: "=", operator: "=",
left: { left: {
type: "Identifier", type: "Identifier",