Skip to content

Commit 3f6b576

Browse files
committed
fix(compiler): remove unused export and unnecessary @internal from isUniqueSymbolDeclaration
1 parent 638c34f commit 3f6b576

File tree

2 files changed

+18
-64
lines changed

2 files changed

+18
-64
lines changed

src/compiler/emitter.ts

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -423,39 +423,11 @@ import {
423423
WriteFileCallbackData,
424424
YieldExpression,
425425
} from "./_namespaces/ts.js";
426-
import {
427-
isTypeReferenceNode,
428-
TypeChecker,
429-
TypeFlags,
430-
} from "./_namespaces/ts.js";
431426
import * as performance from "./_namespaces/ts.performance.js";
427+
432428
const brackets = createBracketsMap();
433429

434430
/** @internal */
435-
export function isUniqueSymbolDeclaration(node: VariableDeclarationList, checker: TypeChecker): boolean {
436-
return node.declarations.some((decl: VariableDeclaration) => {
437-
// 1. If type is explicitly written, handle as before
438-
const typeNode: TypeNode | undefined = decl.type;
439-
if (typeNode) {
440-
if (isTypeReferenceNode(typeNode) && isIdentifier(typeNode.typeName)) {
441-
if (typeNode.typeName.escapedText.toString() === "unique symbol") {
442-
return true;
443-
}
444-
}
445-
if ((typeNode.kind as SyntaxKind) === SyntaxKind.UniqueKeyword) {
446-
return true;
447-
}
448-
}
449-
// 2. Otherwise, check the inferred type
450-
const type = checker.getTypeAtLocation(decl.name);
451-
if (type.flags & TypeFlags.UniqueESSymbol) {
452-
return true;
453-
}
454-
455-
return false;
456-
});
457-
}
458-
459431
export function isBuildInfoFile(file: string): boolean {
460432
return fileExtensionIs(file, Extension.TsBuildInfo);
461433
}
@@ -769,7 +741,6 @@ export function emitFiles(
769741
onlyBuildInfo: boolean,
770742
forceDtsEmit?: boolean,
771743
skipBuildInfo?: boolean,
772-
emitTypeChecker?: TypeChecker,
773744
): EmitResult {
774745
// Why var? It avoids TDZ checks in the runtime which can be costly.
775746
// See: https://github.com/microsoft/TypeScript/issues/52924
@@ -870,7 +841,7 @@ export function emitFiles(
870841
extendedDiagnostics: compilerOptions.extendedDiagnostics,
871842
};
872843

873-
const typeChecker = emitTypeChecker;
844+
// Create a printer to print the nodes
874845
const printer = createPrinter(printerOptions, {
875846
// resolver hooks
876847
hasGlobalName: resolver.hasGlobalName,
@@ -879,11 +850,12 @@ export function emitFiles(
879850
onEmitNode: transform.emitNodeWithNotification,
880851
isEmitNotificationEnabled: transform.isEmitNotificationEnabled,
881852
substituteNode: transform.substituteNode,
882-
}, typeChecker);
853+
});
883854

884855
Debug.assert(transform.transformed.length === 1, "Should only see one output from the transform");
885856
printSourceFileOrBundle(jsFilePath, sourceMapFilePath, transform, printer, compilerOptions);
886857

858+
// Clean up emit nodes on parse tree
887859
transform.dispose();
888860

889861
if (emittedFilesList) {
@@ -928,8 +900,6 @@ export function emitFiles(
928900
}
929901
}
930902

931-
// TypeChecker is already captured in the closure above
932-
933903
const declBlocked = (!!declarationTransform.diagnostics && !!declarationTransform.diagnostics.length) || !!host.isEmitBlocked(declarationFilePath) || !!compilerOptions.noEmit;
934904
emitSkipped = emitSkipped || declBlocked;
935905
if (!declBlocked || forceDtsEmit) {
@@ -1221,7 +1191,7 @@ export const createPrinterWithRemoveCommentsNeverAsciiEscape: () => Printer = /*
12211191
/** @internal */
12221192
export const createPrinterWithRemoveCommentsOmitTrailingSemicolon: () => Printer = /* @__PURE__ */ memoize(() => createPrinter({ removeComments: true, omitTrailingSemicolon: true }));
12231193

1224-
export function createPrinter(printerOptions: PrinterOptions = {}, handlers: PrintHandlers = {}, typeChecker?: TypeChecker): Printer {
1194+
export function createPrinter(printerOptions: PrinterOptions = {}, handlers: PrintHandlers = {}): Printer {
12251195
// Why var? It avoids TDZ checks in the runtime which can be costly.
12261196
// See: https://github.com/microsoft/TypeScript/issues/52924
12271197
/* eslint-disable no-var */
@@ -3373,6 +3343,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
33733343
writeSpace();
33743344
emit(node.caseBlock);
33753345
}
3346+
33763347
function emitLabeledStatement(node: LabeledStatement) {
33773348
emit(node.label);
33783349
emitTokenWithComment(SyntaxKind.ColonToken, node.label.end, writePunctuation, node);
@@ -3425,17 +3396,12 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
34253396
writeKeyword("using");
34263397
}
34273398
else {
3428-
// Check if unique symbol and use const instead of var
3429-
const isUnique = typeChecker && isUniqueSymbolDeclaration(node, typeChecker);
34303399
const head = isLet(node) ? "let" :
34313400
isVarConst(node) ? "const" :
34323401
isVarUsing(node) ? "using" :
3433-
isUnique ? "const" :
34343402
"var";
3435-
34363403
writeKeyword(head);
34373404
}
3438-
34393405
writeSpace();
34403406
emitList(node, node.declarations, ListFormat.VariableDeclarationList);
34413407
}

src/compiler/transformers/declarations.ts

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ import {
111111
isFunctionDeclaration,
112112
isFunctionLike,
113113
isGlobalScopeAugmentation,
114-
isIdentifier,
115114
isIdentifierText,
116115
isImportEqualsDeclaration,
117116
isIndexSignatureDeclaration,
@@ -146,7 +145,6 @@ import {
146145
isTypeNode,
147146
isTypeParameterDeclaration,
148147
isTypeQueryNode,
149-
isTypeReferenceNode,
150148
isVarAwaitUsing,
151149
isVariableDeclaration,
152150
isVarUsing,
@@ -205,6 +203,7 @@ import {
205203
tryCast,
206204
TypeAliasDeclaration,
207205
TypeNode,
206+
TypeOperatorNode,
208207
TypeParameterDeclaration,
209208
TypeReferenceNode,
210209
unescapeLeadingUnderscores,
@@ -219,17 +218,6 @@ import {
219218
VisitResult,
220219
} from "../_namespaces/ts.js";
221220

222-
function isUniqueSymbolByType(type: TypeNode | undefined): boolean {
223-
if (!type) return false;
224-
225-
if (type.kind === SyntaxKind.TypeOperator && (type as any).operator === SyntaxKind.UniqueKeyword) return true;
226-
227-
if (isTypeReferenceNode(type) && isIdentifier(type.typeName)) {
228-
if (type.typeName.escapedText === "unique symbol") return true;
229-
}
230-
return false;
231-
}
232-
233221
/** @internal */
234222
export function getDeclarationDiagnostics(
235223
host: EmitHost,
@@ -1492,17 +1480,10 @@ export function transformDeclarations(context: TransformationContext): Transform
14921480
exportMappings.push([name, nameStr]);
14931481
}
14941482
const varDecl = factory.createVariableDeclaration(name, /*exclamationToken*/ undefined, type, /*initializer*/ undefined);
1495-
const shouldForceConst = isUniqueSymbolByType(type);
1496-
1497-
const declList = factory.createVariableDeclarationList(
1498-
[varDecl],
1499-
shouldForceConst ? NodeFlags.Const : NodeFlags.None,
1500-
);
1501-
1502-
return factory.createVariableStatement(
1503-
isNonContextualKeywordName ? undefined : [factory.createToken(SyntaxKind.ExportKeyword)],
1504-
declList,
1505-
);
1483+
const modifiers = isNonContextualKeywordName ? undefined : [factory.createToken(SyntaxKind.ExportKeyword)];
1484+
const isConst = isUniqueSymbolType(type);
1485+
const variableDeclarationList = factory.createVariableDeclarationList([varDecl], isConst ? NodeFlags.Const : NodeFlags.None);
1486+
return factory.createVariableStatement(modifiers, variableDeclarationList);
15061487
});
15071488
if (!exportMappings.length) {
15081489
declarations = mapDefined(declarations, declaration => factory.replaceModifiers(declaration, ModifierFlags.None));
@@ -1840,6 +1821,13 @@ export function transformDeclarations(context: TransformationContext): Transform
18401821
return isExportAssignment(node) || isExportDeclaration(node);
18411822
}
18421823

1824+
function isUniqueSymbolType(type: TypeNode | undefined): boolean {
1825+
return !!type &&
1826+
type.kind === SyntaxKind.TypeOperator &&
1827+
(type as TypeOperatorNode).operator === SyntaxKind.UniqueKeyword &&
1828+
(type as TypeOperatorNode).type.kind === SyntaxKind.SymbolKeyword;
1829+
}
1830+
18431831
function hasScopeMarker(statements: readonly Statement[]) {
18441832
return some(statements, isScopeMarker);
18451833
}

0 commit comments

Comments
 (0)