-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Shorthand property #1127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shorthand property #1127
Changes from all commits
150e8d3
8a779e1
e9122b4
7e39622
1bf7eca
680999f
bb7a0aa
1888f73
a8ebdf0
8bd7aae
03e0722
880e958
b3078f2
1b66ee6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -202,6 +202,7 @@ module ts { | |
child((<ParameterDeclaration>node).initializer); | ||
case SyntaxKind.Property: | ||
case SyntaxKind.PropertyAssignment: | ||
case SyntaxKind.ShorthandPropertyAssignment: | ||
return child((<PropertyDeclaration>node).name) || | ||
child((<PropertyDeclaration>node).type) || | ||
child((<PropertyDeclaration>node).initializer); | ||
|
@@ -580,6 +581,7 @@ module ts { | |
case SyntaxKind.VariableDeclaration: | ||
case SyntaxKind.Property: | ||
case SyntaxKind.PropertyAssignment: | ||
case SyntaxKind.ShorthandPropertyAssignment: | ||
case SyntaxKind.EnumMember: | ||
case SyntaxKind.Method: | ||
case SyntaxKind.FunctionDeclaration: | ||
|
@@ -2729,22 +2731,41 @@ module ts { | |
return finishNode(node); | ||
} | ||
|
||
function parsePropertyAssignment(): PropertyDeclaration { | ||
var node = <PropertyDeclaration>createNode(SyntaxKind.PropertyAssignment); | ||
node.name = parsePropertyName(); | ||
function parsePropertyAssignment(): Declaration { | ||
var nodePos = scanner.getStartPos(); | ||
var nameToken = token; | ||
var propertyName = parsePropertyName(); | ||
var node: Declaration; | ||
if (token === SyntaxKind.OpenParenToken || token === SyntaxKind.LessThanToken) { | ||
node = <PropertyDeclaration>createNode(SyntaxKind.PropertyAssignment, nodePos); | ||
node.name = propertyName; | ||
var sig = parseSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false); | ||
var body = parseBody(/* ignoreMissingOpenBrace */ false); | ||
// do not propagate property name as name for function expression | ||
// for scenarios like | ||
// var x = 1; | ||
// var y = { x() { } } | ||
// otherwise this will bring y.x into the scope of x which is incorrect | ||
node.initializer = makeFunctionExpression(SyntaxKind.FunctionExpression, node.pos, undefined, sig, body); | ||
(<PropertyDeclaration>node).initializer = makeFunctionExpression(SyntaxKind.FunctionExpression, node.pos, undefined, sig, body); | ||
return finishNode(node); | ||
} | ||
// Disallow optional property assignment | ||
if (token === SyntaxKind.QuestionToken) { | ||
var questionStart = scanner.getTokenPos(); | ||
grammarErrorAtPos(questionStart, scanner.getStartPos() - questionStart, Diagnostics.A_object_member_cannot_be_declared_optional); | ||
nextToken(); | ||
} | ||
|
||
// Parse to check if it is short-hand property assignment or normal property assignment | ||
if (token !== SyntaxKind.ColonToken && nameToken === SyntaxKind.Identifier) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks correct. just make sure you have tests for things like: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will add that test |
||
node = <ShortHandPropertyDeclaration>createNode(SyntaxKind.ShorthandPropertyAssignment, nodePos); | ||
node.name = propertyName; | ||
} | ||
else { | ||
node = <PropertyDeclaration>createNode(SyntaxKind.PropertyAssignment, nodePos); | ||
node.name = propertyName; | ||
parseExpected(SyntaxKind.ColonToken); | ||
node.initializer = parseAssignmentExpression(false); | ||
(<PropertyDeclaration>node).initializer = parseAssignmentExpression(false); | ||
} | ||
return finishNode(node); | ||
} | ||
|
@@ -2794,6 +2815,9 @@ module ts { | |
if (p.kind === SyntaxKind.PropertyAssignment) { | ||
currentKind = Property; | ||
} | ||
else if (p.kind === SyntaxKind.ShorthandPropertyAssignment) { | ||
currentKind = Property; | ||
} | ||
else if (p.kind === SyntaxKind.GetAccessor) { | ||
currentKind = GetAccessor; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert here what type of member decl you think this is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1