Skip to content

Commit

Permalink
[Fix] no-unused-modules: ignore hashbang and BOM while parsing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
silverwind authored and ljharb committed Apr 11, 2022
1 parent f18b676 commit 53a9d5d
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions tests/files/no-unused-modules/prefix-child.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 1;
1 change: 1 addition & 0 deletions tests/files/no-unused-modules/prefix-parent-bom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import {foo} from './prefix-child.js';
2 changes: 2 additions & 0 deletions tests/files/no-unused-modules/prefix-parent-bomhashbang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
import {foo} from './prefix-child.js';
2 changes: 2 additions & 0 deletions tests/files/no-unused-modules/prefix-parent-hashbang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
import {foo} from './prefix-child.js';
2 changes: 2 additions & 0 deletions tests/files/no-unused-modules/prefix-parent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
import {foo} from './prefix-child.js';
69 changes: 68 additions & 1 deletion tests/src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ describe('dynamic imports', () => {
`,
filename: testFilePath('./unused-modules-reexport-crash/src/index.tsx'),
parser: parsers.TS_NEW,
options: [{
options: [{
unusedExports: true,
ignoreExports: ['**/magic/**'],
}],
Expand Down Expand Up @@ -1302,3 +1302,70 @@ describe('support ES2022 Arbitrary module namespace identifier names', () => {
),
});
});

describe('parser ignores prefixes like BOM and hashbang', () => {
// bom, hashbang
ruleTester.run('no-unused-modules', rule, {
valid: [
test({
options: unusedExportsOptions,
code: 'export const foo = 1;\n',
filename: testFilePath('./no-unused-modules/prefix-child.js'),
}),
test({
options: unusedExportsOptions,
code: `\uFEFF#!/usr/bin/env node\nimport {foo} from './prefix-child.js';\n`,
filename: testFilePath('./no-unused-modules/prefix-parent-bom.js'),
}),
],
invalid: [],
});
// no bom, hashbang
ruleTester.run('no-unused-modules', rule, {
valid: [
test({
options: unusedExportsOptions,
code: 'export const foo = 1;\n',
filename: testFilePath('./no-unused-modules/prefix-child.js'),
}),
test({
options: unusedExportsOptions,
code: `#!/usr/bin/env node\nimport {foo} from './prefix-child.js';\n`,
filename: testFilePath('./no-unused-modules/prefix-parent-hashbang.js'),
}),
],
invalid: [],
});
// bom, no hashbang
ruleTester.run('no-unused-modules', rule, {
valid: [
test({
options: unusedExportsOptions,
code: 'export const foo = 1;\n',
filename: testFilePath('./no-unused-modules/prefix-child.js'),
}),
test({
options: unusedExportsOptions,
code: `\uFEFF#!/usr/bin/env node\nimport {foo} from './prefix-child.js';\n`,
filename: testFilePath('./no-unused-modules/prefix-parent-bomhashbang.js'),
}),
],
invalid: [],
});
// no bom, no hashbang
ruleTester.run('no-unused-modules', rule, {
valid: [
test({
options: unusedExportsOptions,
code: 'export const foo = 1;\n',
filename: testFilePath('./no-unused-modules/prefix-child.js'),
}),
test({
options: unusedExportsOptions,
code: `import {foo} from './prefix-child.js';\n`,
filename: testFilePath('./no-unused-modules/prefix-parent.js'),
}),
],
invalid: [],
});
});

0 comments on commit 53a9d5d

Please sign in to comment.