Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: analysis ParenthesizedExpression or FunctionExpression correctly in parser (#2605) #2620

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 31 additions & 19 deletions src/parser.ts
Expand Up @@ -514,19 +514,23 @@ export class Parser extends DiagnosticEmitter {
if (signature) {
if (isInnerParenthesized) {
if (!tn.skip(Token.CloseParen)) {
this.error(
DiagnosticCode._0_expected,
tn.range(), ")"
);
if (!suppressErrors) {
this.error(
DiagnosticCode._0_expected,
tn.range(), ")"
);
}
return null;
}
}
type = signature;
} else if (isInnerParenthesized || this.tryParseSignatureIsSignature) {
this.error(
DiagnosticCode.Unexpected_token,
tn.range()
);
if (!suppressErrors) {
this.error(
DiagnosticCode.Unexpected_token,
tn.range()
);
}
return null;
// Type (',' Type)* ')'
} else if (acceptParenthesized) {
Expand All @@ -545,10 +549,12 @@ export class Parser extends DiagnosticEmitter {
type.range.start = startPos;
type.range.end = tn.pos;
} else {
this.error(
DiagnosticCode.Unexpected_token,
tn.range()
);
if (!suppressErrors) {
this.error(
DiagnosticCode.Unexpected_token,
tn.range()
);
}
return null;
}

Expand Down Expand Up @@ -3780,14 +3786,20 @@ export class Parser extends DiagnosticEmitter {

// if we got here, check for arrow
case Token.CloseParen: {
if (
!tn.skip(Token.Colon) &&
!tn.skip(Token.Equals_GreaterThan)
) {
again = false;
break;
// `Identifier):Type =>` is function expression
if (tn.skip(Token.Colon)) {
let type = this.parseType(tn, true, true);
if (type == null) {
again = false;
break;
}
}
// fall-through
if (tn.skip(Token.Equals_GreaterThan)) {
tn.reset(state);
return this.parseFunctionExpression(tn);
}
again = false;
break;
HerrCai0907 marked this conversation as resolved.
Show resolved Hide resolved
}
// function expression
case Token.Colon: { // type annotation
Expand Down
2 changes: 2 additions & 0 deletions tests/parser/arrow-functions.ts
Expand Up @@ -9,4 +9,6 @@ x => x;

// not an array function
(b ? x : y);
b ? (x) : y;
b ? (x):i32=>1 : y;
(b ? f : g)();
2 changes: 2 additions & 0 deletions tests/parser/arrow-functions.ts.fixture.ts
Expand Up @@ -6,4 +6,6 @@
x => x;
() => {};
(b ? x : y);
b ? (x) : y;
b ? (x): i32 => 1 : y;
(b ? f : g)();