no-extra-parens.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * @author Yosuke Ota
  3. */
  4. 'use strict'
  5. const { isParenthesized } = require('@eslint-community/eslint-utils')
  6. const { wrapCoreRule } = require('../utils')
  7. const { getStyleVariablesContext } = require('../utils/style-variables')
  8. // eslint-disable-next-line internal/no-invalid-meta
  9. module.exports = wrapCoreRule('no-extra-parens', {
  10. skipDynamicArguments: true,
  11. applyDocument: true,
  12. create: createForVueSyntax
  13. })
  14. /**
  15. * Check whether the given token is a left parenthesis.
  16. * @param {Token} token The token to check.
  17. * @returns {boolean} `true` if the token is a left parenthesis.
  18. */
  19. function isLeftParen(token) {
  20. return token.type === 'Punctuator' && token.value === '('
  21. }
  22. /**
  23. * Check whether the given token is a right parenthesis.
  24. * @param {Token} token The token to check.
  25. * @returns {boolean} `true` if the token is a right parenthesis.
  26. */
  27. function isRightParen(token) {
  28. return token.type === 'Punctuator' && token.value === ')'
  29. }
  30. /**
  31. * Check whether the given token is a left brace.
  32. * @param {Token} token The token to check.
  33. * @returns {boolean} `true` if the token is a left brace.
  34. */
  35. function isLeftBrace(token) {
  36. return token.type === 'Punctuator' && token.value === '{'
  37. }
  38. /**
  39. * Check whether the given token is a right brace.
  40. * @param {Token} token The token to check.
  41. * @returns {boolean} `true` if the token is a right brace.
  42. */
  43. function isRightBrace(token) {
  44. return token.type === 'Punctuator' && token.value === '}'
  45. }
  46. /**
  47. * Check whether the given token is a left bracket.
  48. * @param {Token} token The token to check.
  49. * @returns {boolean} `true` if the token is a left bracket.
  50. */
  51. function isLeftBracket(token) {
  52. return token.type === 'Punctuator' && token.value === '['
  53. }
  54. /**
  55. * Check whether the given token is a right bracket.
  56. * @param {Token} token The token to check.
  57. * @returns {boolean} `true` if the token is a right bracket.
  58. */
  59. function isRightBracket(token) {
  60. return token.type === 'Punctuator' && token.value === ']'
  61. }
  62. /**
  63. * Determines if a given expression node is an IIFE
  64. * @param {Expression} node The node to check
  65. * @returns {node is CallExpression & { callee: FunctionExpression } } `true` if the given node is an IIFE
  66. */
  67. function isIIFE(node) {
  68. return (
  69. node.type === 'CallExpression' && node.callee.type === 'FunctionExpression'
  70. )
  71. }
  72. /**
  73. * @param {RuleContext} context - The rule context.
  74. * @returns {TemplateListener} AST event handlers.
  75. */
  76. function createForVueSyntax(context) {
  77. if (!context.parserServices.getTemplateBodyTokenStore) {
  78. return {}
  79. }
  80. const tokenStore = context.parserServices.getTemplateBodyTokenStore()
  81. /**
  82. * Checks if the given node turns into a filter when unwraped.
  83. * @param {Expression} expression node to evaluate
  84. * @returns {boolean} `true` if the given node turns into a filter when unwraped.
  85. */
  86. function isUnwrapChangeToFilter(expression) {
  87. let parenStack = null
  88. for (const token of tokenStore.getTokens(expression)) {
  89. if (parenStack) {
  90. if (parenStack.isUpToken(token)) {
  91. parenStack = parenStack.upper
  92. continue
  93. }
  94. } else {
  95. if (token.value === '|') {
  96. return true
  97. }
  98. }
  99. if (isLeftParen(token)) {
  100. parenStack = { isUpToken: isRightParen, upper: parenStack }
  101. } else if (isLeftBracket(token)) {
  102. parenStack = { isUpToken: isRightBracket, upper: parenStack }
  103. } else if (isLeftBrace(token)) {
  104. parenStack = { isUpToken: isRightBrace, upper: parenStack }
  105. }
  106. }
  107. return false
  108. }
  109. /**
  110. * Checks if the given node is CSS v-bind() without quote.
  111. * @param {VExpressionContainer} node
  112. * @param {Expression} expression
  113. */
  114. function isStyleVariableWithoutQuote(node, expression) {
  115. const styleVars = getStyleVariablesContext(context)
  116. if (!styleVars || !styleVars.vBinds.includes(node)) {
  117. return false
  118. }
  119. const vBindToken = tokenStore.getFirstToken(node)
  120. const tokens = tokenStore.getTokensBetween(vBindToken, expression)
  121. return tokens.every(isLeftParen)
  122. }
  123. /**
  124. * @param {VExpressionContainer & { expression: Expression | VFilterSequenceExpression | null }} node
  125. */
  126. function verify(node) {
  127. if (!node.expression) {
  128. return
  129. }
  130. const expression =
  131. node.expression.type === 'VFilterSequenceExpression'
  132. ? node.expression.expression
  133. : node.expression
  134. if (!isParenthesized(expression, tokenStore)) {
  135. return
  136. }
  137. if (!isParenthesized(2, expression, tokenStore)) {
  138. if (
  139. isIIFE(expression) &&
  140. !isParenthesized(expression.callee, tokenStore)
  141. ) {
  142. return
  143. }
  144. if (isUnwrapChangeToFilter(expression)) {
  145. return
  146. }
  147. if (isStyleVariableWithoutQuote(node, expression)) {
  148. return
  149. }
  150. }
  151. report(expression)
  152. }
  153. /**
  154. * Report the node
  155. * @param {Expression} node node to evaluate
  156. * @returns {void}
  157. * @private
  158. */
  159. function report(node) {
  160. const sourceCode = context.getSourceCode()
  161. const leftParenToken = tokenStore.getTokenBefore(node)
  162. const rightParenToken = tokenStore.getTokenAfter(node)
  163. context.report({
  164. node,
  165. loc: leftParenToken.loc,
  166. messageId: 'unexpected',
  167. fix(fixer) {
  168. const parenthesizedSource = sourceCode.text.slice(
  169. leftParenToken.range[1],
  170. rightParenToken.range[0]
  171. )
  172. return fixer.replaceTextRange(
  173. [leftParenToken.range[0], rightParenToken.range[1]],
  174. parenthesizedSource
  175. )
  176. }
  177. })
  178. }
  179. return {
  180. "VAttribute[directive=true][key.name.name='bind'] > VExpressionContainer":
  181. verify,
  182. 'VElement > VExpressionContainer': verify
  183. }
  184. }