Skip to content

Commit

Permalink
feat: Add Node.getEndLineNumber()
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Feb 4, 2018
1 parent f4ba433 commit f00ca4c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/compiler/common/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,16 @@ export class Node<NodeType extends ts.Node = ts.Node> {
* @param includeJsDocComment - Whether to include the JS doc comment or not.
*/
getStartLineNumber(includeJsDocComment?: boolean) {
return this.getSourceFile().getLineNumberFromPos(this.getStartLinePos(includeJsDocComment));
return this.sourceFile.getLineNumberFromPos(this.getStartLinePos(includeJsDocComment));
}

/**
* Gets the line number of the end of the node.
*/
getEndLineNumber() {
const sourceFileText = this.sourceFile.getFullText();
const endLinePos = getPreviousMatchingPos(sourceFileText, this.getEnd(), char => char === "\n");
return this.sourceFile.getLineNumberFromPos(endLinePos);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/tests/compiler/common/nodeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,13 @@ describe(nameof(Node), () => {
});
});

describe(nameof<Node>(n => n.getEndLineNumber), () => {
it("should get the end line number of the node", () => {
const {firstChild} = getInfoFromText<ClassDeclaration>("\n\nclass MyClass {\n\n prop: string;\n}");
expect(firstChild.getEndLineNumber()).to.equal(6);
});
});

describe(nameof<Node>(n => n.getStart), () => {
function doTest(text: string, expectedPos: number, includeJsDocComment?: boolean) {
const {firstChild} = getInfoFromText(text);
Expand Down

0 comments on commit f00ca4c

Please sign in to comment.