mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-04-13 09:12:52 -05:00
Base method and namespace cleanup
This commit is contained in:
@@ -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 };
|
||||
|
||||
@@ -159,10 +159,44 @@ export abstract class BaseSyntaxObject implements Node {
|
||||
return this.getChildren(sourceFile)[index];
|
||||
}
|
||||
|
||||
abstract forEachChild<T>(cbNode: (node: Node) => T, cbNodeArray?: (nodes: NodeArray<Node>) => T): T | undefined;
|
||||
abstract getChildren(sourceFile?: SourceFileLike): readonly Node[];
|
||||
abstract getFirstToken(sourceFile?: SourceFileLike): Node | undefined;
|
||||
abstract getLastToken(sourceFile?: SourceFileLike): Node | undefined;
|
||||
forEachChild<T>(cbNode: (node: Node) => T, cbNodeArray?: (nodes: NodeArray<Node>) => 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<TKind extends SyntaxKind> extends BaseTokenObject implements Token<TKind> {
|
||||
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<T>(cbNode: (node: Node) => T, cbNodeArray?: (nodes: NodeArray<Node>) => 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<TKind extends SyntaxKind> 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;
|
||||
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user