UNPKG

eslint-plugin-vue

Version:

Official ESLint plugin for Vue.js

89 lines (88 loc) • 2.93 kB
const require_index = require("../utils/index.js"); // lib/rules/new-line-between-multi-line-property.ts /** * @fileoverview Enforce new lines between multi-line properties in Vue components. * @author IWANABETHATGUY */ function isComma(node) { return node.type === "Punctuator" && node.value === ","; } /** * Check whether the between given nodes has empty line. */ function* iterateBetweenTokens(sourceCode, pre, cur) { yield sourceCode.getLastToken(pre); yield* sourceCode.getTokensBetween(pre, cur, { includeComments: true }); yield sourceCode.getFirstToken(cur); } /** * Check whether the between given nodes has empty line. */ function hasEmptyLine(sourceCode, pre, cur) { let preToken = null; for (const token of iterateBetweenTokens(sourceCode, pre, cur)) { if (preToken && token.loc.start.line - preToken.loc.end.line >= 2) return true; preToken = token; } return false; } var new_line_between_multi_line_property_default = { meta: { type: "layout", docs: { description: "enforce new lines between multi-line properties in Vue components", categories: void 0, url: "https://eslint.vuejs.org/rules/new-line-between-multi-line-property.html" }, fixable: "whitespace", schema: [{ type: "object", properties: { minLineOfMultilineProperty: { type: "number", minimum: 2 } }, additionalProperties: false }], messages: { missingEmptyLine: "Enforce new lines between multi-line properties in Vue components." } }, create(context) { const minLineOfMultilineProperty = context.options && context.options[0] && context.options[0].minLineOfMultilineProperty ? context.options[0].minLineOfMultilineProperty : 2; const callStack = []; const sourceCode = context.sourceCode; return Object.assign(require_index.default.defineVueVisitor(context, { CallExpression(node) { callStack.push(node); }, "CallExpression:exit"() { callStack.pop(); }, ObjectExpression(node) { if (callStack.length > 0) return; const properties = node.properties; for (let i = 1; i < properties.length; i++) { const cur = properties[i]; const pre = properties[i - 1]; if (pre.loc.end.line - pre.loc.start.line + 1 < minLineOfMultilineProperty || hasEmptyLine(sourceCode, pre, cur)) continue; context.report({ node: pre, loc: { start: pre.loc.end, end: cur.loc.start }, messageId: "missingEmptyLine", fix(fixer) { let preToken = null; for (const token of iterateBetweenTokens(sourceCode, pre, cur)) { if (preToken && preToken.loc.end.line < token.loc.start.line) return fixer.insertTextAfter(preToken, "\n"); preToken = token; } const commaToken = sourceCode.getTokenAfter(pre, isComma); return fixer.insertTextAfter(commaToken || pre, "\n\n"); } }); } } })); } }; exports.default = new_line_between_multi_line_property_default;