Skip to content

Commit 173e5a9

Browse files
committed
vendor eslint tests
1 parent 28ec6c7 commit 173e5a9

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

apps/oxlint/test/tokens.test.ts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import assert from 'node:assert';
2+
import { describe, it, vi } from 'vitest';
3+
import { getTokens, type Token } from '../src-js/plugins/tokens.js';
4+
import type { Node } from '../src-js/plugins/types.js';
5+
6+
let sourceText = 'null;';
7+
8+
vi.mock('../src-js/plugins/source_code.ts', () => {
9+
return {
10+
get sourceText() {
11+
return sourceText;
12+
},
13+
};
14+
});
15+
16+
/**
17+
* `TokenStore` is abstraction within ESLint.
18+
* We create an object named `store` to use ESLint tests verbatim.
19+
*/
20+
const store = {
21+
getTokens,
22+
};
23+
24+
describe('when calling getTokens', () => {
25+
sourceText = '/*A*/var answer/*B*/=/*C*/a/*D*/* b/*E*///F\n call();\n/*Z*/';
26+
27+
// Parsing source text to AST is done on the rust side.
28+
// To test tokens, we only need the range.
29+
// TODO: a JS API for parsing source text to AST (and buffer) for testing.
30+
const Program = { range: [5, 73] } as Node;
31+
const BinaryExpression = { range: [26, 35] } as Node;
32+
33+
// https://github.com/eslint/eslint/blob/v9.39.1/tests/lib/languages/js/source-code/token-store.js#L63
34+
it('should retrieve all tokens for root node', () => {
35+
check(store.getTokens(Program), ['var', 'answer', '=', 'a', '*', 'b', 'call', '(', ')', ';']);
36+
});
37+
38+
it('should retrieve all tokens for binary expression', () => {
39+
check(store.getTokens(BinaryExpression), ['a', '*', 'b']);
40+
});
41+
42+
it('should retrieve all tokens plus one before for binary expression', () => {
43+
check(store.getTokens(BinaryExpression, 1), ['=', 'a', '*', 'b']);
44+
});
45+
46+
it('should retrieve all tokens plus one after for binary expression', () => {
47+
check(store.getTokens(BinaryExpression, 0, 1), ['a', '*', 'b', 'call']);
48+
});
49+
50+
it('should retrieve all tokens plus two before and one after for binary expression', () => {
51+
check(store.getTokens(BinaryExpression, 2, 1), ['answer', '=', 'a', '*', 'b', 'call']);
52+
});
53+
54+
it('should retrieve all matched tokens for root node with filter', () => {
55+
check(
56+
store.getTokens(Program, (t) => t.type === 'Identifier'),
57+
['answer', 'a', 'b', 'call'],
58+
);
59+
check(
60+
store.getTokens(Program, {
61+
filter: (t) => t.type === 'Identifier',
62+
}),
63+
['answer', 'a', 'b', 'call'],
64+
);
65+
});
66+
67+
it('should retrieve all tokens and comments in the node for root node with includeComments option', () => {
68+
check(store.getTokens(Program, { includeComments: true }), [
69+
'var',
70+
'answer',
71+
'B',
72+
'=',
73+
'C',
74+
'a',
75+
'D',
76+
'*',
77+
'b',
78+
'E',
79+
'F',
80+
'call',
81+
'(',
82+
')',
83+
';',
84+
]);
85+
});
86+
87+
it('should retrieve matched tokens and comments in the node for root node with includeComments and filter options', () => {
88+
check(
89+
store.getTokens(Program, {
90+
includeComments: true,
91+
filter: (t) => t.type.startsWith('Block'),
92+
}),
93+
['B', 'C', 'D', 'E'],
94+
);
95+
});
96+
97+
it('should retrieve all tokens and comments in the node for binary expression with includeComments option', () => {
98+
check(store.getTokens(BinaryExpression, { includeComments: true }), ['a', 'D', '*', 'b']);
99+
});
100+
});
101+
102+
/**
103+
* Checks the values of tokens against an array of expected values.
104+
*/
105+
function check(
106+
/** Tokens returned from the API. */
107+
tokens: Token[],
108+
/** Expected token values */
109+
expected: string[],
110+
) {
111+
assert.deepStrictEqual(
112+
tokens.map((token) => token.value),
113+
expected,
114+
);
115+
}

0 commit comments

Comments
 (0)