|
| 1 | +/** |
| 2 | + * @author Yosuke Ota |
| 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 | +// Helpers |
| 15 | +// ------------------------------------------------------------------------------ |
| 16 | + |
| 17 | +function isMultilineElement (element) { |
| 18 | + return element.loc.start.line < element.endTag.loc.start.line |
| 19 | +} |
| 20 | + |
| 21 | +function parseOptions (options) { |
| 22 | + return Object.assign({ |
| 23 | + 'ignores': ['pre', 'textarea'] |
| 24 | + }, options) |
| 25 | +} |
| 26 | + |
| 27 | +function getPhrase (lineBreaks) { |
| 28 | + switch (lineBreaks) { |
| 29 | + case 0: return 'no' |
| 30 | + default: return `${lineBreaks}` |
| 31 | + } |
| 32 | +} |
| 33 | +/** |
| 34 | + * Check whether the given element is empty or not. |
| 35 | + * This ignores whitespaces, doesn't ignore comments. |
| 36 | + * @param {VElement} node The element node to check. |
| 37 | + * @param {SourceCode} sourceCode The source code object of the current context. |
| 38 | + * @returns {boolean} `true` if the element is empty. |
| 39 | + */ |
| 40 | +function isEmpty (node, sourceCode) { |
| 41 | + const start = node.startTag.range[1] |
| 42 | + const end = node.endTag.range[0] |
| 43 | + return sourceCode.text.slice(start, end).trim() === '' |
| 44 | +} |
| 45 | + |
| 46 | +// ------------------------------------------------------------------------------ |
| 47 | +// Rule Definition |
| 48 | +// ------------------------------------------------------------------------------ |
| 49 | + |
| 50 | +module.exports = { |
| 51 | + meta: { |
| 52 | + docs: { |
| 53 | + description: 'require a line break before and after the contents of a multiline element', |
| 54 | + category: undefined, |
| 55 | + url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.2/docs/rules/multiline-html-element-content-newline.md' |
| 56 | + }, |
| 57 | + fixable: 'whitespace', |
| 58 | + schema: [{ |
| 59 | + type: 'object', |
| 60 | + properties: { |
| 61 | + 'ignores': { |
| 62 | + type: 'array', |
| 63 | + items: { type: 'string' }, |
| 64 | + uniqueItems: true, |
| 65 | + additionalItems: false |
| 66 | + } |
| 67 | + }, |
| 68 | + additionalProperties: false |
| 69 | + }], |
| 70 | + messages: { |
| 71 | + unexpectedAfterClosingBracket: 'Expected 1 line break after opening tag (`<{{name}}>`), but {{actual}} line breaks found.', |
| 72 | + unexpectedBeforeOpeningBracket: 'Expected 1 line break before closing tag (`</{{name}}>`), but {{actual}} line breaks found.' |
| 73 | + } |
| 74 | + }, |
| 75 | + |
| 76 | + create (context) { |
| 77 | + const ignores = parseOptions(context.options[0]).ignores |
| 78 | + const template = context.parserServices.getTemplateBodyTokenStore && context.parserServices.getTemplateBodyTokenStore() |
| 79 | + const sourceCode = context.getSourceCode() |
| 80 | + |
| 81 | + let inIgnoreElement |
| 82 | + |
| 83 | + return utils.defineTemplateBodyVisitor(context, { |
| 84 | + 'VElement' (node) { |
| 85 | + if (inIgnoreElement) { |
| 86 | + return |
| 87 | + } |
| 88 | + if (ignores.indexOf(node.name) >= 0) { |
| 89 | + // ignore element name |
| 90 | + inIgnoreElement = node |
| 91 | + return |
| 92 | + } |
| 93 | + if (node.startTag.selfClosing || !node.endTag) { |
| 94 | + // self closing |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + if (!isMultilineElement(node)) { |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + const getTokenOption = { includeComments: true, filter: (token) => token.type !== 'HTMLWhitespace' } |
| 103 | + const contentFirst = template.getTokenAfter(node.startTag, getTokenOption) |
| 104 | + const contentLast = template.getTokenBefore(node.endTag, getTokenOption) |
| 105 | + |
| 106 | + const beforeLineBreaks = contentFirst.loc.start.line - node.startTag.loc.end.line |
| 107 | + const afterLineBreaks = node.endTag.loc.start.line - contentLast.loc.end.line |
| 108 | + if (beforeLineBreaks !== 1) { |
| 109 | + context.report({ |
| 110 | + node: template.getLastToken(node.startTag), |
| 111 | + loc: { |
| 112 | + start: node.startTag.loc.end, |
| 113 | + end: contentFirst.loc.start |
| 114 | + }, |
| 115 | + messageId: 'unexpectedAfterClosingBracket', |
| 116 | + data: { |
| 117 | + name: node.name, |
| 118 | + actual: getPhrase(beforeLineBreaks) |
| 119 | + }, |
| 120 | + fix (fixer) { |
| 121 | + const range = [node.startTag.range[1], contentFirst.range[0]] |
| 122 | + return fixer.replaceTextRange(range, '\n') |
| 123 | + } |
| 124 | + }) |
| 125 | + } |
| 126 | + |
| 127 | + if (isEmpty(node, sourceCode)) { |
| 128 | + return |
| 129 | + } |
| 130 | + |
| 131 | + if (afterLineBreaks !== 1) { |
| 132 | + context.report({ |
| 133 | + node: template.getFirstToken(node.endTag), |
| 134 | + loc: { |
| 135 | + start: contentLast.loc.end, |
| 136 | + end: node.endTag.loc.start |
| 137 | + }, |
| 138 | + messageId: 'unexpectedBeforeOpeningBracket', |
| 139 | + data: { |
| 140 | + name: node.name, |
| 141 | + actual: getPhrase(afterLineBreaks) |
| 142 | + }, |
| 143 | + fix (fixer) { |
| 144 | + const range = [contentLast.range[1], node.endTag.range[0]] |
| 145 | + return fixer.replaceTextRange(range, '\n') |
| 146 | + } |
| 147 | + }) |
| 148 | + } |
| 149 | + }, |
| 150 | + 'VElement:exit' (node) { |
| 151 | + if (inIgnoreElement === node) { |
| 152 | + inIgnoreElement = null |
| 153 | + } |
| 154 | + } |
| 155 | + }) |
| 156 | + } |
| 157 | +} |
0 commit comments