|
| 1 | +/** |
| 2 | + * @author Yosuke Ota |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const utils = require('../utils') |
| 8 | +module.exports = { |
| 9 | + meta: { |
| 10 | + type: 'suggestion', |
| 11 | + docs: { |
| 12 | + description: 'disallow static inline `style` attributes', |
| 13 | + category: undefined, |
| 14 | + url: 'https://eslint.vuejs.org/rules/no-static-inline-styles.html' |
| 15 | + }, |
| 16 | + fixable: null, |
| 17 | + schema: [ |
| 18 | + { |
| 19 | + type: 'object', |
| 20 | + properties: { |
| 21 | + allowBinding: { |
| 22 | + type: 'boolean' |
| 23 | + } |
| 24 | + }, |
| 25 | + additionalProperties: false |
| 26 | + } |
| 27 | + ], |
| 28 | + messages: { |
| 29 | + forbiddenStaticInlineStyle: 'Static inline `style` are forbidden.', |
| 30 | + forbiddenStyleAttr: '`style` attributes are forbidden.' |
| 31 | + } |
| 32 | + }, |
| 33 | + create (context) { |
| 34 | + /** |
| 35 | + * Checks whether if the given property node is a static value. |
| 36 | + * @param {AssignmentProperty} prop property node to check |
| 37 | + * @returns {boolean} `true` if the given property node is a static value. |
| 38 | + */ |
| 39 | + function isStaticValue (prop) { |
| 40 | + return ( |
| 41 | + !prop.computed && |
| 42 | + prop.value.type === 'Literal' && |
| 43 | + (prop.key.type === 'Identifier' || prop.key.type === 'Literal') |
| 44 | + ) |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Gets the static properties of a given expression node. |
| 49 | + * - If `SpreadElement` or computed property exists, it gets only the static properties before it. |
| 50 | + * `:style="{ color: 'red', display: 'flex', ...spread, width: '16px' }"` |
| 51 | + * ^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ |
| 52 | + * - If non-static object exists, it gets only the static properties up to that object. |
| 53 | + * `:style="[ { color: 'red' }, { display: 'flex', color, width: '16px' }, { height: '16px' } ]"` |
| 54 | + * ^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^ |
| 55 | + * - If all properties are static properties, it returns one root node. |
| 56 | + * `:style="[ { color: 'red' }, { display: 'flex', width: '16px' } ]"` |
| 57 | + * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 58 | + * @param {VAttribute} node `:style` node to check |
| 59 | + * @returns {AssignmentProperty[] | [VAttribute]} the static properties. |
| 60 | + */ |
| 61 | + function getReportNodes (node) { |
| 62 | + const { value } = node |
| 63 | + if (!value) { |
| 64 | + return [] |
| 65 | + } |
| 66 | + const { expression } = value |
| 67 | + if (!expression) { |
| 68 | + return [] |
| 69 | + } |
| 70 | + |
| 71 | + let elements |
| 72 | + if (expression.type === 'ObjectExpression') { |
| 73 | + elements = [expression] |
| 74 | + } else if (expression.type === 'ArrayExpression') { |
| 75 | + elements = expression.elements |
| 76 | + } else { |
| 77 | + return [] |
| 78 | + } |
| 79 | + const staticProperties = [] |
| 80 | + for (const element of elements) { |
| 81 | + if (!element) { |
| 82 | + continue |
| 83 | + } |
| 84 | + if (element.type !== 'ObjectExpression') { |
| 85 | + return staticProperties |
| 86 | + } |
| 87 | + |
| 88 | + let isAllStatic = true |
| 89 | + for (const prop of element.properties) { |
| 90 | + if (prop.type === 'SpreadElement' || prop.computed) { |
| 91 | + // If `SpreadElement` or computed property exists, it gets only the static properties before it. |
| 92 | + return staticProperties |
| 93 | + } |
| 94 | + if (isStaticValue(prop)) { |
| 95 | + staticProperties.push(prop) |
| 96 | + } else { |
| 97 | + isAllStatic = false |
| 98 | + } |
| 99 | + } |
| 100 | + if (!isAllStatic) { |
| 101 | + // If non-static object exists, it gets only the static properties up to that object. |
| 102 | + return staticProperties |
| 103 | + } |
| 104 | + } |
| 105 | + // If all properties are static properties, it returns one root node. |
| 106 | + return [node] |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Reports if the value is static. |
| 111 | + * @param {VAttribute} node `:style` node to check |
| 112 | + */ |
| 113 | + function verifyVBindStyle (node) { |
| 114 | + for (const n of getReportNodes(node)) { |
| 115 | + context.report({ |
| 116 | + node: n, |
| 117 | + messageId: 'forbiddenStaticInlineStyle' |
| 118 | + }) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + const visitor = { |
| 123 | + "VAttribute[directive=false][key.name='style']" (node) { |
| 124 | + context.report({ |
| 125 | + node, |
| 126 | + messageId: 'forbiddenStyleAttr' |
| 127 | + }) |
| 128 | + } |
| 129 | + } |
| 130 | + if (!context.options[0] || !context.options[0].allowBinding) { |
| 131 | + visitor[ |
| 132 | + "VAttribute[directive=true][key.name.name='bind'][key.argument.name='style']" |
| 133 | + ] = verifyVBindStyle |
| 134 | + } |
| 135 | + |
| 136 | + return utils.defineTemplateBodyVisitor(context, visitor) |
| 137 | + } |
| 138 | +} |
0 commit comments