Skip to content

Commit 9f1b747

Browse files
Made the first-declaration check conservative in the TypeScript transform.
1 parent 5cb5cf1 commit 9f1b747

2 files changed

Lines changed: 10 additions & 9 deletions

File tree

src/compiler/transformers/ts.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2639,9 +2639,6 @@ namespace ts {
26392639
/**
26402640
* Records that a declaration was emitted in the current scope, if it was the first
26412641
* declaration for the provided symbol.
2642-
*
2643-
* NOTE: if there is ever a transformation above this one, we may not be able to rely
2644-
* on symbol names.
26452642
*/
26462643
function recordEmittedDeclarationInScope(node: Node) {
26472644
const name = node.symbol && node.symbol.escapedName;
@@ -2657,18 +2654,22 @@ namespace ts {
26572654
}
26582655

26592656
/**
2660-
* Determines whether a declaration is the first declaration with the same name emitted
2661-
* in the current scope.
2657+
* Determines whether a declaration is *could* be the first declaration with
2658+
* the same name emitted in the current scope. Only returns false if we are absolutely
2659+
* certain a previous declaration has been emitted.
26622660
*/
2663-
function isFirstEmittedDeclarationInScope(node: Node) {
2661+
function isPotentiallyFirstEmittedDeclarationInScope(node: Node) {
2662+
// If the node has a named symbol, then we have enough knowledge to determine
2663+
// whether a prior declaration has been emitted.
26642664
if (currentScopeFirstDeclarationsOfName) {
26652665
const name = node.symbol && node.symbol.escapedName;
26662666
if (name) {
26672667
return currentScopeFirstDeclarationsOfName.get(name) === node;
26682668
}
26692669
}
26702670

2671-
return false;
2671+
// Otherwise, we can't be sure. For example, this node could be synthetic.
2672+
return true;
26722673
}
26732674

26742675
/**
@@ -2690,7 +2691,7 @@ namespace ts {
26902691
setOriginalNode(statement, node);
26912692

26922693
recordEmittedDeclarationInScope(node);
2693-
if (isFirstEmittedDeclarationInScope(node)) {
2694+
if (isPotentiallyFirstEmittedDeclarationInScope(node)) {
26942695
// Adjust the source map emit to match the old emitter.
26952696
if (node.kind === SyntaxKind.EnumDeclaration) {
26962697
setSourceMapRange(statement.declarationList, node);

src/harness/unittests/transform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ namespace ts {
9999
}
100100
};
101101
}
102-
})
102+
});
103103
});
104104
}
105105

0 commit comments

Comments
 (0)