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