Skip to content

Commit

Permalink
fix: analysis ParenthesizedExpression or FunctionExpression more …
Browse files Browse the repository at this point in the history
…correctly in parser (AssemblyScript#2605)
  • Loading branch information
HerrCai0907 committed Jan 17, 2023
1 parent 7348584 commit b66617a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
50 changes: 31 additions & 19 deletions src/parser.ts
Original file line number Diff line number Diff line change
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;
}
// function expression
case Token.Colon: { // type annotation
Expand Down
2 changes: 2 additions & 0 deletions tests/parser/arrow-functions.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
x => x;
() => {};
(b ? x : y);
b ? (x) : y;
b ? (x): i32 => 1 : y;
(b ? f : g)();

0 comments on commit b66617a

Please sign in to comment.