no-static-inline-styles.js 4.2 KB

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