From 45206951f27763782278e94ecfd017aedc7dee4c Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Sun, 19 Oct 2025 12:16:19 +0000 Subject: [PATCH] refactor(linter/plugins): reorganise `SourceCode` methods (#14773) Pure refactor. Tidy up and reorder code in `source_code.js`, for clarity. --- apps/oxlint/src-js/plugins/location.ts | 5 ++ apps/oxlint/src-js/plugins/source_code.ts | 67 +++++++++++------------ 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/apps/oxlint/src-js/plugins/location.ts b/apps/oxlint/src-js/plugins/location.ts index ff65e6fdb4928..7ca2620414213 100644 --- a/apps/oxlint/src-js/plugins/location.ts +++ b/apps/oxlint/src-js/plugins/location.ts @@ -1,3 +1,8 @@ +/* + * `SourceCode` methods related to `LineColumn`. + * Functions for converting between `LineColumn` and offsets, and splitting source text into lines. + */ + import { initSourceText, sourceText } from './source_code.js'; import type { LineColumn, Location, Node } from './types.ts'; diff --git a/apps/oxlint/src-js/plugins/source_code.ts b/apps/oxlint/src-js/plugins/source_code.ts index 7fd91f0345084..c66bbf397b0d4 100644 --- a/apps/oxlint/src-js/plugins/source_code.ts +++ b/apps/oxlint/src-js/plugins/source_code.ts @@ -158,6 +158,25 @@ export const SOURCE_CODE = Object.freeze({ return sourceText.slice(start, end); }, + /** + * Get all the ancestors of a given node. + * @param node - AST node + * @returns All the ancestor nodes in the AST, not including the provided node, + * starting from the root node at index 0 and going inwards to the parent node. + */ + getAncestors(node: Node): Node[] { + const ancestors = []; + + while (true) { + // @ts-expect-error `parent` property should be present on `Node` type + node = node.parent; + if (node === null) break; + ancestors.push(node); + } + + return ancestors.reverse(); + }, + /** * Determine if two nodes or tokens have at least one whitespace character between them. * Order does not matter. Returns `false` if the given nodes or tokens overlap. @@ -171,6 +190,20 @@ export const SOURCE_CODE = Object.freeze({ throw new Error('`sourceCode.isSpaceBetween` not implemented yet'); // TODO }, + /** + * Get the deepest node containing a range index. + * @param index Range index of the desired node. + * @returns The node if found, or `null` if not found. + */ + // oxlint-disable-next-line no-unused-vars + getNodeByRangeIndex(index: number): Node | null { + throw new Error('`sourceCode.getNodeByRangeIndex` not implemented yet'); // TODO + }, + + // Location methods + getLocFromIndex: getLineColumnFromOffset, + getIndexFromLoc: getOffsetFromLineColumn, + // Comment methods getAllComments: commentMethods.getAllComments, getCommentsBefore: commentMethods.getCommentsBefore, @@ -200,40 +233,6 @@ export const SOURCE_CODE = Object.freeze({ getLastTokenBetween: tokenMethods.getLastTokenBetween, getLastTokensBetween: tokenMethods.getLastTokensBetween, getTokenByRangeStart: tokenMethods.getTokenByRangeStart, - - /** - * Get the deepest node containing a range index. - * @param index Range index of the desired node. - * @returns The node if found, or `null` if not found. - */ - // oxlint-disable-next-line no-unused-vars - getNodeByRangeIndex(index: number): Node | null { - throw new Error('`sourceCode.getNodeByRangeIndex` not implemented yet'); // TODO - }, - - getLocFromIndex: getLineColumnFromOffset, - getIndexFromLoc: getOffsetFromLineColumn, - - getAncestors, }); export type SourceCode = typeof SOURCE_CODE; - -/** - * Get all the ancestors of a given node. - * @param node - AST node - * @returns All the ancestor nodes in the AST, not including the provided node, - * starting from the root node at index 0 and going inwards to the parent node. - */ -function getAncestors(node: Node): Node[] { - const ancestors = []; - - while (true) { - // @ts-expect-error `parent` property should be present on `Node` type - node = node.parent; - if (node === null) break; - ancestors.push(node); - } - - return ancestors.reverse(); -}