UNPKG

eslint-plugin-putout

Version:
67 lines (51 loc) 2.06 kB
import {types} from 'putout'; const {isObjectPattern} = types; export default { meta: { type: 'suggestion', docs: { description: 'Remove newline between declarations', category: 'putout', recommended: true, }, fixable: 'code', }, create(context) { return { VariableDeclaration(node) { const {sourceCode} = context; const text = sourceCode.getText(node); const newline = sourceCode .getText(node, 0, 2) .replace(text, ''); if (newline !== '\n\n') return; const nextNode = sourceCode.getNodeByRangeIndex(node.range[1] + 2); if (!nextNode || nextNode.type !== 'VariableDeclaration') return; const nodeId = node.declarations[0].id; if (!isObjectPattern(nodeId)) return; if (nodeId.properties.length !== 1) return; const textId = sourceCode.getText(nodeId.properties[0].value); const nextNodeInit = nextNode.declarations[0].init; const nextTextInit = sourceCode.getText(nextNodeInit); if (textId !== nextTextInit) return; context.report({ node, message: 'Remove empty newline between declarations', fix(fixer) { return [ fixer.removeRange([ node.range[1], node.range[1] + 1, ]), ]; }, }); }, }; }, };