From ad0f525218f0d7e7ac435c255087908edbe94a7b Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Tue, 10 Dec 2024 08:25:49 +0800 Subject: [PATCH] fix: Correctly set position for `@(a.b)()` (#17013) fix --- packages/babel-parser/src/parser/statement.ts | 12 ++++++++---- .../babel-parser/src/plugins/typescript/index.ts | 8 ++++++-- .../output.json | 2 +- .../output.json | 2 +- .../decorators/parenthesized/output.json | 2 +- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/babel-parser/src/parser/statement.ts b/packages/babel-parser/src/parser/statement.ts index 0da3281a1fdb..142f3e5f2570 100644 --- a/packages/babel-parser/src/parser/statement.ts +++ b/packages/babel-parser/src/parser/statement.ts @@ -771,7 +771,7 @@ export default abstract class StatementParser extends ExpressionParser { expr = this.wrapParenthesis(startLoc, expr); const paramsStartLoc = this.state.startLoc; - node.expression = this.parseMaybeDecoratorArguments(expr); + node.expression = this.parseMaybeDecoratorArguments(expr, startLoc); if ( this.getPluginOption("decorators", "allowCallParenthesized") === false && @@ -801,7 +801,7 @@ export default abstract class StatementParser extends ExpressionParser { expr = this.finishNode(node, "MemberExpression"); } - node.expression = this.parseMaybeDecoratorArguments(expr); + node.expression = this.parseMaybeDecoratorArguments(expr, startLoc); } } else { node.expression = this.parseExprSubscripts(); @@ -809,9 +809,13 @@ export default abstract class StatementParser extends ExpressionParser { return this.finishNode(node, "Decorator"); } - parseMaybeDecoratorArguments(this: Parser, expr: N.Expression): N.Expression { + parseMaybeDecoratorArguments( + this: Parser, + expr: N.Expression, + startLoc: Position, + ): N.Expression { if (this.eat(tt.parenL)) { - const node = this.startNodeAtNode(expr); + const node = this.startNodeAt(startLoc); node.callee = expr; node.arguments = this.parseCallExpressionArguments(tt.parenR); this.toReferencedList(node.arguments); diff --git a/packages/babel-parser/src/plugins/typescript/index.ts b/packages/babel-parser/src/plugins/typescript/index.ts index dd862dc49b91..40cd61484dde 100644 --- a/packages/babel-parser/src/plugins/typescript/index.ts +++ b/packages/babel-parser/src/plugins/typescript/index.ts @@ -3756,7 +3756,10 @@ export default (superClass: ClassWithMixin) => return super.parseBindingAtom(); } - parseMaybeDecoratorArguments(expr: N.Expression): N.Expression { + parseMaybeDecoratorArguments( + expr: N.Expression, + startLoc: Position, + ): N.Expression { // handles `@f<` if (this.match(tt.lt) || this.match(tt.bitShiftL)) { const typeArguments = this.tsParseTypeArgumentsInExpression(); @@ -3764,6 +3767,7 @@ export default (superClass: ClassWithMixin) => if (this.match(tt.parenL)) { const call = super.parseMaybeDecoratorArguments( expr, + startLoc, ) as N.CallExpression; call.typeParameters = typeArguments; return call; @@ -3772,7 +3776,7 @@ export default (superClass: ClassWithMixin) => this.unexpected(null, tt.parenL); } - return super.parseMaybeDecoratorArguments(expr); + return super.parseMaybeDecoratorArguments(expr, startLoc); } checkCommaAfterRest( diff --git a/packages/babel-parser/test/fixtures/experimental/decorators/parenthesized-allowCallParenthesized-false-invalid/output.json b/packages/babel-parser/test/fixtures/experimental/decorators/parenthesized-allowCallParenthesized-false-invalid/output.json index 436ea880e3cf..e1ec6bed1b50 100644 --- a/packages/babel-parser/test/fixtures/experimental/decorators/parenthesized-allowCallParenthesized-false-invalid/output.json +++ b/packages/babel-parser/test/fixtures/experimental/decorators/parenthesized-allowCallParenthesized-false-invalid/output.json @@ -32,7 +32,7 @@ "start":14,"end":30,"loc":{"start":{"line":2,"column":2,"index":14},"end":{"line":2,"column":18,"index":30}}, "expression": { "type": "CallExpression", - "start":16,"end":30,"loc":{"start":{"line":2,"column":4,"index":16},"end":{"line":2,"column":18,"index":30}}, + "start":15,"end":30,"loc":{"start":{"line":2,"column":3,"index":15},"end":{"line":2,"column":18,"index":30}}, "callee": { "type": "MemberExpression", "start":16,"end":24,"loc":{"start":{"line":2,"column":4,"index":16},"end":{"line":2,"column":12,"index":24}}, diff --git a/packages/babel-parser/test/fixtures/experimental/decorators/parenthesized-allowCallParenthesized-true/output.json b/packages/babel-parser/test/fixtures/experimental/decorators/parenthesized-allowCallParenthesized-true/output.json index 881c8a693af6..d0a537f2c2c3 100644 --- a/packages/babel-parser/test/fixtures/experimental/decorators/parenthesized-allowCallParenthesized-true/output.json +++ b/packages/babel-parser/test/fixtures/experimental/decorators/parenthesized-allowCallParenthesized-true/output.json @@ -29,7 +29,7 @@ "start":14,"end":30,"loc":{"start":{"line":2,"column":2,"index":14},"end":{"line":2,"column":18,"index":30}}, "expression": { "type": "CallExpression", - "start":16,"end":30,"loc":{"start":{"line":2,"column":4,"index":16},"end":{"line":2,"column":18,"index":30}}, + "start":15,"end":30,"loc":{"start":{"line":2,"column":3,"index":15},"end":{"line":2,"column":18,"index":30}}, "callee": { "type": "MemberExpression", "start":16,"end":24,"loc":{"start":{"line":2,"column":4,"index":16},"end":{"line":2,"column":12,"index":24}}, diff --git a/packages/babel-parser/test/fixtures/experimental/decorators/parenthesized/output.json b/packages/babel-parser/test/fixtures/experimental/decorators/parenthesized/output.json index 0d826cdd1f1d..544a70660437 100644 --- a/packages/babel-parser/test/fixtures/experimental/decorators/parenthesized/output.json +++ b/packages/babel-parser/test/fixtures/experimental/decorators/parenthesized/output.json @@ -153,7 +153,7 @@ "start":93,"end":109,"loc":{"start":{"line":7,"column":2,"index":93},"end":{"line":7,"column":18,"index":109}}, "expression": { "type": "CallExpression", - "start":95,"end":109,"loc":{"start":{"line":7,"column":4,"index":95},"end":{"line":7,"column":18,"index":109}}, + "start":94,"end":109,"loc":{"start":{"line":7,"column":3,"index":94},"end":{"line":7,"column":18,"index":109}}, "callee": { "type": "MemberExpression", "start":95,"end":103,"loc":{"start":{"line":7,"column":4,"index":95},"end":{"line":7,"column":12,"index":103}},