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

[no-named-as-default] Add ignorePaths option #3131

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions docs/rules/no-named-as-default.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ export foo from './foo.js';
export bar from './foo.js';
```

## Options

This rule has an option for ignoring specific paths. This is useful for cases where the default export is aliased to a named export, or where the default export is not used.

```json
{
"rules": {
"import/no-named-as-default": ["warn", { "ignorePaths": ["./foo.js"] }]
}
}
```

## Further Reading

- ECMAScript Proposal: [export ns from]
Expand Down
25 changes: 24 additions & 1 deletion src/rules/no-named-as-default.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ExportMapBuilder from '../exportMap/builder';
import importDeclaration from '../importDeclaration';
import includes from 'array-includes';
import docsUrl from '../docsUrl';

module.exports = {
Expand All @@ -10,10 +11,27 @@ module.exports = {
description: 'Forbid use of exported name as identifier of default export.',
url: docsUrl('no-named-as-default'),
},
schema: [],
schema: [
{
type: 'object',
properties: {
ignorePaths: {
type: 'array',
items: {
type: 'string',
},
uniqueItems: true,
},
},
additionalProperties: false,
},
],
},

create(context) {
const options = context.options[0] || {};
const ignorePaths = options.ignorePaths || [];

function checkDefault(nameKey, defaultSpecifier) {
/**
* For ImportDefaultSpecifier we're interested in the "local" name (`foo` for `import {bar as foo} ...`)
Expand Down Expand Up @@ -45,6 +63,11 @@ module.exports = {
return;
}

if (includes(ignorePaths, declaration.source.value)) {
// The user has explicitly ignored this path
return;
}

/**
* FIXME: We can verify if a default and a named export are pointing to the same symbol only
* if they are both `reexports`. In case one of the symbols is not a re-export, but defined
Expand Down
6 changes: 6 additions & 0 deletions tests/src/rules/no-named-as-default.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ ruleTester.run('no-named-as-default', rule, {
test({
code: 'import variable from "./no-named-as-default/misleading-re-exports.js";',
}),
test({
code: 'import foo from "./bar";',
options: [{
ignorePaths: ['./bar'],
}],
}),
test({
// incorrect import
code: 'import foobar from "./no-named-as-default/no-default-export.js";',
Expand Down
Loading