Skip to content

Commit

Permalink
Add ignorePaths option to no-named-as-default include tests and doc u…
Browse files Browse the repository at this point in the history
…pdate
  • Loading branch information
Sahar541998 committed Dec 26, 2024
1 parent e5edf49 commit 32173f1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
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", { "ignore": ["./foo.js"] }]
}
}
```

## Further Reading

- ECMAScript Proposal: [export ns from]
Expand Down
24 changes: 23 additions & 1 deletion src/rules/no-named-as-default.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,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 +62,11 @@ module.exports = {
return;
}

if (ignorePaths.includes(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

0 comments on commit 32173f1

Please sign in to comment.