Skip to content

Commit

Permalink
[New] no-unused-modules: support arbitrary module namespace names
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki authored and ljharb committed Jan 17, 2022
1 parent 8a6730f commit 4e93a84
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/ExportMap.js
Expand Up @@ -593,7 +593,7 @@ ExportMap.parse = function (path, content, context) {
importedSpecifiers.add(specifier.type);
}
if (specifier.type === 'ImportSpecifier') {
importedSpecifiers.add(specifier.imported.name);
importedSpecifiers.add(specifier.imported.name || specifier.imported.value);
}

// import { type Foo } (Flow)
Expand Down
10 changes: 5 additions & 5 deletions src/rules/no-unused-modules.js
Expand Up @@ -604,7 +604,7 @@ module.exports = {
if (specifiers.length > 0) {
specifiers.forEach(specifier => {
if (specifier.exported) {
newExportIdentifiers.add(specifier.exported.name);
newExportIdentifiers.add(specifier.exported.name || specifier.exported.value);
}
});
}
Expand Down Expand Up @@ -715,8 +715,8 @@ module.exports = {
if (astNode.source) {
resolvedPath = resolve(astNode.source.raw.replace(/('|")/g, ''), context);
astNode.specifiers.forEach(specifier => {
const name = specifier.local.name;
if (specifier.local.name === DEFAULT) {
const name = specifier.local.name || specifier.local.value;
if (name === DEFAULT) {
newDefaultImports.add(resolvedPath);
} else {
newImports.set(name, resolvedPath);
Expand Down Expand Up @@ -753,7 +753,7 @@ module.exports = {
specifier.type === IMPORT_NAMESPACE_SPECIFIER) {
return;
}
newImports.set(specifier.imported.name, resolvedPath);
newImports.set(specifier.imported.name || specifier.imported.value, resolvedPath);
});
}
});
Expand Down Expand Up @@ -942,7 +942,7 @@ module.exports = {
},
'ExportNamedDeclaration': node => {
node.specifiers.forEach(specifier => {
checkUsage(node, specifier.exported.name);
checkUsage(node, specifier.exported.name || specifier.exported.value);
});
forEachDeclarationIdentifier(node.declaration, (name) => {
checkUsage(node, name);
Expand Down
@@ -0,0 +1,2 @@
const foo = 333
export { foo as "foo" }
@@ -0,0 +1 @@
import { "foo" as foo } from "./arbitrary-module-namespace-identifier-name-a.js"
@@ -0,0 +1,2 @@
const foo = 333
export { foo as "foo" }
32 changes: 31 additions & 1 deletion tests/src/rules/no-unused-modules.js
@@ -1,4 +1,4 @@
import { test, testFilePath, getTSParsers } from '../utils';
import { test, testVersion, testFilePath, getTSParsers } from '../utils';
import jsxConfig from '../../../config/react';
import typescriptConfig from '../../../config/typescript';

Expand Down Expand Up @@ -1261,3 +1261,33 @@ describe('support (nested) destructuring assignment', () => {
invalid: [],
});
});

describe('support ES2022 Arbitrary module namespace identifier names', () => {
ruleTester.run('no-unused-module', rule, {
valid: [].concat(
testVersion('>= 8.7', () => ({
options: unusedExportsOptions,
code: `import { "foo" as foo } from "./arbitrary-module-namespace-identifier-name-a"`,
parserOptions: { ecmaVersion: 2022 },
filename: testFilePath('./no-unused-modules/arbitrary-module-namespace-identifier-name-b.js'),
})),
testVersion('>= 8.7', () => ({
options: unusedExportsOptions,
code: 'const foo = 333;\nexport { foo as "foo" }',
parserOptions: { ecmaVersion: 2022 },
filename: testFilePath('./no-unused-modules/arbitrary-module-namespace-identifier-name-a.js'),
})),
),
invalid: [].concat(
testVersion('>= 8.7', () => ({
options: unusedExportsOptions,
code: 'const foo = 333\nexport { foo as "foo" }',
parserOptions: { ecmaVersion: 2022 },
filename: testFilePath('./no-unused-modules/arbitrary-module-namespace-identifier-name-c.js'),
errors: [
error(`exported declaration 'foo' not used within other modules`),
],
})),
),
});
});

0 comments on commit 4e93a84

Please sign in to comment.