eslint-plugin-canonical
Version:
Canonical linting rules for ESLint.
86 lines (85 loc) • 3.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("@typescript-eslint/types");
const utilities_1 = require("../utilities");
const messages = {
noReassignImports: 'Do not reassign imports.',
};
exports.default = (0, utilities_1.createRule)({
create: (context) => {
const imported = [];
return {
ExportDefaultDeclaration(node) {
const declaration = node.declaration;
if ((declaration === null || declaration === void 0 ? void 0 : declaration.type) !== 'ObjectExpression') {
return;
}
for (const property of declaration.properties) {
if (property.type !== 'Property') {
continue;
}
const key = property.key;
if (key.type !== 'Identifier') {
continue;
}
if (imported.includes(key.name)) {
context.report({
messageId: 'noReassignImports',
node: key,
});
}
}
},
ExportNamedDeclaration(node) {
const declaration = node.declaration;
if ((declaration === null || declaration === void 0 ? void 0 : declaration.type) !== 'VariableDeclaration') {
return;
}
const variableDeclaration = declaration.declarations[0];
if (variableDeclaration.type !== 'VariableDeclarator') {
return;
}
const init = variableDeclaration.init;
if ((init === null || init === void 0 ? void 0 : init.type) !== 'ObjectExpression') {
return;
}
for (const property of init.properties) {
if (property.type !== 'Property') {
continue;
}
const key = property.key;
if (key.type !== 'Identifier') {
continue;
}
if (imported.includes(key.name)) {
context.report({
messageId: 'noReassignImports',
node: key,
});
}
}
},
ImportDeclaration(node) {
for (const specifier of node.specifiers) {
if (specifier.type !== types_1.AST_NODE_TYPES.ImportSpecifier) {
continue;
}
if (specifier.imported.type !== types_1.AST_NODE_TYPES.Identifier) {
continue;
}
imported.push(specifier.imported.name);
}
},
};
},
defaultOptions: [],
meta: {
docs: {
description: 'Restricts re-assigning imports to variables that are exported.',
},
messages,
schema: [],
type: 'suggestion',
},
name: 'no-reassign-imports',
});