-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,6 +95,14 @@ export class SourceFile extends SourceFileBase<ts.SourceFile> { | |
return FileUtils.getDirPath(this.compilerNode.fileName); | ||
} | ||
|
||
/** | ||
* Gets the line number of the provided position. | ||
* @param pos - Position | ||
*/ | ||
getLineNumberFromPos(pos: number) { | ||
return StringUtils.getLineNumberFromPos(this.getFullText(), pos); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
dsherret
Author
Owner
|
||
} | ||
|
||
/** | ||
* Copy this source file to a new file. | ||
* @param filePath - A new file path. Can be relative to the original file or an absolute path. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import {expect} from "chai"; | ||
import * as errors from "./../../errors"; | ||
import {StringUtils} from "./../../utils"; | ||
|
||
describe(nameof(StringUtils), () => { | ||
describe(nameof(StringUtils.getLineNumberFromPos), () => { | ||
it("should throw if providing a negative pos", () => { | ||
expect(() => StringUtils.getLineNumberFromPos("", -1)).to.throw(errors.ArgumentOutOfRangeError); | ||
}); | ||
|
||
it("should not throw if providing a pos the length of the string", () => { | ||
expect(() => StringUtils.getLineNumberFromPos("", 1)).to.not.throw(); | ||
}); | ||
|
||
it("should throw if providing a pos greater than the length + 1", () => { | ||
expect(() => StringUtils.getLineNumberFromPos("", 2)).to.throw(errors.ArgumentOutOfRangeError); | ||
}); | ||
|
||
function doTest(newLineType: string) { | ||
let str = `testing${newLineType}this${newLineType}out`; | ||
const pos = str.length; | ||
str += `${newLineType}more and more${newLineType}and more`; | ||
expect(StringUtils.getLineNumberFromPos(str, pos)).to.equal(3); | ||
} | ||
|
||
it("should get the line position for the specified pos when using \r newlines", () => { | ||
doTest("\r"); | ||
}); | ||
|
||
it("should get the line position for the specified pos when using \n newlines", () => { | ||
doTest("\n"); | ||
}); | ||
|
||
it("should get the line position for the specified pos when using \r\n newlines", () => { | ||
doTest("\r\n"); | ||
}); | ||
|
||
it("should get the line position for the specified pos when right after the newline when mixing newlines", () => { | ||
let str = "testing\r\nthis\nout\rmore\r\nandmore\n"; | ||
const pos = str.length; | ||
str += "out\r\nmore and more"; | ||
expect(StringUtils.getLineNumberFromPos(str, pos)).to.equal(6); | ||
}); | ||
}); | ||
}); |
Why using in-home implementation StringUtils.getLineNumberFromPos() while we have native
ts.getLineAndCharacterOfPosition and ts.getPositionOfLineAndCharacter
? is it because compiler node and simple-ast nodes might not be in sync? I want to add missing getPositionOfLineAndCharacter equivalent in SourceFile and I was planning to use the native method that's why I'm asking. Thanks