Skip to content
Merged
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
57 changes: 30 additions & 27 deletions apps/oxlint/src-js/plugins/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ function initTokens() {
}));
}

/**
* Initialize `tokensWithComments`.
*
* Caller must ensure `tokens` and `comments` are initialized before calling this function.
*/
function initTokensWithComments() {
debugAssertIsNonNull(tokens);
debugAssertIsNonNull(comments);
// TODO: `tokens` and `comments` are already sorted, so there's a more efficient algorithm to merge them.
// That'd certainly be faster in Rust, but maybe here it's faster to leave it to JS engine to sort them?
// TODO: Replace `range[0]` with `start` once we have our own tokens which have `start` property.
tokensWithComments = [...tokens, ...comments].sort((a, b) => a.range[0] - b.range[0]);
}

/**
* Discard TS-ESLint tokens to free memory.
*/
Expand Down Expand Up @@ -228,12 +242,8 @@ export function getTokens(
// Source array of tokens to search in
let nodeTokens: Token[] | null = null;
if (includeComments) {
if (tokensWithComments === null) {
// TODO: `tokens` and `comments` are already sorted, so there's a more efficient algorithm to merge them.
// That'd certainly be faster in Rust, but maybe here it's faster to leave it to JS engine to sort them?
// TODO: Once we have our own tokens which have `start` and `end` properties, we can use them instead of `range`.
tokensWithComments = [...tokens, ...comments].sort((a, b) => a.range[0] - b.range[0]);
}
if (tokensWithComments === null) initTokensWithComments();
debugAssertIsNonNull(tokensWithComments);
nodeTokens = tokensWithComments;
} else {
nodeTokens = tokens;
Expand Down Expand Up @@ -316,9 +326,8 @@ export function getFirstToken(node: Node, skipOptions?: SkipOptions | number | F
// Source array of tokens
let nodeTokens: Token[] | null = null;
if (includeComments) {
if (tokensWithComments === null) {
tokensWithComments = [...tokens, ...comments].sort((a, b) => a.range[0] - b.range[0]);
}
if (tokensWithComments === null) initTokensWithComments();
debugAssertIsNonNull(tokensWithComments);
nodeTokens = tokensWithComments;
} else {
nodeTokens = tokens;
Expand Down Expand Up @@ -419,9 +428,8 @@ export function getFirstTokens(node: Node, countOptions?: CountOptions | number

let nodeTokens: Token[] | null = null;
if (includeComments) {
if (tokensWithComments === null) {
tokensWithComments = [...tokens, ...comments].sort((a, b) => a.range[0] - b.range[0]);
}
if (tokensWithComments === null) initTokensWithComments();
debugAssertIsNonNull(tokensWithComments);
nodeTokens = tokensWithComments;
} else {
nodeTokens = tokens;
Expand Down Expand Up @@ -536,9 +544,8 @@ export function getLastTokens(node: Node, countOptions?: CountOptions | number |
// Source array of tokens to search in
let nodeTokens: Token[] | null = null;
if (includeComments) {
if (tokensWithComments === null) {
tokensWithComments = [...tokens, ...comments].sort((a, b) => a.range[0] - b.range[0]);
}
if (tokensWithComments === null) initTokensWithComments();
debugAssertIsNonNull(tokensWithComments);
nodeTokens = tokensWithComments;
} else {
nodeTokens = tokens;
Expand Down Expand Up @@ -644,9 +651,8 @@ export function getTokenBefore(
// Source array of tokens to search in
let nodeTokens: Token[] | null = null;
if (includeComments) {
if (tokensWithComments === null) {
tokensWithComments = [...tokens, ...comments].sort((a, b) => a.range[0] - b.range[0]);
}
if (tokensWithComments === null) initTokensWithComments();
debugAssertIsNonNull(tokensWithComments);
nodeTokens = tokensWithComments;
} else {
nodeTokens = tokens;
Expand Down Expand Up @@ -757,9 +763,8 @@ export function getTokensBefore(
// Source array of tokens to search in
let nodeTokens: Token[] | null = null;
if (includeComments) {
if (tokensWithComments === null) {
tokensWithComments = [...tokens, ...comments].sort((a, b) => a.range[0] - b.range[0]);
}
if (tokensWithComments === null) initTokensWithComments();
debugAssertIsNonNull(tokensWithComments);
nodeTokens = tokensWithComments;
} else {
nodeTokens = tokens;
Expand Down Expand Up @@ -850,9 +855,8 @@ export function getTokenAfter(
// Source array of tokens to search in
let nodeTokens: Token[] | null = null;
if (includeComments) {
if (tokensWithComments === null) {
tokensWithComments = [...tokens, ...comments].sort((a, b) => a.range[0] - b.range[0]);
}
if (tokensWithComments === null) initTokensWithComments();
debugAssertIsNonNull(tokensWithComments);
nodeTokens = tokensWithComments;
} else {
nodeTokens = tokens;
Expand Down Expand Up @@ -958,9 +962,8 @@ export function getTokensAfter(

let nodeTokens: Token[];
if (includeComments) {
if (tokensWithComments === null) {
tokensWithComments = [...tokens, ...comments].sort((a, b) => a.range[0] - b.range[0]);
}
if (tokensWithComments === null) initTokensWithComments();
debugAssertIsNonNull(tokensWithComments);
nodeTokens = tokensWithComments;
} else {
nodeTokens = tokens;
Expand Down
Loading