Skip to content

Commit 6e9d1a4

Browse files
authored
fix(es/helpers): Fix resolving of usingCtx helper (#8874)
**Related issue:** - Closes #8872
1 parent 10bb67a commit 6e9d1a4

File tree

1 file changed

+82
-1
lines changed
  • crates/swc_ecma_transforms_base/src/helpers

1 file changed

+82
-1
lines changed

crates/swc_ecma_transforms_base/src/helpers/mod.rs

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ impl VisitMut for Marker {
585585

586586
fn visit_mut_var_declarator(&mut self, v: &mut VarDeclarator) {
587587
if let Pat::Ident(i) = &mut v.name {
588-
if &*i.id.sym == "id" {
588+
if &*i.id.sym == "id" || &*i.id.sym == "resource" {
589589
i.id.span.ctxt = self.base;
590590
self.decls.insert(i.id.sym.clone(), self.base);
591591
return;
@@ -690,6 +690,7 @@ let _throw1 = null;
690690
Default::default,
691691
)
692692
}
693+
693694
#[test]
694695
fn use_strict_abort() {
695696
crate::tests::test_transform(
@@ -705,4 +706,84 @@ let x = 4;",
705706
Default::default,
706707
);
707708
}
709+
710+
#[test]
711+
fn issue_8871() {
712+
crate::tests::test_transform(
713+
Default::default(),
714+
|_| {
715+
enable_helper!(using_ctx);
716+
as_folder(inject_helpers(Mark::new()))
717+
},
718+
"let _throw = null",
719+
r#"
720+
function _using_ctx() {
721+
var _disposeSuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed) {
722+
var err = new Error();
723+
err.name = "SuppressedError";
724+
err.suppressed = suppressed;
725+
err.error = error;
726+
return err;
727+
}, empty = {}, stack = [];
728+
function using(isAwait, value) {
729+
if (value != null) {
730+
if (Object(value) !== value) {
731+
throw new TypeError("using declarations can only be used with objects, functions, null, or undefined.");
732+
}
733+
if (isAwait) {
734+
var dispose = value[Symbol.asyncDispose || Symbol.for("Symbol.asyncDispose")];
735+
}
736+
if (dispose == null) {
737+
dispose = value[Symbol.dispose || Symbol.for("Symbol.dispose")];
738+
}
739+
if (typeof dispose !== "function") {
740+
throw new TypeError(`Property [Symbol.dispose] is not a function.`);
741+
}
742+
stack.push({
743+
v: value,
744+
d: dispose,
745+
a: isAwait
746+
});
747+
} else if (isAwait) {
748+
stack.push({
749+
d: value,
750+
a: isAwait
751+
});
752+
}
753+
return value;
754+
}
755+
return {
756+
e: empty,
757+
u: using.bind(null, false),
758+
a: using.bind(null, true),
759+
d: function() {
760+
var error = this.e;
761+
function next() {
762+
while(resource = stack.pop()){
763+
try {
764+
var resource, disposalResult = resource.d && resource.d.call(resource.v);
765+
if (resource.a) {
766+
return Promise.resolve(disposalResult).then(next, err);
767+
}
768+
} catch (e) {
769+
return err(e);
770+
}
771+
}
772+
if (error !== empty) throw error;
773+
}
774+
function err(e) {
775+
error = error !== empty ? new _disposeSuppressedError(error, e) : e;
776+
return next();
777+
}
778+
return next();
779+
}
780+
};
781+
}
782+
783+
let _throw = null;
784+
"#,
785+
false,
786+
Default::default,
787+
)
788+
}
708789
}

0 commit comments

Comments
 (0)