|
| 1 | +/** |
| 2 | + * @typedef {Object} Option |
| 3 | + * @property {"widely" | "newly" | number} Option.available |
| 4 | + * @typedef { import("../types").RuleModule<[Option]> } RuleModule |
| 5 | + * @typedef {import("@html-eslint/types").Attribute} Attribute |
| 6 | + * @typedef {import("@html-eslint/types").Tag} Tag |
| 7 | + * @typedef {import("@html-eslint/types").ScriptTag} ScriptTag |
| 8 | + * @typedef {import("@html-eslint/types").StyleTag} StyleTag |
| 9 | + */ |
| 10 | + |
| 11 | +const { RULE_CATEGORY } = require("../constants"); |
| 12 | +const { |
| 13 | + elements, |
| 14 | + globalAttributes, |
| 15 | + BASELINE_HIGH, |
| 16 | + BASELINE_LOW, |
| 17 | +} = require("./utils/baseline"); |
| 18 | +const { createVisitors } = require("./utils/visitors"); |
| 19 | + |
| 20 | +const MESSAGE_IDS = { |
| 21 | + NOT_BASELINE_ELEMENT: "notBaselineElement", |
| 22 | + NOT_BASELINE_ELEMENT_ATTRIBUTE: "notBaselineElementAttribute", |
| 23 | + NOT_BASELINE_GLOBAL_ATTRIBUTE: "notBaselineGlobalAttribute", |
| 24 | +}; |
| 25 | + |
| 26 | +/** |
| 27 | + * @type {RuleModule} |
| 28 | + */ |
| 29 | +module.exports = { |
| 30 | + meta: { |
| 31 | + type: "code", |
| 32 | + docs: { |
| 33 | + description: "Enforce the use of baseline features.", |
| 34 | + recommended: true, |
| 35 | + category: RULE_CATEGORY.BEST_PRACTICE, |
| 36 | + }, |
| 37 | + fixable: null, |
| 38 | + schema: [ |
| 39 | + { |
| 40 | + type: "object", |
| 41 | + properties: { |
| 42 | + available: { |
| 43 | + anyOf: [ |
| 44 | + { |
| 45 | + enum: ["widely", "newly"], |
| 46 | + }, |
| 47 | + { |
| 48 | + // baseline year |
| 49 | + type: "integer", |
| 50 | + minimum: 2000, |
| 51 | + maximum: new Date().getFullYear(), |
| 52 | + }, |
| 53 | + ], |
| 54 | + }, |
| 55 | + }, |
| 56 | + additionalProperties: false, |
| 57 | + }, |
| 58 | + ], |
| 59 | + |
| 60 | + messages: { |
| 61 | + [MESSAGE_IDS.NOT_BASELINE_ELEMENT]: |
| 62 | + "Element '{{element}}' is not a {{availability}} available baseline feature.", |
| 63 | + [MESSAGE_IDS.NOT_BASELINE_ELEMENT_ATTRIBUTE]: |
| 64 | + "Attribute '{{attr}}' on '{{element}}' is not a {{availability}} available baseline feature.", |
| 65 | + [MESSAGE_IDS.NOT_BASELINE_GLOBAL_ATTRIBUTE]: |
| 66 | + "Attribute '{{attr}}' is not a {{availability}} available baseline feature.", |
| 67 | + }, |
| 68 | + }, |
| 69 | + |
| 70 | + create(context) { |
| 71 | + const options = context.options[0] || { available: "widely" }; |
| 72 | + const available = options.available; |
| 73 | + |
| 74 | + const baseYear = typeof available === "number" ? available : null; |
| 75 | + const baseStatus = available === "widely" ? BASELINE_HIGH : BASELINE_LOW; |
| 76 | + const availability = String(available); |
| 77 | + |
| 78 | + /** |
| 79 | + * @param {string} element |
| 80 | + * @returns {boolean} |
| 81 | + */ |
| 82 | + function isCustomElement(element) { |
| 83 | + return element.includes("-"); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @param {string} encoded |
| 88 | + * @returns {[number, number]} |
| 89 | + */ |
| 90 | + function decodeStatus(encoded) { |
| 91 | + const [status, year = NaN] = encoded |
| 92 | + .split(":") |
| 93 | + .map((part) => Number(part)); |
| 94 | + return [status, year]; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @param {string} encoded |
| 99 | + * @returns {boolean} |
| 100 | + */ |
| 101 | + function isSupported(encoded) { |
| 102 | + const [status, year = NaN] = decodeStatus(encoded); |
| 103 | + if (baseYear) { |
| 104 | + return year <= baseYear; |
| 105 | + } |
| 106 | + return status >= baseStatus; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @param {string} element |
| 111 | + * @returns {boolean} |
| 112 | + */ |
| 113 | + function isSupportedElement(element) { |
| 114 | + const elementEncoded = elements.get(element); |
| 115 | + if (!elementEncoded) { |
| 116 | + return true; |
| 117 | + } |
| 118 | + return isSupported(elementEncoded); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @param {string[]} parts |
| 123 | + * @returns {string} |
| 124 | + */ |
| 125 | + function toStatusKey(...parts) { |
| 126 | + return parts.map((part) => part.toLowerCase().trim()).join("."); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @param {string} element |
| 131 | + * @param {string} key |
| 132 | + * @returns {boolean} |
| 133 | + */ |
| 134 | + function isSupportedElementAttributeKey(element, key) { |
| 135 | + const elementStatus = elements.get(toStatusKey(element, key)); |
| 136 | + if (!elementStatus) { |
| 137 | + return true; |
| 138 | + } |
| 139 | + return isSupported(elementStatus); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @param {string} key |
| 144 | + * @returns {boolean} |
| 145 | + */ |
| 146 | + function isSupportedGlobalAttributeKey(key) { |
| 147 | + const globalAttrStatus = globalAttributes.get(toStatusKey(key)); |
| 148 | + if (!globalAttrStatus) { |
| 149 | + return true; |
| 150 | + } |
| 151 | + return isSupported(globalAttrStatus); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @param {string} element |
| 156 | + * @param {string} key |
| 157 | + * @param {string} value |
| 158 | + * @returns {boolean} |
| 159 | + */ |
| 160 | + function isSupportedElementAttributeKeyValue(element, key, value) { |
| 161 | + const elementStatus = elements.get(toStatusKey(element, key, value)); |
| 162 | + if (!elementStatus) { |
| 163 | + return true; |
| 164 | + } |
| 165 | + return isSupported(elementStatus); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * @param {string} key |
| 170 | + * @param {string} value |
| 171 | + * @returns {boolean} |
| 172 | + */ |
| 173 | + function isSupportedGlobalAttributeKeyValue(key, value) { |
| 174 | + const globalAttrStatus = globalAttributes.get(toStatusKey(key, value)); |
| 175 | + if (!globalAttrStatus) { |
| 176 | + return true; |
| 177 | + } |
| 178 | + return isSupported(globalAttrStatus); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * @param {Tag | ScriptTag | StyleTag} node |
| 183 | + * @param {string} elementName |
| 184 | + * @param {Attribute[]} attributes |
| 185 | + */ |
| 186 | + function check(node, elementName, attributes) { |
| 187 | + if (isCustomElement(elementName)) { |
| 188 | + return; |
| 189 | + } |
| 190 | + |
| 191 | + if (!isSupportedElement(elementName)) { |
| 192 | + context.report({ |
| 193 | + node: node.openStart, |
| 194 | + messageId: MESSAGE_IDS.NOT_BASELINE_ELEMENT, |
| 195 | + data: { |
| 196 | + element: `<${elementName}>`, |
| 197 | + availability, |
| 198 | + }, |
| 199 | + }); |
| 200 | + } |
| 201 | + attributes.forEach((attribute) => { |
| 202 | + if (!isSupportedElementAttributeKey(elementName, attribute.key.value)) { |
| 203 | + context.report({ |
| 204 | + node: attribute.key, |
| 205 | + messageId: MESSAGE_IDS.NOT_BASELINE_ELEMENT_ATTRIBUTE, |
| 206 | + data: { |
| 207 | + element: `<${elementName}>`, |
| 208 | + attr: attribute.key.value, |
| 209 | + availability, |
| 210 | + }, |
| 211 | + }); |
| 212 | + } else if (!isSupportedGlobalAttributeKey(attribute.key.value)) { |
| 213 | + context.report({ |
| 214 | + node: attribute.key, |
| 215 | + messageId: MESSAGE_IDS.NOT_BASELINE_GLOBAL_ATTRIBUTE, |
| 216 | + data: { |
| 217 | + attr: attribute.key.value, |
| 218 | + availability, |
| 219 | + }, |
| 220 | + }); |
| 221 | + } else if (attribute.value) { |
| 222 | + if ( |
| 223 | + !isSupportedElementAttributeKeyValue( |
| 224 | + elementName, |
| 225 | + attribute.key.value, |
| 226 | + attribute.value.value |
| 227 | + ) |
| 228 | + ) { |
| 229 | + context.report({ |
| 230 | + node: attribute.key, |
| 231 | + messageId: MESSAGE_IDS.NOT_BASELINE_ELEMENT_ATTRIBUTE, |
| 232 | + data: { |
| 233 | + element: `<${elementName}>`, |
| 234 | + attr: `${attribute.key.value}="${attribute.value.value}"`, |
| 235 | + availability, |
| 236 | + }, |
| 237 | + }); |
| 238 | + } else if ( |
| 239 | + !isSupportedGlobalAttributeKeyValue( |
| 240 | + attribute.key.value, |
| 241 | + attribute.value.value |
| 242 | + ) |
| 243 | + ) { |
| 244 | + context.report({ |
| 245 | + node: attribute, |
| 246 | + messageId: MESSAGE_IDS.NOT_BASELINE_GLOBAL_ATTRIBUTE, |
| 247 | + data: { |
| 248 | + attr: `${attribute.key.value}="${attribute.value.value}"`, |
| 249 | + availability, |
| 250 | + }, |
| 251 | + }); |
| 252 | + } |
| 253 | + } |
| 254 | + }); |
| 255 | + } |
| 256 | + |
| 257 | + return createVisitors(context, { |
| 258 | + ScriptTag(node) { |
| 259 | + check(node, "script", node.attributes); |
| 260 | + }, |
| 261 | + StyleTag(node) { |
| 262 | + check(node, "style", node.attributes); |
| 263 | + }, |
| 264 | + Tag(node) { |
| 265 | + check(node, node.name, node.attributes); |
| 266 | + }, |
| 267 | + }); |
| 268 | + }, |
| 269 | +}; |
0 commit comments