no-extra-parens.js 4.9 KB

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