Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New] support ES2022 "Arbitrary module namespace identifier names" #2358

Merged
merged 10 commits into from
Jan 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

## [Unreleased]

### Added
- [`no-named-default`, `no-default-export`, `prefer-default-export`, `no-named-export`, `export`, `named`, `namespace`, `no-unused-modules`]: support arbitrary module namespace names ([#2358], thanks [@sosukesuzuki])

### Changed
- [Tests] `no-nodejs-modules`: add tests for node protocol URL ([#2367], thanks [@sosukesuzuki])
- [Tests] `default`, `no-anonymous-default-export`, `no-mutable-exports`, `no-named-as-default-member`, `no-named-as-default`: add tests for arbitrary module namespace names ([#2358], thanks [@sosukesuzuki])

## [2.25.4] - 2022-01-02

Expand Down Expand Up @@ -959,6 +963,7 @@ for info on changes for earlier releases.
[`memo-parser`]: ./memo-parser/README.md

[#2367]: https://github.com/import-js/eslint-plugin-import/pull/2367
[#2358]: https://github.com/import-js/eslint-plugin-import/pull/2358
[#2341]: https://github.com/import-js/eslint-plugin-import/pull/2341
[#2334]: https://github.com/import-js/eslint-plugin-import/pull/2334
[#2305]: https://github.com/import-js/eslint-plugin-import/pull/2305
Expand Down
6 changes: 3 additions & 3 deletions src/ExportMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,11 +479,11 @@ ExportMap.parse = function (path, content, context) {
}));
return;
case 'ExportAllDeclaration':
m.namespace.set(s.exported.name, addNamespace(exportMeta, s.source.value));
m.namespace.set(s.exported.name || s.exported.value, addNamespace(exportMeta, s.source.value));
return;
case 'ExportSpecifier':
if (!n.source) {
m.namespace.set(s.exported.name, addNamespace(exportMeta, s.local));
m.namespace.set(s.exported.name || s.exported.value, addNamespace(exportMeta, s.local));
return;
}
// else falls through
Expand Down 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
2 changes: 1 addition & 1 deletion src/rules/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ module.exports = {
'ExportDefaultDeclaration': (node) => addNamed('default', node, getParent(node)),

'ExportSpecifier': (node) => addNamed(
node.exported.name,
node.exported.name || node.exported.value,
node.exported,
getParent(node.parent),
),
Expand Down
8 changes: 5 additions & 3 deletions src/rules/named.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,19 @@ module.exports = {
return;
}

const deepLookup = imports.hasDeep(im[key].name);
const name = im[key].name || im[key].value;

const deepLookup = imports.hasDeep(name);

if (!deepLookup.found) {
if (deepLookup.path.length > 1) {
const deepPath = deepLookup.path
.map(i => path.relative(path.dirname(context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename()), i.path))
.join(' -> ');

context.report(im[key], `${im[key].name} not found via ${deepPath}`);
context.report(im[key], `${name} not found via ${deepPath}`);
} else {
context.report(im[key], im[key].name + ' not found in \'' + node.source.value + '\'');
context.report(im[key], name + ' not found in \'' + node.source.value + '\'');
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/rules/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ module.exports = {
case 'ImportSpecifier': {
const meta = imports.get(
// default to 'default' for default https://i.imgur.com/nj6qAWy.jpg
specifier.imported ? specifier.imported.name : 'default',
specifier.imported ? (specifier.imported.name || specifier.imported.value) : 'default',
);
if (!meta || !meta.namespace) { break; }
namespaces.set(specifier.local.name, meta.namespace);
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-default-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = {
},

ExportNamedDeclaration(node) {
node.specifiers.filter(specifier => specifier.exported.name === 'default').forEach(specifier => {
node.specifiers.filter(specifier => (specifier.exported.name || specifier.exported.value) === 'default').forEach(specifier => {
const { loc } = context.getSourceCode().getFirstTokens(node)[1] || {};
if (specifier.type === 'ExportDefaultSpecifier') {
context.report({ node, message: preferNamed, loc });
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-named-default.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
return;
}

if (im.type === 'ImportSpecifier' && im.imported.name === 'default') {
if (im.type === 'ImportSpecifier' && (im.imported.name || im.imported.value) === 'default') {
context.report({
node: im.local,
message: `Use default import syntax to import '${im.local.name}'.` });
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-named-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = {
return context.report({ node, message });
}

const someNamed = node.specifiers.some(specifier => specifier.exported.name !== 'default');
const someNamed = node.specifiers.some(specifier => (specifier.exported.name || specifier.exported.value) !== 'default');
if (someNamed) {
context.report({ node, message });
}
Expand Down
10 changes: 5 additions & 5 deletions src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion src/rules/prefer-default-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = {
},

'ExportSpecifier': function (node) {
if (node.exported.name === 'default') {
if ((node.exported.name || node.exported.value) === 'default') {
hasDefaultExport = true;
} else {
specifierExportCount++;
Expand Down
40 changes: 38 additions & 2 deletions tests/files/.eslintrc → tests/files/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
const eslintPkg = require('eslint/package.json');
const semver = require('semver');

const supportsArbitraryModuleNamespaceIdentifierNames = semver.satisfies(eslintPkg.version, '>= 8.7');

const config = {
"parser": "babel-eslint",
"parserOptions": {
"sourceType": "module",
Expand Down Expand Up @@ -281,5 +286,36 @@
"import/no-duplicates": 0,
"import/no-extraneous-dependencies": 0,
"import/unambiguous": 0
}
},
ignorePatterns: [
"default-export-namespace-string.js",
"default-export-string.js",
"export-default-string-and-named.js",
"no-unused-modules/arbitrary-module-namespace-identifier-name-a.js",
"no-unused-modules/arbitrary-module-namespace-identifier-name-b.js",
"no-unused-modules/arbitrary-module-namespace-identifier-name-c.js"
],
}

if (supportsArbitraryModuleNamespaceIdentifierNames) {
config.ignorePatterns = [];
config.overrides = [
// For parsing arbitrary module namespace names
{
"files": [
"default-export-namespace-string.js",
"default-export-string.js",
"export-default-string-and-named.js",
"no-unused-modules/arbitrary-module-namespace-identifier-name-a.js",
"no-unused-modules/arbitrary-module-namespace-identifier-name-b.js",
"no-unused-modules/arbitrary-module-namespace-identifier-name-c.js"
],
"parser": "espree",
"parserOptions": {
"ecmaVersion": 2022
}
}
];
}

module.exports = config;
1 change: 1 addition & 0 deletions tests/files/default-export-namespace-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as "default" from "./named-exports";
3 changes: 3 additions & 0 deletions tests/files/default-export-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function foo() { return 'bar' }

export { foo as "default" }
4 changes: 4 additions & 0 deletions tests/files/export-default-string-and-named.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const bar = "bar";
export function foo() {}

export { bar as "default" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const foo = 333
export { foo as "foo" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { "foo" as foo } from "./arbitrary-module-namespace-identifier-name-a.js"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const foo = 333
export { foo as "foo" }
14 changes: 11 additions & 3 deletions tests/src/rules/default.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import { test, SYNTAX_CASES, getTSParsers } from '../utils';
import { test, testVersion, SYNTAX_CASES, getTSParsers } from '../utils';
import { RuleTester } from 'eslint';

import { CASE_SENSITIVE_FS } from 'eslint-module-utils/resolve';
Expand All @@ -8,7 +8,7 @@ const ruleTester = new RuleTester();
const rule = require('rules/default');

ruleTester.run('default', rule, {
valid: [
valid: [].concat(
test({ code: 'import "./malformed.js"' }),

test({ code: 'import foo from "./empty-folder";' }),
Expand Down Expand Up @@ -92,8 +92,16 @@ ruleTester.run('default', rule, {
parser: require.resolve('babel-eslint'),
}),

// es2022: Arbitrary module namespace identifier names
testVersion('>= 8.7', () => ({
code: 'export { "default" as bar } from "./bar"',
parserOptions: {
ecmaVersion: 2022,
},
})),

...SYNTAX_CASES,
],
),

invalid: [
test({
Expand Down
16 changes: 14 additions & 2 deletions tests/src/rules/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ ruleTester.run('export', rule, {
})) || [],
),

invalid: [
invalid: [].concat(
// multiple defaults
// test({
// code: 'export default foo; export default bar',
Expand Down Expand Up @@ -122,7 +122,19 @@ ruleTester.run('export', rule, {
code: 'export * from "./default-export"',
errors: [`No named exports found in module './default-export'.`],
}),
],

// es2022: Arbitrary module namespace identifier names
testVersion('>= 8.7', () => ({
code: 'let foo; export { foo as "foo" }; export * from "./export-all"',
errors: [
'Multiple exports of name \'foo\'.',
'Multiple exports of name \'foo\'.',
],
parserOptions: {
ecmaVersion: 2022,
},
})),
),
});


Expand Down
32 changes: 27 additions & 5 deletions tests/src/rules/named.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import { CASE_SENSITIVE_FS } from 'eslint-module-utils/resolve';
const ruleTester = new RuleTester();
const rule = require('rules/named');

function error(name, module) {
return { message: name + ' not found in \'' + module + '\'',
type: 'Identifier' };
function error(name, module, type = 'Identifier') {
return { message: name + ' not found in \'' + module + '\'', type };
}

ruleTester.run('named', rule, {
Expand Down Expand Up @@ -199,10 +198,16 @@ ruleTester.run('named', rule, {

testVersion('>=7.8.0', () => ({ code: 'import { something } from "./dynamic-import-in-commonjs"',
parserOptions: { ecmaVersion: 2021 } })),

// es2022: Arbitrary module namespace identifier names
testVersion('>= 8.7', () => ({
code: 'import { "foo" as foo } from "./bar"', parserOptions: { ecmaVersion: 2022 } })),
testVersion('>= 8.7', () => ({
code: 'import { "foo" as foo } from "./empty-module"', parserOptions: { ecmaVersion: 2022 } })),
),
],

invalid: [
invalid: [].concat(
test({ code: 'import { somethingElse } from "./test-module"',
errors: [ error('somethingElse', './test-module') ] }),

Expand Down Expand Up @@ -323,7 +328,24 @@ ruleTester.run('named', rule, {
code: 'import { default as barDefault } from "./re-export"',
errors: [`default not found in './re-export'`],
}),
],

// es2022: Arbitrary module namespace identifier names
testVersion('>= 8.7', () => ({
code: 'import { "somethingElse" as somethingElse } from "./test-module"',
errors: [ error('somethingElse', './test-module', 'Literal') ],
parserOptions: { ecmaVersion: 2022 },
})),
testVersion('>= 8.7', () => ({
code: 'import { "baz" as baz, "bop" as bop } from "./bar"',
errors: [error('baz', './bar', 'Literal'), error('bop', './bar', 'Literal')],
parserOptions: { ecmaVersion: 2022 },
})),
testVersion('>= 8.7', () => ({
code: 'import { "default" as barDefault } from "./re-export"',
errors: [`default not found in './re-export'`],
parserOptions: { ecmaVersion: 2022 },
})),
),
});

// #311: import of mismatched case
Expand Down
Loading