UNPKG

eslint-plugin-canonical

Version:
65 lines (64 loc) 2.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const utilities_1 = require("../utilities"); exports.default = (0, utilities_1.createRule)({ create: (context) => { return { ImportDeclaration: (node) => { var _a; const sourceCode = (_a = context.sourceCode) !== null && _a !== void 0 ? _a : context.getSourceCode(); const importSpecifiers = node.specifiers.filter((specifier) => { return specifier.type === 'ImportSpecifier'; }); for (let index = 1; index < importSpecifiers.length; index++) { const lastTokenOfPreviousProperty = sourceCode.getLastToken(importSpecifiers[index - 1]); const firstTokenOfCurrentProperty = sourceCode.getFirstToken(importSpecifiers[index]); if (lastTokenOfPreviousProperty === null) { continue; } if (firstTokenOfCurrentProperty === null) { continue; } if (lastTokenOfPreviousProperty.loc.end.line === 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: '', }, fixable: 'whitespace', messages: { specifiersOnNewline: 'Import specifiers must go on a new line.', }, schema: [], type: 'layout', }, name: 'import-specifiers-newline', });