Skip to content

Commit

Permalink
fix: should keep locally defined variables in class (#6183)
Browse files Browse the repository at this point in the history
  • Loading branch information
h-a-n-a authored Apr 10, 2024
1 parent 958b557 commit 26e4eef
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use swc_core::ecma::ast::{FnExpr, ForHead, Function, Ident, KeyValueProp};
use swc_core::ecma::ast::{ForInStmt, ForOfStmt, ForStmt, IfStmt, LabeledStmt, WithStmt};
use swc_core::ecma::ast::{MetaPropExpr, NamedExport, NewExpr, ObjectLit, OptCall, OptChainBase};
use swc_core::ecma::ast::{ModuleDecl, ModuleItem, ObjectPat, ObjectPatProp, Stmt, WhileStmt};
use swc_core::ecma::ast::{OptChainExpr, ParamOrTsParamProp, Pat, ThisExpr, UnaryOp};
use swc_core::ecma::ast::{OptChainExpr, Pat, ThisExpr, UnaryOp};
use swc_core::ecma::ast::{Prop, PropName, PropOrSpread, RestPat, ReturnStmt, SeqExpr, TaggedTpl};
use swc_core::ecma::ast::{SwitchCase, SwitchStmt, Tpl, TryStmt, VarDecl, YieldExpr};
use swc_core::ecma::ast::{ThrowStmt, UnaryExpr, UpdateExpr};
Expand Down Expand Up @@ -1071,7 +1071,7 @@ impl<'parser> JavascriptParser<'parser> {
Pat::Object(obj) => self.walk_object_pattern(obj),
Pat::Rest(rest) => self.walk_rest_element(rest),
Pat::Expr(expr) => self.walk_expression(expr),
Pat::Ident(ident) => self.walk_identifier(ident),
Pat::Ident(_) => (),
Pat::Invalid(_) => (),
}
}
Expand Down Expand Up @@ -1166,16 +1166,16 @@ impl<'parser> JavascriptParser<'parser> {
let was_top_level = this.top_level_scope;
this.top_level_scope = TopLevelScope::False;

for prop in &ctor.params {
match prop {
ParamOrTsParamProp::Param(param) => this.walk_pattern(&param.pat),
ParamOrTsParamProp::TsParamProp(_) => unreachable!(),
let params = ctor.params.iter().map(|p| {
let p = &p.as_param().expect("should only contain param").pat;
Cow::Borrowed(p)
});
this.in_function_scope(true, params, |this| {
// TODO: `hooks.body_value`;
if let Some(body) = &ctor.body {
this.walk_block_statement(body);
}
}
// TODO: `hooks.body_value`;
if let Some(body) = &ctor.body {
this.walk_block_statement(body);
}
});

this.top_level_scope = was_top_level;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Example {
constructor(global = false) {
this.global = global;
}
}

it("should keep `global` as a local variable", function () {
expect(new Example().global).toBe(false);
});

2 comments on commit 26e4eef

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

task failure

Please sign in to comment.