Skip to content
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

Allow dynamic names in types #15473

Merged
merged 65 commits into from
Nov 16, 2017
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
d8cb3c6
Allow some dynamic names in types
rbuckton Apr 29, 2017
0c1eef7
Add support for declaration emit
rbuckton Apr 29, 2017
d572a54
Report errors from duplicate member names
rbuckton May 1, 2017
2714295
Add declaration file output to tests
rbuckton May 2, 2017
3181a8d
Merge branch 'master' into dynamicNames
rbuckton May 2, 2017
b5f1169
Accept baselines
rbuckton May 2, 2017
3b684d4
PR feedback
rbuckton May 2, 2017
64fd857
fix symbol display for computed properties
rbuckton May 3, 2017
d8ae9c0
Early support for unique symbol type
rbuckton May 4, 2017
83b5a75
Add freshness to unique symbol types
rbuckton May 4, 2017
57674dd
Emit dynamic names for object literal types
rbuckton May 5, 2017
fc61863
Merge branch 'dynamicNames' into symbolLiterals
rbuckton May 5, 2017
fe414a2
Ensure we get the correct symbol for nodes, clean up
rbuckton May 6, 2017
93ea56b
Improve union type reduction for symbol()
rbuckton May 6, 2017
2a73d08
Merge branch 'master' into dynamicNames
rbuckton May 10, 2017
625b37e
Merge branch 'master' into dynamicNames
rbuckton May 31, 2017
8c3b73f
Merge branch 'master' into dynamicNames
rbuckton May 31, 2017
c4e9ce5
Merge branch 'master' into dynamicNames
rbuckton Jun 7, 2017
5854e87
comment typo
rbuckton Jun 9, 2017
3f83b55
Added comments for fresh/regular unique symbol types
rbuckton Jun 9, 2017
38ee475
Fix escaping and follow symbols for element access
rbuckton Jun 9, 2017
022e81b
Merge branch 'master' into dynamicNames
rbuckton Jun 9, 2017
d7ef995
Merge branch 'master' into dynamicNames
rbuckton Sep 21, 2017
e81c83c
Merge branch 'master' into dynamicNames
rbuckton Sep 22, 2017
891e71d
Remove freshness, more comprehensive grammar checks and diagnostic me…
rbuckton Sep 22, 2017
7eedf2e
Update baselines
rbuckton Oct 1, 2017
fb3168d
Merge branch 'master' into dynamicNames
rbuckton Oct 2, 2017
1b45a05
Update baselines
rbuckton Oct 2, 2017
6f05e43
Merge branch 'master' into dynamicNames
rbuckton Oct 3, 2017
7ab451b
Merge branch 'master' into dynamicNames
rbuckton Oct 3, 2017
1745e17
Merge branch 'master' into dynamicNames
rbuckton Oct 3, 2017
4395f25
PR Feedback and API baselines
rbuckton Oct 3, 2017
43c151a
Merge branch 'master' into dynamicNames
rbuckton Oct 4, 2017
ee23f93
Switch to 'unique symbol'
rbuckton Oct 4, 2017
51ded0b
Additional tests
rbuckton Oct 5, 2017
fea6a87
General tidying up and comments.
rbuckton Oct 5, 2017
36f90b6
General tidying up and comments.
rbuckton Oct 5, 2017
906a79d
Support dynamic names on static members of class.
rbuckton Oct 5, 2017
180ca23
Additional documentation
rbuckton Oct 5, 2017
7fd38c8
Merge branch 'master' into dynamicNames
rbuckton Oct 5, 2017
55e63a8
Simplify getLateBoundSymbol
rbuckton Oct 20, 2017
3341e07
Refactor widening
rbuckton Oct 21, 2017
0b31860
Revert some minor changes to clean up PR
rbuckton Oct 21, 2017
ccd98af
Simplify property symbol logic in checkObjectLiteral
rbuckton Oct 21, 2017
b5a7b03
Address PR feedback
rbuckton Oct 21, 2017
51929ac
Merge branch 'master' into dynamicNames
rbuckton Oct 21, 2017
3febc80
More repetitive but less complex widening logic for literals/symbols
rbuckton Oct 26, 2017
170e6ec
Ensure correct errors when emitting declarations
rbuckton Oct 26, 2017
44117e1
Reduce noise in PR, minor cleanup
rbuckton Oct 27, 2017
ec90dbc
Unify logic for getMembers/Exports of symbol
rbuckton Oct 27, 2017
26ca98c
Merge branch 'master' into dynamicNames
rbuckton Oct 31, 2017
208dfa6
Merge branch 'master' into dynamicNames
rbuckton Nov 4, 2017
211b2f0
Shave off ~100ms by extracting ExpandingFlags
rbuckton Nov 4, 2017
33e09f9
Merge branch 'master' into dynamicNames
rbuckton Nov 6, 2017
8b717d3
PR Feedback
rbuckton Nov 7, 2017
ee36e6a
Merge branch 'master' into dynamicNames
rbuckton Nov 7, 2017
444e282
Update baselines after merge
rbuckton Nov 7, 2017
d0fb7e4
PR Feedback
rbuckton Nov 7, 2017
b9dbf5d
Simplify literal/unique symbol widening
rbuckton Nov 10, 2017
ae11ae5
Fix getReturnTypeFromBody widening
rbuckton Nov 13, 2017
804c7d3
Merge branch 'master' into dynamicNames
rbuckton Nov 13, 2017
a21a129
Merge branch 'master' into dynamicNames
rbuckton Nov 15, 2017
86b0759
PR feedback
rbuckton Nov 16, 2017
0b24f02
Use correct base primitive type
rbuckton Nov 16, 2017
ccba128
Use correct base primitive type
rbuckton Nov 16, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
600 changes: 487 additions & 113 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

53 changes: 35 additions & 18 deletions src/compiler/declarationEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ namespace ts {
case SyntaxKind.ThisType:
case SyntaxKind.LiteralType:
return writeTextOfNode(currentText, type);
case SyntaxKind.ESSymbolType:
return write("symbol()");
case SyntaxKind.ExpressionWithTypeArguments:
return emitExpressionWithTypeArguments(<ExpressionWithTypeArguments>type);
case SyntaxKind.TypeReference:
Expand Down Expand Up @@ -460,19 +462,6 @@ namespace ts {
return emitTypePredicate(<TypePredicateNode>type);
}

function writeEntityName(entityName: EntityName | Expression) {
if (entityName.kind === SyntaxKind.Identifier) {
writeTextOfNode(currentText, entityName);
}
else {
const left = entityName.kind === SyntaxKind.QualifiedName ? (<QualifiedName>entityName).left : (<PropertyAccessExpression>entityName).expression;
const right = entityName.kind === SyntaxKind.QualifiedName ? (<QualifiedName>entityName).right : (<PropertyAccessExpression>entityName).name;
writeEntityName(left);
write(".");
writeTextOfNode(currentText, right);
}
}

function emitEntityName(entityName: EntityNameOrEntityNameExpression) {
const visibilityResult = resolver.isEntityNameVisible(entityName,
// Aliases can be written asynchronously so use correct enclosing declaration
Expand Down Expand Up @@ -592,6 +581,19 @@ namespace ts {
}
}

function writeEntityName(entityName: EntityName | Expression) {
if (entityName.kind === SyntaxKind.Identifier) {
writeTextOfNode(currentText, entityName);
}
else {
const left = entityName.kind === SyntaxKind.QualifiedName ? (<QualifiedName>entityName).left : (<PropertyAccessExpression>entityName).expression;
const right = entityName.kind === SyntaxKind.QualifiedName ? (<QualifiedName>entityName).right : (<PropertyAccessExpression>entityName).name;
writeEntityName(left);
write(".");
writeTextOfNode(currentText, right);
}
}

function emitSourceFile(node: SourceFile) {
currentText = node.text;
currentLineMap = getLineStarts(node);
Expand Down Expand Up @@ -1227,7 +1229,7 @@ namespace ts {
}

function emitPropertyDeclaration(node: Declaration) {
if (hasDynamicName(node)) {
if (hasDynamicName(node) && !resolver.isLiteralDynamicName(<ComputedPropertyName>getNameOfDeclaration(node))) {
return;
}

Expand All @@ -1246,10 +1248,17 @@ namespace ts {
emitBindingPattern(<BindingPattern>node.name);
}
else {
// If this node is a computed name, it can only be a symbol, because we've already skipped
// it if it's not a well known symbol. In that case, the text of the name will be exactly
// what we want, namely the name expression enclosed in brackets.
writeTextOfNode(currentText, node.name);
if (isDynamicName(node.name)) {
// If this node has a dynamic name, it can only be an identifier or property access because
// we've already skipped it otherwise.
emitDynamicName(<EntityNameExpression>(<ComputedPropertyName>node.name).expression);
}
else {
// If this node is a computed name, it can only be a symbol, because we've already skipped
// it if it's not a well known symbol. In that case, the text of the name will be exactly
// what we want, namely the name expression enclosed in brackets.
writeTextOfNode(currentText, node.name);
}
// If optional property emit ? but in the case of parameterProperty declaration with "?" indicating optional parameter for the constructor
// we don't want to emit property declaration with "?"
if ((node.kind === SyntaxKind.PropertyDeclaration || node.kind === SyntaxKind.PropertySignature ||
Expand Down Expand Up @@ -1314,6 +1323,14 @@ namespace ts {
} : undefined;
}

function emitDynamicName(entityName: EntityNameExpression) {
writer.getSymbolAccessibilityDiagnostic = getVariableDeclarationTypeVisibilityError;
const visibilityResult = resolver.isEntityNameVisible(entityName, enclosingDeclaration);
handleSymbolAccessibilityError(visibilityResult);
recordTypeReferenceDirectivesIfNecessary(resolver.getTypeReferenceDirectivesForEntityName(entityName));
writeTextOfNode(currentText, node.name);
}

function emitBindingPattern(bindingPattern: BindingPattern) {
// Only select non-omitted expression from the bindingPattern's elements.
// We have to do this to avoid emitting trailing commas.
Expand Down
62 changes: 57 additions & 5 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -491,23 +491,23 @@
"category": "Error",
"code": 1164
},
"A computed property name in an ambient context must directly refer to a built-in symbol.": {
"A computed property name in an ambient context must refer to an expression whose type is a literal type or a unique 'symbol()' type.": {
"category": "Error",
"code": 1165
},
"A computed property name in a class property declaration must directly refer to a built-in symbol.": {
"A computed property name in a class property declaration must refer to an expression whose type is a literal type or a unique 'symbol()' type.": {
"category": "Error",
"code": 1166
},
"A computed property name in a method overload must directly refer to a built-in symbol.": {
"A computed property name in a method overload must refer to an expression whose type is a literal type or a unique 'symbol()' type.": {
"category": "Error",
"code": 1168
},
"A computed property name in an interface must directly refer to a built-in symbol.": {
"A computed property name in an interface must refer to an expression whose type is a literal type or a unique 'symbol()' type.": {
"category": "Error",
"code": 1169
},
"A computed property name in a type literal must directly refer to a built-in symbol.": {
"A computed property name in a type literal must refer to an expression whose type is a literal type or a unique 'symbol()' type.": {
"category": "Error",
"code": 1170
},
Expand Down Expand Up @@ -907,6 +907,50 @@
"category": "Error",
"code": 1328
},
"Unique 'symbol()' types are not allowed in a union type.": {
"category": "Error",
"code": 1329
},
"Unique 'symbol()' types are not allowed in an intersection type.": {
"category": "Error",
"code": 1330
},
"Unique 'symbol()' types are not allowed in an array type.": {
"category": "Error",
"code": 1331
},
"Unique 'symbol()' types are not allowed in a tuple type.": {
"category": "Error",
"code": 1332
},
"Unique 'symbol()' types are not allowed in a mapped type.": {
"category": "Error",
"code": 1333
},
"A property of an interface or type literal whose type is a unique 'symbol()' type must be 'readonly'.": {
"category": "Error",
"code": 1334
},
"A property of a class whose type is a unique 'symbol()' type must be both 'static' and 'readonly'.": {
"category": "Error",
"code": 1335
},
"A variable whose type is a unique 'symbol()' type must be 'const'.": {
"category": "Error",
"code": 1336
},
"Unique 'symbol()' types may not be used on a variable declaration with a binding name.": {
"category": "Error",
"code": 1337
},
"Unique 'symbol()' types are only allowed on variables in a variable statement.": {
"category": "Error",
"code": 1338
},
"Unique 'symbol()' types are not allowed here.": {
"category": "Error",
"code": 1339
},

"Duplicate identifier '{0}'.": {
"category": "Error",
Expand Down Expand Up @@ -2216,6 +2260,14 @@
"category": "Error",
"code": 2714
},
"Subsequent property declarations must have the same type. Property '{0}' must be of type '{1}', but here has type '{2}'.": {
"category": "Error",
"code": 2715
},
"Duplicate declaration '{0}'.": {
"category": "Error",
"code": 2716
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,8 @@ namespace ts {
return emitMappedType(<MappedTypeNode>node);
case SyntaxKind.LiteralType:
return emitLiteralType(<LiteralTypeNode>node);
case SyntaxKind.ESSymbolType:
return write("symbol()");

// Binding patterns
case SyntaxKind.ObjectBindingPattern:
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,10 @@ namespace ts {
: node;
}

export function createESSymbolTypeNode() {
return <ESSymbolTypeNode>createSynthesizedNode(SyntaxKind.ESSymbolType);
}

// Binding Patterns

export function createObjectBindingPattern(elements: ReadonlyArray<BindingElement>) {
Expand Down
16 changes: 15 additions & 1 deletion src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2609,6 +2609,19 @@ namespace ts {
return token() === SyntaxKind.DotToken ? undefined : node;
}

function parseSymbolType(): TypeNode | undefined {
const fullStart = scanner.getStartPos();
parseExpected(SyntaxKind.SymbolKeyword);
if (token() === SyntaxKind.DotToken) return undefined;
if (parseOptional(SyntaxKind.OpenParenToken)) {
parseExpected(SyntaxKind.CloseParenToken);
return finishNode(<ESSymbolTypeNode>createNode(SyntaxKind.ESSymbolType, fullStart));
}
else {
return finishNode(<TypeNode>createNode(SyntaxKind.SymbolKeyword, fullStart));
}
}

function parseLiteralTypeNode(negative?: boolean): LiteralTypeNode {
const node = createNode(SyntaxKind.LiteralType) as LiteralTypeNode;
let unaryMinusExpression: PrefixUnaryExpression;
Expand Down Expand Up @@ -2639,12 +2652,13 @@ namespace ts {
case SyntaxKind.StringKeyword:
case SyntaxKind.NumberKeyword:
case SyntaxKind.BooleanKeyword:
case SyntaxKind.SymbolKeyword:
case SyntaxKind.UndefinedKeyword:
case SyntaxKind.NeverKeyword:
case SyntaxKind.ObjectKeyword:
// If these are followed by a dot, then parse these out as a dotted type reference instead.
return tryParse(parseKeywordAndNoDot) || parseTypeReference();
case SyntaxKind.SymbolKeyword:
return tryParse(parseSymbolType) || parseTypeReference();
case SyntaxKind.AsteriskToken:
return parseJSDocAllType();
case SyntaxKind.QuestionToken:
Expand Down
Loading