Skip to content

Commit

Permalink
refactor: Rename CompilerExtendedComment to CompilerCommentNode.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: CompilerExtendedComment is now called CompilerCommentNode.
  • Loading branch information
dsherret committed Aug 31, 2019
1 parent da78437 commit e3db1db
Show file tree
Hide file tree
Showing 7 changed files with 382 additions and 599 deletions.
920 changes: 352 additions & 568 deletions lib/ts-morph.d.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/compiler/ast/CompilerNodeToWrappedType.ts
@@ -1,6 +1,6 @@
import * as compiler from "./index";
import { ts } from "../../typescript";
import { CompilerExtendedComment, CompilerCommentStatement, CompilerCommentClassElement, CompilerCommentObjectLiteralElement, CompilerCommentTypeElement,
import { CompilerCommentNode, CompilerCommentStatement, CompilerCommentClassElement, CompilerCommentObjectLiteralElement, CompilerCommentTypeElement,
CompilerCommentEnumMember } from "./comment";
import { ImplementedKindToNodeMappings } from "./kindToNodeMappings";

Expand Down
12 changes: 6 additions & 6 deletions src/compiler/ast/comment/CompilerComments.ts
Expand Up @@ -9,7 +9,7 @@ export enum CommentNodeKind {
EnumMember
}

export abstract class CompilerExtendedComment implements ts.Node {
export abstract class CompilerCommentNode implements ts.Node {
private _fullStart: number;
private _start: number;
private _sourceFile: ts.SourceFile;
Expand Down Expand Up @@ -105,27 +105,27 @@ export abstract class CompilerExtendedComment implements ts.Node {
}
}

export class CompilerCommentStatement extends CompilerExtendedComment implements ts.Statement {
export class CompilerCommentStatement extends CompilerCommentNode implements ts.Statement {
_statementBrand: any;
/** @internal */
_commentKind = CommentNodeKind.Statement;
}

export class CompilerCommentClassElement extends CompilerExtendedComment implements ts.ClassElement {
export class CompilerCommentClassElement extends CompilerCommentNode implements ts.ClassElement {
_classElementBrand: any;
_declarationBrand: any;
/** @internal */
_commentKind = CommentNodeKind.ClassElement;
}

export class CompilerCommentTypeElement extends CompilerExtendedComment implements ts.TypeElement {
export class CompilerCommentTypeElement extends CompilerCommentNode implements ts.TypeElement {
_typeElementBrand: any;
_declarationBrand: any;
/** @internal */
_commentKind = CommentNodeKind.TypeElement;
}

export class CompilerCommentObjectLiteralElement extends CompilerExtendedComment implements ts.ObjectLiteralElement {
export class CompilerCommentObjectLiteralElement extends CompilerCommentNode implements ts.ObjectLiteralElement {
_declarationBrand: any;
_objectLiteralBrandBrand: any; // ts < 3.4
_objectLiteralBrand: any; // ts >= 3.5
Expand All @@ -134,7 +134,7 @@ export class CompilerCommentObjectLiteralElement extends CompilerExtendedComment
_commentKind = CommentNodeKind.ObjectLiteralElement;
}

export class CompilerCommentEnumMember extends CompilerExtendedComment implements ts.Node {
export class CompilerCommentEnumMember extends CompilerCommentNode implements ts.Node {
/** @internal */
_commentKind = CommentNodeKind.EnumMember;
}
30 changes: 15 additions & 15 deletions src/compiler/ast/utils/CommentNodeParser.ts
@@ -1,7 +1,7 @@
import { ts, SyntaxKind } from "../../../typescript";
import * as errors from "../../../errors";
import { StringUtils, getSyntaxKindName } from "../../../utils";
import { CompilerExtendedComment, CompilerCommentStatement, CompilerCommentClassElement, CompilerCommentTypeElement, CompilerCommentObjectLiteralElement,
import { CompilerCommentNode, CompilerCommentStatement, CompilerCommentClassElement, CompilerCommentTypeElement, CompilerCommentObjectLiteralElement,
CompilerCommentEnumMember, CommentNodeKind } from "../comment/CompilerComments";

enum CommentKind {
Expand All @@ -25,8 +25,8 @@ export type ContainerNodes = StatementContainerNodes
| ts.ObjectLiteralExpression;

type CommentSyntaxKinds = SyntaxKind.SingleLineCommentTrivia | SyntaxKind.MultiLineCommentTrivia;
const childrenSaver = new WeakMap<ContainerNodes, (ts.Node | CompilerExtendedComment)[]>();
const extendedCommentParserKinds = new Set<SyntaxKind>([
const childrenSaver = new WeakMap<ContainerNodes, (ts.Node | CompilerCommentNode)[]>();
const commentNodeParserKinds = new Set<SyntaxKind>([
SyntaxKind.SourceFile,
SyntaxKind.Block,
SyntaxKind.ModuleBlock,
Expand Down Expand Up @@ -61,7 +61,7 @@ export class CommentNodeParser {

static shouldParseChildren(container: ts.Node): container is ContainerNodes {
// this needs to be really fast because it's used whenever getting the children, so use a map
return extendedCommentParserKinds.has(container.kind)
return commentNodeParserKinds.has(container.kind)
// Ignore zero length nodes... for some reason this might happen when parsing
// jsx in non-jsx files.
&& container.pos !== container.end;
Expand All @@ -75,23 +75,23 @@ export class CommentNodeParser {
}

static isCommentStatement(node: ts.Node): node is CompilerCommentStatement {
return (node as CompilerExtendedComment)._commentKind === CommentNodeKind.Statement;
return (node as CompilerCommentNode)._commentKind === CommentNodeKind.Statement;
}

static isCommentClassElement(node: ts.Node): node is CompilerCommentClassElement {
return (node as CompilerExtendedComment)._commentKind === CommentNodeKind.ClassElement;
return (node as CompilerCommentNode)._commentKind === CommentNodeKind.ClassElement;
}

static isCommentTypeElement(node: ts.Node): node is CompilerCommentTypeElement {
return (node as CompilerExtendedComment)._commentKind === CommentNodeKind.TypeElement;
return (node as CompilerCommentNode)._commentKind === CommentNodeKind.TypeElement;
}

static isCommentObjectLiteralElement(node: ts.Node): node is CompilerCommentObjectLiteralElement {
return (node as CompilerExtendedComment)._commentKind === CommentNodeKind.ObjectLiteralElement;
return (node as CompilerCommentNode)._commentKind === CommentNodeKind.ObjectLiteralElement;
}

static isCommentEnumMember(node: ts.Node): node is CompilerCommentEnumMember {
return (node as CompilerExtendedComment)._commentKind === CommentNodeKind.EnumMember;
return (node as CompilerCommentNode)._commentKind === CommentNodeKind.EnumMember;
}

static getContainerBodyPos(container: ContainerNodes, sourceFile: ts.SourceFile) {
Expand Down Expand Up @@ -126,27 +126,27 @@ export class CommentNodeParser {
}
}

function* getNodes(container: ContainerNodes, sourceFile: ts.SourceFile): IterableIterator<ts.Node | CompilerExtendedComment> {
function* getNodes(container: ContainerNodes, sourceFile: ts.SourceFile): IterableIterator<ts.Node | CompilerCommentNode> {
const sourceFileText = sourceFile.text;
const childNodes = getContainerChildren();
const createComment = getCreationFunction();

if (childNodes.length === 0) {
const bodyStartPos = CommentNodeParser.getContainerBodyPos(container, sourceFile);
yield* getExtendedComments(bodyStartPos, false); // do not skip js docs because they won't have a node to be attached to
yield* getCommentNodes(bodyStartPos, false); // do not skip js docs because they won't have a node to be attached to
}
else {
for (const childNode of childNodes) {
yield* getExtendedComments(childNode.pos, true);
yield* getCommentNodes(childNode.pos, true);
yield childNode;
}

// get the comments on a newline after the last node
const lastChild = childNodes[childNodes.length - 1];
yield* getExtendedComments(lastChild.end, false); // parse any jsdocs afterwards
yield* getCommentNodes(lastChild.end, false); // parse any jsdocs afterwards
}

function* getExtendedComments(pos: number, stopAtJsDoc: boolean) {
function* getCommentNodes(pos: number, stopAtJsDoc: boolean) {
const fullStart = pos;
skipTrailingLine();

Expand Down Expand Up @@ -287,7 +287,7 @@ function* getNodes(container: ContainerNodes, sourceFile: ts.SourceFile): Iterab
return errors.throwNotImplementedForNeverValueError(container);
}

function getCreationFunction(): (fullStart: number, pos: number, end: number, kind: CommentSyntaxKinds) => CompilerExtendedComment {
function getCreationFunction(): (fullStart: number, pos: number, end: number, kind: CommentSyntaxKinds) => CompilerCommentNode {
const ctor = getCtor();
return (fullStart: number, pos: number, end: number, kind: CommentSyntaxKinds) => new ctor(fullStart, pos, end, kind, sourceFile, container);

Expand Down
6 changes: 3 additions & 3 deletions src/factories/CompilerFactory.ts
@@ -1,6 +1,6 @@
import { CompilerNodeToWrappedType, DefinitionInfo, Diagnostic, DiagnosticMessageChain, DiagnosticWithLocation, DocumentSpan, JSDocTagInfo, Node,
ReferencedSymbol, ReferencedSymbolDefinitionInfo, ReferenceEntry, Signature, SourceFile, Symbol, SymbolDisplayPart, Type, TypeParameter, CommentStatement,
CommentClassElement, CommentTypeElement, CommentObjectLiteralElement, CompilerExtendedComment, CommentEnumMember } from "../compiler";
CommentClassElement, CommentTypeElement, CommentObjectLiteralElement, CompilerCommentNode, CommentEnumMember } from "../compiler";
import { CommentNodeParser } from "../compiler/ast/utils";
import * as errors from "../errors";
import { Directory } from "../fileSystem";
Expand Down Expand Up @@ -294,8 +294,8 @@ export class CompilerFactory {
return new ctor(this.context, compilerNode, sourceFile) as Node<NodeType>;
}

function isCommentNode(node: ts.Node): node is CompilerExtendedComment {
return (node as CompilerExtendedComment)._commentKind != null;
function isCommentNode(node: ts.Node): node is CompilerCommentNode {
return (node as CompilerCommentNode)._commentKind != null;
}

function initializeNode(this: CompilerFactory, node: Node<NodeType>) {
Expand Down
1 change: 0 additions & 1 deletion src/next-major-deprecations.md
@@ -1,6 +1,5 @@
# Deprecations in the next major

* Rename ExtendedComment to CommentNode
* Remove DirectoryEmitResult#getSkippedFilePaths()

# Future version
Expand Down
10 changes: 5 additions & 5 deletions src/tests/compiler/ast/comment/compilerCommentsTests.ts
@@ -1,9 +1,9 @@
import { expect } from "chai";
import { CompilerExtendedComment, CompilerCommentStatement } from "../../../../compiler";
import { CompilerCommentNode, CompilerCommentStatement } from "../../../../compiler";
import { getInfoFromText } from "../../testHelpers";
import { SyntaxKind, ts } from "../../../../typescript";

describe(nameof(CompilerExtendedComment), () => {
describe(nameof(CompilerCommentNode), () => {
interface ExpectedResult {
pos: number;
end: number;
Expand All @@ -20,7 +20,7 @@ describe(nameof(CompilerExtendedComment), () => {
text: string;
}

function runTests(comment: CompilerExtendedComment, expected: ExpectedResult) {
function runTests(comment: CompilerCommentNode, expected: ExpectedResult) {
it("should have the correct pos", () => {
expect(comment.pos).to.equal(expected.pos);
});
Expand Down Expand Up @@ -104,7 +104,7 @@ describe(nameof(CompilerExtendedComment), () => {

describe("single line comment", () => {
const sourceFile = getInfoFromText(" //a ").sourceFile;
const comment = sourceFile.getStatementsWithComments()[0].compilerNode as CompilerCommentStatement as CompilerExtendedComment;
const comment = sourceFile.getStatementsWithComments()[0].compilerNode as CompilerCommentStatement as CompilerCommentNode;

runTests(comment, {
pos: 2,
Expand All @@ -125,7 +125,7 @@ describe(nameof(CompilerExtendedComment), () => {

describe("multi line comment", () => {
const sourceFile = getInfoFromText(" /*1*/ ").sourceFile;
const comment = sourceFile.getStatementsWithComments()[0].compilerNode as CompilerCommentStatement as CompilerExtendedComment;
const comment = sourceFile.getStatementsWithComments()[0].compilerNode as CompilerCommentStatement as CompilerCommentNode;

runTests(comment, {
pos: 2,
Expand Down

0 comments on commit e3db1db

Please sign in to comment.