|
| 1 | +/** |
| 2 | + * @author dev1437 |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +const utils = require('../utils') |
| 12 | + |
| 13 | +/** |
| 14 | + * Split the source code into multiple lines based on the line delimiters. |
| 15 | + * Copied from padding-line-between-blocks |
| 16 | + * @param {string} text Source code as a string. |
| 17 | + * @returns {string[]} Array of source code lines. |
| 18 | + */ |
| 19 | +function splitLines(text) { |
| 20 | + return text.split(/\r\n|[\r\n\u2028\u2029]/gu) |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * @param {RuleContext} context |
| 25 | + * @param {VElement} tag |
| 26 | + * @param {VElement} sibling |
| 27 | + */ |
| 28 | +function insertNewLine(context, tag, sibling) { |
| 29 | + context.report({ |
| 30 | + messageId: 'always', |
| 31 | + loc: sibling.loc, |
| 32 | + // @ts-ignore |
| 33 | + fix(fixer) { |
| 34 | + return fixer.insertTextAfter(tag, '\n') |
| 35 | + } |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * @param {RuleContext} context |
| 41 | + * @param {VEndTag | VStartTag} endTag |
| 42 | + * @param {VElement} sibling |
| 43 | + */ |
| 44 | +function removeExcessLines(context, endTag, sibling) { |
| 45 | + context.report({ |
| 46 | + messageId: 'never', |
| 47 | + loc: sibling.loc, |
| 48 | + // @ts-ignore |
| 49 | + fix(fixer) { |
| 50 | + const start = endTag.range[1] |
| 51 | + const end = sibling.range[0] |
| 52 | + const paddingText = context.getSourceCode().text.slice(start, end) |
| 53 | + const textBetween = splitLines(paddingText) |
| 54 | + let newTextBetween = `\n${textBetween.pop()}` |
| 55 | + for (let i = textBetween.length - 1; i >= 0; i--) { |
| 56 | + if (!/^\s*$/.test(textBetween[i])) { |
| 57 | + newTextBetween = `${i === 0 ? '' : '\n'}${ |
| 58 | + textBetween[i] |
| 59 | + }${newTextBetween}` |
| 60 | + } |
| 61 | + } |
| 62 | + return fixer.replaceTextRange([start, end], `${newTextBetween}`) |
| 63 | + } |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +// ------------------------------------------------------------------------------ |
| 68 | +// Rule Definition |
| 69 | +// ------------------------------------------------------------------------------ |
| 70 | + |
| 71 | +/** |
| 72 | + * @param {RuleContext} context |
| 73 | + */ |
| 74 | +function checkNewline(context) { |
| 75 | + /** @type {Array<{blankLine: "always" | "never", prev: string, next: string}>} */ |
| 76 | + const configureList = context.options[0] || [ |
| 77 | + { blankLine: 'always', prev: '*', next: '*' } |
| 78 | + ] |
| 79 | + |
| 80 | + /** |
| 81 | + * @param {VElement} block |
| 82 | + */ |
| 83 | + return (block) => { |
| 84 | + if (!block.parent.parent) { |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + const endTag = block.endTag || block.startTag |
| 89 | + const lowerSiblings = block.parent.children |
| 90 | + .filter( |
| 91 | + (element) => |
| 92 | + element.type === 'VElement' && element.range !== block.range |
| 93 | + ) |
| 94 | + .filter((sibling) => sibling.range[0] - endTag.range[1] >= 0) |
| 95 | + |
| 96 | + if (lowerSiblings.length === 0) { |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + const closestSibling = /** @type {VElement} */ (lowerSiblings[0]) |
| 101 | + |
| 102 | + for (let i = configureList.length - 1; i >= 0; --i) { |
| 103 | + const configure = configureList[i] |
| 104 | + const matched = |
| 105 | + (configure.prev === '*' || block.name === configure.prev) && |
| 106 | + (configure.next === '*' || closestSibling.name === configure.next) |
| 107 | + |
| 108 | + if (matched) { |
| 109 | + const lineDifference = |
| 110 | + closestSibling.loc.start.line - endTag.loc.end.line |
| 111 | + if (configure.blankLine === 'always') { |
| 112 | + if (lineDifference === 1) { |
| 113 | + insertNewLine(context, block, closestSibling) |
| 114 | + } else if (lineDifference === 0) { |
| 115 | + context.report({ |
| 116 | + messageId: 'always', |
| 117 | + loc: closestSibling.loc, |
| 118 | + // @ts-ignore |
| 119 | + fix(fixer) { |
| 120 | + const lastSpaces = /** @type {RegExpExecArray} */ ( |
| 121 | + /^\s*/.exec( |
| 122 | + context.getSourceCode().lines[endTag.loc.start.line - 1] |
| 123 | + ) |
| 124 | + )[0] |
| 125 | + |
| 126 | + return fixer.insertTextAfter(endTag, `\n\n${lastSpaces}`) |
| 127 | + } |
| 128 | + }) |
| 129 | + } |
| 130 | + } else { |
| 131 | + if (lineDifference > 1) { |
| 132 | + let hasOnlyTextBetween = true |
| 133 | + for ( |
| 134 | + let i = endTag.loc.start.line; |
| 135 | + i < closestSibling.loc.start.line - 1 && hasOnlyTextBetween; |
| 136 | + i++ |
| 137 | + ) { |
| 138 | + hasOnlyTextBetween = !/^\s*$/.test( |
| 139 | + context.getSourceCode().lines[i] |
| 140 | + ) |
| 141 | + } |
| 142 | + if (!hasOnlyTextBetween) { |
| 143 | + removeExcessLines(context, endTag, closestSibling) |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + break |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +module.exports = { |
| 154 | + meta: { |
| 155 | + type: 'layout', |
| 156 | + docs: { |
| 157 | + description: |
| 158 | + 'require or disallow newlines between sibling tags in template', |
| 159 | + categories: undefined, |
| 160 | + url: 'https://eslint.vuejs.org/rules/padding-line-between-tags.html' |
| 161 | + }, |
| 162 | + fixable: 'whitespace', |
| 163 | + schema: [ |
| 164 | + { |
| 165 | + type: 'array', |
| 166 | + items: { |
| 167 | + type: 'object', |
| 168 | + properties: { |
| 169 | + blankLine: { enum: ['always', 'never'] }, |
| 170 | + prev: { type: 'string' }, |
| 171 | + next: { type: 'string' } |
| 172 | + }, |
| 173 | + additionalProperties: false, |
| 174 | + required: ['blankLine', 'prev', 'next'] |
| 175 | + } |
| 176 | + } |
| 177 | + ], |
| 178 | + messages: { |
| 179 | + never: 'Unexpected blank line before this tag.', |
| 180 | + always: 'Expected blank line before this tag.' |
| 181 | + } |
| 182 | + }, |
| 183 | + /** @param {RuleContext} context */ |
| 184 | + create(context) { |
| 185 | + return utils.defineTemplateBodyVisitor(context, { |
| 186 | + VElement: checkNewline(context) |
| 187 | + }) |
| 188 | + } |
| 189 | +} |
0 commit comments