10000 Merge pull request #8233 from Microsoft/transforms-fixShortHandProper… · sweshgit/TypeScript@43914ff · GitHub
[go: up one dir, main page]

Skip to content

Commit 43914ff

Browse files
committed
Merge pull request microsoft#8233 from Microsoft/transforms-fixShortHandPropertiesAndExportedModules
[Transforms] Fixes for ShorthandPropertyAssignments and exported namespaces
2 parents 5481259 + 0da185d commit 43914ff

File tree

12 files changed

+342
-214
lines changed

12 files changed

+342
-214
lines changed

src/compiler/checker.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16578,10 +16578,10 @@ namespace ts {
1657816578
}
1657916579

1658016580
// Gets the type of object literal or array literal of destructuring assignment.
16581-
// { a } from
16581+
// { a } from
1658216582
// for ( { a } of elems) {
1658316583
// }
16584-
// [ a ] from
16584+
// [ a ] from
1658516585
// [a] = [ some array ...]
1658616586
function getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(expr: Expression): Type {
1658716587
Debug.assert(expr.kind === SyntaxKind.ObjectLiteralExpression || expr.kind === SyntaxKind.ArrayLiteralExpression);
@@ -16614,10 +16614,10 @@ namespace ts {
1661416614
}
1661516615

1661616616
// Gets the property symbol corresponding to the property in destructuring assignment
16617-
// 'property1' from
16617+
// 'property1' from
1661816618
// for ( { property1: a } of elems) {
1661916619
// }
16620-
// 'property1' at location 'a' from:
16620+
// 'property1' at location 'a' from:
1662116621
// [a] = [ property1, property2 ]
1662216622
function getPropertySymbolOfDestructuringAssignment(location: Identifier) {
1662316623
// Get the type of the object or array literal and then look for property of given name in the type
@@ -16721,19 +16721,28 @@ namespace ts {
1672116721
}
1672216722
}
1672316723

16724+
function isNameOfModuleOrEnumDeclaration(node: Identifier) {
16725+
const parent = node.parent;
16726+
return isModuleOrEnumDeclaration(parent) && node === parent.name;
16727+
}
16728+
1672416729
// When resolved as an expression identifier, if the given node references an exported entity, return the declaration
1672516730
// node of the exported entity's container. Otherwise, return undefined.
1672616731
function getReferencedExportContainer(node: Identifier, prefixLocals?: boolean): SourceFile | ModuleDeclaration | EnumDeclaration {
1672716732
node = getSourceTreeNodeOfType(node, isIdentifier);
1672816733
if (node) {
16729-
let symbol = getReferencedValueSymbol(node);
16734+
// When resolving the export container for the name of a module or enum
16735+
// declaration, we need to start resolution at the declaration's container.
16736+
// Otherwise, we could incorrectly resolve the export container as the
16737+
// declaration if it contains an exported member with the same name.
16738+
let symbol = getReferencedValueSymbol(node, /*startInDeclarationContainer*/ isNameOfModuleOrEnumDeclaration(node));
1673016739
if (symbol) {
1673116740
if (symbol.flags & SymbolFlags.ExportValue) {
1673216741
// If we reference an exported entity within the same module declaration, then whether
1673316742
// we prefix depends on the kind of entity. SymbolFlags.ExportHasLocal encompasses all the
1673416743
// kinds that we do NOT prefix.
1673516744
const exportSymbol = getMergedSymbol(symbol.exportSymbol);
16736-
if (exportSymbol.flags & SymbolFlags.ExportHasLocal && !prefixLocals) {
16745+
if (!prefixLocals && exportSymbol.flags & SymbolFlags.ExportHasLocal) {
1673716746
return undefined;
1673816747
}
1673916748
symbol = exportSymbol;
@@ -16744,7 +16753,7 @@ namespace ts {
1674416753
return <SourceFile>parentSymbol.valueDeclaration;
1674516754
}
1674616755
for (let n = node.parent; n; n = n.parent) {
16747-
if ((n.kind === SyntaxKind.ModuleDeclaration || n.kind === SyntaxKind.EnumDeclaration) && getSymbolOfNode(n) === parentSymbol) {
16756+
if (isModuleOrEnumDeclaration(n) && getSymbolOfNode(n) === parentSymbol) {
1674816757
return <ModuleDeclaration | EnumDeclaration>n;
1674916758
}
1675016759
}
@@ -17039,10 +17048,23 @@ namespace ts {
1703917048
return hasProperty(globals, name);
1704017049
}
1704117050

17042-
function getReferencedValueSymbol(reference: Identifier): Symbol {
17043-
return getNodeLinks(reference).resolvedSymbol ||
17044-
resolveName(reference, reference.text, SymbolFlags.Value | SymbolFlags.ExportValue | SymbolFlags.Alias,
17045-
/*nodeNotFoundMessage*/ undefined, /*nameArg*/ undefined);
17051+
function getReferencedValueSymbol(reference: Identifier, startInDeclarationContainer?: boolean): Symbol {
17052+
const resolvedSymbol = getNodeLinks(reference).resolvedSymbol;
17053+
if (resolvedSymbol) {
17054+
return resolvedSymbol;
17055+
}
17056+
17057+
let location: Node = reference;
17058+
if (startInDeclarationContainer) {
17059+
// When resolving the name of a declaration as a value, we need to start resolution
17060+
// at a point outside of the declaration.
17061+
const parent = reference.parent;
17062+
if (isDeclaration(parent) && reference === parent.name) {
17063+
location = getDeclarationContainer(parent);
17064+
}
17065+
}
17066+
17067+
return resolveName(location, reference.text, SymbolFlags.Value | SymbolFlags.ExportValue | SymbolFlags.Alias, /*nodeNotFoundMessage*/ undefined, /*nameArg*/ undefined);
1704617068 F438
}
1704717069

1704817070
function getReferencedValueDeclaration(reference: Identifier): Declaration {

src/compiler/printer.ts

Lines changed: 16 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,11 @@ const _super = (function (geti, seti) {
170170
} = comments;
171171

172172
let context: TransformationContext;
173-
let startLexicalEnvironment: () => void;
174-
let endLexicalEnvironment: () => Statement[];
175173
let getNodeEmitFlags: (node: Node) => NodeEmitFlags;
176174
let setNodeEmitFlags: (node: Node, flags: NodeEmitFlags) => void;
177-
let isExpressionSubstitutionEnabled: (node: Node) => boolean;
175+
let isSubstitutionEnabled: (node: Node) => boolean;
178176
let isEmitNotificationEnabled: (node: Node) => boolean;
179-
let expressionSubstitution: (node: Expression) => Expression;
180-
let identifierSubstitution: (node: Identifier) => Identifier;
177+
let onSubstituteNode: (node: Node, isExpression: boolean) => Node;
181178
let onEmitNode: (node: Node, emit: (node: Node) => void) => void;
182179
let nodeToGeneratedName: string[];
183180
let generatedNameSet: Map<string>;
@@ -233,14 +230,11 @@ const _super = (function (geti, seti) {
233230
comments.reset();
234231
writer.reset();
235232

236-
startLexicalEnvironment = undefined;
237-
endLexicalEnvironment = undefined;
238233
getNodeEmitFlags = undefined;
239234
setNodeEmitFlags = undefined;
240-
isExpressionSubstitutionEnabled = undefined;
235+
isSubstitutionEnabled = undefined;
241236
isEmitNotificationEnabled = undefined;
242-
expressionSubstitution = undefined;
243-
identifierSubstitution = undefined;
237+
onSubstituteNode = undefined;
244238
onEmitNode = undefined;
245239
tempFlags = TempFlags.Auto;
246240
currentSourceFile = undefined;
@@ -255,14 +249,11 @@ const _super = (function (geti, seti) {
255249

256250
function initializePrinter(_context: TransformationContext) {
257251
context = _context;
258-
startLexicalEnvironment = context.startLexicalEnvironment;
259-
endLexicalEnvironment = context.endLexicalEnvironment;
260252
getNodeEmitFlags = context.getNodeEmitFlags;
261253
setNodeEmitFlags = context.setNodeEmitFlags;
262-
isExpressionSubstitutionEnabled = context.isExpressionSubstitutionEnabled;
254+
isSubstitutionEnabled = context.isSubstitutionEnabled;
263255
isEmitNotificationEnabled = context.isEmitNotificationEnabled;
264-
expressionSubstitution = context.expressionSubstitution;
265-
identifierSubstitution = context.identifierSubstitution;
256+
onSubstituteNode = context.onSubstituteNode;
266257
onEmitNode = context.onEmitNode;
267258
return printSourceFile;
268259
}
@@ -416,6 +407,10 @@ const _super = (function (geti, seti) {
416407
}
417408

418409
function emitWorker(node: Node): void {
410+
if (tryEmitSubstitute(node, emitWorker, /*isExpression*/ false)) {
411+
return;
412+
}
413+
419414
const kind = node.kind;
420415
switch (kind) {
421416
// Pseudo-literals
@@ -426,10 +421,6 @@ const _super = (function (geti, seti) {
426421

427422
// Identifiers
428423
case SyntaxKind.Identifier:
429-
if (tryEmitSubstitute(node, identifierSubstitution)) {
430-
return;
431-
}
432-
433424
return emitIdentifier(<Identifier>node);
434425

435426
// Reserved words
@@ -675,11 +666,11 @@ const _super = (function (geti, seti) {
675666
}
676667

677668
function emitExpressionWorker(node: Node) {
678-
const kind = node.kind;
679-
if (isExpressionSubstitutionEnabled(node) && tryEmitSubstitute(node, expressionSubstitution)) {
669+
if (tryEmitSubstitute(node, emitExpressionWorker, /*isExpression*/ true)) {
680670
return;
681671
}
682672

673+
const kind = node.kind;
683674
switch (kind) {
684675
// Literals
685676
case SyntaxKind.NumericLiteral:
@@ -1531,7 +1522,6 @@ const _super = (function (geti, seti) {
15311522

15321523
const savedTempFlags = tempFlags;
15331524
tempFlags = 0;
1534-
startLexicalEnvironment();
15351525
emitSignatureHead(node);
15361526
emitBlockFunctionBodyAndEndLexicalEnvironment(node, body);
15371527
if (indentedFlag) {
@@ -1609,7 +1599,6 @@ const _super = (function (geti, seti) {
16091599
write(" {");
16101600
}
16111601

1612-
const startingLine = writer.getLine();
16131602
increaseIndent();
16141603
emitLeadingDetachedComments(body.statements, body, shouldSkipLeadingCommentsForNode);
16151604

@@ -1626,8 +1615,6 @@ const _super = (function (geti, seti) {
16261615
emitList(body, body.statements, ListFormat.MultiLineFunctionBodyStatements, statementOffset);
16271616
}
16281617

1629-
const endingLine = writer.getLine();
1630-
emitLexicalEnvironment(endLexicalEnvironment(), /*newLine*/ startingLine !== endingLine);
16311618
emitTrailingDetachedComments(body.statements, body, shouldSkipTrailingCommentsForNode);
16321619
decreaseIndent();
16331620
writeToken(SyntaxKind.CloseBraceToken, body.statements.end);
@@ -1725,15 +1712,9 @@ const _super = (function (geti, seti) {
17251712
else {
17261713
const savedTempFlags = tempFlags;
17271714
tempFlags = 0;
1728-
startLexicalEnvironment();
17291715
write("{");
17301716
increaseIndent();
1731-
1732-
const startingLine = writer.getLine();
17331717
emitBlockStatements(node);
1734-
1735-
const endingLine = writer.getLine();
1736-
emitLexicalEnvironment(endLexicalEnvironment(), /*newLine*/ startingLine !== endingLine);
17371718
write("}");
17381719
tempFlags = savedTempFlags;
17391720
}
@@ -2022,10 +2003,8 @@ const _super = (function (geti, seti) {
20222003
else {
20232004
const savedTempFlags = tempFlags;
20242005
tempFlags = 0;
2025-
startLexicalEnvironment();
20262006
emitHelpers(node);
20272007
emitList(node, statements, ListFormat.MultiLine, statementOffset);
2028-
emitLexicalEnvironment(endLexicalEnvironment(), /*newLine*/ true);
20292008
tempFlags = savedTempFlags;
20302009
}
20312010

@@ -2038,28 +2017,6 @@ const _super = (function (geti, seti) {
20382017
emitExpression(node.expression);
20392018
}
20402019

2041-
function emitLexicalEnvironment(declarations: Statement[], newLine: boolean) {
2042-
if (declarations && declarations.length > 0) {
2043-
for (const node of declarations) {
2044-
if (newLine) {
2045-
writeLine();
2046-
}
2047-
else {
2048-
write(" ");
2049-
}
2050-
2051-
emit(node);
2052-
}
2053-
2054-
if (newLine) {
2055-
writeLine();
2056-
}
2057-
else {
2058-
write(" ");
2059-
}
2060-
}
2061-
}
2062-
20632020
/**
20642021
* Emits any prologue directives at the start of a Statement list, returning the
20652022
* number of prologue directives written to the output.
@@ -2218,12 +2175,12 @@ const _super = (function (geti, seti) {
22182175
}
22192176
}
22202177

2221-
function tryEmitSubstitute(node: Node, substitution: (node: Node) => Node) {
2222-
if (substitution && (getNodeEmitFlags(node) & NodeEmitFlags.NoSubstitution) === 0) {
2223-
const substitute = substitution(node);
2178+
function tryEmitSubstitute(node: Node, emitNode: (node: Node) => void, isExpression: boolean) {
2179+
if (isSubstitutionEnabled(node) && (getNodeEmitFlags(node) & NodeEmitFlags.NoSubstitution) === 0) {
2180+
const substitute = onSubstituteNode(node, isExpression);
22242181
if (substitute !== node) {
22252182
setNodeEmitFlags(substitute, NodeEmitFlags.NoSubstitution | getNodeEmitFlags(substitute));
2226-
emitWorker(substitute);
2183+
emitNode(substitute);
22272184
return true;
22282185
}
22292186
}

0 commit comments

Comments
 (0)
0