From 3180e68bf27fb95ff00bd24677ae7e96b3aa6c62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Mon, 28 Aug 2023 03:23:32 +0900 Subject: [PATCH] fix(es/compat): Apply transforms for explicit resource management (#7881) **Related issue:** - Closes #7879 --- crates/swc/src/config/mod.rs | 9 ++++++++- crates/swc_ecma_parser/src/lib.rs | 2 +- crates/swc_ecma_parser/src/parser/stmt.rs | 4 ++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/crates/swc/src/config/mod.rs b/crates/swc/src/config/mod.rs index 6b86cab03ae6..003a6c9d4125 100644 --- a/crates/swc/src/config/mod.rs +++ b/crates/swc/src/config/mod.rs @@ -57,7 +57,10 @@ use swc_ecma_transforms::{ modules::{path::NodeImportResolver, rewriter::import_rewriter}, optimization::{const_modules, json_parse, simplifier}, pass::{noop, Optional}, - proposals::{decorators, export_default_from, import_assertions}, + proposals::{ + decorators, explicit_resource_management::explicit_resource_management, + export_default_from, import_assertions, + }, react::{self, default_pragma, default_pragma_frag}, resolver, typescript::{self, TsEnumConfig, TsImportExportAssignConfig}, @@ -792,6 +795,10 @@ impl Options { }, syntax.decorators() ), + Optional::new( + explicit_resource_management(), + syntax.explicit_resource_management() + ), // The transform strips import assertions, so it's only enabled if // keep_import_assertions is false. Optional::new(import_assertions(), !keep_import_assertions), diff --git a/crates/swc_ecma_parser/src/lib.rs b/crates/swc_ecma_parser/src/lib.rs index fe50ea65dc64..e3f25e721226 100644 --- a/crates/swc_ecma_parser/src/lib.rs +++ b/crates/swc_ecma_parser/src/lib.rs @@ -282,7 +282,7 @@ impl Syntax { } } - fn using_decl(&self) -> bool { + pub fn explicit_resource_management(&self) -> bool { match self { Syntax::Es(EsConfig { using_decl, .. }) => *using_decl, Syntax::Typescript(_) => true, diff --git a/crates/swc_ecma_parser/src/parser/stmt.rs b/crates/swc_ecma_parser/src/parser/stmt.rs index 273d447843ee..7556a81038a6 100644 --- a/crates/swc_ecma_parser/src/parser/stmt.rs +++ b/crates/swc_ecma_parser/src/parser/stmt.rs @@ -816,7 +816,7 @@ impl<'a, I: Tokens> Parser { decls.push(self.parse_var_declarator(false, VarDeclKind::Var)?); } - if !self.syntax().using_decl() { + if !self.syntax().explicit_resource_management() { self.emit_err(span!(self, start), SyntaxError::UsingDeclNotEnabled); } @@ -1272,7 +1272,7 @@ impl<'a, I: Tokens> Parser { let start = cur_pos!(self); let init = self.include_in_expr(false).parse_for_head_prefix()?; - let is_using_decl = self.input.syntax().using_decl() + let is_using_decl = self.input.syntax().explicit_resource_management() && match *init { Expr::Ident(Ident { sym: js_word!("using"),