Skip to content

Commit

Permalink
feat: #355 - Add getLengthFromLineStartAtPos to replace `getColumnA…
Browse files Browse the repository at this point in the history
…tPos` in next major.
  • Loading branch information
dsherret committed Jul 4, 2018
1 parent 80896bb commit e82cdff
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/compiler/common/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -950,13 +950,15 @@ export class Node<NodeType extends ts.Node = ts.Node> {
/**
* Gets the length from the start of the line to the start of the node.
* @param includeJsDocComment - Whether to include the JS doc comment or not.
* @deprecated - Use `sourceFile.getLengthFromLineStartAtPos(node.getStart())`
*/
getStartColumn(includeJsDocComment?: boolean) {
return this.sourceFile.getColumnAtPos(this.getStart(includeJsDocComment));
}

/**
* Gets the length from the start of the line to the end of the node.
* @deprecated - Use `sourceFile.getLengthFromLineStartAtPos(node.getEnd())`
*/
getEndColumn() {
return this.sourceFile.getColumnAtPos(this.getEnd());
Expand Down
11 changes: 10 additions & 1 deletion src/compiler/file/SourceFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,18 @@ export class SourceFile extends SourceFileBase<ts.SourceFile> {
/**
* Gets the length from the start of the line to the provided position.
* @param pos - Position.
* @deprecated - Use `getLengthFromLineStartAtPos`
*/
getColumnAtPos(pos: number) {
return StringUtils.getColumnAtPos(this.getFullText(), pos);
return this.getLengthFromLineStartAtPos(pos);
}

/**
* Gets the character count from the start of the line to the provided position.
* @param pos - Position.
*/
getLengthFromLineStartAtPos(pos: number) {
return StringUtils.getLengthFromLineStartAtPos(this.getFullText(), pos);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/next-major-deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

* CompilerApiNodeBrandPropertyNamesType
* getLineNumberFromPos
* getStartColumn
* getEndColumn
* getColumnAtPos
10 changes: 5 additions & 5 deletions src/tests/utils/stringUtilsTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@ describe(nameof(StringUtils), () => {
});
});

describe(nameof(StringUtils.getColumnAtPos), () => {
describe(nameof(StringUtils.getLengthFromLineStartAtPos), () => {
it("should throw if providing a negative pos", () => {
expect(() => StringUtils.getColumnAtPos("", -1)).to.throw(errors.ArgumentOutOfRangeError);
expect(() => StringUtils.getLengthFromLineStartAtPos("", -1)).to.throw(errors.ArgumentOutOfRangeError);
});

it("should not throw if providing a pos the length of the string", () => {
expect(() => StringUtils.getColumnAtPos("", 1)).to.not.throw();
expect(() => StringUtils.getLengthFromLineStartAtPos("", 1)).to.not.throw();
});

it("should throw if providing a pos greater than the length + 1", () => {
expect(() => StringUtils.getColumnAtPos("", 2)).to.throw(errors.ArgumentOutOfRangeError);
expect(() => StringUtils.getLengthFromLineStartAtPos("", 2)).to.throw(errors.ArgumentOutOfRangeError);
});

function doTest(text: string, pos: number, expected: number) {
expect(StringUtils.getColumnAtPos(text, pos)).to.equal(expected);
expect(StringUtils.getLengthFromLineStartAtPos(text, pos)).to.equal(expected);
}

function doNewlineTest(newLineKind: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/StringUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class StringUtils {
return count + 1; // convert count to line number
}

static getColumnAtPos(str: string, pos: number) {
static getLengthFromLineStartAtPos(str: string, pos: number) {
errors.throwIfOutOfRange(pos, [0, str.length + 1], nameof(pos));
const startPos = pos;

Expand Down

0 comments on commit e82cdff

Please sign in to comment.