Skip to content

Commit 19ec83f

Browse files
authored
Refactor declaration emitter into declaration transformer (#21930)
* Refactor declaration emitter into declaration transformer * Slight cleanup from code review feedback * Incorporate fix for new test * Swaths of PR feedback * Merge public methods * Per-file output * Preserve input import ordering more often * Unify jsdoc comment start detection under more lenient rule * Move to per-file transformations to reduce the memory that msut be retained * Fix typo
1 parent 162a273 commit 19ec83f

File tree

124 files changed

+2656
-2729
lines changed

Some content is hidden

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

124 files changed

+2656
-2729
lines changed

src/compiler/checker.ts

+32-19
Original file line numberDiff line numberDiff line change
@@ -3684,10 +3684,6 @@ namespace ts {
36843684
isExternalModuleAugmentation(node.parent.parent);
36853685
}
36863686

3687-
function literalTypeToString(type: LiteralType) {
3688-
return type.flags & TypeFlags.StringLiteral ? '"' + escapeString((<StringLiteralType>type).value) + '"' : "" + (<NumberLiteralType>type).value;
3689-
}
3690-
36913687
interface NodeBuilderContext {
36923688
enclosingDeclaration: Node | undefined;
36933689
flags: NodeBuilderFlags | undefined;
@@ -3748,7 +3744,7 @@ namespace ts {
37483744
return symbolName(symbol);
37493745
}
37503746

3751-
function isDeclarationVisible(node: Declaration): boolean {
3747+
function isDeclarationVisible(node: Declaration | AnyImportSyntax): boolean {
37523748
if (node) {
37533749
const links = getNodeLinks(node);
37543750
if (links.isVisible === undefined) {
@@ -25497,6 +25493,7 @@ namespace ts {
2549725493

2549825494
function isImplementationOfOverload(node: SignatureDeclaration) {
2549925495
if (nodeIsPresent((node as FunctionLikeDeclaration).body)) {
25496+
if (isGetAccessor(node) || isSetAccessor(node)) return false; // Get or set accessors can never be overload implementations, but can have up to 2 signatures
2550025497
const symbol = getSymbolOfNode(node);
2550125498
const signaturesOfSymbol = getSignaturesOfSymbol(symbol);
2550225499
// If this function body corresponds to function with multiple signature, it is implementation of overload
@@ -25636,30 +25633,42 @@ namespace ts {
2563625633
}
2563725634
}
2563825635

25639-
function writeTypeOfDeclaration(declaration: AccessorDeclaration | VariableLikeDeclaration, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: EmitTextWriter) {
25636+
function createTypeOfDeclaration(declaration: AccessorDeclaration | VariableLikeDeclaration, enclosingDeclaration: Node, flags: NodeBuilderFlags, tracker: SymbolTracker, addUndefined?: boolean) {
25637+
declaration = getParseTreeNode(declaration, isVariableLikeOrAccessor);
25638+
if (!declaration) {
25639+
return createToken(SyntaxKind.AnyKeyword) as KeywordTypeNode;
25640+
}
2564025641
// Get type of the symbol if this is the valid symbol otherwise get type at location
2564125642
const symbol = getSymbolOfNode(declaration);
2564225643
let type = symbol && !(symbol.flags & (SymbolFlags.TypeLiteral | SymbolFlags.Signature))
2564325644
? getWidenedLiteralType(getTypeOfSymbol(symbol))
2564425645
: unknownType;
2564525646
if (type.flags & TypeFlags.UniqueESSymbol &&
2564625647
type.symbol === symbol) {
25647-
flags |= TypeFormatFlags.AllowUniqueESSymbolType;
25648+
flags |= NodeBuilderFlags.AllowUniqueESSymbolType;
2564825649
}
25649-
if (flags & TypeFormatFlags.AddUndefined) {
25650+
if (addUndefined) {
2565025651
type = getOptionalType(type);
2565125652
}
25652-
typeToString(type, enclosingDeclaration, flags | TypeFormatFlags.MultilineObjectLiterals, writer);
25653+
return nodeBuilder.typeToTypeNode(type, enclosingDeclaration, flags | NodeBuilderFlags.MultilineObjectLiterals, tracker);
2565325654
}
2565425655

25655-
function writeReturnTypeOfSignatureDeclaration(signatureDeclaration: SignatureDeclaration, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: EmitTextWriter) {
25656+
function createReturnTypeOfSignatureDeclaration(signatureDeclaration: SignatureDeclaration, enclosingDeclaration: Node, flags: NodeBuilderFlags, tracker: SymbolTracker) {
25657+
signatureDeclaration = getParseTreeNode(signatureDeclaration, isFunctionLike);
25658+
if (!signatureDeclaration) {
25659+
return createToken(SyntaxKind.AnyKeyword) as KeywordTypeNode;
25660+
}
2565625661
const signature = getSignatureFromDeclaration(signatureDeclaration);
25657-
typeToString(getReturnTypeOfSignature(signature), enclosingDeclaration, flags | TypeFormatFlags.MultilineObjectLiterals, writer);
25662+
return nodeBuilder.typeToTypeNode(getReturnTypeOfSignature(signature), enclosingDeclaration, flags | NodeBuilderFlags.MultilineObjectLiterals, tracker);
2565825663
}
2565925664

25660-
function writeTypeOfExpression(expr: Expression, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: EmitTextWriter) {
25665+
function createTypeOfExpression(expr: Expression, enclosingDeclaration: Node, flags: NodeBuilderFlags, tracker: SymbolTracker) {
25666+
expr = getParseTreeNode(expr, isExpression);
25667+
if (!expr) {
25668+
return createToken(SyntaxKind.AnyKeyword) as KeywordTypeNode;
25669+
}
2566125670
const type = getWidenedType(getRegularTypeOfExpression(expr));
25662-
typeToString(type, enclosingDeclaration, flags | TypeFormatFlags.MultilineObjectLiterals, writer);
25671+
return nodeBuilder.typeToTypeNode(type, enclosingDeclaration, flags | NodeBuilderFlags.MultilineObjectLiterals, tracker);
2566325672
}
2566425673

2566525674
function hasGlobalName(name: string): boolean {
@@ -25707,9 +25716,13 @@ namespace ts {
2570725716
return false;
2570825717
}
2570925718

25710-
function writeLiteralConstValue(node: VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration, writer: EmitTextWriter) {
25719+
function literalTypeToNode(type: LiteralType): Expression {
25720+
return createLiteral(type.value);
25721+
}
25722+
25723+
function createLiteralConstValue(node: VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration) {
2571125724
const type = getTypeOfSymbol(getSymbolOfNode(node));
25712-
writer.writeStringLiteral(literalTypeToString(<LiteralType>type));
25725+
return literalTypeToNode(<LiteralType>type);
2571325726
}
2571425727

2571525728
function createResolver(): EmitResolver {
@@ -25753,9 +25766,10 @@ namespace ts {
2575325766
isImplementationOfOverload,
2575425767
isRequiredInitializedParameter,
2575525768
isOptionalUninitializedParameterProperty,
25756-
writeTypeOfDeclaration,
25757-
writeReturnTypeOfSignatureDeclaration,
25758-
writeTypeOfExpression,
25769+
createTypeOfDeclaration,
25770+
createReturnTypeOfSignatureDeclaration,
25771+
createTypeOfExpression,
25772+
createLiteralConstValue,
2575925773
isSymbolAccessible,
2576025774
isEntityNameVisible,
2576125775
getConstantValue: node => {
@@ -25777,7 +25791,6 @@ namespace ts {
2577725791
const symbol = node && getSymbolOfNode(node);
2577825792
return !!(symbol && getCheckFlags(symbol) & CheckFlags.Late);
2577925793
},
25780-
writeLiteralConstValue,
2578125794
getJsxFactoryEntity: location => location ? (getJsxNamespace(location), (getSourceFileOfNode(location).localJsxFactory || _jsxFactoryEntity)) : _jsxFactoryEntity
2578225795
};
2578325796

src/compiler/comments.ts

+10
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,15 @@ namespace ts {
260260
}
261261
}
262262

263+
function shouldWriteComment(text: string, pos: number) {
264+
if (printerOptions.onlyPrintJsDocStyle) {
265+
return (isJSDocLikeText(text, pos) || isPinnedComment(text, pos));
266+
}
267+
return true;
268+
}
269+
263270
function emitLeadingComment(commentPos: number, commentEnd: number, kind: SyntaxKind, hasTrailingNewLine: boolean, rangePos: number) {
271+
if (!shouldWriteComment(currentText, commentPos)) return;
264272
if (!hasWrittenComment) {
265273
emitNewLineBeforeLeadingCommentOfPosition(currentLineMap, writer, rangePos, commentPos);
266274
hasWrittenComment = true;
@@ -292,6 +300,7 @@ namespace ts {
292300
}
293301

294302
function emitTrailingComment(commentPos: number, commentEnd: number, _kind: SyntaxKind, hasTrailingNewLine: boolean) {
303+
if (!shouldWriteComment(currentText, commentPos)) return;
295304
// trailing comments are emitted at space/*trailing comment1 */space/*trailing comment2*/
296305
if (!writer.isAtStartOfLine()) {
297306
writer.write(" ");
@@ -404,6 +413,7 @@ namespace ts {
404413
}
405414

406415
function writeComment(text: string, lineMap: number[], writer: EmitTextWriter, commentPos: number, commentEnd: number, newLine: string) {
416+
if (!shouldWriteComment(currentText, commentPos)) return;
407417
if (emitPos) emitPos(commentPos);
408418
writeCommentRange(text, lineMap, writer, commentPos, commentEnd, newLine);
409419
if (emitPos) emitPos(commentEnd);

0 commit comments

Comments
 (0)