Skip to content

Commit

Permalink
[New] named: 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 16, 2022
1 parent 4382b34 commit 8cd3a0e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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`]: support arbitrary module namespace names ([#2358], thanks [@sosukesuzuki])
- [`no-named-default`, `no-default-export`, `prefer-default-export`, `no-named-export`, `export`, `named`]: support arbitrary module namespace names ([#2358], thanks [@sosukesuzuki])

### Changed
- [Tests] `no-nodejs-modules`: add tests for node protocol URL ([#2367], thanks [@sosukesuzuki])
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
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

0 comments on commit 8cd3a0e

Please sign in to comment.