Skip to content

Commit

Permalink
[New] namespace: 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 8cd3a0e commit 37126ec
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
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`, `named`]: support arbitrary module namespace names ([#2358], thanks [@sosukesuzuki])
- [`no-named-default`, `no-default-export`, `prefer-default-export`, `no-named-export`, `export`, `named`, `namespace`]: support arbitrary module namespace names ([#2358], thanks [@sosukesuzuki])

### Changed
- [Tests] `no-nodejs-modules`: add tests for node protocol URL ([#2367], thanks [@sosukesuzuki])
Expand Down
4 changes: 2 additions & 2 deletions src/ExportMap.js
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
2 changes: 1 addition & 1 deletion src/rules/namespace.js
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
1 change: 1 addition & 0 deletions tests/files/default-export-namespace-string.js
@@ -0,0 +1 @@
export * as "default" from "./named-exports";
3 changes: 3 additions & 0 deletions tests/files/default-export-string.js
@@ -0,0 +1,3 @@
function foo() { return 'bar' }

export { foo as "default" }
42 changes: 39 additions & 3 deletions tests/src/rules/namespace.js
Expand Up @@ -183,10 +183,35 @@ const valid = [
parserOptions: {
ecmaVersion: 2020,
},
})) || []),
})),
// es2022: Arbitrary module namespace identifier names
testVersion('>= 8.7', () => ({
code: "import * as names from './default-export-string';",
parserOptions: { ecmaVersion: 2022 },
})),
testVersion('>= 8.7', () => ({
code: "import * as names from './default-export-string'; console.log(names.default)",
parserOptions: { ecmaVersion: 2022 },
})),
testVersion('>= 8.7', () => ({
code: "import * as names from './default-export-namespace-string';",
parserOptions: { ecmaVersion: 2022 },
})),
testVersion('>= 8.7', () => ({
code: "import * as names from './default-export-namespace-string'; console.log(names.default)",
parserOptions: { ecmaVersion: 2022 },
})),
testVersion('>= 8.7', () => ({
code: `import { "b" as b } from "./deep/a"; console.log(b.c.d.e)`,
parserOptions: { ecmaVersion: 2022 },
})),
testVersion('>= 8.7', () => ({
code: `import { "b" as b } from "./deep/a"; var {c:{d:{e}}} = b`,
parserOptions: { ecmaVersion: 2022 },
}))),
];

const invalid = [
const invalid = [].concat(
test({ code: "import * as names from './named-exports'; " +
' console.log(names.c);',
errors: [error('c', 'names')] }),
Expand Down Expand Up @@ -275,7 +300,18 @@ const invalid = [
},
}),

]
// es2022: Arbitrary module namespace identifier names
testVersion('>= 8.7', () => ({
code: `import { "b" as b } from "./deep/a"; console.log(b.e)`,
errors: [ "'e' not found in imported namespace 'b'." ],
parserOptions: { ecmaVersion: 2022 },
})),
testVersion('>= 8.7', () => ({
code: `import { "b" as b } from "./deep/a"; console.log(b.c.e)`,
errors: [ "'e' not found in deeply imported namespace 'b.c'." ],
parserOptions: { ecmaVersion: 2022 },
})),
)

///////////////////////
// deep dereferences //
Expand Down

0 comments on commit 37126ec

Please sign in to comment.