Skip to content

Commit

Permalink
Fix shorthand properties named get or set with defaults in object…
Browse files Browse the repository at this point in the history
… destructuring

    const {get = …} = {}
  • Loading branch information
charmander committed Oct 14, 2017
1 parent bf94140 commit e70228b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/expression.js
Expand Up @@ -583,10 +583,11 @@ pp.parsePropertyValue = function(prop, isPattern, isGenerator, isAsync, startPos
prop.kind = "init"
prop.method = true
prop.value = this.parseMethod(isGenerator, isAsync)
} else if (this.options.ecmaVersion >= 5 && !prop.computed && prop.key.type === "Identifier" &&
} else if (!isPattern &&
this.options.ecmaVersion >= 5 && !prop.computed && prop.key.type === "Identifier" &&
(prop.key.name === "get" || prop.key.name === "set") &&
(this.type != tt.comma && this.type != tt.braceR)) {
if (isGenerator || isAsync || isPattern) this.unexpected()
if (isGenerator || isAsync) this.unexpected()
prop.kind = prop.key.name
this.parsePropertyName(prop)
prop.value = this.parseMethod(false)
Expand Down
2 changes: 1 addition & 1 deletion src/loose/expression.js
Expand Up @@ -391,7 +391,7 @@ lp.parseObj = function() {
prop.value = this.parseMethod(isGenerator, isAsync)
} else if (this.options.ecmaVersion >= 5 && prop.key.type === "Identifier" &&
!prop.computed && (prop.key.name === "get" || prop.key.name === "set") &&
(this.tok.type != tt.comma && this.tok.type != tt.braceR)) {
(this.tok.type != tt.comma && this.tok.type != tt.braceR && this.tok.type != tt.eq)) {
prop.kind = prop.key.name
this.parsePropertyName(prop)
prop.value = this.parseMethod(false)
Expand Down
54 changes: 54 additions & 0 deletions test/tests-harmony.js
Expand Up @@ -13402,6 +13402,60 @@ test("var {propName = defaultValue} = obj", {
locations: true
});

test("var {get = defaultValue} = obj", {
type: "Program",
range: [0, 30],
body: [{
type: "VariableDeclaration",
range: [0, 30],
declarations: [{
type: "VariableDeclarator",
range: [4, 30],
id: {
type: "ObjectPattern",
range: [4, 24],
properties: [{
type: "Property",
range: [5, 23],
method: false,
shorthand: true,
computed: false,
key: {
type: "Identifier",
range: [5, 8],
name: "get"
},
kind: "init",
value: {
type: "AssignmentPattern",
range: [5, 23],
left: {
type: "Identifier",
range: [5, 8],
name: "get"
},
right: {
type: "Identifier",
range: [11, 23],
name: "defaultValue"
}
}
}]
},
init: {
type: "Identifier",
range: [27, 30],
name: "obj"
}
}],
kind: "var"
}]
}, {
ecmaVersion: 6,
ranges: true,
locations: true
});

test("var [localVar = defaultValue] = obj", {
type: "Program",
range: [0, 35],
Expand Down

0 comments on commit e70228b

Please sign in to comment.