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
31 changes: 28 additions & 3 deletions apps/oxlint/src-js/plugins/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,34 @@ function initTokensWithComments() {
while (commentsIndex < commentsLength) tokensWithComments.push(comments[commentsIndex++]);
while (tokensIndex < tokensLength) tokensWithComments.push(tokens[tokensIndex++]);

if (DEBUG) {
if (tokensWithComments.length !== tokensLength + commentsLength) {
throw new Error("Not all tokens and comments were merged");
debugCheckTokensWithComments();
}

/**
* Check `tokensWithComments` contains all tokens and comments, in ascending order.
*
* Only runs in debug build (tests). In release build, this function is entirely removed by minifier.
*/
function debugCheckTokensWithComments() {
if (!DEBUG) return;

debugAssertIsNonNull(tokens);
debugAssertIsNonNull(comments);
debugAssertIsNonNull(tokensWithComments);

const expected = [...tokens, ...comments];
expected.sort((a, b) => {
if (a.range[0] === b.range[0]) throw new Error("2 tokens/comments have the same start");
return a.range[0] - b.range[0];
});

if (tokensWithComments.length !== expected.length) {
throw new Error("`tokensWithComments` has wrong length");
}

for (let i = 0; i < tokensWithComments.length; i++) {
if (tokensWithComments[i] !== expected[i]) {
throw new Error("`tokensWithComments` is not correctly ordered");
}
}
}
Expand Down
Loading