eslint-plugin-canonical
Version:
Canonical linting rules for ESLint.
61 lines (60 loc) • 2.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const utilities_1 = require("../utilities");
const removeTypeSpecifier = function* (fixer, sourceCode, node) {
const importKeyword = sourceCode.getFirstToken(node);
const typeIdentifier = sourceCode.getTokenAfter(importKeyword);
yield fixer.remove(typeIdentifier);
if (importKeyword.loc.end.column + 1 === typeIdentifier.loc.start.column) {
yield fixer.removeRange([
importKeyword.range[1],
importKeyword.range[1] + 1,
]);
}
};
exports.default = (0, utilities_1.createRule)({
create: (context) => {
var _a;
const sourceCode = (_a = context.sourceCode) !== null && _a !== void 0 ? _a : context.getSourceCode();
return {
ImportDeclaration: (node) => {
var _a, _b;
if (node.importKind !== 'type') {
return;
}
// import type Foo from 'foo';
if (((_a = node.specifiers[0]) === null || _a === void 0 ? void 0 : _a.type) === 'ImportDefaultSpecifier') {
return;
}
// import type * as Foo from 'foo';
if (((_b = node.specifiers[0]) === null || _b === void 0 ? void 0 : _b.type) === 'ImportNamespaceSpecifier') {
return;
}
context.report({
*fix(fixer) {
yield* removeTypeSpecifier(fixer, sourceCode, node);
for (const specifier of node.specifiers) {
yield fixer.insertTextBefore(specifier, 'type ');
}
},
loc: node.loc,
messageId: 'noTypeImport',
node,
});
},
};
},
defaultOptions: [],
meta: {
docs: {
description: '',
},
fixable: 'code',
messages: {
noTypeImport: 'Type imports must be inlined',
},
schema: [],
type: 'suggestion',
},
name: 'prefer-inline-type-imports',
});