no-extra-parens.js 4.9 KB

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