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
5 changes: 5 additions & 0 deletions apps/oxlint/src-js/plugins/location.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
67 changes: 33 additions & 34 deletions apps/oxlint/src-js/plugins/source_code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down Expand Up @@ -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();
}
Loading