Skip to content

Commit 53a9d5d

Browse files
silverwindljharb
authored andcommitted
[Fix] no-unused-modules: ignore hashbang and BOM while parsing
ESLint does this outside their espree parser, so we need to do it as well. Just like ESLint, the code will convert hashbang to comments and strip off the BOM completely before handing the content to the parser.
1 parent f18b676 commit 53a9d5d

File tree

6 files changed

+76
-1
lines changed

6 files changed

+76
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const foo = 1;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import {foo} from './prefix-child.js';
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env node
2+
import {foo} from './prefix-child.js';
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env node
2+
import {foo} from './prefix-child.js';
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env node
2+
import {foo} from './prefix-child.js';

tests/src/rules/no-unused-modules.js

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ describe('dynamic imports', () => {
307307
`,
308308
filename: testFilePath('./unused-modules-reexport-crash/src/index.tsx'),
309309
parser: parsers.TS_NEW,
310-
options: [{
310+
options: [{
311311
unusedExports: true,
312312
ignoreExports: ['**/magic/**'],
313313
}],
@@ -1302,3 +1302,70 @@ describe('support ES2022 Arbitrary module namespace identifier names', () => {
13021302
),
13031303
});
13041304
});
1305+
1306+
describe('parser ignores prefixes like BOM and hashbang', () => {
1307+
// bom, hashbang
1308+
ruleTester.run('no-unused-modules', rule, {
1309+
valid: [
1310+
test({
1311+
options: unusedExportsOptions,
1312+
code: 'export const foo = 1;\n',
1313+
filename: testFilePath('./no-unused-modules/prefix-child.js'),
1314+
}),
1315+
test({
1316+
options: unusedExportsOptions,
1317+
code: `\uFEFF#!/usr/bin/env node\nimport {foo} from './prefix-child.js';\n`,
1318+
filename: testFilePath('./no-unused-modules/prefix-parent-bom.js'),
1319+
}),
1320+
],
1321+
invalid: [],
1322+
});
1323+
// no bom, hashbang
1324+
ruleTester.run('no-unused-modules', rule, {
1325+
valid: [
1326+
test({
1327+
options: unusedExportsOptions,
1328+
code: 'export const foo = 1;\n',
1329+
filename: testFilePath('./no-unused-modules/prefix-child.js'),
1330+
}),
1331+
test({
1332+
options: unusedExportsOptions,
1333+
code: `#!/usr/bin/env node\nimport {foo} from './prefix-child.js';\n`,
1334+
filename: testFilePath('./no-unused-modules/prefix-parent-hashbang.js'),
1335+
}),
1336+
],
1337+
invalid: [],
1338+
});
1339+
// bom, no hashbang
1340+
ruleTester.run('no-unused-modules', rule, {
1341+
valid: [
1342+
test({
1343+
options: unusedExportsOptions,
1344+
code: 'export const foo = 1;\n',
1345+
filename: testFilePath('./no-unused-modules/prefix-child.js'),
1346+
}),
1347+
test({
1348+
options: unusedExportsOptions,
1349+
code: `\uFEFF#!/usr/bin/env node\nimport {foo} from './prefix-child.js';\n`,
1350+
filename: testFilePath('./no-unused-modules/prefix-parent-bomhashbang.js'),
1351+
}),
1352+
],
1353+
invalid: [],
1354+
});
1355+
// no bom, no hashbang
1356+
ruleTester.run('no-unused-modules', rule, {
1357+
valid: [
1358+
test({
1359+
options: unusedExportsOptions,
1360+
code: 'export const foo = 1;\n',
1361+
filename: testFilePath('./no-unused-modules/prefix-child.js'),
1362+
}),
1363+
test({
1364+
options: unusedExportsOptions,
1365+
code: `import {foo} from './prefix-child.js';\n`,
1366+
filename: testFilePath('./no-unused-modules/prefix-parent.js'),
1367+
}),
1368+
],
1369+
invalid: [],
1370+
});
1371+
});

0 commit comments

Comments
 (0)