Skip to content

Commit

Permalink
ES Parser will emit error for missing paren in if-expr
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Apr 8, 2023
1 parent c6b28f9 commit 498c2c9
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 16 deletions.
14 changes: 1 addition & 13 deletions crates/swc_ecma_parser/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,20 +470,8 @@ impl<'a, I: Tokens> Parser<I> {
},
)
})?;
if !eat!(self, ')') {
self.emit_err(self.input.cur_span(), SyntaxError::TS1005);

let span = span!(self, start);
return Ok(IfStmt {
span,
test,
cons: Box::new(Stmt::Expr(ExprStmt {
span,
expr: Box::new(Expr::Invalid(Invalid { span })),
})),
alt: Default::default(),
});
}
expect!(self, ')');

let cons = {
// Prevent stack overflow
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const foo = <T extends {}>() => {
if (bar() {
console.log(1);
}
};
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

x An arrow function is not allowed here
,-[input.js:1:1]
1 | const foo = <T extends {}>() => {
: ^^
2 | if (bar() {
`----

x Expression expected
,-[input.js:1:1]
1 | const foo = <T extends {}>() => {
: ^
2 | if (bar() {
`----
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const foo = () => {
if (bar() {
console.log(1);
}
};
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

x Expected ')', got '{'
,-[input.js:1:1]
1 | const foo = () => {
2 | if (bar() {
: ^
3 | console.log(1);
`----
8 changes: 5 additions & 3 deletions crates/swc_ecma_transforms_typescript/tests/strip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use swc_ecma_transforms_compat::{
es2022::{class_properties, static_blocks},
};
use swc_ecma_transforms_proposal::decorators;
use swc_ecma_transforms_testing::{test, test_exec, test_fixture, Tester};
use swc_ecma_transforms_testing::{test, test_exec, test_fixture, FixtureTestConfig, Tester};
use swc_ecma_transforms_typescript::{strip, strip::strip_with_config, TsImportExportAssignConfig};
use swc_ecma_visit::Fold;

Expand Down Expand Up @@ -846,7 +846,6 @@ test!(
__decorate([
DefineAction()
], Child.prototype, "action", void 0);
"#,
ok_if_code_eq
);
Expand Down Expand Up @@ -4395,7 +4394,10 @@ fn exec(input: PathBuf) {
&|t| chain!(tr(), properties(t, true)),
&input,
&output,
Default::default(),
FixtureTestConfig {
allow_error: true,
..Default::default()
},
);
}

Expand Down

0 comments on commit 498c2c9

Please sign in to comment.