Skip to content

Commit

Permalink
feat: release no-import-namespace-destructure
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Nov 17, 2023
1 parent 74b45c0 commit 030c408
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions .README/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ See [ESLint documentation](https://eslint.org/docs/user-guide/configuring/config
{"gitdown": "include", "file": "./rules/import-specifier-newline.md"}
{"gitdown": "include", "file": "./rules/no-barrel-import.md"}
{"gitdown": "include", "file": "./rules/no-export-all.md"}
{"gitdown": "include", "file": "./rules/no-import-namespace-destructure.md"}
{"gitdown": "include", "file": "./rules/no-reassign-imports.md"}
{"gitdown": "include", "file": "./rules/no-restricted-imports.md"}
{"gitdown": "include", "file": "./rules/no-restricted-strings.md"}
Expand Down
7 changes: 7 additions & 0 deletions .README/rules/no-import-namespace-destructure.md
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 -->
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import idMatch from './rules/idMatch';
import importSpecifierNewline from './rules/importSpecifierNewline';
import noBarrelImport from './rules/noBarrelImport';
import noExportAll from './rules/noExportAll';
import noImportNamespaceDestructure from './rules/noImportNamespaceDestructure';
import noReassignImports from './rules/noReassignImports';
import noRestrictedImports from './rules/noRestrictedImports';
import noRestrictedStrings from './rules/noRestrictedStrings';
Expand Down Expand Up @@ -37,6 +38,7 @@ export = {
'import-specifier-newline': importSpecifierNewline,
'no-barrel-import': noBarrelImport,
'no-export-all': noExportAll,
'no-import-namespace-destructure': noImportNamespaceDestructure,
'no-reassign-imports': noReassignImports,
'no-restricted-imports': noRestrictedImports,
'no-restricted-strings': noRestrictedStrings,
Expand Down
51 changes: 51 additions & 0 deletions src/rules/noImportNamespaceDestructure.ts
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',
});
25 changes: 25 additions & 0 deletions tests/rules/noImportNamespaceDestructure.ts
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'`,
},
],
},
);

0 comments on commit 030c408

Please sign in to comment.