Skip to content

Commit 1599bc9

Browse files
captaindonaldljharb
authored andcommitted
[Tests] cover eslint 10 getTokenOrComment and listFilesToProcess fallbacks
Exercise both branches of the `getTokenOrComment{Before,After}` shims, and the `listFilesToProcess` fallback to `listFilesWithNodeFs` when the legacy `glob-utils` module is no longer exported (`ERR_PACKAGE_PATH_NOT_EXPORTED`, eslint 10) in addition to the existing `MODULE_NOT_FOUND` case.
1 parent ac5eab7 commit 1599bc9

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
2525
### Changed
2626
- [Refactor] [`order`]: extract the ESLint 10 token/comment compatibility shims into a reusable `getTokenOrComment` util ([#3230], thanks [@captaindonald])
2727
- [Tests] add parser version guards for ESLint v10 (TS_NEW / ANGULAR / BABEL_NEW) ([#3230], thanks [@rasmi])
28+
- [Tests] cover ESLint 10 `getTokenOrComment` and `listFilesToProcess` API fallbacks ([#3230], thanks [@captaindonald])
2829
- [Docs] `no-unused-modules`: Fix docs of `ignoreUnusedTypeExports` option ([#3233], thanks [@ej612])
2930

3031
## [2.32.0] - 2025-06-20
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { expect } from 'chai';
2+
3+
import { getTokenOrCommentAfter, getTokenOrCommentBefore } from 'core/getTokenOrComment';
4+
5+
// These helpers feature-detect the `SourceCode` API that ESLint v10 removed, so both
6+
// branches need to be exercised regardless of which `eslint` version the tests run on.
7+
describe('getTokenOrComment', function () {
8+
const node = { type: 'Node' };
9+
const token = { type: 'Token' };
10+
11+
describe('getTokenOrCommentAfter', function () {
12+
it('uses sourceCode.getTokenOrCommentAfter when present (ESLint < 10)', function () {
13+
const calls = [];
14+
const sourceCode = {
15+
getTokenOrCommentAfter(nodeOrToken) {
16+
calls.push(nodeOrToken);
17+
return token;
18+
},
19+
};
20+
expect(getTokenOrCommentAfter(sourceCode, node)).to.equal(token);
21+
expect(calls).to.deep.equal([node]);
22+
});
23+
24+
it('falls back to getTokenAfter with includeComments (ESLint >= 10)', function () {
25+
let received;
26+
const sourceCode = {
27+
getTokenAfter(nodeOrToken, options) {
28+
received = { nodeOrToken, options };
29+
return token;
30+
},
31+
};
32+
expect(getTokenOrCommentAfter(sourceCode, node)).to.equal(token);
33+
expect(received).to.deep.equal({ nodeOrToken: node, options: { includeComments: true } });
34+
});
35+
});
36+
37+
describe('getTokenOrCommentBefore', function () {
38+
it('uses sourceCode.getTokenOrCommentBefore when present (ESLint < 10)', function () {
39+
const calls = [];
40+
const sourceCode = {
41+
getTokenOrCommentBefore(nodeOrToken) {
42+
calls.push(nodeOrToken);
43+
return token;
44+
},
45+
};
46+
expect(getTokenOrCommentBefore(sourceCode, node)).to.equal(token);
47+
expect(calls).to.deep.equal([node]);
48+
});
49+
50+
it('falls back to getTokenBefore with includeComments (ESLint >= 10)', function () {
51+
let received;
52+
const sourceCode = {
53+
getTokenBefore(nodeOrToken, options) {
54+
received = { nodeOrToken, options };
55+
return token;
56+
},
57+
};
58+
expect(getTokenOrCommentBefore(sourceCode, node)).to.equal(token);
59+
expect(received).to.deep.equal({ nodeOrToken: node, options: { includeComments: true } });
60+
});
61+
});
62+
});

tests/src/core/listFilesToProcess.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ function moduleNotFound() {
3333
throw e;
3434
}
3535

36+
// ESLint v10 removed the legacy `glob-utils` from the package's `exports` map, so
37+
// requiring it throws `ERR_PACKAGE_PATH_NOT_EXPORTED` rather than `MODULE_NOT_FOUND`.
38+
function packagePathNotExported() {
39+
const e = new Error('Package subpath \'./lib/util/glob-utils\' is not defined by "exports"');
40+
e.code = 'ERR_PACKAGE_PATH_NOT_EXPORTED';
41+
throw e;
42+
}
43+
3644
describe('listFilesToProcess', function () {
3745
describe('listFilesUsingFileEnumerator', function () {
3846
const originalFlatConfig = process.env.ESLINT_USE_FLAT_CONFIG;
@@ -93,6 +101,15 @@ describe('listFilesToProcess', function () {
93101
expect(result).to.deep.equal([f('src', 'a.js')]);
94102
});
95103

104+
// ESLint v10+: requiring the removed legacy module throws ERR_PACKAGE_PATH_NOT_EXPORTED.
105+
it('falls back to listFilesWithNodeFs when the legacy module is no longer exported', function () {
106+
const result = listFilesToProcess([f('src', 'a*.js')], ['.js'], {
107+
getFileEnumerator: () => undefined,
108+
getLegacyFiles: packagePathNotExported,
109+
});
110+
expect(result).to.deep.equal([f('src', 'a.js')]);
111+
});
112+
96113
it('rethrows non-module-resolution errors from the legacy path', function () {
97114
expect(() => listFilesToProcess([root], ['.js'], {
98115
getFileEnumerator: () => undefined,

0 commit comments

Comments
 (0)