Skip to content

Commit 3781b22

Browse files
committed
refactor(linter/plugins): move token-related SourceCode methods into separate file
1 parent 86ecae1 commit 3781b22

File tree

3 files changed

+298
-262
lines changed

3 files changed

+298
-262
lines changed

apps/oxlint/src-js/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export type {
1515
ScopeType,
1616
Variable,
1717
} from './plugins/scope.ts';
18-
export type { CountOptions, FilterFn, RangeOptions, SkipOptions, SourceCode } from './plugins/source_code.ts';
18+
export type { SourceCode } from './plugins/source_code.ts';
19+
export type { CountOptions, FilterFn, RangeOptions, SkipOptions } from './plugins/tokens.ts';
1920
export type {
2021
AfterHook,
2122
BeforeHook,

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

Lines changed: 18 additions & 261 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ import {
2121
lines,
2222
resetLines,
2323
} from './location.js';
24+
import * as tokenMethods from './tokens.js';
2425

2526
import type { Program } from '../generated/types.d.ts';
2627
import type { Scope, ScopeManager, Variable } from './scope.ts';
27-
import type { BufferWithArrays, Comment, Node, NodeOrToken, Ranged, Token } from './types.ts';
28+
import type { BufferWithArrays, Node, NodeOrToken, Ranged } from './types.ts';
2829

2930
const { max } = Math;
3031

@@ -191,237 +192,22 @@ export const SOURCE_CODE = Object.freeze({
191192
throw new Error('`sourceCode.isGlobalReference` not implemented yet'); // TODO
192193
},
193194

194-
/**
195-
* Get all tokens that are related to the given node.
196-
* @param node - The AST node.
197-
* @param countOptions? - Options object. If this is a function then it's `options.filter`.
198-
* @returns Array of `Token`s.
199-
*/
200-
/**
201-
* Get all tokens that are related to the given node.
202-
* @param node - The AST node.
203-
* @param beforeCount? - The number of tokens before the node to retrieve.
204-
* @param afterCount? - The number of tokens after the node to retrieve.
205-
* @returns Array of `Token`s.
206-
*/
207-
/* oxlint-disable no-unused-vars */
208-
getTokens(
209-
node: Node,
210-
countOptions?: CountOptions | number | FilterFn | null | undefined,
211-
afterCount?: number | null | undefined,
212-
): Token[] {
213-
throw new Error('`sourceCode.getTokens` not implemented yet'); // TODO
214-
},
215-
/* oxlint-enable no-unused-vars */
216-
217-
/**
218-
* Get the first token of the given node.
219-
* @param node - The AST node.
220-
* @param skipOptions? - Options object. If this is a number then it's `options.skip`.
221-
* If this is a function then it's `options.filter`.
222-
* @returns `Token`, or `null` if all were skipped.
223-
*/
224-
// oxlint-disable-next-line no-unused-vars
225-
getFirstToken(node: Node, skipOptions?: SkipOptions | number | FilterFn | null | undefined): Token | null {
226-
throw new Error('`sourceCode.getFirstToken` not implemented yet'); // TODO
227-
},
228-
229-
/**
230-
* Get the first tokens of the given node.
231-
* @param node - The AST node.
232-
* @param countOptions? - Options object. If this is a number then it's `options.count`.
233-
* If this is a function then it's `options.filter`.
234-
* @returns Array of `Token`s.
235-
*/
236-
// oxlint-disable-next-line no-unused-vars
237-
getFirstTokens(node: Node, countOptions?: CountOptions | number | FilterFn | null | undefined): Token[] {
238-
throw new Error('`sourceCode.getFirstTokens` not implemented yet'); // TODO
239-
},
240-
241-
/**
242-
* Get the last token of the given node.
243-
* @param node - The AST node.
244-
* @param skipOptions? - Options object. Same options as `getFirstToken()`.
245-
* @returns `Token`, or `null` if all were skipped.
246-
*/
247-
// oxlint-disable-next-line no-unused-vars
248-
getLastToken(node: Node, skipOptions?: SkipOptions | number | FilterFn | null | undefined): Token | null {
249-
throw new Error('`sourceCode.getLastToken` not implemented yet'); // TODO
250-
},
251-
252-
/**
253-
* Get the last tokens of the given node.
254-
* @param node - The AST node.
255-
* @param countOptions? - Options object. Same options as `getFirstTokens()`.
256-
* @returns Array of `Token`s.
257-
*/
258-
// oxlint-disable-next-line no-unused-vars
259-
getLastTokens(node: Node, countOptions?: CountOptions | number | FilterFn | null | undefined): Token[] {
260-
throw new Error('`sourceCode.getLastTokens` not implemented yet'); // TODO
261-
},
262-
263-
/**
264-
* Get the token that precedes a given node or token.
265-
* @param nodeOrToken - The AST node or token.
266-
* @param skipOptions? - Options object. Same options as `getFirstToken()`.
267-
* @returns `Token`, or `null` if all were skipped.
268-
*/
269-
/* oxlint-disable no-unused-vars */
270-
getTokenBefore(
271-
nodeOrToken: NodeOrToken | Comment,
272-
skipOptions?: SkipOptions | number | FilterFn | null | undefined,
273-
): Token | null {
274-
throw new Error('`sourceCode.getTokenBefore` not implemented yet'); // TODO
275-
},
276-
/* oxlint-enable no-unused-vars */
277-
278-
/**
279-
* Get the tokens that precedes a given node or token.
280-
* @param nodeOrToken - The AST node or token.
281-
* @param countOptions? - Options object. Same options as `getFirstTokens()`.
282-
* @returns Array of `Token`s.
283-
*/
284-
/* oxlint-disable no-unused-vars */
285-
getTokensBefore(
286-
nodeOrToken: NodeOrToken | Comment,
287-
countOptions?: CountOptions | number | FilterFn | null | undefined,
288-
): Token[] {
289-
throw new Error('`sourceCode.getTokensBefore` not implemented yet'); // TODO
290-
},
291-
/* oxlint-enable no-unused-vars */
292-
293-
/**
294-
* Get the token that follows a given node or token.
295-
* @param nodeOrToken - The AST node or token.
296-
* @param skipOptions? - Options object. Same options as `getFirstToken()`.
297-
* @returns `Token`, or `null` if all were skipped.
298-
*/
299-
/* oxlint-disable no-unused-vars */
300-
getTokenAfter(
301-
nodeOrToken: NodeOrToken | Comment,
302-
skipOptions?: SkipOptions | number | FilterFn | null | undefined,
303-
): Token | null {
304-
throw new Error('`sourceCode.getTokenAfter` not implemented yet'); // TODO
305-
},
306-
/* oxlint-enable no-unused-vars */
307-
308-
/**
309-
* Get the tokens that follow a given node or token.
310-
* @param nodeOrToken - The AST node or token.
311-
* @param countOptions? - Options object. Same options as `getFirstTokens()`.
312-
* @returns Array of `Token`s.
313-
*/
314-
/* oxlint-disable no-unused-vars */
315-
getTokensAfter(
316-
nodeOrToken: NodeOrToken | Comment,
317-
countOptions?: CountOptions | number | FilterFn | null | undefined,
318-
): Token[] {
319-
throw new Error('`sourceCode.getTokensAfter` not implemented yet'); // TODO
320-
},
321-
/* oxlint-enable no-unused-vars */
322-
323-
/**
324-
* Get all of the tokens between two non-overlapping nodes.
325-
* @param nodeOrToken1 - Node before the desired token range.
326-
* @param nodeOrToken2 - Node after the desired token range.
327-
* @param countOptions? - Options object. If this is a function then it's `options.filter`.
328-
* @returns Array of `Token`s between `nodeOrToken1` and `nodeOrToken2`.
329-
*/
330-
/**
331-
* Get all of the tokens between two non-overlapping nodes.
332-
* @param nodeOrToken1 - Node before the desired token range.
333-
* @param nodeOrToken2 - Node after the desired token range.
334-
* @param padding - Number of extra tokens on either side of center.
335-
* @returns Array of `Token`s between `nodeOrToken1` and `nodeOrToken2`.
336-
*/
337-
/* oxlint-disable no-unused-vars */
338-
getTokensBetween(
339-
nodeOrToken1: NodeOrToken | Comment,
340-
nodeOrToken2: NodeOrToken | Comment,
341-
countOptions?: CountOptions | number | FilterFn | null | undefined,
342-
): Token[] {
343-
throw new Error('`sourceCode.getTokensBetween` not implemented yet'); // TODO
344-
},
345-
/* oxlint-enable no-unused-vars */
346-
347-
/**
348-
* Get the first token between two non-overlapping nodes.
349-
* @param nodeOrToken1 - Node before the desired token range.
350-
* @param nodeOrToken2 - Node after the desired token range.
351-
* @param countOptions? - Options object. Same options as `getFirstToken()`.
352-
* @returns `Token`, or `null` if all were skipped.
353-
*/
354-
/* oxlint-disable no-unused-vars */
355-
getFirstTokenBetween(
356-
nodeOrToken1: NodeOrToken | Comment,
357-
nodeOrToken2: NodeOrToken | Comment,
358-
skipOptions?: SkipOptions | null | undefined,
359-
): Token | null {
360-
throw new Error('`sourceCode.getFirstTokenBetween` not implemented yet'); // TODO
361-
},
362-
/* oxlint-enable no-unused-vars */
363-
364-
/**
365-
* Get the first tokens between two non-overlapping nodes.
366-
* @param nodeOrToken1 - Node before the desired token range.
367-
* @param nodeOrToken2 - Node after the desired token range.
368-
* @param countOptions? - Options object. Same options as `getFirstTokens()`.
369-
* @returns Array of `Token`s between `nodeOrToken1` and `nodeOrToken2`.
370-
*/
371-
/* oxlint-disable no-unused-vars */
372-
getFirstTokensBetween(
373-
nodeOrToken1: NodeOrToken | Comment,
374-
nodeOrToken2: NodeOrToken | Comment,
375-
countOptions?: CountOptions | number | FilterFn | null | undefined,
376-
): Token[] {
377-
throw new Error('`sourceCode.getFirstTokensBetween` not implemented yet'); // TODO
378-
},
379-
/* oxlint-enable no-unused-vars */
380-
381-
/**
382-
* Get the last token between two non-overlapping nodes.
383-
* @param nodeOrToken1 - Node before the desired token range.
384-
* @param nodeOrToken2 - Node after the desired token range.
385-
* @param skipOptions? - Options object. Same options as `getFirstToken()`.
386-
* @returns `Token`, or `null` if all were skipped.
387-
*/
388-
/* oxlint-disable no-unused-vars */
389-
getLastTokenBetween(
390-
nodeOrToken1: NodeOrToken | Comment,
391-
nodeOrToken2: NodeOrToken | Comment,
392-
skipOptions?: SkipOptions | null | undefined,
393-
): Token | null {
394-
throw new Error('`sourceCode.getLastTokenBetween` not implemented yet'); // TODO
395-
},
396-
/* oxlint-enable no-unused-vars */
397-
398-
/**
399-
* Get the last tokens between two non-overlapping nodes.
400-
* @param nodeOrToken1 - Node before the desired token range.
401-
* @param nodeOrToken2 - Node after the desired token range.
402-
* @param countOptions? - Options object. Same options as `getFirstTokens()`.
403-
* @returns Array of `Token`s between `nodeOrToken1` and `nodeOrToken2`.
404-
*/
405-
/* oxlint-disable no-unused-vars */
406-
getLastTokensBetween(
407-
nodeOrToken1: NodeOrToken | Comment,
408-
nodeOrToken2: NodeOrToken | Comment,
409-
countOptions?: CountOptions | number | FilterFn | null | undefined,
410-
): Token[] {
411-
throw new Error('`sourceCode.getLastTokensBetween` not implemented yet'); // TODO
412-
},
413-
/* oxlint-enable no-unused-vars */
414-
415-
/**
416-
* Get the token starting at the specified index.
417-
* @param index - Index of the start of the token's range.
418-
* @param options - Options object.
419-
* @returns The token starting at index, or `null` if no such token.
420-
*/
421-
// oxlint-disable-next-line no-unused-vars
422-
getTokenByRangeStart(index: number, rangeOptions?: RangeOptions | null | undefined): Token | null {
423-
throw new Error('`sourceCode.getTokenByRangeStart` not implemented yet'); // TODO
424-
},
195+
// Token methods
196+
getTokens: tokenMethods.getTokens,
197+
getFirstToken: tokenMethods.getFirstToken,
198+
getFirstTokens: tokenMethods.getFirstTokens,
199+
getLastToken: tokenMethods.getLastToken,
200+
getLastTokens: tokenMethods.getLastTokens,
201+
getTokenBefore: tokenMethods.getTokenBefore,
202+
getTokensBefore: tokenMethods.getTokensBefore,
203+
getTokenAfter: tokenMethods.getTokenAfter,
204+
getTokensAfter: tokenMethods.getTokensAfter,
205+
getTokensBetween: tokenMethods.getTokensBetween,
206+
getFirstTokenBetween: tokenMethods.getFirstTokenBetween,
207+
getFirstTokensBetween: tokenMethods.getFirstTokensBetween,
208+
getLastTokenBetween: tokenMethods.getLastTokenBetween,
209+
getLastTokensBetween: tokenMethods.getLastTokensBetween,
210+
getTokenByRangeStart: tokenMethods.getTokenByRangeStart,
425211

426212
/**
427213
* Get the deepest node containing a range index.
@@ -491,32 +277,3 @@ function getAncestors(node: Node): Node[] {
491277

492278
return ancestors.reverse();
493279
}
494-
495-
// Options for various `SourceCode` methods e.g. `getFirstToken`.
496-
export interface SkipOptions {
497-
// Number of skipping tokens
498-
skip?: number;
499-
// `true` to include comment tokens in the result
500-
includeComments?: boolean;
501-
// Function to filter tokens
502-
filter?: FilterFn | null;
503-
}
504-
505-
// Options for various `SourceCode` methods e.g. `getFirstTokens`.
506-
export interface CountOptions {
507-
// Maximum number of tokens to return
508-
count?: number;
509-
// `true` to include comment tokens in the result
510-
includeComments?: boolean;
511-
// Function to filter tokens
512-
filter?: FilterFn | null;
513-
}
514-
515-
// Options for various `SourceCode` methods e.g. `getTokenByRangeStart`.
516-
export interface RangeOptions {
517-
// `true` to include comment tokens in the result
518-
includeComments?: boolean;
519-
}
520-
521-
// Filter function, passed as `filter` property of `SkipOptions` and `CountOptions`.
522-
export type FilterFn = (token: Token) => boolean;

0 commit comments

Comments
 (0)