Skip to content

Commit

Permalink
feat: Add Node#getWidth(true) for including js docs in the width (s…
Browse files Browse the repository at this point in the history
…imilar to `Node#getStart(true)` in the compiler api).
  • Loading branch information
dsherret committed Oct 17, 2019
1 parent e798d95 commit 03dda0b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/compiler/ast/common/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -965,9 +965,12 @@ export class Node<NodeType extends ts.Node = ts.Node> {

/**
* Gets the text length of the node without trivia.
* @param includeJsDocComments - Whether to include the JS doc comments in the width or not.
*/
getWidth() {
return this.compilerNode.getWidth(this._sourceFile.compilerNode);
getWidth(includeJsDocComments?: boolean) {
// Compiler code is this.getEnd() - this.getStart(sourceFile), but this
// takes into account js doc comments as well.
return this.getEnd() - this.getStart(includeJsDocComments);
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/tests/compiler/ast/common/nodeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,22 @@ class MyClass {
});
});

describe(nameof<Node>(n => n.getWidth), () => {
function doTest(text: string, expectedWidth: number, includeJsDocComment?: boolean) {
const { firstChild } = getInfoFromText(text);
expect(firstChild.getWidth(includeJsDocComment)).to.equal(expectedWidth);
}

it("should return the width without trivia", () => {
doTest("\n \t /* comment */ //comment \r\n \t enum MyEnum {\n}\n", 15);
});

it("should return the width at the start of the js doc comment if specified", () => {
const text = "/**\n * Test\n */\nenum MyEnum {\n}\n";
doTest(text, text.length - 1, true);
});
});

describe(nameof<Node>(n => n.getCombinedModifierFlags), () => {
const { firstChild } = getInfoFromText<ClassDeclaration>("export class Identifier {}");
it("should get the combined modifier flags", () => {
Expand Down

0 comments on commit 03dda0b

Please sign in to comment.