Skip to content

Commit 2825c2f

Browse files
committed
lexer: support var foo = exports;foo.bar=true pattern
1 parent 3e292be commit 2825c2f

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

lexer/src/lexer.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -425,11 +425,16 @@ impl ModuleLexer {
425425
}
426426

427427
// var foo = module.exports = {};
428-
// foo === module.exports;
429-
fn try_to_mark_exports_alias(&mut self, decl: &VarDeclarator) {
428+
// var foo = module.exports;
429+
// var foo = exports;
430+
fn mark_exports_alias_from_var_decl(&mut self, decl: &VarDeclarator) {
430431
if let Pat::Ident(id) = &decl.name {
431432
if let Some(init) = &decl.init {
432-
if is_member(init, "module", "exports") {
433+
if let Expr::Ident(init_id) = init.as_ref() {
434+
if init_id.sym.as_ref().eq("exports") {
435+
self.exports_alias.insert(id.sym.as_ref().to_owned());
436+
}
437+
} else if is_member(init, "module", "exports") {
433438
self.exports_alias.insert(id.id.sym.as_ref().to_owned());
434439
} else if let Expr::Assign(assign) = init.as_ref() {
435440
if let Some(member) = get_member_expr_from_assign_target(&assign.left) {
@@ -845,7 +850,7 @@ impl ModuleLexer {
845850
Stmt::Decl(decl) => match decl {
846851
Decl::Var(var) => {
847852
for decl in &var.decls {
848-
self.try_to_mark_exports_alias(decl);
853+
self.mark_exports_alias_from_var_decl(decl);
849854
match &decl.name {
850855
Pat::Ident(BindingIdent { id, .. }) => {
851856
let id = id.sym.as_ref();
@@ -1227,7 +1232,7 @@ impl ModuleLexer {
12271232
// var foo = exports.foo = "bar"
12281233
Stmt::Decl(Decl::Var(var)) => {
12291234
for decl in var.as_ref().decls.iter() {
1230-
self.try_to_mark_exports_alias(decl);
1235+
self.mark_exports_alias_from_var_decl(decl);
12311236
if let Some(init_expr) = &decl.init {
12321237
if let Some(name) = self.get_export_name_from_bin_expr(init_expr) {
12331238
self.named_exports.insert(name);

lexer/src/test.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,28 @@ mod tests {
181181
assert_eq!(reexports.join(","), "lib");
182182
}
183183

184+
#[test]
185+
fn parse_cjs_exports_case_10_4() {
186+
let source = r#"
187+
var e = exports
188+
e.foo = 'bar'
189+
"#;
190+
let lexer = CommonJSModuleLexer::init("index.cjs", source).expect("could not parse the module");
191+
let (exports, _) = lexer.analyze("development", false);
192+
assert_eq!(exports.join(","), "foo");
193+
}
194+
195+
#[test]
196+
fn parse_cjs_exports_case_10_5() {
197+
let source = r#"
198+
var mod = module.exports
199+
mod.foo = 'bar'
200+
"#;
201+
let lexer = CommonJSModuleLexer::init("index.cjs", source).expect("could not parse the module");
202+
let (exports, _) = lexer.analyze("development", false);
203+
assert_eq!(exports.join(","), "foo");
204+
}
205+
184206
#[test]
185207
fn parse_cjs_exports_case_11() {
186208
let source = r#"

0 commit comments

Comments
 (0)