Skip to content

Commit 620234c

Browse files
committed
feat(linter/plugins): implement deprecated SourceCode tokens methods
1 parent a210b12 commit 620234c

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,10 @@ export const SOURCE_CODE = Object.freeze({
226226
getLastToken: tokenMethods.getLastToken,
227227
getLastTokens: tokenMethods.getLastTokens,
228228
getTokenBefore: tokenMethods.getTokenBefore,
229+
getTokenOrCommentBefore: tokenMethods.getTokenOrCommentBefore,
229230
getTokensBefore: tokenMethods.getTokensBefore,
230231
getTokenAfter: tokenMethods.getTokenAfter,
232+
getTokenOrCommentAfter: tokenMethods.getTokenOrCommentAfter,
231233
getTokensAfter: tokenMethods.getTokensAfter,
232234
getTokensBetween: tokenMethods.getTokensBetween,
233235
getFirstTokenBetween: tokenMethods.getFirstTokenBetween,
@@ -236,6 +238,7 @@ export const SOURCE_CODE = Object.freeze({
236238
getLastTokensBetween: tokenMethods.getLastTokensBetween,
237239
getTokenByRangeStart: tokenMethods.getTokenByRangeStart,
238240
isSpaceBetween: tokenMethods.isSpaceBetween,
241+
isSpaceBetweenTokens: tokenMethods.isSpaceBetweenTokens,
239242
});
240243

241244
export type SourceCode = typeof SOURCE_CODE;

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,24 @@ export function getTokenBefore(
139139
}
140140
/* oxlint-enable no-unused-vars */
141141

142+
/**
143+
* Get the token that precedes a given node or token.
144+
*
145+
* @deprecated Use `sourceCode.getTokenAfter` with `includeComments: true` instead.
146+
*
147+
* @param nodeOrToken The AST node or token.
148+
* @param skip - Number of tokens to skip.
149+
* @returns `Token`, or `null` if all were skipped.
150+
*/
151+
/* oxlint-disable no-unused-vars */
152+
export function getTokenOrCommentBefore(nodeOrToken: NodeOrToken | Comment, skip?: number): Token | null {
153+
// TODO: Implement equivalent of:
154+
// `return getTokenBefore(nodeOrToken, { includeComments: true, skip });`
155+
// But could use a const object at top level for options object, to avoid creating temporary object on each call.
156+
throw new Error('`sourceCode.getTokenOrCommentBefore` not implemented yet');
157+
}
158+
/* oxlint-enable no-unused-vars */
159+
142160
/**
143161
* Get the tokens that precede a given node or token.
144162
* @param nodeOrToken - The AST node or token.
@@ -169,6 +187,24 @@ export function getTokenAfter(
169187
}
170188
/* oxlint-enable no-unused-vars */
171189

190+
/**
191+
* Get the token that follows a given node or token.
192+
*
193+
* @deprecated Use `sourceCode.getTokenAfter` with `includeComments: true` instead.
194+
*
195+
* @param nodeOrToken The AST node or token.
196+
* @param skip - Number of tokens to skip.
197+
* @returns `Token`, or `null` if all were skipped.
198+
*/
199+
/* oxlint-disable no-unused-vars */
200+
export function getTokenOrCommentAfter(nodeOrToken: NodeOrToken | Comment, skip?: number): Token | null {
201+
// TODO: Implement equivalent of:
202+
// `return getTokenAfter(nodeOrToken, { includeComments: true, skip });`
203+
// But could use a const object at top level for options object, to avoid creating temporary object on each call.
204+
throw new Error('`sourceCode.getTokenOrCommentAfter` not implemented yet');
205+
}
206+
/* oxlint-enable no-unused-vars */
207+
172208
/**
173209
* Get the tokens that follow a given node or token.
174210
* @param nodeOrToken - The AST node or token.
@@ -346,3 +382,29 @@ export function isSpaceBetween(nodeOrToken1: NodeOrToken, nodeOrToken2: NodeOrTo
346382

347383
return WHITESPACE_REGEXP.test(sourceText.slice(gapStart, gapEnd));
348384
}
385+
386+
/**
387+
* Determine if two nodes or tokens have at least one whitespace character between them.
388+
* Order does not matter.
389+
*
390+
* Returns `false` if the given nodes or tokens overlap.
391+
*
392+
* Checks for whitespace *between tokens*, not including whitespace *inside tokens*.
393+
* e.g. Returns `false` for `isSpaceBetween(x, y)` in `x+" "+y`.
394+
*
395+
* Unlike `SourceCode#isSpaceBetween`, this function does return `true` if there is a `JSText` token between the two
396+
* input tokens, and it contains whitespace.
397+
* e.g. Returns `true` for `isSpaceBetweenTokens(x, slash)` in `<X>a b</X>`.
398+
*
399+
* @deprecated Use `sourceCode.isSpaceBetween` instead.
400+
*
401+
* TODO: Implementation is not quite right at present, for same reasons as `SourceCode#isSpaceBetween`.
402+
*
403+
* @param nodeOrToken1 - The first node or token to check between.
404+
* @param nodeOrToken2 - The second node or token to check between.
405+
* @returns `true` if there is a whitespace character between
406+
* any of the tokens found between the two given nodes or tokens.
407+
*/
408+
export function isSpaceBetweenTokens(token1: Token, token2: Token): boolean {
409+
return isSpaceBetween(token1, token2);
410+
}

0 commit comments

Comments
 (0)