eslint-plugin-canonical
Version:
Canonical linting rules for ESLint.
45 lines (44 loc) • 2.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const utilities_1 = require("../utilities");
exports.default = (0, utilities_1.createRule)({
create: (context) => {
var _a;
const sourceCode = (_a = context.sourceCode) !== null && _a !== void 0 ? _a : context.getSourceCode();
return {
VariableDeclarator(node) {
var _a, _b;
if (node.id.type === 'ObjectPattern' &&
node.init &&
node.init.type === 'Identifier') {
const scope = sourceCode.getScope
? sourceCode.getScope(node)
: context.getScope();
const importDeclaration = (_b = (_a = scope
// @ts-expect-error we expect .name to be set
.variables.find((variable) => { var _a; return variable.name === ((_a = node.init) === null || _a === void 0 ? void 0 : _a.name); })) === null || _a === void 0 ? void 0 : _a.defs[0]) === null || _b === void 0 ? void 0 : _b.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',
});