|
1 | 1 | import assert from 'node:assert'; |
2 | 2 | import { describe, it, vi } from 'vitest'; |
3 | | -import { getTokens } from '../src-js/plugins/tokens.js'; |
| 3 | +import { getTokens, getTokenBefore } from '../src-js/plugins/tokens.js'; |
| 4 | +import { resetSourceAndAst } from '../src-js/plugins/source_code.js'; |
4 | 5 | import type { Node } from '../src-js/plugins/types.js'; |
5 | 6 |
|
6 | | -let sourceText = 'null;'; |
| 7 | +let sourceText = '/*A*/var answer/*B*/=/*C*/a/*D*/* b/*E*///F\n call();\n/*Z*/'; |
7 | 8 |
|
8 | | -vi.mock('../src-js/plugins/source_code.ts', () => { |
| 9 | +vi.mock('../src-js/plugins/source_code.ts', async (importOriginal) => { |
| 10 | + const original: any = await importOriginal(); |
9 | 11 | return { |
| 12 | + ...original, |
10 | 13 | get sourceText() { |
11 | 14 | return sourceText; |
12 | 15 | }, |
13 | 16 | }; |
14 | 17 | }); |
15 | 18 |
|
| 19 | +// TODO: We are lying about `Program`'s range here. |
| 20 | +// The range provided by `@typescript-eslint/typescript-estree` does not match the assertions for that of `espree`. |
| 21 | +// The deviation is being corrected in upcoming releases of ESLint and TS-ESLint. |
| 22 | +// https://eslint.org/blog/2025/10/whats-coming-in-eslint-10.0.0/#updates-to-program-ast-node-range-coverage |
| 23 | +// https://github.com/typescript-eslint/typescript-eslint/issues/11026#issuecomment-3421887632 |
| 24 | +const Program = { range: [5, 55] } as Node; |
| 25 | +const BinaryExpression = { range: [26, 35] } as Node; |
| 26 | + |
16 | 27 | // https://github.com/eslint/eslint/blob/v9.39.1/tests/lib/languages/js/source-code/token-store.js#L62 |
17 | 28 | describe('when calling getTokens', () => { |
18 | | - sourceText = '/*A*/var answer/*B*/=/*C*/a/*D*/* b/*E*///F\n call();\n/*Z*/'; |
19 | | - |
20 | | - // TODO: We are lying about `Program`'s range here. |
21 | | - // The range provided by `@typescript-eslint/typescript-estree` does not match the assertions for that of `espree`. |
22 | | - // The deviation is being corrected in upcoming releases of ESLint and TS-ESLint. |
23 | | - // https://eslint.org/blog/2025/10/whats-coming-in-eslint-10.0.0/#updates-to-program-ast-node-range-coverage |
24 | | - // https://github.com/typescript-eslint/typescript-eslint/issues/11026#issuecomment-3421887632 |
25 | | - const Program = { range: [5, 55] } as Node; |
26 | | - const BinaryExpression = { range: [26, 35] } as Node; |
27 | | - |
28 | 29 | it('should retrieve all tokens for root node', () => { |
29 | 30 | assert.deepStrictEqual( |
30 | 31 | getTokens(Program).map((token) => token.value), |
@@ -104,3 +105,93 @@ describe('when calling getTokens', () => { |
104 | 105 | ); |
105 | 106 | }); |
106 | 107 | }); |
| 108 | + |
| 109 | +describe('when calling getTokenBefore', () => { |
| 110 | + it('should retrieve one token before a node', () => { |
| 111 | + assert.strictEqual(getTokenBefore(BinaryExpression)!.value, '='); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should skip a given number of tokens', () => { |
| 115 | + assert.strictEqual(getTokenBefore(BinaryExpression, 1)!.value, 'answer'); |
| 116 | + assert.strictEqual(getTokenBefore(BinaryExpression, 2)!.value, 'var'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should skip a given number of tokens with skip option', () => { |
| 120 | + assert.strictEqual(getTokenBefore(BinaryExpression, { skip: 1 })!.value, 'answer'); |
| 121 | + assert.strictEqual(getTokenBefore(BinaryExpression, { skip: 2 })!.value, 'var'); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should retrieve matched token with filter option', () => { |
| 125 | + assert.strictEqual(getTokenBefore(BinaryExpression, (t) => t.value !== '=')!.value, 'answer'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should retrieve matched token with skip and filter options', () => { |
| 129 | + assert.strictEqual( |
| 130 | + getTokenBefore(BinaryExpression, { |
| 131 | + skip: 1, |
| 132 | + filter: (t) => t.value !== '=', |
| 133 | + })!.value, |
| 134 | + 'var', |
| 135 | + ); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should retrieve one token or comment before a node with includeComments option', () => { |
| 139 | + assert.strictEqual( |
| 140 | + getTokenBefore(BinaryExpression, { |
| 141 | + includeComments: true, |
| 142 | + })!.value, |
| 143 | + 'C', |
| 144 | + ); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should retrieve one token or comment before a node with includeComments and skip options', () => { |
| 148 | + assert.strictEqual( |
| 149 | + getTokenBefore(BinaryExpression, { |
| 150 | + includeComments: true, |
| 151 | + skip: 1, |
| 152 | + })!.value, |
| 153 | + '=', |
| 154 | + ); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should retrieve one token or comment before a node with includeComments and skip and filter options', () => { |
| 158 | + assert.strictEqual( |
| 159 | + getTokenBefore(BinaryExpression, { |
| 160 | + includeComments: true, |
| 161 | + skip: 1, |
| 162 | + filter: (t) => t.type.startsWith('Block'), |
| 163 | + })!.value, |
| 164 | + 'B', |
| 165 | + ); |
| 166 | + }); |
| 167 | + |
| 168 | + it('should retrieve the previous node if the comment at the end of source code is specified.', () => { |
| 169 | + resetSourceAndAst(); |
| 170 | + sourceText = 'a + b /*comment*/'; |
| 171 | + // TODO: this verbatim range should be replaced with `ast.comments[0]` |
| 172 | + const token = getTokenBefore({ range: [6, 14] } as Node); |
| 173 | + |
| 174 | + assert.strictEqual(token!.value, 'b'); |
| 175 | + resetSourceAndAst(); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should retrieve the previous comment if the first token is specified.', () => { |
| 179 | + resetSourceAndAst(); |
| 180 | + sourceText = '/*comment*/ a + b'; |
| 181 | + // TODO: this verbatim range should be replaced with `ast.tokens[0]` |
| 182 | + const token = getTokenBefore({ range: [12, 13] } as Node, { includeComments: true }); |
| 183 | + |
| 184 | + assert.strictEqual(token!.value, 'comment'); |
| 185 | + resetSourceAndAst(); |
| 186 | + }); |
| 187 | + |
| 188 | + it('should retrieve null if the first comment is specified.', () => { |
| 189 | + resetSourceAndAst(); |
| 190 | + sourceText = '/*comment*/ a + b'; |
| 191 | + // TODO: this verbatim range should be replaced with `ast.comments[0]` |
| 192 | + const token = getTokenBefore({ range: [0, 11] } as Node, { includeComments: true }); |
| 193 | + |
| 194 | + assert.strictEqual(token, null); |
| 195 | + resetSourceAndAst(); |
| 196 | + }); |
| 197 | +}); |
0 commit comments