From bb7688fa19d4904b509c6151a09ec5cdd025f7eb Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Fri, 12 Apr 2024 11:44:01 -0400 Subject: [PATCH] Base method and namespace cleanup --- src/compiler/_namespaces/ts.ts | 4 -- src/compiler/nodeConstructors.ts | 87 ++++++++++++++++---------------- src/services/services.ts | 14 ++--- 3 files changed, 50 insertions(+), 55 deletions(-) diff --git a/src/compiler/_namespaces/ts.ts b/src/compiler/_namespaces/ts.ts index 230138b17e3..06cd67e2081 100644 --- a/src/compiler/_namespaces/ts.ts +++ b/src/compiler/_namespaces/ts.ts @@ -75,7 +75,3 @@ import * as moduleSpecifiers from "./ts.moduleSpecifiers"; export { moduleSpecifiers }; import * as performance from "./ts.performance"; export { performance }; -/** @internal */ import * as NodeConstructors from "../nodeConstructors"; -/** @internal */ export { NodeConstructors }; -/** @internal */ import * as ObjectConstructors from "../objectConstructors"; -/** @internal */ export { ObjectConstructors }; diff --git a/src/compiler/nodeConstructors.ts b/src/compiler/nodeConstructors.ts index 2be61da47cf..0ad254df865 100644 --- a/src/compiler/nodeConstructors.ts +++ b/src/compiler/nodeConstructors.ts @@ -159,10 +159,44 @@ export abstract class BaseSyntaxObject implements Node { return this.getChildren(sourceFile)[index]; } - abstract forEachChild(cbNode: (node: Node) => T, cbNodeArray?: (nodes: NodeArray) => T): T | undefined; - abstract getChildren(sourceFile?: SourceFileLike): readonly Node[]; - abstract getFirstToken(sourceFile?: SourceFileLike): Node | undefined; - abstract getLastToken(sourceFile?: SourceFileLike): Node | undefined; + forEachChild(cbNode: (node: Node) => T, cbNodeArray?: (nodes: NodeArray) => T): T | undefined { + return forEachChild(this, cbNode, cbNodeArray); + } + + getChildren(sourceFile?: SourceFileLike): readonly Node[] { + Debug.assertValidTextRange(this, "Node without a real position cannot be scanned and thus has no token nodes - use forEachChild and collect the result if that's fine"); + return getNodeChildren(this) ?? setNodeChildren(this, createChildren(this, sourceFile)); + } + + getFirstToken(sourceFile?: SourceFileLike): Node | undefined { + Debug.assertValidTextRange(this); + const children = this.getChildren(sourceFile); + if (!children.length) { + return undefined; + } + + const child = find(children, child => child.kind < SyntaxKind.FirstJSDocNode || child.kind > SyntaxKind.LastJSDocNode); + if (!child) { + return undefined; + } + + return child.kind < SyntaxKind.FirstNode ? child : (child as BaseSyntaxObject).getFirstToken(sourceFile); + } + + getLastToken(sourceFile?: SourceFileLike): Node | undefined { + Debug.assertValidTextRange(this); + const children = this.getChildren(sourceFile); + if (!children.length) { + return undefined; + } + + const child = lastOrUndefined(children); + if (!child) { + return undefined; + } + + return child.kind < SyntaxKind.FirstNode ? child : (child as BaseSyntaxObject).getLastToken(sourceFile); + } } /** @internal */ @@ -196,6 +230,7 @@ export abstract class BaseTokenObject extends BaseSyntaxObject { /** @internal */ export class TokenObject extends BaseTokenObject implements Token { declare kind: TKind; + constructor(kind: TKind) { super(kind); } @@ -263,45 +298,6 @@ export abstract class BaseNodeObject extends BaseSyntaxObject { override emitNode: EmitNode | undefined = undefined; // NOTE: Non-token nodes may have modifiers, so they are defined to reduce polymorphism override modifierFlagsCache: ModifierFlags = ModifierFlags.None; // TODO: move this off `Node` - - override forEachChild(cbNode: (node: Node) => T, cbNodeArray?: (nodes: NodeArray) => T): T | undefined { - return forEachChild(this, cbNode, cbNodeArray); - } - - override getChildren(sourceFile?: SourceFileLike): readonly Node[] { - Debug.assertValidTextRange(this, "Node without a real position cannot be scanned and thus has no token nodes - use forEachChild and collect the result if that's fine"); - return getNodeChildren(this) ?? setNodeChildren(this, createChildren(this, sourceFile)); - } - - override getFirstToken(sourceFile?: SourceFileLike): Node | undefined { - Debug.assertValidTextRange(this); - const children = this.getChildren(sourceFile); - if (!children.length) { - return undefined; - } - - const child = find(children, child => child.kind < SyntaxKind.FirstJSDocNode || child.kind > SyntaxKind.LastJSDocNode); - if (!child) { - return undefined; - } - - return child.kind < SyntaxKind.FirstNode ? child : (child as BaseSyntaxObject).getFirstToken(sourceFile); - } - - override getLastToken(sourceFile?: SourceFileLike): Node | undefined { - Debug.assertValidTextRange(this); - const children = this.getChildren(sourceFile); - if (!children.length) { - return undefined; - } - - const child = lastOrUndefined(children); - if (!child) { - return undefined; - } - - return child.kind < SyntaxKind.FirstNode ? child : (child as BaseSyntaxObject).getLastToken(sourceFile); - } } /** @internal */ @@ -314,9 +310,12 @@ export class NodeObject extends BaseNodeObject impleme } /** @internal */ -export class SourceFileObject extends BaseNodeObject implements SourceFile { +export class SourceFileObject extends BaseSyntaxObject implements SourceFile { declare kind: SyntaxKind.SourceFile; + // NOTE: Non-token nodes often need emitNode entries, so they are defined to reduce polymorphism. + override emitNode: EmitNode | undefined = undefined; + declare _declarationBrand: any; declare _localsContainerBrand: any; diff --git a/src/services/services.ts b/src/services/services.ts index 9f9340fab53..0ec25031b2f 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1,3 +1,4 @@ +import { SignatureObject, SymbolObject } from "../compiler/objectConstructors"; import { __String, ApplicableRefactorInfo, @@ -182,7 +183,6 @@ import { noop, normalizePath, NumericLiteral, - ObjectConstructors, ObjectLiteralElement, ObjectLiteralExpression, OperationCanceledException, @@ -287,7 +287,7 @@ function ensureSymbolExtraFields(symbol: Symbol) { return extra; } -ObjectConstructors.SymbolObject.prototype.getDocumentationComment = function (this: Symbol | TransientSymbol, checker: TypeChecker | undefined): SymbolDisplayPart[] { +SymbolObject.prototype.getDocumentationComment = function (this: Symbol | TransientSymbol, checker: TypeChecker | undefined): SymbolDisplayPart[] { const extra = ensureSymbolExtraFields(this); if (!extra.documentationComment) { extra.documentationComment = emptyArray; // Set temporarily to avoid an infinite loop finding inherited docs @@ -303,7 +303,7 @@ ObjectConstructors.SymbolObject.prototype.getDocumentationComment = function (th return extra.documentationComment; }; -ObjectConstructors.SymbolObject.prototype.getContextualDocumentationComment = function (this: Symbol, context: Node | undefined, checker: TypeChecker | undefined): SymbolDisplayPart[] { +SymbolObject.prototype.getContextualDocumentationComment = function (this: Symbol, context: Node | undefined, checker: TypeChecker | undefined): SymbolDisplayPart[] { if (context) { const extra = ensureSymbolExtraFields(this); if (isGetAccessor(context)) { @@ -322,12 +322,12 @@ ObjectConstructors.SymbolObject.prototype.getContextualDocumentationComment = fu return this.getDocumentationComment(checker); }; -ObjectConstructors.SymbolObject.prototype.getJsDocTags = function (this: Symbol, checker?: TypeChecker): JSDocTagInfo[] { +SymbolObject.prototype.getJsDocTags = function (this: Symbol, checker?: TypeChecker): JSDocTagInfo[] { const extra = ensureSymbolExtraFields(this); return extra.tags ??= getJsDocTagsOfDeclarations(this.declarations, checker); }; -ObjectConstructors.SymbolObject.prototype.getContextualJsDocTags = function (this: Symbol, context: Node | undefined, checker: TypeChecker | undefined): JSDocTagInfo[] { +SymbolObject.prototype.getContextualJsDocTags = function (this: Symbol, context: Node | undefined, checker: TypeChecker | undefined): JSDocTagInfo[] { if (context) { const extra = ensureSymbolExtraFields(this); if (isGetAccessor(context)) { @@ -361,12 +361,12 @@ function ensureSignatureExtraFields(signature: Signature) { return extra; } -ObjectConstructors.SignatureObject.prototype.getDocumentationComment = function (this: Signature): SymbolDisplayPart[] { +SignatureObject.prototype.getDocumentationComment = function (this: Signature): SymbolDisplayPart[] { const extra = ensureSignatureExtraFields(this); return extra.documentationComment ??= getDocumentationComment(singleElementArray(this.declaration), this.checker); }; -ObjectConstructors.SignatureObject.prototype.getJsDocTags = function (this: Signature): JSDocTagInfo[] { +SignatureObject.prototype.getJsDocTags = function (this: Signature): JSDocTagInfo[] { const extra = ensureSignatureExtraFields(this); return extra.jsDocTags ??= getJsDocTagsOfDeclarations(singleElementArray(this.declaration), this.checker); };