|
| 1 | +/** |
| 2 | + * @fileoverview Disallow use other than available `lang` |
| 3 | + * @author Yosuke Ota |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | +const utils = require('../utils') |
| 7 | + |
| 8 | +/** |
| 9 | + * @typedef {object} BlockOptions |
| 10 | + * @property {Set<string>} lang |
| 11 | + * @property {boolean} allowNoLang |
| 12 | + */ |
| 13 | +/** |
| 14 | + * @typedef { { [element: string]: BlockOptions | undefined } } Options |
| 15 | + */ |
| 16 | +/** |
| 17 | + * @typedef {object} UserBlockOptions |
| 18 | + * @property {string[] | string} [lang] |
| 19 | + * @property {boolean} [allowNoLang] |
| 20 | + */ |
| 21 | +/** |
| 22 | + * @typedef { { [element: string]: UserBlockOptions | undefined } } UserOptions |
| 23 | + */ |
| 24 | + |
| 25 | +/** |
| 26 | + * https://vuejs.github.io/vetur/guide/highlighting.html |
| 27 | + * <template lang="html"></template> |
| 28 | + * <style lang="css"></style> |
| 29 | + * <script lang="js"></script> |
| 30 | + * <script lang="javascript"></script> |
| 31 | + * @type {Record<string, string[] | undefined>} |
| 32 | + */ |
| 33 | +const DEFAULT_LANGUAGES = { |
| 34 | + template: ['html'], |
| 35 | + style: ['css'], |
| 36 | + script: ['js', 'javascript'] |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * @param {NonNullable<BlockOptions['lang']>} lang |
| 41 | + */ |
| 42 | +function getAllowsLangPhrase(lang) { |
| 43 | + const langs = [...lang].map((s) => `"${s}"`) |
| 44 | + switch (langs.length) { |
| 45 | + case 1: |
| 46 | + return langs[0] |
| 47 | + default: |
| 48 | + return `${langs.slice(0, -1).join(', ')}, and ${langs[langs.length - 1]}` |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Normalizes a given option. |
| 54 | + * @param {string} blockName The block name. |
| 55 | + * @param { UserBlockOptions } option An option to parse. |
| 56 | + * @returns {BlockOptions} Normalized option. |
| 57 | + */ |
| 58 | +function normalizeOption(blockName, option) { |
| 59 | + const lang = new Set( |
| 60 | + Array.isArray(option.lang) ? option.lang : option.lang ? [option.lang] : [] |
| 61 | + ) |
| 62 | + let hasDefault = false |
| 63 | + for (const def of DEFAULT_LANGUAGES[blockName] || []) { |
| 64 | + if (lang.has(def)) { |
| 65 | + lang.delete(def) |
| 66 | + hasDefault = true |
| 67 | + } |
| 68 | + } |
| 69 | + if (lang.size === 0) { |
| 70 | + return { |
| 71 | + lang, |
| 72 | + allowNoLang: true |
| 73 | + } |
| 74 | + } |
| 75 | + return { |
| 76 | + lang, |
| 77 | + allowNoLang: hasDefault || Boolean(option.allowNoLang) |
| 78 | + } |
| 79 | +} |
| 80 | +/** |
| 81 | + * Normalizes a given options. |
| 82 | + * @param { UserOptions } options An option to parse. |
| 83 | + * @returns {Options} Normalized option. |
| 84 | + */ |
| 85 | +function normalizeOptions(options) { |
| 86 | + if (!options) { |
| 87 | + return {} |
| 88 | + } |
| 89 | + |
| 90 | + /** @type {Options} */ |
| 91 | + const normalized = {} |
| 92 | + |
| 93 | + for (const blockName of Object.keys(options)) { |
| 94 | + const value = options[blockName] |
| 95 | + if (value) { |
| 96 | + normalized[blockName] = normalizeOption(blockName, value) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return normalized |
| 101 | +} |
| 102 | + |
| 103 | +// ------------------------------------------------------------------------------ |
| 104 | +// Rule Definition |
| 105 | +// ------------------------------------------------------------------------------ |
| 106 | + |
| 107 | +module.exports = { |
| 108 | + meta: { |
| 109 | + type: 'suggestion', |
| 110 | + docs: { |
| 111 | + description: 'disallow use other than available `lang`', |
| 112 | + categories: undefined, |
| 113 | + url: 'https://eslint.vuejs.org/rules/block-lang.html' |
| 114 | + }, |
| 115 | + schema: [ |
| 116 | + { |
| 117 | + type: 'object', |
| 118 | + patternProperties: { |
| 119 | + '^(?:\\S+)$': { |
| 120 | + oneOf: [ |
| 121 | + { |
| 122 | + type: 'object', |
| 123 | + properties: { |
| 124 | + lang: { |
| 125 | + anyOf: [ |
| 126 | + { type: 'string' }, |
| 127 | + { |
| 128 | + type: 'array', |
| 129 | + items: { |
| 130 | + type: 'string' |
| 131 | + }, |
| 132 | + uniqueItems: true, |
| 133 | + additionalItems: false |
| 134 | + } |
| 135 | + ] |
| 136 | + }, |
| 137 | + allowNoLang: { type: 'boolean' } |
| 138 | + }, |
| 139 | + additionalProperties: false |
| 140 | + } |
| 141 | + ] |
| 142 | + } |
| 143 | + }, |
| 144 | + minProperties: 1, |
| 145 | + additionalProperties: false |
| 146 | + } |
| 147 | + ], |
| 148 | + messages: { |
| 149 | + expected: |
| 150 | + "Only {{allows}} can be used for the 'lang' attribute of '<{{tag}}>'.", |
| 151 | + missing: "The 'lang' attribute of '<{{tag}}>' is missing.", |
| 152 | + unexpected: "Do not specify the 'lang' attribute of '<{{tag}}>'.", |
| 153 | + useOrNot: |
| 154 | + "Only {{allows}} can be used for the 'lang' attribute of '<{{tag}}>'. Or, not specifying the `lang` attribute is allowed.", |
| 155 | + unexpectedDefault: |
| 156 | + "Do not explicitly specify the default language for the 'lang' attribute of '<{{tag}}>'." |
| 157 | + } |
| 158 | + }, |
| 159 | + /** @param {RuleContext} context */ |
| 160 | + create(context) { |
| 161 | + const options = normalizeOptions( |
| 162 | + context.options[0] || { |
| 163 | + script: { allowNoLang: true }, |
| 164 | + template: { allowNoLang: true }, |
| 165 | + style: { allowNoLang: true } |
| 166 | + } |
| 167 | + ) |
| 168 | + if (!Object.keys(options).length) { |
| 169 | + // empty |
| 170 | + return {} |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * @param {VElement} element |
| 175 | + * @returns {void} |
| 176 | + */ |
| 177 | + function verify(element) { |
| 178 | + const tag = element.name |
| 179 | + const option = options[tag] |
| 180 | + if (!option) { |
| 181 | + return |
| 182 | + } |
| 183 | + const lang = utils.getAttribute(element, 'lang') |
| 184 | + if (lang == null || lang.value == null) { |
| 185 | + if (!option.allowNoLang) { |
| 186 | + context.report({ |
| 187 | + node: element.startTag, |
| 188 | + messageId: 'missing', |
| 189 | + data: { |
| 190 | + tag |
| 191 | + } |
| 192 | + }) |
| 193 | + } |
| 194 | + return |
| 195 | + } |
| 196 | + if (!option.lang.has(lang.value.value)) { |
| 197 | + let messageId |
| 198 | + if (!option.allowNoLang) { |
| 199 | + messageId = 'expected' |
| 200 | + } else if (option.lang.size === 0) { |
| 201 | + if ((DEFAULT_LANGUAGES[tag] || []).includes(lang.value.value)) { |
| 202 | + messageId = 'unexpectedDefault' |
| 203 | + } else { |
| 204 | + messageId = 'unexpected' |
| 205 | + } |
| 206 | + } else { |
| 207 | + messageId = 'useOrNot' |
| 208 | + } |
| 209 | + context.report({ |
| 210 | + node: lang, |
| 211 | + messageId, |
| 212 | + data: { |
| 213 | + tag, |
| 214 | + allows: getAllowsLangPhrase(option.lang) |
| 215 | + } |
| 216 | + }) |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + return utils.defineDocumentVisitor(context, { |
| 221 | + 'VDocumentFragment > VElement': verify |
| 222 | + }) |
| 223 | + } |
| 224 | +} |
0 commit comments