|
| 1 | +/** |
| 2 | + * @author Toru Nagashima |
| 3 | + * @copyright 2016 Toru Nagashima. All rights reserved. |
| 4 | + * See LICENSE file in root directory for full license. |
| 5 | + */ |
| 6 | +'use strict' |
| 7 | + |
| 8 | +// ------------------------------------------------------------------------------ |
| 9 | +// Requirements |
| 10 | +// ------------------------------------------------------------------------------ |
| 11 | + |
| 12 | +const utils = require('../utils') |
| 13 | + |
| 14 | +// ------------------------------------------------------------------------------ |
| 15 | +// Helpers |
| 16 | +// ------------------------------------------------------------------------------ |
| 17 | + |
| 18 | +/** |
| 19 | + * Kind strings. |
| 20 | + * This strings wil be displayed in error messages. |
| 21 | + */ |
| 22 | +const KIND = Object.freeze({ |
| 23 | + NORMAL: 'HTML elements', |
| 24 | + VOID: 'HTML void elements', |
| 25 | + COMPONENT: 'Vue.js custom components', |
| 26 | + SVG: 'SVG elements', |
| 27 | + MATH: 'MathML elements' |
| 28 | +}) |
| 29 | + |
| 30 | +/** |
| 31 | + * Normalize the given options. |
| 32 | + * @param {Object|undefined} options The raw options object. |
| 33 | + * @returns {Object} Normalized options. |
| 34 | + */ |
| 35 | +function parseOptions (options) { |
| 36 | + return { |
| 37 | + [KIND.NORMAL]: (options && options.html && options.html.normal) || 'never', |
| 38 | + [KIND.VOID]: (options && options.html && options.html.void) || 'never', |
| 39 | + [KIND.COMPONENT]: (options && options.html && options.html.component) || 'always', |
| 40 | + [KIND.SVG]: (options && options.svg) || 'always', |
| 41 | + [KIND.MATH]: (options && options.math) || 'always' |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Get the kind of the given element. |
| 47 | + * @param {VElement} node The element node to get. |
| 48 | + * @returns {string} The kind of the element. |
| 49 | + */ |
| 50 | +function getKind (node) { |
| 51 | + if (utils.isCustomComponent(node)) { |
| 52 | + return KIND.COMPONENT |
| 53 | + } |
| 54 | + if (utils.isHtmlElementNode(node)) { |
| 55 | + if (utils.isHtmlVoidElementName(node.name)) { |
| 56 | + return KIND.VOID |
| 57 | + } |
| 58 | + return KIND.NORMAL |
| 59 | + } |
| 60 | + if (utils.isSvgElementNode(node)) { |
| 61 | + return KIND.SVG |
| 62 | + } |
| 63 | + if (utils.isMathMLElementNode(node)) { |
| 64 | + return KIND.MATH |
| 65 | + } |
| 66 | + return 'unknown elements' |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Check whether the given element is empty or not. |
| 71 | + * This ignores whitespaces. |
| 72 | + * @param {VElement} node The element node to check. |
| 73 | + * @returns {boolean} `true` if the element is empty. |
| 74 | + */ |
| 75 | +function isEmpty (node) { |
| 76 | + return node.children.every(child => child.type === 'VText' && child.value.trim() === '') |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Creates AST event handlers for html-self-closing-style. |
| 81 | + * |
| 82 | + * @param {RuleContext} context - The rule context. |
| 83 | + * @returns {object} AST event handlers. |
| 84 | + */ |
| 85 | +function create (context) { |
| 86 | + const options = parseOptions(context.options[0]) |
| 87 | + |
| 88 | + utils.registerTemplateBodyVisitor(context, { |
| 89 | + 'VElement' (node) { |
| 90 | + const kind = getKind(node) |
| 91 | + const mode = options[kind] |
| 92 | + |
| 93 | + if (mode === 'always' && !node.startTag.selfClosing && isEmpty(node)) { |
| 94 | + context.report({ |
| 95 | + node, |
| 96 | + loc: node.loc, |
| 97 | + message: 'Require self-closing on {{kind}}.', |
| 98 | + data: { kind }, |
| 99 | + fix: (fixer) => { |
| 100 | + const tokens = context.parserServices.getTemplateBodyTokenStore() |
| 101 | + const close = tokens.getLastToken(node.startTag) |
| 102 | + if (close.type !== 'HTMLTagClose') { |
| 103 | + return null |
| 104 | + } |
| 105 | + return fixer.replaceTextRange([close.range[0], node.range[1]], '/>') |
| 106 | + } |
| 107 | + }) |
| 108 | + } |
| 109 | + |
| 110 | + if (mode === 'never' && node.startTag.selfClosing) { |
| 111 | + context.report({ |
| 112 | + node, |
| 113 | + loc: node.loc, |
| 114 | + message: 'Disallow self-closing on {{kind}}.', |
| 115 | + data: { kind }, |
| 116 | + fix: (fixer) => { |
| 117 | + const tokens = context.parserServices.getTemplateBodyTokenStore() |
| 118 | + const close = tokens.getLastToken(node.startTag) |
| 119 | + if (close.type !== 'HTMLSelfClosingTagClose') { |
| 120 | + return null |
| 121 | + } |
| 122 | + if (kind === KIND.VOID) { |
| 123 | + return fixer.replaceText(close, '>') |
| 124 | + } |
| 125 | + return fixer.replaceText(close, `></${node.rawName}>`) |
| 126 | + } |
| 127 | + }) |
| 128 | + } |
| 129 | + } |
| 130 | + }) |
| 131 | + |
| 132 | + return {} |
| 133 | +} |
| 134 | + |
| 135 | +// ------------------------------------------------------------------------------ |
| 136 | +// Rule Definition |
| 137 | +// ------------------------------------------------------------------------------ |
| 138 | + |
| 139 | +module.exports = { |
| 140 | + create, |
| 141 | + meta: { |
| 142 | + docs: { |
| 143 | + description: 'enforce self-closing style.', |
| 144 | + category: 'Stylistic Issues', |
| 145 | + recommended: false |
| 146 | + }, |
| 147 | + fixable: 'code', |
| 148 | + schema: { |
| 149 | + definitions: { |
| 150 | + optionValue: { |
| 151 | + enum: ['always', 'never', 'any'] |
| 152 | + } |
| 153 | + }, |
| 154 | + type: 'array', |
| 155 | + items: [{ |
| 156 | + type: 'object', |
| 157 | + properties: { |
| 158 | + html: { |
| 159 | + type: 'object', |
| 160 | + properties: { |
| 161 | + normal: { $ref: '#/definitions/optionValue' }, |
| 162 | + void: { $ref: '#/definitions/optionValue' }, |
| 163 | + component: { $ref: '#/definitions/optionValue' } |
| 164 | + }, |
| 165 | + additionalProperties: false |
| 166 | + }, |
| 167 | + svg: { $ref: '#/definitions/optionValue' }, |
| 168 | + math: { $ref: '#/definitions/optionValue' } |
| 169 | + }, |
| 170 | + additionalProperties: false |
| 171 | + }], |
| 172 | + maxItems: 1 |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments