Skip to content

Commit 36b6325

Browse files
authored
Remove EndOfDeclarationMarker and MergeDeclarationMarker nodes (microsoft#53901)
1 parent d3bbef3 commit 36b6325

File tree

235 files changed

+1132
-1634
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

235 files changed

+1132
-1634
lines changed

src/compiler/checker.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3037,6 +3037,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
30373037
// (it refers to the constant type of the expression instead)
30383038
return undefined;
30393039
}
3040+
if (isModuleDeclaration(location) && lastLocation && location.name === lastLocation) {
3041+
// If this is the name of a namespace, skip the parent since it will have is own locals that could
3042+
// conflict.
3043+
lastLocation = location;
3044+
location = location.parent;
3045+
}
30403046
// Locals of a source file are not in scope (because they get merged into the global symbol table)
30413047
if (canHaveLocals(location) && location.locals && !isGlobalSourceFile(location)) {
30423048
if (result = lookup(location.locals, name, meaning)) {
@@ -3331,6 +3337,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
33313337
}
33323338
}
33333339
break;
3340+
case SyntaxKind.ExportSpecifier:
3341+
// External module export bindings shouldn't be resolved to local symbols.
3342+
if (lastLocation &&
3343+
lastLocation === (location as ExportSpecifier).propertyName &&
3344+
(location as ExportSpecifier).parent.parent.moduleSpecifier) {
3345+
location = location.parent.parent.parent;
3346+
}
3347+
break;
33343348
}
33353349
if (isSelfReferenceLocation(location)) {
33363350
lastSelfReferenceLocation = location;
@@ -46474,6 +46488,43 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4647446488
return undefined;
4647546489
}
4647646490

46491+
function getReferencedValueDeclarations(referenceIn: Identifier): Declaration[] | undefined {
46492+
if (!isGeneratedIdentifier(referenceIn)) {
46493+
const reference = getParseTreeNode(referenceIn, isIdentifier);
46494+
if (reference) {
46495+
const symbol = getReferencedValueSymbol(reference);
46496+
if (symbol) {
46497+
return filter(getExportSymbolOfValueSymbolIfExported(symbol).declarations, declaration => {
46498+
switch (declaration.kind) {
46499+
case SyntaxKind.VariableDeclaration:
46500+
case SyntaxKind.Parameter:
46501+
case SyntaxKind.BindingElement:
46502+
case SyntaxKind.PropertyDeclaration:
46503+
case SyntaxKind.PropertyAssignment:
46504+
case SyntaxKind.ShorthandPropertyAssignment:
46505+
case SyntaxKind.EnumMember:
46506+
case SyntaxKind.ObjectLiteralExpression:
46507+
case SyntaxKind.FunctionDeclaration:
46508+
case SyntaxKind.FunctionExpression:
46509+
case SyntaxKind.ArrowFunction:
46510+
case SyntaxKind.ClassDeclaration:
46511+
case SyntaxKind.ClassExpression:
46512+
case SyntaxKind.EnumDeclaration:
46513+
case SyntaxKind.MethodDeclaration:
46514+
case SyntaxKind.GetAccessor:
46515+
case SyntaxKind.SetAccessor:
46516+
case SyntaxKind.ModuleDeclaration:
46517+
return true;
46518+
}
46519+
return false;
46520+
});
46521+
}
46522+
}
46523+
}
46524+
46525+
return undefined;
46526+
}
46527+
4647746528
function isLiteralConstDeclaration(node: VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration): boolean {
4647846529
if (isDeclarationReadonly(node) || isVariableDeclaration(node) && isVarConst(node)) {
4647946530
return isFreshLiteralType(getTypeOfSymbol(getSymbolOfDeclaration(node)));
@@ -46581,6 +46632,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4658146632
},
4658246633
collectLinkedAliases,
4658346634
getReferencedValueDeclaration,
46635+
getReferencedValueDeclarations,
4658446636
getTypeReferenceSerializationKind,
4658546637
isOptionalParameter,
4658646638
moduleExportsSomeValue,

src/compiler/emitter.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,7 @@ export const notImplementedResolver: EmitResolver = {
11551155
// Returns the constant value this property access resolves to: notImplemented, or 'undefined' for a non-constant
11561156
getConstantValue: notImplemented,
11571157
getReferencedValueDeclaration: notImplemented,
1158+
getReferencedValueDeclarations: notImplemented,
11581159
getTypeReferenceSerializationKind: notImplemented,
11591160
isOptionalParameter: notImplemented,
11601161
moduleExportsSomeValue: notImplemented,
@@ -2178,8 +2179,6 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
21782179

21792180
// Transformation nodes
21802181
case SyntaxKind.NotEmittedStatement:
2181-
case SyntaxKind.EndOfDeclarationMarker:
2182-
case SyntaxKind.MergeDeclarationMarker:
21832182
return;
21842183
}
21852184
if (isExpression(node)) {
@@ -2298,9 +2297,6 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
22982297
return emitPartiallyEmittedExpression(node as PartiallyEmittedExpression);
22992298
case SyntaxKind.CommaListExpression:
23002299
return emitCommaList(node as CommaListExpression);
2301-
case SyntaxKind.MergeDeclarationMarker:
2302-
case SyntaxKind.EndOfDeclarationMarker:
2303-
return;
23042300
case SyntaxKind.SyntheticReferenceExpression:
23052301
return Debug.fail("SyntheticReferenceExpression should not be printed");
23062302
}

src/compiler/factory/nodeFactory.ts

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ import {
7979
EmitNode,
8080
emptyArray,
8181
EmptyStatement,
82-
EndOfDeclarationMarker,
8382
EndOfFileToken,
8483
EntityName,
8584
EnumDeclaration,
@@ -121,6 +120,7 @@ import {
121120
getLineAndCharacterOfPosition,
122121
getNameOfDeclaration,
123122
getNodeId,
123+
getNonAssignedNameOfDeclaration,
124124
getSourceMapRange,
125125
getSyntheticLeadingComments,
126126
getSyntheticTrailingComments,
@@ -298,7 +298,6 @@ import {
298298
MemberName,
299299
memoize,
300300
memoizeOne,
301-
MergeDeclarationMarker,
302301
MetaProperty,
303302
MethodDeclaration,
304303
MethodSignature,
@@ -946,8 +945,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
946945
updatePartiallyEmittedExpression,
947946
createCommaListExpression,
948947
updateCommaListExpression,
949-
createEndOfDeclarationMarker,
950-
createMergeDeclarationMarker,
951948
createSyntheticReferenceExpression,
952949
updateSyntheticReferenceExpression,
953950
cloneNode,
@@ -6210,30 +6207,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
62106207
: node;
62116208
}
62126209

6213-
/**
6214-
* Creates a synthetic element to act as a placeholder for the end of an emitted declaration in
6215-
* order to properly emit exports.
6216-
*/
6217-
// @api
6218-
function createEndOfDeclarationMarker(original: Node) {
6219-
const node = createBaseNode<EndOfDeclarationMarker>(SyntaxKind.EndOfDeclarationMarker);
6220-
node.emitNode = {} as EmitNode;
6221-
node.original = original;
6222-
return node;
6223-
}
6224-
6225-
/**
6226-
* Creates a synthetic element to act as a placeholder for the beginning of a merged declaration in
6227-
* order to properly emit exports.
6228-
*/
6229-
// @api
6230-
function createMergeDeclarationMarker(original: Node) {
6231-
const node = createBaseNode<MergeDeclarationMarker>(SyntaxKind.MergeDeclarationMarker);
6232-
node.emitNode = {} as EmitNode;
6233-
node.original = original;
6234-
return node;
6235-
}
6236-
62376210
// @api
62386211
function createSyntheticReferenceExpression(expression: Expression, thisArg: Expression) {
62396212
const node = createBaseNode<SyntheticReferenceExpression>(SyntaxKind.SyntheticReferenceExpression);
@@ -6672,8 +6645,8 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
66726645
: reduceLeft(expressions, factory.createComma)!;
66736646
}
66746647

6675-
function getName(node: Declaration | undefined, allowComments?: boolean, allowSourceMaps?: boolean, emitFlags: EmitFlags = 0) {
6676-
const nodeName = getNameOfDeclaration(node);
6648+
function getName(node: Declaration | undefined, allowComments?: boolean, allowSourceMaps?: boolean, emitFlags: EmitFlags = 0, ignoreAssignedName?: boolean) {
6649+
const nodeName = ignoreAssignedName ? node && getNonAssignedNameOfDeclaration(node) : getNameOfDeclaration(node);
66776650
if (nodeName && isIdentifier(nodeName) && !isGeneratedIdentifier(nodeName)) {
66786651
// TODO(rbuckton): Does this need to be parented?
66796652
const name = setParent(setTextRange(cloneNode(nodeName), nodeName), nodeName.parent);
@@ -6710,9 +6683,10 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
67106683
* @param node The declaration.
67116684
* @param allowComments A value indicating whether comments may be emitted for the name.
67126685
* @param allowSourceMaps A value indicating whether source maps may be emitted for the name.
6686+
* @param ignoreAssignedName Indicates that the assigned name of a declaration shouldn't be considered.
67136687
*/
6714-
function getLocalName(node: Declaration, allowComments?: boolean, allowSourceMaps?: boolean) {
6715-
return getName(node, allowComments, allowSourceMaps, EmitFlags.LocalName);
6688+
function getLocalName(node: Declaration, allowComments?: boolean, allowSourceMaps?: boolean, ignoreAssignedName?: boolean) {
6689+
return getName(node, allowComments, allowSourceMaps, EmitFlags.LocalName, ignoreAssignedName);
67166690
}
67176691

67186692
/**

src/compiler/factory/nodeTests.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import {
4646
DotDotDotToken,
4747
ElementAccessExpression,
4848
EmptyStatement,
49-
EndOfDeclarationMarker,
5049
EnumDeclaration,
5150
EnumMember,
5251
EqualsGreaterThanToken,
@@ -137,7 +136,6 @@ import {
137136
LabeledStatement,
138137
LiteralTypeNode,
139138
MappedTypeNode,
140-
MergeDeclarationMarker,
141139
MetaProperty,
142140
MethodDeclaration,
143141
MethodSignature,
@@ -902,16 +900,6 @@ export function isSyntheticReference(node: Node): node is SyntheticReferenceExpr
902900
return node.kind === SyntaxKind.SyntheticReferenceExpression;
903901
}
904902

905-
/** @internal */
906-
export function isMergeDeclarationMarker(node: Node): node is MergeDeclarationMarker {
907-
return node.kind === SyntaxKind.MergeDeclarationMarker;
908-
}
909-
910-
/** @internal */
911-
export function isEndOfDeclarationMarker(node: Node): node is EndOfDeclarationMarker {
912-
return node.kind === SyntaxKind.EndOfDeclarationMarker;
913-
}
914-
915903
// Module References
916904

917905
export function isExternalModuleReference(node: Node): node is ExternalModuleReference {

src/compiler/transformers/classFields.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ import {
9393
isConstructorDeclaration,
9494
isDestructuringAssignment,
9595
isElementAccessExpression,
96+
isExportOrDefaultModifier,
9697
isExpression,
9798
isExpressionStatement,
9899
isForInitializer,
@@ -1843,25 +1844,13 @@ export function transformClassFields(context: TransformationContext): (x: Source
18431844
}
18441845
}
18451846

1846-
const modifiers = visitNodes(node.modifiers, modifierVisitor, isModifier);
1847+
const isExport = hasSyntacticModifier(node, ModifierFlags.Export);
1848+
const isDefault = hasSyntacticModifier(node, ModifierFlags.Default);
1849+
let modifiers = visitNodes(node.modifiers, modifierVisitor, isModifier);
18471850
const heritageClauses = visitNodes(node.heritageClauses, heritageClauseVisitor, isHeritageClause);
18481851
const { members, prologue } = transformClassMembers(node);
1849-
const classDecl = factory.updateClassDeclaration(
1850-
node,
1851-
modifiers,
1852-
node.name,
1853-
/*typeParameters*/ undefined,
1854-
heritageClauses,
1855-
members
1856-
);
18571852

18581853
const statements: Statement[] = [];
1859-
if (prologue) {
1860-
statements.push(factory.createExpressionStatement(prologue));
1861-
}
1862-
1863-
statements.push(classDecl);
1864-
18651854
if (pendingClassReferenceAssignment) {
18661855
getPendingExpressions().unshift(pendingClassReferenceAssignment);
18671856
}
@@ -1884,6 +1873,29 @@ export function transformClassFields(context: TransformationContext): (x: Source
18841873
}
18851874
}
18861875

1876+
if (statements.length > 0 && isExport && isDefault) {
1877+
modifiers = visitNodes(modifiers, node => isExportOrDefaultModifier(node) ? undefined : node, isModifier);
1878+
statements.push(factory.createExportAssignment(
1879+
/*modifiers*/ undefined,
1880+
/*isExportEquals*/ false,
1881+
factory.getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true)
1882+
));
1883+
}
1884+
1885+
const classDecl = factory.updateClassDeclaration(
1886+
node,
1887+
modifiers,
1888+
node.name,
1889+
/*typeParameters*/ undefined,
1890+
heritageClauses,
1891+
members
1892+
);
1893+
statements.unshift(classDecl);
1894+
1895+
if (prologue) {
1896+
statements.unshift(factory.createExpressionStatement(prologue));
1897+
}
1898+
18871899
return statements;
18881900
}
18891901

src/compiler/transformers/es2015.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -966,13 +966,6 @@ export function transformES2015(context: TransformationContext): (x: SourceFile
966966
statements.push(exportStatement);
967967
}
968968

969-
const emitFlags = getEmitFlags(node);
970-
if ((emitFlags & EmitFlags.HasEndOfDeclarationMarker) === 0) {
971-
// Add a DeclarationMarker as a marker for the end of the declaration
972-
statements.push(factory.createEndOfDeclarationMarker(node));
973-
setEmitFlags(statement, emitFlags | EmitFlags.HasEndOfDeclarationMarker);
974-
}
975-
976969
return singleOrMany(statements);
977970
}
978971

src/compiler/transformers/esDecorators.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ export function transformESDecorators(context: TransformationContext): (x: Sourc
640640
function transformClassLike(node: ClassLikeDeclaration, className: Expression) {
641641
startLexicalEnvironment();
642642

643-
const classReference = node.name ?? factory.getGeneratedNameForNode(node);
643+
const classReference = factory.getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ false, /*ignoreAssignedName*/ true);
644644
const classInfo = createClassInfo(node);
645645
const classDefinitionStatements: Statement[] = [];
646646
let leadingBlockStatements: Statement[] | undefined;
@@ -1013,6 +1013,8 @@ export function transformESDecorators(context: TransformationContext): (x: Sourc
10131013
factory.getInternalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true) :
10141014
factory.getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
10151015
const varDecl = factory.createVariableDeclaration(declName, /*exclamationToken*/ undefined, /*type*/ undefined, iife);
1016+
setOriginalNode(varDecl, node);
1017+
10161018
const varDecls = factory.createVariableDeclarationList([varDecl], NodeFlags.Let);
10171019
const statement = factory.createVariableStatement(modifiers, varDecls);
10181020
setOriginalNode(statement, node);

0 commit comments

Comments
 (0)