Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/oxlint/src-js/plugins/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const LINE_BREAK_PATTERN = /\r\n|[\r\n\u2028\u2029]/gu;
// Lazily populated when `SOURCE_CODE.lines` is accessed.
// `lineStartIndices` starts as `[0]`, and `resetLines` doesn't remove that initial element, so it's never empty.
export const lines: string[] = [];
const lineStartIndices: number[] = [0];
export const lineStartIndices: number[] = [0];

/**
* Split source text into lines.
Expand Down
10 changes: 10 additions & 0 deletions apps/oxlint/src-js/plugins/source_code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
getOffsetFromLineColumn,
initLines,
lines,
lineStartIndices,
resetLines,
} from "./location.ts";
import { resetScopeManager, SCOPE_MANAGER } from "./scope.ts";
Expand Down Expand Up @@ -180,6 +181,15 @@ export const SOURCE_CODE = Object.freeze({
return lines;
},

/**
* Character offset of the first character of each line in source text,
* split according to specification's definition of line breaks.
*/
get lineStartIndices(): number[] {
if (lines.length === 0) initLines();
return lineStartIndices;
},

/**
* Array of all tokens and comments in the file, in source order.
*/
Expand Down
4 changes: 4 additions & 0 deletions apps/oxlint/test/fixtures/sourceCode/output.snap.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
| text: "let foo, bar;\n\n// x\n// y\n"
| getText(): "let foo, bar;\n\n// x\n// y\n"
| lines: ["let foo, bar;","","// x","// y",""]
| lineStartIndices: [0,14,15,20,25]
| locs:
| 0 => { line: 1, column: 0 }("l")
| 1 => { line: 1, column: 1 }("e")
Expand Down Expand Up @@ -55,6 +56,7 @@
| text: "let foo, bar;\n\n// x\n// y\n"
| getText(): "let foo, bar;\n\n// x\n// y\n"
| lines: ["let foo, bar;","","// x","// y",""]
| lineStartIndices: [0,14,15,20,25]
| locs:
| 0 => { line: 1, column: 0 }("l")
| 1 => { line: 1, column: 1 }("e")
Expand Down Expand Up @@ -163,6 +165,7 @@
| text: "let qux;\n"
| getText(): "let qux;\n"
| lines: ["let qux;",""]
| lineStartIndices: [0,9]
| locs:
| 0 => { line: 1, column: 0 }("l")
| 1 => { line: 1, column: 1 }("e")
Expand Down Expand Up @@ -193,6 +196,7 @@
| text: "let qux;\n"
| getText(): "let qux;\n"
| lines: ["let qux;",""]
| lineStartIndices: [0,9]
| locs:
| 0 => { line: 1, column: 0 }("l")
| 1 => { line: 1, column: 1 }("e")
Expand Down
14 changes: 11 additions & 3 deletions apps/oxlint/test/fixtures/sourceCode/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ const SPAN: Node = {

const createRule: Rule = {
create(context) {
const { sourceCode } = context,
{ ast, lines, text } = sourceCode;
const { sourceCode } = context;

// Get these first to check they work before `sourceText` or `ast` are accessed
const { lineStartIndices, lines } = sourceCode;
const { ast, text } = sourceCode;

assert(context.getSourceCode() === sourceCode);

Expand All @@ -36,6 +39,7 @@ const createRule: Rule = {
`text: ${JSON.stringify(text)}\n` +
`getText(): ${JSON.stringify(sourceCode.getText())}\n` +
`lines: ${JSON.stringify(lines)}\n` +
`lineStartIndices: ${JSON.stringify(lineStartIndices)}\n` +
`locs:${locs}\n` +
// @ts-ignore
`ast: "${ast.body[0].declarations[0].id.name}"\n` +
Expand Down Expand Up @@ -83,8 +87,11 @@ const createOnceRule: Rule = {
return {
before() {
const { sourceCode } = context;

// Get these first to check they work before `sourceText` or `ast` are accessed
const { lineStartIndices, lines } = sourceCode;
ast = sourceCode.ast;
const { lines, text } = sourceCode;
const { text } = sourceCode;

let locs = "";
for (let offset = 0; offset <= text.length; offset++) {
Expand All @@ -101,6 +108,7 @@ const createOnceRule: Rule = {
`text: ${JSON.stringify(text)}\n` +
`getText(): ${JSON.stringify(sourceCode.getText())}\n` +
`lines: ${JSON.stringify(lines)}\n` +
`lineStartIndices: ${JSON.stringify(lineStartIndices)}\n` +
`locs:${locs}\n` +
// @ts-ignore
`ast: "${ast.body[0].declarations[0].id.name}"\n` +
Expand Down
Loading