Skip to content

Commit 1c9ab27

Browse files
authored
fix(es/transforms): Check errors::HANDLER.is_set() before failing (#11130)
**Related issue:** - denoland/deno#30858
1 parent c79e1e5 commit 1c9ab27

File tree

4 files changed

+161
-125
lines changed

4 files changed

+161
-125
lines changed

.changeset/sour-mugs-learn.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
swc_ecma_transforms_react: patch
3+
swc_ecma_transforms_typescript: patch
4+
swc_ts_fast_strip: patch
5+
swc_core: patch
6+
---
7+
8+
fix(es/transforms): Check errors::HANDLER.is_set() before failing

crates/swc_ecma_transforms_react/src/jsx/mod.rs

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -340,14 +340,16 @@ impl JsxDirectives {
340340
Some("automatic") => res.runtime = Some(Runtime::Automatic),
341341
None => {}
342342
_ => {
343-
HANDLER.with(|handler| {
344-
handler
345-
.struct_span_err(
346-
cmt.span,
347-
"Runtime must be either `classic` or `automatic`.",
348-
)
349-
.emit()
350-
});
343+
if HANDLER.is_set() {
344+
HANDLER.with(|handler| {
345+
handler
346+
.struct_span_err(
347+
cmt.span,
348+
"Runtime must be either `classic` or `automatic`.",
349+
)
350+
.emit()
351+
});
352+
}
351353
}
352354
},
353355
Some("@jsxImportSource") => {
@@ -691,7 +693,7 @@ where
691693
.and_then(jsx_attr_value_to_expr)
692694
.map(|expr| expr.as_arg());
693695

694-
if key.is_none() {
696+
if key.is_none() && HANDLER.is_set() {
695697
HANDLER.with(|handler| {
696698
handler
697699
.struct_span_err(
@@ -766,7 +768,7 @@ where
766768
name,
767769
..
768770
}) => {
769-
if self.throw_if_namespace {
771+
if self.throw_if_namespace && HANDLER.is_set() {
770772
HANDLER.with(|handler| {
771773
handler
772774
.struct_span_err(
@@ -1069,14 +1071,16 @@ where
10691071

10701072
if let Some(pragma) = pragma {
10711073
if let Runtime::Automatic = self.runtime {
1072-
HANDLER.with(|handler| {
1073-
handler
1074-
.struct_span_err(
1075-
pragma.span(),
1076-
"pragma cannot be set when runtime is automatic",
1077-
)
1078-
.emit()
1079-
});
1074+
if HANDLER.is_set() {
1075+
HANDLER.with(|handler| {
1076+
handler
1077+
.struct_span_err(
1078+
pragma.span(),
1079+
"pragma cannot be set when runtime is automatic",
1080+
)
1081+
.emit()
1082+
});
1083+
}
10801084
}
10811085

10821086
found = true;
@@ -1085,14 +1089,16 @@ where
10851089

10861090
if let Some(pragma_frag) = pragma_frag {
10871091
if let Runtime::Automatic = self.runtime {
1088-
HANDLER.with(|handler| {
1089-
handler
1090-
.struct_span_err(
1091-
pragma_frag.span(),
1092-
"pragmaFrag cannot be set when runtime is automatic",
1093-
)
1094-
.emit()
1095-
});
1092+
if HANDLER.is_set() {
1093+
HANDLER.with(|handler| {
1094+
handler
1095+
.struct_span_err(
1096+
pragma_frag.span(),
1097+
"pragmaFrag cannot be set when runtime is automatic",
1098+
)
1099+
.emit()
1100+
});
1101+
}
10961102
}
10971103

10981104
found = true;
@@ -1296,7 +1302,7 @@ where
12961302
JSXElementName::JSXNamespacedName(JSXNamespacedName {
12971303
ref ns, ref name, ..
12981304
}) => {
1299-
if self.throw_if_namespace {
1305+
if self.throw_if_namespace && HANDLER.is_set() {
13001306
HANDLER.with(|handler| {
13011307
handler
13021308
.struct_span_err(

crates/swc_ecma_transforms_typescript/src/transform.rs

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,26 +1008,30 @@ impl Transform {
10081008
}
10091009
TsModuleRef::TsExternalModuleRef(..) => {
10101010
// TS1147
1011-
HANDLER.with(|handler| {
1012-
handler
1013-
.struct_span_err(
1014-
decl.span,
1015-
r#"Import declarations in a namespace cannot reference a module."#,
1016-
)
1017-
.emit();
1018-
});
1011+
if HANDLER.is_set() {
1012+
HANDLER.with(|handler| {
1013+
handler
1014+
.struct_span_err(
1015+
decl.span,
1016+
r#"Import declarations in a namespace cannot reference a module."#,
1017+
)
1018+
.emit();
1019+
});
1020+
}
10191021
}
10201022
}
10211023
}
10221024
item => {
1023-
HANDLER.with(|handler| {
1024-
handler
1025-
.struct_span_err(
1026-
item.span(),
1027-
r#"ESM-style module declarations are not permitted in a namespace."#,
1028-
)
1029-
.emit();
1030-
});
1025+
if HANDLER.is_set() {
1026+
HANDLER.with(|handler| {
1027+
handler
1028+
.struct_span_err(
1029+
item.span(),
1030+
r#"ESM-style module declarations are not permitted in a namespace."#,
1031+
)
1032+
.emit();
1033+
});
1034+
}
10311035
}
10321036
}
10331037
}
@@ -1318,13 +1322,15 @@ impl Transform {
13181322
}
13191323
TsImportExportAssignConfig::EsNext => {
13201324
// TS1202
1321-
HANDLER.with(|handler| {
1322-
handler.struct_span_err(
1323-
decl.span,
1324-
r#"Import assignment cannot be used when targeting ECMAScript modules. Consider using `import * as ns from "mod"`, `import {a} from "mod"`, `import d from "mod"`, or another module format instead."#,
1325-
)
1326-
.emit();
1327-
});
1325+
if HANDLER.is_set() {
1326+
HANDLER.with(|handler| {
1327+
handler.struct_span_err(
1328+
decl.span,
1329+
r#"Import assignment cannot be used when targeting ECMAScript modules. Consider using `import * as ns from "mod"`, `import {a} from "mod"`, `import d from "mod"`, or another module format instead."#,
1330+
)
1331+
.emit();
1332+
});
1333+
}
13281334
}
13291335
}
13301336
}
@@ -1411,14 +1417,16 @@ impl Transform {
14111417
}
14121418
TsImportExportAssignConfig::NodeNext | TsImportExportAssignConfig::EsNext => {
14131419
// TS1203
1414-
HANDLER.with(|handler| {
1415-
handler
1416-
.struct_span_err(
1417-
cjs_export_assign.span,
1418-
r#"Export assignment cannot be used when targeting ECMAScript modules. Consider using `export default` or another module format instead."#,
1419-
)
1420-
.emit()
1421-
});
1420+
if HANDLER.is_set() {
1421+
HANDLER.with(|handler| {
1422+
handler
1423+
.struct_span_err(
1424+
cjs_export_assign.span,
1425+
r#"Export assignment cannot be used when targeting ECMAScript modules. Consider using `export default` or another module format instead."#,
1426+
)
1427+
.emit()
1428+
});
1429+
}
14221430
}
14231431
}
14241432
}

0 commit comments

Comments
 (0)