|
| 1 | +/** |
| 2 | + * @author Mussin Benarbia |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const { isVElement } = require('../utils') |
| 8 | + |
| 9 | +/** |
| 10 | + * check whether a tag has the `scoped` attribute |
| 11 | + * @param {VElement} componentBlock |
| 12 | + */ |
| 13 | +function isScoped(componentBlock) { |
| 14 | + return componentBlock.startTag.attributes.some( |
| 15 | + (attribute) => !attribute.directive && attribute.key.name === 'scoped' |
| 16 | + ) |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * check whether a tag has the `module` attribute |
| 21 | + * @param {VElement} componentBlock |
| 22 | + */ |
| 23 | +function isModule(componentBlock) { |
| 24 | + return componentBlock.startTag.attributes.some( |
| 25 | + (attribute) => !attribute.directive && attribute.key.name === 'module' |
| 26 | + ) |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * check if a tag doesn't have either the `scoped` nor `module` attribute |
| 31 | + * @param {VElement} componentBlock |
| 32 | + */ |
| 33 | +function isPlain(componentBlock) { |
| 34 | + return !isScoped(componentBlock) && !isModule(componentBlock) |
| 35 | +} |
| 36 | + |
| 37 | +function getUserDefinedAllowedAttrs(context) { |
| 38 | + if (context.options[0] && context.options[0].allow) { |
| 39 | + return context.options[0].allow |
| 40 | + } |
| 41 | + return [] |
| 42 | +} |
| 43 | + |
| 44 | +const defaultAllowedAttrs = ['scoped'] |
| 45 | + |
| 46 | +module.exports = { |
| 47 | + meta: { |
| 48 | + type: 'suggestion', |
| 49 | + docs: { |
| 50 | + description: |
| 51 | + 'enforce or forbid the use of the `scoped` and `module` attributes in SFC top level style tags', |
| 52 | + categories: undefined, |
| 53 | + url: 'https://eslint.vuejs.org/rules/enforce-style-attribute.html' |
| 54 | + }, |
| 55 | + fixable: null, |
| 56 | + schema: [ |
| 57 | + { |
| 58 | + type: 'object', |
| 59 | + properties: { |
| 60 | + allow: { |
| 61 | + type: 'array', |
| 62 | + minItems: 1, |
| 63 | + uniqueItems: true, |
| 64 | + items: { |
| 65 | + type: 'string', |
| 66 | + enum: ['plain', 'scoped', 'module'] |
| 67 | + } |
| 68 | + } |
| 69 | + }, |
| 70 | + additionalProperties: false |
| 71 | + } |
| 72 | + ], |
| 73 | + messages: { |
| 74 | + notAllowedScoped: |
| 75 | + 'The scoped attribute is not allowed. Allowed: {{ allowedAttrsString }}.', |
| 76 | + notAllowedModule: |
| 77 | + 'The module attribute is not allowed. Allowed: {{ allowedAttrsString }}.', |
| 78 | + notAllowedPlain: |
| 79 | + 'Plain <style> tags are not allowed. Allowed: {{ allowedAttrsString }}.' |
| 80 | + } |
| 81 | + }, |
| 82 | + |
| 83 | + /** @param {RuleContext} context */ |
| 84 | + create(context) { |
| 85 | + const sourceCode = context.getSourceCode() |
| 86 | + if (!sourceCode.parserServices.getDocumentFragment) { |
| 87 | + return {} |
| 88 | + } |
| 89 | + const documentFragment = sourceCode.parserServices.getDocumentFragment() |
| 90 | + if (!documentFragment) { |
| 91 | + return {} |
| 92 | + } |
| 93 | + |
| 94 | + const topLevelElements = documentFragment.children.filter(isVElement) |
| 95 | + const topLevelStyleTags = topLevelElements.filter( |
| 96 | + (element) => element.rawName === 'style' |
| 97 | + ) |
| 98 | + |
| 99 | + if (topLevelStyleTags.length === 0) { |
| 100 | + return {} |
| 101 | + } |
| 102 | + |
| 103 | + const userDefinedAllowedAttrs = getUserDefinedAllowedAttrs(context) |
| 104 | + const allowedAttrs = |
| 105 | + userDefinedAllowedAttrs.length > 0 |
| 106 | + ? userDefinedAllowedAttrs |
| 107 | + : defaultAllowedAttrs |
| 108 | + |
| 109 | + const allowsPlain = allowedAttrs.includes('plain') |
| 110 | + const allowsScoped = allowedAttrs.includes('scoped') |
| 111 | + const allowsModule = allowedAttrs.includes('module') |
| 112 | + const allowedAttrsString = [...allowedAttrs].sort().join(', ') |
| 113 | + |
| 114 | + return { |
| 115 | + Program() { |
| 116 | + for (const styleTag of topLevelStyleTags) { |
| 117 | + if (!allowsPlain && isPlain(styleTag)) { |
| 118 | + context.report({ |
| 119 | + node: styleTag, |
| 120 | + messageId: 'notAllowedPlain', |
| 121 | + data: { |
| 122 | + allowedAttrsString |
| 123 | + } |
| 124 | + }) |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + if (!allowsScoped && isScoped(styleTag)) { |
| 129 | + context.report({ |
| 130 | + node: styleTag, |
| 131 | + messageId: 'notAllowedScoped', |
| 132 | + data: { |
| 133 | + allowedAttrsString |
| 134 | + } |
| 135 | + }) |
| 136 | + return |
| 137 | + } |
| 138 | + |
| 139 | + if (!allowsModule && isModule(styleTag)) { |
| 140 | + context.report({ |
| 141 | + node: styleTag, |
| 142 | + messageId: 'notAllowedModule', |
| 143 | + data: { |
| 144 | + allowedAttrsString |
| 145 | + } |
| 146 | + }) |
| 147 | + return |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments