Skip to content

Commit

Permalink
fix(es/parser): Remove wrong check about const without init (#9970)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #9883
  • Loading branch information
kdy1 authored Jan 29, 2025
1 parent 772cc30 commit 1b57261
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/cuddly-rocks-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_ecma_parser: patch
swc_core: patch
---

fix(es/parser): Remove wrong check about `const` without init
3 changes: 1 addition & 2 deletions crates/swc_ecma_parser/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1006,8 +1006,7 @@ impl<'a, I: Tokens> Parser<I> {
// typescript allows `declare` vars not to have initializers.
if self.ctx().in_declare {
None
} else if kind == VarDeclKind::Const && self.ctx().strict && !self.ctx().in_declare
{
} else if kind == VarDeclKind::Const && !for_loop && !self.ctx().in_declare {
self.emit_err(
span!(self, start),
SyntaxError::ConstDeclarationsRequireInitialization,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
x 'const' declarations must be initialized
,-[$DIR/tests/typescript-errors/issue-1170-1/input.ts:1:1]
1 | const toString: (local)(this: Function) => string) = undefined;
: ^^^^^^^^^^^^^^^^^
`----
x Expected a semicolon
,-[$DIR/tests/typescript-errors/issue-1170-1/input.ts:1:1]
1 | const toString: (local)(this: Function) => string) = undefined;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
x 'const' declarations must be initialized
,-[$DIR/tests/typescript-errors/issue-1391/case1/input.ts:1:1]
1 | const Methods {
: ^^^^^^^
2 | f: (x, y) => x + y,
`----
x Expected a semicolon
,-[$DIR/tests/typescript-errors/issue-1391/case1/input.ts:1:1]
1 | const Methods {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
x 'const' declarations must be initialized
,----
1 | interface Foo { }; const foo;
: ^^^
`----
1 change: 1 addition & 0 deletions crates/swc_fast_ts_strip/tests/errors/invalid-syntax-1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
interface Foo { }; const foo;

0 comments on commit 1b57261

Please sign in to comment.