Skip to content

Commit 675326a

Browse files
committed
[New] no-unused-modules: support arbitrary module namespace names
1 parent de5d4f6 commit 675326a

File tree

6 files changed

+42
-7
lines changed

6 files changed

+42
-7
lines changed

src/ExportMap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ ExportMap.parse = function (path, content, context) {
593593
importedSpecifiers.add(specifier.type);
594594
}
595595
if (specifier.type === 'ImportSpecifier') {
596-
importedSpecifiers.add(specifier.imported.name);
596+
importedSpecifiers.add(specifier.imported.name || specifier.imported.value);
597597
}
598598

599599
// import { type Foo } (Flow)

src/rules/no-unused-modules.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ module.exports = {
604604
if (specifiers.length > 0) {
605605
specifiers.forEach(specifier => {
606606
if (specifier.exported) {
607-
newExportIdentifiers.add(specifier.exported.name);
607+
newExportIdentifiers.add(specifier.exported.name || specifier.exported.value);
608608
}
609609
});
610610
}
@@ -715,8 +715,8 @@ module.exports = {
715715
if (astNode.source) {
716716
resolvedPath = resolve(astNode.source.raw.replace(/('|")/g, ''), context);
717717
astNode.specifiers.forEach(specifier => {
718-
const name = specifier.local.name;
719-
if (specifier.local.name === DEFAULT) {
718+
const name = specifier.local.name || specifier.local.value;
719+
if (name === DEFAULT) {
720720
newDefaultImports.add(resolvedPath);
721721
} else {
722722
newImports.set(name, resolvedPath);
@@ -753,7 +753,7 @@ module.exports = {
753753
specifier.type === IMPORT_NAMESPACE_SPECIFIER) {
754754
return;
755755
}
756-
newImports.set(specifier.imported.name, resolvedPath);
756+
newImports.set(specifier.imported.name || specifier.imported.value, resolvedPath);
757757
});
758758
}
759759
});
@@ -942,7 +942,7 @@ module.exports = {
942942
},
943943
'ExportNamedDeclaration': node => {
944944
node.specifiers.forEach(specifier => {
945-
checkUsage(node, specifier.exported.name);
945+
checkUsage(node, specifier.exported.name || specifier.exported.value);
946946
});
947947
forEachDeclarationIdentifier(node.declaration, (name) => {
948948
checkUsage(node, name);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const foo = 333
2+
export { foo as "foo" }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import { "foo" as foo } from "./arbitrary-module-namespace-identifier-name-a.js"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const foo = 333
2+
export { foo as "foo" }

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { test, testFilePath, getTSParsers } from '../utils';
1+
import { test, testVersion, testFilePath, getTSParsers } from '../utils';
22
import jsxConfig from '../../../config/react';
33
import typescriptConfig from '../../../config/typescript';
44

@@ -1261,3 +1261,33 @@ describe('support (nested) destructuring assignment', () => {
12611261
invalid: [],
12621262
});
12631263
});
1264+
1265+
describe('support ES2022 Arbitrary module namespace identifier names', () => {
1266+
ruleTester.run('no-unused-module', rule, {
1267+
valid: [].concat(
1268+
testVersion('>= 8.7', () => ({
1269+
options: unusedExportsOptions,
1270+
code: `import { "foo" as foo } from "./arbitrary-module-namespace-identifier-name-a"`,
1271+
parserOptions: { ecmaVersion: 2022 },
1272+
filename: testFilePath('./no-unused-modules/arbitrary-module-namespace-identifier-name-b.js'),
1273+
})),
1274+
testVersion('>= 8.7', () => ({
1275+
options: unusedExportsOptions,
1276+
code: 'const foo = 333;\nexport { foo as "foo" }',
1277+
parserOptions: { ecmaVersion: 2022 },
1278+
filename: testFilePath('./no-unused-modules/arbitrary-module-namespace-identifier-name-a.js'),
1279+
})),
1280+
),
1281+
invalid: [].concat(
1282+
testVersion('>= 8.7', () => ({
1283+
options: unusedExportsOptions,
1284+
code: 'const foo = 333\nexport { foo as "foo" }',
1285+
parserOptions: { ecmaVersion: 2022 },
1286+
filename: testFilePath('./no-unused-modules/arbitrary-module-namespace-identifier-name-c.js'),
1287+
errors: [
1288+
error(`exported declaration 'foo' not used within other modules`),
1289+
],
1290+
})),
1291+
),
1292+
});
1293+
});

0 commit comments

Comments
 (0)