eslint-plugin-canonical
Version:
Canonical linting rules for ESLint.
59 lines (58 loc) • 2.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const utilities_1 = require("../utilities");
exports.default = (0, utilities_1.createRule)({
create: (context) => {
return {
ExportNamedDeclaration: (node) => {
var _a;
const sourceCode = (_a = context.sourceCode) !== null && _a !== void 0 ? _a : context.getSourceCode();
for (let index = 1; index < node.specifiers.length; index++) {
const lastTokenOfPreviousProperty = sourceCode.getLastToken(node.specifiers[index - 1]);
const firstTokenOfCurrentProperty = sourceCode.getFirstToken(node.specifiers[index]);
if (lastTokenOfPreviousProperty === null) {
return;
}
if ((lastTokenOfPreviousProperty === null || lastTokenOfPreviousProperty === void 0 ? void 0 : lastTokenOfPreviousProperty.loc.end.line) ===
(firstTokenOfCurrentProperty === null || firstTokenOfCurrentProperty === void 0 ? void 0 : firstTokenOfCurrentProperty.loc.start.line)) {
context.report({
fix(fixer) {
const comma = sourceCode.getTokenBefore(firstTokenOfCurrentProperty);
if (comma === null) {
return null;
}
const rangeAfterComma = [
comma.range[1],
firstTokenOfCurrentProperty.range[0],
];
// Don't perform a fix if there are any comments between the comma and the next property.
if (sourceCode.text
.slice(rangeAfterComma[0], rangeAfterComma[1])
.trim()) {
return null;
}
return fixer.replaceTextRange(rangeAfterComma, '\n');
},
loc: firstTokenOfCurrentProperty.loc,
messageId: 'specifiersOnNewline',
node,
});
}
}
},
};
},
defaultOptions: [],
meta: {
docs: {
description: 'Forces every export specifier to be on a new line.',
},
fixable: 'whitespace',
messages: {
specifiersOnNewline: 'Export specifiers must go on a new line.',
},
schema: [],
type: 'layout',
},
name: 'export-specifier-newline',
});