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 3 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
175 changes: 164 additions & 11 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

56 changes: 38 additions & 18 deletions src/compiler/declarationEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,19 +452,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 @@ -584,6 +571,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 @@ -1202,7 +1202,7 @@ namespace ts {
}

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

Expand All @@ -1221,10 +1221,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 @@ -1287,6 +1294,19 @@ namespace ts {
} : undefined;
}

function emitDynamicName(entityName: EntityNameExpression) {
writer.getSymbolAccessibilityDiagnostic = getVariableDeclarationTypeVisibilityError;
const visibilityResult = resolver.isEntityNameVisible(entityName, enclosingDeclaration);
if (visibilityResult.accessibility !== SymbolAccessibility.Accessible) {
resolver.writeTypeOfExpression(entityName, enclosingDeclaration, TypeFormatFlags.None, writer);
}
else {
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
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2123,6 +2123,10 @@
"category": "Error",
"code": 2710
},
"Subsequent property declarations must have the same type. Property '{0}' must be of type '{1}', but here has type '{2}'.": {
"category": "Error",
"code": 2711
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2721,6 +2721,7 @@ namespace ts {
isTopLevelValueImportEqualsWithEntityName(node: ImportEqualsDeclaration): boolean;
getNodeCheckFlags(node: Node): NodeCheckFlags;
isDeclarationVisible(node: Declaration): boolean;
isLiteralDynamicName(node: ComputedPropertyName): boolean;
collectLinkedAliases(node: Identifier): Node[];
isImplementationOfOverload(node: FunctionLikeDeclaration): boolean;
isRequiredInitializedParameter(node: ParameterDeclaration): boolean;
Expand Down Expand Up @@ -2775,6 +2776,7 @@ namespace ts {
ExportStar = 1 << 25, // Export * declaration
Optional = 1 << 26, // Optional property
Transient = 1 << 27, // Transient symbol (created during type check)
Dynamic = 1 << 28, // Dynamically resolved symbol from computed property

Enum = RegularEnum | ConstEnum,
Variable = FunctionScopedVariable | BlockScopedVariable,
Expand Down Expand Up @@ -2869,6 +2871,8 @@ namespace ts {
isDeclarationWithCollidingName?: boolean; // True if symbol is block scoped redeclaration
bindingElement?: BindingElement; // Binding element associated with property symbol
exportsSomeValue?: boolean; // True if module exports some value (not just types)
dynamicMembers?: SymbolTable; // Dynamic members with literal names resolved during check
resolvedMembers?: SymbolTable;
}

/* @internal */
Expand Down
3 changes: 3 additions & 0 deletions src/harness/unittests/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ namespace ts {
transformers: {
before: [replaceUndefinedWithVoid0],
after: [replaceIdentifiersNamedOldNameWithNewName]
},
compilerOptions: {
newLine: ts.NewLineKind.CarriageReturnLineFeed
}
}).outputText;
});
Expand Down
142 changes: 142 additions & 0 deletions tests/baselines/reference/dynamicNames.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
//// [tests/cases/compiler/dynamicNames.ts] ////

//// [module.ts]
export const c0 = "a";
export const c1 = 1;
export interface T0 {
[c0]: number;
[c1]: string;
}
export declare class T1 implements T2 {
[c0]: number;
[c1]: string;
}
export declare class T2 extends T1 {
}
export declare type T3 = {
[c0]: number;
[c1]: string;
};

//// [main.ts]
import { c0, c1, T0, T1, T2, T3 } from "./module";
import * as M from "./module";

namespace N {
export const c2 = "a";
export const c3 = 1;

export interface T4 {
[N.c2]: number;
[N.c3]: string;
}
export declare class T5 implements T4 {
[N.c2]: number;
[N.c3]: string;
}
export declare class T6 extends T5 {
}
export declare type T7 = {
[N.c2]: number;
[N.c3]: string;
};
}

const c4 = "a";
const c5 = 1;

interface T8 {
[c4]: number;
[c5]: string;
}
declare class T9 implements T8 {
[c4]: number;
[c5]: string;
}
declare class T10 extends T9 {
}
declare type T11 = {
[c4]: number;
[c5]: string;
};

interface T12 {
a: number;
1: string;
}
declare class T13 implements T2 {
a: number;
1: string;
}
declare class T14 extends T13 {
}
declare type T15 = {
a: number;
1: string;
};

let t0: T0;
let t1: T1;
let t2: T2;
let t3: T3;
let t0_1: M.T0;
let t1_1: M.T1;
let t2_1: M.T2;
let t3_1: M.T3;
let t4: N.T4;
let t5: N.T5;
let t6: N.T6;
let t7: N.T7;
let t8: T8;
let t9: T9;
let t10: T10;
let t11: T11;
let t12: T12;
let t13: T13;
let t14: T14;
let t15: T15;

// assignability
t0 = t1, t0 = t2, t0 = t3, t1 = t0, t1 = t2, t1 = t3, t2 = t0, t2 = t1, t2 = t3, t3 = t0, t3 = t1, t3 = t2;
t4 = t5, t4 = t6, t4 = t7, t5 = t4, t5 = t6, t5 = t7, t6 = t4, t6 = t5, t6 = t7, t7 = t4, t7 = t5, t7 = t6;
t0 = t12, t0 = t13, t0 = t14, t0 = t15, t12 = t0, t13 = t0, t14 = t0, t15 = t0;

//// [module.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.c0 = "a";
exports.c1 = 1;
//// [main.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var N;
(function (N) {
N.c2 = "a";
N.c3 = 1;
})(N || (N = {}));
const c4 = "a";
const c5 = 1;
let t0;
let t1;
let t2;
let t3;
let t0_1;
let t1_1;
let t2_1;
let t3_1;
let t4;
let t5;
let t6;
let t7;
let t8;
let t9;
let t10;
let t11;
let t12;
let t13;
let t14;
let t15;
// assignability
t0 = t1, t0 = t2, t0 = t3, t1 = t0, t1 = t2, t1 = t3, t2 = t0, t2 = t1, t2 = t3, t3 = t0, t3 = t1, t3 = t2;
t4 = t5, t4 = t6, t4 = t7, t5 = t4, t5 = t6, t5 = t7, t6 = t4, t6 = t5, t6 = t7, t7 = t4, t7 = t5, t7 = t6;
t0 = t12, t0 = t13, t0 = t14, t0 = t15, t12 = t0, t13 = t0, t14 = t0, t15 = t0;
Loading