Skip to content

Commit 4520695

Browse files
committed
refactor(linter/plugins): reorganise SourceCode methods (#14773)
Pure refactor. Tidy up and reorder code in `source_code.js`, for clarity.
1 parent 6942d75 commit 4520695

File tree

2 files changed

+38
-34
lines changed

2 files changed

+38
-34
lines changed

apps/oxlint/src-js/plugins/location.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* `SourceCode` methods related to `LineColumn`.
3+
* Functions for converting between `LineColumn` and offsets, and splitting source text into lines.
4+
*/
5+
16
import { initSourceText, sourceText } from './source_code.js';
27

38
import type { LineColumn, Location, Node } from './types.ts';

apps/oxlint/src-js/plugins/source_code.ts

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,25 @@ export const SOURCE_CODE = Object.freeze({
158158
return sourceText.slice(start, end);
159159
},
160160

161+
/**
162+
* Get all the ancestors of a given node.
163+
* @param node - AST node
164+
* @returns All the ancestor nodes in the AST, not including the provided node,
165+
* starting from the root node at index 0 and going inwards to the parent node.
166+
*/
167+
getAncestors(node: Node): Node[] {
168+
const ancestors = [];
169+
170+
while (true) {
171+
// @ts-expect-error `parent` property should be present on `Node` type
172+
node = node.parent;
173+
if (node === null) break;
174+
ancestors.push(node);
175+
}
176+
177+
return ancestors.reverse();
178+
},
179+
161180
/**
162181
* Determine if two nodes or tokens have at least one whitespace character between them.
163182
* Order does not matter. Returns `false` if the given nodes or tokens overlap.
@@ -171,6 +190,20 @@ export const SOURCE_CODE = Object.freeze({
171190
throw new Error('`sourceCode.isSpaceBetween` not implemented yet'); // TODO
172191
},
173192

193+
/**
194+
* Get the deepest node containing a range index.
195+
* @param index Range index of the desired node.
196+
* @returns The node if found, or `null` if not found.
197+
*/
198+
// oxlint-disable-next-line no-unused-vars
199+
getNodeByRangeIndex(index: number): Node | null {
200+
throw new Error('`sourceCode.getNodeByRangeIndex` not implemented yet'); // TODO
201+
},
202+
203+
// Location methods
204+
getLocFromIndex: getLineColumnFromOffset,
205+
getIndexFromLoc: getOffsetFromLineColumn,
206+
174207
// Comment methods
175208
getAllComments: commentMethods.getAllComments,
176209
getCommentsBefore: commentMethods.getCommentsBefore,
@@ -200,40 +233,6 @@ export const SOURCE_CODE = Object.freeze({
200233
getLastTokenBetween: tokenMethods.getLastTokenBetween,
201234
getLastTokensBetween: tokenMethods.getLastTokensBetween,
202235
getTokenByRangeStart: tokenMethods.getTokenByRangeStart,
203-
204-
/**
205-
* Get the deepest node containing a range index.
206-
* @param index Range index of the desired node.
207-
* @returns The node if found, or `null` if not found.
208-
*/
209-
// oxlint-disable-next-line no-unused-vars
210-
getNodeByRangeIndex(index: number): Node | null {
211-
throw new Error('`sourceCode.getNodeByRangeIndex` not implemented yet'); // TODO
212-
},
213-
214-
getLocFromIndex: getLineColumnFromOffset,
215-
getIndexFromLoc: getOffsetFromLineColumn,
216-
217-
getAncestors,
218236
});
219237

220238
export type SourceCode = typeof SOURCE_CODE;
221-
222-
/**
223-
* Get all the ancestors of a given node.
224-
* @param node - AST node
225-
* @returns All the ancestor nodes in the AST, not including the provided node,
226-
* starting from the root node at index 0 and going inwards to the parent node.
227-
*/
228-
function getAncestors(node: Node): Node[] {
229-
const ancestors = [];
230-
231-
while (true) {
232-
// @ts-expect-error `parent` property should be present on `Node` type
233-
node = node.parent;
234-
if (node === null) break;
235-
ancestors.push(node);
236-
}
237-
238-
return ancestors.reverse();
239-
}

0 commit comments

Comments
 (0)