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 all 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
2 changes: 1 addition & 1 deletion src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1680,7 +1680,7 @@ namespace ts {

function bindAnonymousDeclaration(node: Declaration, symbolFlags: SymbolFlags, name: __String) {
const symbol = createSymbol(symbolFlags, name);
if (symbolFlags & SymbolFlags.EnumMember) {
if (symbolFlags & (SymbolFlags.EnumMember | SymbolFlags.ClassMember)) {
symbol.parent = container.symbol;
}
addDeclarationToSymbol(symbol, node, symbolFlags);
Expand Down
639 changes: 525 additions & 114 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

118 changes: 107 additions & 11 deletions src/compiler/declarationEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ namespace ts {
const writer = <EmitTextWriterWithSymbolWriter>createTextWriter(newLine);
writer.trackSymbol = trackSymbol;
writer.reportInaccessibleThisError = reportInaccessibleThisError;
writer.reportInaccessibleUniqueSymbolError = reportInaccessibleUniqueSymbolError;
writer.reportPrivateInBaseOfClassExpression = reportPrivateInBaseOfClassExpression;
writer.writeKeyword = writer.write;
writer.writeOperator = writer.write;
Expand Down Expand Up @@ -322,11 +323,21 @@ namespace ts {
}
}

function reportInaccessibleUniqueSymbolError() {
if (errorNameNode) {
reportedDeclarationError = true;
emitterDiagnostics.add(createDiagnosticForNode(errorNameNode, Diagnostics.The_inferred_type_of_0_references_an_inaccessible_1_type_A_type_annotation_is_necessary,
declarationNameToString(errorNameNode),
"unique symbol"));
}
}

function reportInaccessibleThisError() {
if (errorNameNode) {
reportedDeclarationError = true;
emitterDiagnostics.add(createDiagnosticForNode(errorNameNode, Diagnostics.The_inferred_type_of_0_references_an_inaccessible_this_type_A_type_annotation_is_necessary,
declarationNameToString(errorNameNode)));
emitterDiagnostics.add(createDiagnosticForNode(errorNameNode, Diagnostics.The_inferred_type_of_0_references_an_inaccessible_1_type_A_type_annotation_is_necessary,
declarationNameToString(errorNameNode),
"this"));
}
}

Expand Down Expand Up @@ -1227,7 +1238,7 @@ namespace ts {
}

function emitPropertyDeclaration(node: Declaration) {
if (hasDynamicName(node)) {
if (hasDynamicName(node) && !resolver.isLateBound(node)) {
return;
}

Expand All @@ -1246,10 +1257,8 @@ 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);
writeNameOfDeclaration(node, getVariableDeclarationTypeVisibilityError);

// 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 @@ -1387,7 +1396,7 @@ namespace ts {
}

function emitAccessorDeclaration(node: AccessorDeclaration) {
if (hasDynamicName(node)) {
if (hasDynamicName(node) && !resolver.isLateBound(node)) {
return;
}

Expand All @@ -1398,7 +1407,7 @@ namespace ts {
emitJsDocComments(accessors.getAccessor);
emitJsDocComments(accessors.setAccessor);
emitClassMemberDeclarationFlags(getModifierFlags(node) | (accessors.setAccessor ? 0 : ModifierFlags.Readonly));
writeTextOfNode(currentText, node.name);
writeNameOfDeclaration(node, getAccessorNameVisibilityError);
if (!hasModifier(node, ModifierFlags.Private)) {
accessorWithTypeAnnotation = node;
let type = getTypeAnnotationFromAccessor(node);
Expand Down Expand Up @@ -1426,6 +1435,37 @@ namespace ts {
}
}

function getAccessorNameVisibilityError(symbolAccessibilityResult: SymbolAccessibilityResult) {
const diagnosticMessage = getAccessorNameVisibilityDiagnosticMessage(symbolAccessibilityResult);
return diagnosticMessage !== undefined ? {
diagnosticMessage,
errorNode: node,
typeName: node.name
} : undefined;
}

function getAccessorNameVisibilityDiagnosticMessage(symbolAccessibilityResult: SymbolAccessibilityResult) {
if (hasModifier(node, ModifierFlags.Static)) {
return symbolAccessibilityResult.errorModuleName ?
symbolAccessibilityResult.accessibility === SymbolAccessibility.CannotBeNamed ?
Diagnostics.Public_static_property_0_of_exported_class_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named :
Diagnostics.Public_static_property_0_of_exported_class_has_or_is_using_name_1_from_private_module_2 :
Diagnostics.Public_static_property_0_of_exported_class_has_or_is_using_private_name_1;
}
else if (node.parent.kind === SyntaxKind.ClassDeclaration) {
return symbolAccessibilityResult.errorModuleName ?
symbolAccessibilityResult.accessibility === SymbolAccessibility.CannotBeNamed ?
Diagnostics.Public_property_0_of_exported_class_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named :
Diagnostics.Public_property_0_of_exported_class_has_or_is_using_name_1_from_private_module_2 :
Diagnostics.Public_property_0_of_exported_class_has_or_is_using_private_name_1;
}
else {
return symbolAccessibilityResult.errorModuleName ?
Diagnostics.Property_0_of_exported_interface_has_or_is_using_name_1_from_private_module_2 :
Diagnostics.Property_0_of_exported_interface_has_or_is_using_private_name_1;
}
}

function getAccessorDeclarationTypeVisibilityError(symbolAccessibilityResult: SymbolAccessibilityResult): SymbolAccessibilityDiagnostic {
let diagnosticMessage: DiagnosticMessage;
if (accessorWithTypeAnnotation.kind === SyntaxKind.SetAccessor) {
Expand Down Expand Up @@ -1467,7 +1507,7 @@ namespace ts {
}

function writeFunctionDeclaration(node: FunctionLikeDeclaration) {
if (hasDynamicName(node)) {
if (hasDynamicName(node) && !resolver.isLateBound(node)) {
return;
}

Expand All @@ -1489,13 +1529,69 @@ namespace ts {
write("constructor");
}
else {
writeTextOfNode(currentText, node.name);
writeNameOfDeclaration(node, getMethodNameVisibilityError);
if (hasQuestionToken(node)) {
write("?");
}
}
emitSignatureDeclaration(node);
}

function getMethodNameVisibilityError(symbolAccessibilityResult: SymbolAccessibilityResult): SymbolAccessibilityDiagnostic {
const diagnosticMessage = getMethodNameVisibilityDiagnosticMessage(symbolAccessibilityResult);
return diagnosticMessage !== undefined ? {
diagnosticMessage,
errorNode: node,
typeName: node.name
} : undefined;
}

function getMethodNameVisibilityDiagnosticMessage(symbolAccessibilityResult: SymbolAccessibilityResult) {
if (hasModifier(node, ModifierFlags.Static)) {
return symbolAccessibilityResult.errorModuleName ?
symbolAccessibilityResult.accessibility === SymbolAccessibility.CannotBeNamed ?
Diagnostics.Public_static_method_0_of_exported_class_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named :
Diagnostics.Public_static_method_0_of_exported_class_has_or_is_using_name_1_from_private_module_2 :
Diagnostics.Public_static_method_0_of_exported_class_has_or_is_using_private_name_1;
}
else if (node.parent.kind === SyntaxKind.ClassDeclaration) {
return symbolAccessibilityResult.errorModuleName ?
symbolAccessibilityResult.accessibility === SymbolAccessibility.CannotBeNamed ?
Diagnostics.Public_method_0_of_exported_class_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named :
Diagnostics.Public_method_0_of_exported_class_has_or_is_using_name_1_from_private_module_2 :
Diagnostics.Public_method_0_of_exported_class_has_or_is_using_private_name_1;
}
else {
return symbolAccessibilityResult.errorModuleName ?
Diagnostics.Method_0_of_exported_interface_has_or_is_using_name_1_from_private_module_2 :
Diagnostics.Method_0_of_exported_interface_has_or_is_using_private_name_1;
}
}
}

function writeNameOfDeclaration(node: NamedDeclaration, getSymbolAccessibilityDiagnostic: GetSymbolAccessibilityDiagnostic) {
if (hasDynamicName(node)) {
// If this node has a dynamic name, it can only be an identifier or property access because
// we've already skipped it otherwise.
Debug.assert(resolver.isLateBound(node));

writeLateBoundNameOfDeclaration(node as LateBoundDeclaration, getSymbolAccessibilityDiagnostic);
}
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);
}
}

function writeLateBoundNameOfDeclaration(node: LateBoundDeclaration, getSymbolAccessibilityDiagnostic: GetSymbolAccessibilityDiagnostic) {
writer.getSymbolAccessibilityDiagnostic = getSymbolAccessibilityDiagnostic;
const entityName = node.name.expression;
const visibilityResult = resolver.isEntityNameVisible(entityName, enclosingDeclaration);
handleSymbolAccessibilityError(visibilityResult);
recordTypeReferenceDirectivesIfNecessary(resolver.getTypeReferenceDirectivesForEntityName(entityName));
writeTextOfNode(currentText, node.name);
}

function emitSignatureDeclarationWithJsDocComments(node: SignatureDeclaration) {
Expand Down
77 changes: 71 additions & 6 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 @@ -911,6 +911,30 @@
"category": "Error",
"code": 1329
},
"A property of an interface or type literal whose type is a 'unique symbol' type must be 'readonly'.": {
"category": "Error",
"code": 1330
},
"A property of a class whose type is a 'unique symbol' type must be both 'static' and 'readonly'.": {
"category": "Error",
"code": 1331
},
"A variable whose type is a 'unique symbol' type must be 'const'.": {
"category": "Error",
"code": 1332
},
"'unique symbol' types may not be used on a variable declaration with a binding name.": {
"category": "Error",
"code": 1333
},
"'unique symbol' types are only allowed on variables in a variable statement.": {
"category": "Error",
"code": 1334
},
"'unique symbol' types are not allowed here.": {
"category": "Error",
"code": 1335
},

"Duplicate identifier '{0}'.": {
"category": "Error",
Expand Down Expand Up @@ -1780,7 +1804,7 @@
"category": "Error",
"code": 2526
},
"The inferred type of '{0}' references an inaccessible 'this' type. A type annotation is necessary.": {
"The inferred type of '{0}' references an inaccessible '{1}' type. A type annotation is necessary.": {
"category": "Error",
"code": 2527
},
Expand Down Expand Up @@ -2228,6 +2252,14 @@
"category": "Error",
"code": 2716
},
"Subsequent property declarations must have the same type. Property '{0}' has type '{1}' at {2}, but here has type '{3}'.": {
"category": "Error",
"code": 2717
},
"Duplicate declaration '{0}'.": {
"category": "Error",
"code": 2718
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down Expand Up @@ -2530,6 +2562,39 @@
"code": 4094
},

"Public static method '{0}' of exported class has or is using name '{1}' from external module {2} but cannot be named.": {
"category": "Error",
"code": 4095
},
"Public static method '{0}' of exported class has or is using name '{1}' from private module '{2}'.": {
"category": "Error",
"code": 4096
},
"Public static method '{0}' of exported class has or is using private name '{1}'.": {
"category": "Error",
"code": 4097
},
"Public method '{0}' of exported class has or is using name '{1}' from external module {2} but cannot be named.": {
"category": "Error",
"code": 4098
},
"Public method '{0}' of exported class has or is using name '{1}' from private module '{2}'.": {
"category": "Error",
"code": 4099
},
"Public method '{0}' of exported class has or is using private name '{1}'.": {
"category": "Error",
"code": 4100
},
"Method '{0}' of exported interface has or is using name '{1}' from private module '{2}'.": {
"category": "Error",
"code": 4101
},
"Method '{0}' of exported interface has or is using private name '{1}'.": {
"category": "Error",
"code": 4102
},

"The current host does not support the '{0}' option.": {
"category": "Error",
"code": 5001
Expand Down
10 changes: 6 additions & 4 deletions src/compiler/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,15 +736,17 @@ namespace ts {
return <ThisTypeNode>createSynthesizedNode(SyntaxKind.ThisType);
}

export function createTypeOperatorNode(type: TypeNode) {
export function createTypeOperatorNode(type: TypeNode): TypeOperatorNode;
export function createTypeOperatorNode(operator: SyntaxKind.KeyOfKeyword | SyntaxKind.UniqueKeyword, type: TypeNode): TypeOperatorNode;
export function createTypeOperatorNode(operatorOrType: SyntaxKind.KeyOfKeyword | SyntaxKind.UniqueKeyword | TypeNode, type?: TypeNode) {
const node = createSynthesizedNode(SyntaxKind.TypeOperator) as TypeOperatorNode;
node.operator = SyntaxKind.KeyOfKeyword;
node.type = parenthesizeElementTypeMember(type);
node.operator = typeof operatorOrType === "number" ? operatorOrType : SyntaxKind.KeyOfKeyword;
node.type = parenthesizeElementTypeMember(typeof operatorOrType === "number" ? type : operatorOrType);
return node;
}

export function updateTypeOperatorNode(node: TypeOperatorNode, type: TypeNode) {
return node.type !== type ? updateNode(createTypeOperatorNode(type), node) : node;
return node.type !== type ? updateNode(createTypeOperatorNode(node.operator, type), node) : node;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should check node.operator in addition to node.type, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. operator is not a Node and you can't change it during update. We have a very small number of cases where we allow this, but generally we shouldn't. If you want to change the operator you should call createTypeOperatorNode instead.

}

export function createIndexedAccessTypeNode(objectType: TypeNode, indexType: TypeNode) {
Expand Down
11 changes: 7 additions & 4 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2665,8 +2665,8 @@ namespace ts {
case SyntaxKind.AnyKeyword:
case SyntaxKind.StringKeyword:
case SyntaxKind.NumberKeyword:
case SyntaxKind.BooleanKeyword:
case SyntaxKind.SymbolKeyword:
case SyntaxKind.BooleanKeyword:
case SyntaxKind.UndefinedKeyword:
case SyntaxKind.NeverKeyword:
case SyntaxKind.ObjectKeyword:
Expand Down Expand Up @@ -2720,6 +2720,7 @@ namespace ts {
case SyntaxKind.NumberKeyword:
case SyntaxKind.BooleanKeyword:
case SyntaxKind.SymbolKeyword:
case SyntaxKind.UniqueKeyword:
case SyntaxKind.VoidKeyword:
case SyntaxKind.UndefinedKeyword:
case SyntaxKind.NullKeyword:
Expand Down Expand Up @@ -2805,7 +2806,7 @@ namespace ts {
return finishNode(postfix);
}

function parseTypeOperator(operator: SyntaxKind.KeyOfKeyword) {
function parseTypeOperator(operator: SyntaxKind.KeyOfKeyword | SyntaxKind.UniqueKeyword) {
const node = <TypeOperatorNode>createNode(SyntaxKind.TypeOperator);
parseExpected(operator);
node.operator = operator;
Expand All @@ -2814,9 +2815,11 @@ namespace ts {
}

function parseTypeOperatorOrHigher(): TypeNode {
switch (token()) {
const operator = token();
switch (operator) {
case SyntaxKind.KeyOfKeyword:
return parseTypeOperator(SyntaxKind.KeyOfKeyword);
case SyntaxKind.UniqueKeyword:
return parseTypeOperator(operator);
case SyntaxKind.DotDotDotToken: {
const result = createNode(SyntaxKind.JSDocVariadicType) as JSDocVariadicType;
nextToken();
Expand Down
Loading