Skip to content

Commit

Permalink
feat: Node .getStart & .getStartLinePos - add includeJsDocComment.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jan 14, 2018
1 parent b6917c7 commit af8bb55
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
11 changes: 7 additions & 4 deletions src/compiler/common/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,11 @@ export class Node<NodeType extends ts.Node = ts.Node> {

/**
* Gets the start position without leading trivia.
* @param includeJsDocComment - Whether to include the JS doc comment.
*/
getStart() {
return this.compilerNode.getStart(this.sourceFile.compilerNode);
getStart(includeJsDocComment?: boolean) {
// rare time a bool parameter will be used... it's because it's done in the ts compiler
return this.compilerNode.getStart(this.sourceFile.compilerNode, includeJsDocComment);
}

/**
Expand Down Expand Up @@ -715,10 +717,11 @@ export class Node<NodeType extends ts.Node = ts.Node> {

/**
* Gets the position of the start of the line that this node starts on.
* @param includeJsDocComment - Whether to include the JS doc comment or not.
*/
getStartLinePos() {
getStartLinePos(includeJsDocComment?: boolean) {
const sourceFileText = this.sourceFile.getFullText();
return getPreviousMatchingPos(sourceFileText, this.getStart(), char => char === "\n");
return getPreviousMatchingPos(sourceFileText, this.getStart(includeJsDocComment), char => char === "\n");
}

/**
Expand Down
34 changes: 26 additions & 8 deletions src/tests/compiler/common/nodeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,17 @@ describe(nameof(Node), () => {
});

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

it("should return the start of the file when it's on the first line", () => {
const {firstChild} = getInfoFromText("enum MyEnum {\n}\n");
expect(firstChild.getStartLinePos()).to.equal(0);
doTest("enum MyEnum {\n}\n", 0);
});

it("should return the start of the file when it's on the first line", () => {
const {firstChild} = getInfoFromText(" enum MyEnum {\n}\n");
expect(firstChild.getStartLinePos()).to.equal(0);
doTest(" enum MyEnum {\n}\n", 0);
});

it("should return the start of the line when it's not on the first line", () => {
Expand All @@ -300,15 +303,30 @@ describe(nameof(Node), () => {
});

it("should return the start of the line when the past line is a \\r\\n", () => {
const {firstChild} = getInfoFromText("\n \t \r\nenum MyEnum {\n}\n");
expect(firstChild.getStartLinePos()).to.equal(8);
doTest("\n \t \r\nenum MyEnum {\n}\n", 8);
});

it("should get the start line position of the JS doc when specified", () => {
doTest("/**\n * Test\n */\nenum MyEnum {\n}\n", 0, true);
});

it("should get the start line position of not the JS doc when not specified", () => {
doTest("/**\n * Test\n */\nenum MyEnum {\n}\n", 16);
});
});

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

it("should return the pos without trivia", () => {
const {firstChild} = getInfoFromText("\n \t /* comment */ //comment \r\n \t enum MyEnum {\n}\n");
expect(firstChild.getStart()).to.equal(37);
doTest("\n \t /* comment */ //comment \r\n \t enum MyEnum {\n}\n", 37);
});

it("should return the pos at the start of the js doc comment if specified", () => {
doTest("/**\n * Test\n */\nenum MyEnum {\n}\n", 0, true);
});
});

Expand Down

0 comments on commit af8bb55

Please sign in to comment.