|
| 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 isMultiline (node, contentFirst, contentLast) { |
| 18 | + if (node.startTag.loc.start.line !== node.startTag.loc.end.line || |
| 19 | + node.endTag.loc.start.line !== node.endTag.loc.end.line) { |
| 20 | + // multiline tag |
| 21 | + return true |
| 22 | + } |
| 23 | + if (contentFirst.loc.start.line < contentLast.loc.end.line) { |
| 24 | + // multiline contents |
| 25 | + return true |
| 26 | + } |
| 27 | + return false |
| 28 | +} |
| 29 | + |
| 30 | +function parseOptions (options) { |
| 31 | + return Object.assign({ |
| 32 | + 'singleline': 'ignore', |
| 33 | + 'multiline': 'always', |
| 34 | + 'ignoreNames': ['pre', 'textarea'] |
| 35 | + }, options) |
| 36 | +} |
| 37 | + |
| 38 | +function getPhrase (lineBreaks) { |
| 39 | + switch (lineBreaks) { |
| 40 | + case 0: return 'no line breaks' |
| 41 | + case 1: return '1 line break' |
| 42 | + default: return `${lineBreaks} line breaks` |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// ------------------------------------------------------------------------------ |
| 47 | +// Rule Definition |
| 48 | +// ------------------------------------------------------------------------------ |
| 49 | + |
| 50 | +module.exports = { |
| 51 | + meta: { |
| 52 | + docs: { |
| 53 | + description: 'require or disallow a line break before and after html contents', |
| 54 | + category: undefined, |
| 55 | + url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v4.4.0/docs/rules/html-content-newline.md' |
| 56 | + }, |
| 57 | + fixable: 'whitespace', |
| 58 | + schema: [{ |
| 59 | + type: 'object', |
| 60 | + properties: { |
| 61 | + 'singleline': { enum: ['ignore', 'always', 'never'] }, |
| 62 | + 'multiline': { enum: ['ignore', 'always', 'never'] }, |
| 63 | + 'ignoreNames': { |
| 64 | + type: 'array', |
| 65 | + items: { type: 'string' }, |
| 66 | + uniqueItems: true, |
| 67 | + additionalItems: false |
| 68 | + } |
| 69 | + }, |
| 70 | + additionalProperties: false |
| 71 | + }] |
| 72 | + }, |
| 73 | + |
| 74 | + create (context) { |
| 75 | + const options = parseOptions(context.options[0]) |
| 76 | + const template = context.parserServices.getTemplateBodyTokenStore && context.parserServices.getTemplateBodyTokenStore() |
| 77 | + |
| 78 | + return utils.defineTemplateBodyVisitor(context, { |
| 79 | + 'VElement' (node) { |
| 80 | + if (node.startTag.selfClosing || !node.endTag) { |
| 81 | + // self closing |
| 82 | + return |
| 83 | + } |
| 84 | + let target = node |
| 85 | + while (target.type === 'VElement') { |
| 86 | + if (options.ignoreNames.indexOf(target.name) >= 0) { |
| 87 | + // ignore element name |
| 88 | + return |
| 89 | + } |
| 90 | + target = target.parent |
| 91 | + } |
| 92 | + const getTokenOption = { includeComments: true, filter: (token) => token.type !== 'HTMLWhitespace' } |
| 93 | + const contentFirst = template.getTokenAfter(node.startTag, getTokenOption) |
| 94 | + const contentLast = template.getTokenBefore(node.endTag, getTokenOption) |
| 95 | + const type = isMultiline(node, contentFirst, contentLast) ? options.multiline : options.singleline |
| 96 | + if (type === 'ignore') { |
| 97 | + // 'ignore' option |
| 98 | + return |
| 99 | + } |
| 100 | + const beforeLineBreaks = contentFirst.loc.start.line - node.startTag.loc.end.line |
| 101 | + const afterLineBreaks = node.endTag.loc.start.line - contentLast.loc.end.line |
| 102 | + const expectedLineBreaks = type === 'always' ? 1 : 0 |
| 103 | + if (expectedLineBreaks !== beforeLineBreaks) { |
| 104 | + context.report({ |
| 105 | + node: template.getLastToken(node.startTag), |
| 106 | + loc: { |
| 107 | + start: node.startTag.loc.end, |
| 108 | + end: contentFirst.loc.start |
| 109 | + }, |
| 110 | + message: `Expected {{expected}} after closing bracket of the "{{name}}" element, but {{actual}} found.`, |
| 111 | + data: { |
| 112 | + name: node.name, |
| 113 | + expected: getPhrase(expectedLineBreaks), |
| 114 | + actual: getPhrase(beforeLineBreaks) |
| 115 | + }, |
| 116 | + fix (fixer) { |
| 117 | + const range = [node.startTag.range[1], contentFirst.range[0]] |
| 118 | + const text = '\n'.repeat(expectedLineBreaks) |
| 119 | + return fixer.replaceTextRange(range, text) |
| 120 | + } |
| 121 | + }) |
| 122 | + } |
| 123 | + |
| 124 | + if (expectedLineBreaks !== afterLineBreaks) { |
| 125 | + context.report({ |
| 126 | + node: template.getFirstToken(node.endTag), |
| 127 | + loc: { |
| 128 | + start: contentLast.loc.end, |
| 129 | + end: node.endTag.loc.start |
| 130 | + }, |
| 131 | + message: 'Expected {{expected}} before opening bracket of the "{{name}}" element, but {{actual}} found.', |
| 132 | + data: { |
| 133 | + name: node.name, |
| 134 | + expected: getPhrase(expectedLineBreaks), |
| 135 | + actual: getPhrase(afterLineBreaks) |
| 136 | + }, |
| 137 | + fix (fixer) { |
| 138 | + const range = [contentLast.range[1], node.endTag.range[0]] |
| 139 | + const text = '\n'.repeat(expectedLineBreaks) |
| 140 | + return fixer.replaceTextRange(range, text) |
| 141 | + } |
| 142 | + }) |
| 143 | + } |
| 144 | + } |
| 145 | + }) |
| 146 | + } |
| 147 | +} |
0 commit comments