-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: release no-import-namespace-destructure
- Loading branch information
Showing
5 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
### `no-import-namespace-destructure` | ||
|
||
_The `--fix` option on the command line automatically fixes problems reported by this rule._ | ||
|
||
Disallows the practice of importing an entire module's namespace using import * as Namespace and then destructuring specific exports from it. Instead, it encourages direct importing of only the necessary named exports from a module. | ||
|
||
<!-- assertions noImportNamespaceDestructure --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { createRule } from '../utilities'; | ||
|
||
type Options = []; | ||
|
||
type MessageIds = 'noDestructureNamespace'; | ||
|
||
export default createRule<Options, MessageIds>({ | ||
create: (context) => { | ||
return { | ||
VariableDeclarator(node) { | ||
if ( | ||
node.id.type === 'ObjectPattern' && | ||
node.init && | ||
node.init.type === 'Identifier' | ||
) { | ||
const importDeclaration = context | ||
.getScope() | ||
// @ts-expect-error we expect .name to be set | ||
.variables.find((variable) => variable.name === node.init?.name) | ||
?.defs[0]?.parent; | ||
if ( | ||
importDeclaration && | ||
importDeclaration.type === 'ImportDeclaration' && | ||
importDeclaration.specifiers.some( | ||
(specifier) => specifier.type === 'ImportNamespaceSpecifier', | ||
) | ||
) { | ||
context.report({ | ||
messageId: 'noDestructureNamespace', | ||
node, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
defaultOptions: [], | ||
meta: { | ||
docs: { | ||
description: | ||
"Disallows the practice of importing an entire module's namespace using import * as Namespace and then destructuring specific exports from it. Instead, it encourages direct importing of only the necessary named exports from a module.", | ||
}, | ||
messages: { | ||
noDestructureNamespace: | ||
'Do not destructure namespace imports; import only specific members needed.', | ||
}, | ||
schema: [], | ||
type: 'layout', | ||
}, | ||
name: 'no-import-namespace-destructure', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import rule from '../../src/rules/noImportNamespaceDestructure'; | ||
import { createRuleTester } from '../RuleTester'; | ||
|
||
export default createRuleTester( | ||
'no-import-namespace-destructure', | ||
rule, | ||
{ parser: '@typescript-eslint/parser' }, | ||
{ | ||
invalid: [ | ||
{ | ||
code: `import * as bar from 'bar'; const { foo } = bar;`, | ||
errors: [ | ||
{ | ||
messageId: 'noDestructureNamespace', | ||
}, | ||
], | ||
}, | ||
], | ||
valid: [ | ||
{ | ||
code: `import * as bar from 'bar'`, | ||
}, | ||
], | ||
}, | ||
); |