|
| 1 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 2 | +module.exports = { |
| 3 | + meta: { |
| 4 | + type: 'problem', |
| 5 | + docs: { |
| 6 | + description: 'disallow invalid meta tags', |
| 7 | + category: 'Best Practices', |
| 8 | + url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-invalid-meta.md', |
| 9 | + templateMode: 'both', |
| 10 | + }, |
| 11 | + fixable: null, |
| 12 | + schema: [], |
| 13 | + messages: { |
| 14 | + invalidCharset: 'Meta charset should be "utf-8". Found: "{{charset}}".', |
| 15 | + metaRefreshRedirect: 'A meta redirect should not have a delay value greater than zero.', |
| 16 | + metaRefreshDelay: 'A meta refresh should have a delay greater than 72000 seconds.', |
| 17 | + viewportUserScalable: 'A meta viewport should not restrict user-scalable.', |
| 18 | + viewportMaximumScale: 'A meta viewport should not set a maximum scale on content.', |
| 19 | + metaMissingContent: |
| 20 | + 'A meta content attribute must be defined if the name, property, itemprop, or http-equiv attribute is defined.', |
| 21 | + metaMissingIdentifier: |
| 22 | + 'A meta content attribute cannot be defined if the name, property, itemprop, nor the http-equiv attributes are defined.', |
| 23 | + }, |
| 24 | + originallyFrom: { |
| 25 | + name: 'ember-template-lint', |
| 26 | + rule: 'lib/rules/no-invalid-meta.js', |
| 27 | + docs: 'docs/rule/no-invalid-meta.md', |
| 28 | + tests: 'test/unit/rules/no-invalid-meta-test.js', |
| 29 | + }, |
| 30 | + }, |
| 31 | + |
| 32 | + create(context) { |
| 33 | + return { |
| 34 | + // eslint-disable-next-line complexity |
| 35 | + GlimmerElementNode(node) { |
| 36 | + if (node.tag !== 'meta') { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + function findAttr(name) { |
| 41 | + return node.attributes.find((a) => a.type === 'GlimmerAttrNode' && a.name === name); |
| 42 | + } |
| 43 | + |
| 44 | + function getAttrText(name) { |
| 45 | + const attr = findAttr(name); |
| 46 | + if (attr && attr.value && attr.value.type === 'GlimmerTextNode') { |
| 47 | + return attr.value.chars; |
| 48 | + } |
| 49 | + return undefined; |
| 50 | + } |
| 51 | + |
| 52 | + const hasCharset = Boolean(findAttr('charset')); |
| 53 | + const hasName = Boolean(findAttr('name')); |
| 54 | + const hasHttpEquiv = Boolean(findAttr('http-equiv')); |
| 55 | + const hasProperty = Boolean(findAttr('property')); |
| 56 | + const hasItemprop = Boolean(findAttr('itemprop')); |
| 57 | + const hasContent = Boolean(findAttr('content')); |
| 58 | + const hasIdentifier = hasName || hasHttpEquiv || hasProperty || hasItemprop; |
| 59 | + |
| 60 | + // Check for invalid charset value |
| 61 | + const charsetValue = getAttrText('charset'); |
| 62 | + if (hasCharset && charsetValue) { |
| 63 | + const normalizedCharset = charsetValue.toLowerCase().replaceAll('-', ''); |
| 64 | + if (normalizedCharset !== 'utf8') { |
| 65 | + context.report({ |
| 66 | + node, |
| 67 | + messageId: 'invalidCharset', |
| 68 | + data: { charset: charsetValue }, |
| 69 | + }); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Check: identifier present but no content |
| 74 | + if (hasIdentifier && !hasContent && !hasCharset) { |
| 75 | + context.report({ |
| 76 | + node, |
| 77 | + messageId: 'metaMissingContent', |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + // Check: content present but no identifier or charset |
| 82 | + if (hasContent && !hasIdentifier && !hasCharset) { |
| 83 | + context.report({ |
| 84 | + node, |
| 85 | + messageId: 'metaMissingIdentifier', |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + // Check content-based validations |
| 90 | + const contentValue = getAttrText('content'); |
| 91 | + |
| 92 | + if (hasContent && typeof contentValue === 'string') { |
| 93 | + // http-equiv="refresh" checks |
| 94 | + if (hasHttpEquiv) { |
| 95 | + if (contentValue.includes(';')) { |
| 96 | + // Redirect: should not have delay > 0 |
| 97 | + if (contentValue.charAt(0) !== '0') { |
| 98 | + context.report({ |
| 99 | + node, |
| 100 | + messageId: 'metaRefreshRedirect', |
| 101 | + }); |
| 102 | + } |
| 103 | + } else { |
| 104 | + // Plain refresh: delay should be > 72000 |
| 105 | + const delay = Number.parseInt(contentValue, 10); |
| 106 | + if (delay <= 72_000) { |
| 107 | + context.report({ |
| 108 | + node, |
| 109 | + messageId: 'metaRefreshDelay', |
| 110 | + }); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // Viewport checks |
| 116 | + const userScalableRegExp = /user-scalable(\s*?)=(\s*?)no/gim; |
| 117 | + if (userScalableRegExp.test(contentValue)) { |
| 118 | + context.report({ |
| 119 | + node, |
| 120 | + messageId: 'viewportUserScalable', |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + if (contentValue.includes('maximum-scale')) { |
| 125 | + context.report({ |
| 126 | + node, |
| 127 | + messageId: 'viewportMaximumScale', |
| 128 | + }); |
| 129 | + } |
| 130 | + } |
| 131 | + }, |
| 132 | + }; |
| 133 | + }, |
| 134 | +}; |
0 commit comments