@@ -224,40 +224,10 @@ function needsParens(path, options) {
224224 ) ;
225225
226226 case "Decorator" :
227- if ( key === "expression" ) {
228- if ( isMemberExpression ( node ) && node . computed ) {
229- return true ;
230- }
231-
232- let hasCallExpression = false ;
233- let hasMemberExpression = false ;
234- let current = node ;
235- while ( current ) {
236- switch ( current . type ) {
237- case "MemberExpression" :
238- hasMemberExpression = true ;
239- current = current . object ;
240- break ;
241- case "CallExpression" :
242- if (
243- /** @(x().y) */ hasMemberExpression ||
244- /** @(x().y()) */ hasCallExpression
245- ) {
246- return options . parser !== "typescript" ;
247- }
248- hasCallExpression = true ;
249- current = current . callee ;
250- break ;
251- case "Identifier" :
252- return false ;
253- case "TaggedTemplateExpression" :
254- // babel-parser cannot parse
255- // @foo `bar`
256- return options . parser !== "typescript" ;
257- default :
258- return true ;
259- }
260- }
227+ if (
228+ key === "expression" &&
229+ ! canDecoratorExpressionUnparenthesized ( node )
230+ ) {
261231 return true ;
262232 }
263233 break ;
@@ -1276,4 +1246,36 @@ function shouldAddParenthesesToChainElement(path) {
12761246 return false ;
12771247}
12781248
1249+ function isDecoratorMemberExpression ( node ) {
1250+ if ( node . type === "Identifier" ) {
1251+ return true ;
1252+ }
1253+
1254+ if ( isMemberExpression ( node ) ) {
1255+ return (
1256+ ! node . computed &&
1257+ ! node . optional &&
1258+ node . property . type === "Identifier" &&
1259+ isDecoratorMemberExpression ( node . object )
1260+ ) ;
1261+ }
1262+
1263+ return false ;
1264+ }
1265+
1266+ // Based on babel implementation
1267+ // https://github.com/nicolo-ribaudo/babel/blob/c4b88a4e5005364255f7e964fe324cf7bfdfb019/packages/babel-generator/src/node/index.ts#L111
1268+ function canDecoratorExpressionUnparenthesized ( node ) {
1269+ if ( node . type === "ChainExpression" ) {
1270+ node = node . expression ;
1271+ }
1272+
1273+ return (
1274+ isDecoratorMemberExpression ( node ) ||
1275+ ( isCallExpression ( node ) &&
1276+ ! node . optional &&
1277+ isDecoratorMemberExpression ( node . callee ) )
1278+ ) ;
1279+ }
1280+
12791281export default needsParens ;
0 commit comments