Skip to content

Commit f6ba92b

Browse files
authored
fix(es/resolver): Correctly check strict mode (#8851)
**Related issue:** - Closes #8842
1 parent d4b89db commit f6ba92b

File tree

10 files changed

+52
-23
lines changed

10 files changed

+52
-23
lines changed

crates/swc_ecma_minifier/tests/terser/compress/dead_code/dead_code_2_should_warn_strict/output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22
function f() {
3-
var x, g;
3+
var x, g1;
44
g();
55
x = 10;
66
throw Error("foo");

crates/swc_ecma_minifier/tests/terser/compress/dead_code/dead_code_2_should_warn_strict/output.mangleOnly.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22
function n() {
3-
o();
3+
g();
44
n = 10;
55
throw new Error("foo");
66
if (n) {

crates/swc_ecma_transforms_base/src/resolver/mod.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -558,11 +558,14 @@ impl<'a> VisitMut for Resolver<'a> {
558558
child.mark_block(&mut s.span);
559559

560560
let old_strict_mode = child.strict_mode;
561-
child.strict_mode = s
562-
.stmts
563-
.first()
564-
.map(|stmt| stmt.is_use_strict())
565-
.unwrap_or(false);
561+
562+
if !child.strict_mode {
563+
child.strict_mode = s
564+
.stmts
565+
.first()
566+
.map(|stmt| stmt.is_use_strict())
567+
.unwrap_or(false);
568+
}
566569
// Prevent creating new scope.
567570
s.stmts.visit_mut_with(child);
568571
child.strict_mode = old_strict_mode;
@@ -901,11 +904,13 @@ impl<'a> VisitMut for Resolver<'a> {
901904
Some(body) => {
902905
self.mark_block(&mut body.span);
903906
let old_strict_mode = self.strict_mode;
904-
self.strict_mode = body
905-
.stmts
906-
.first()
907-
.map(|stmt| stmt.is_use_strict())
908-
.unwrap_or(false);
907+
if !self.strict_mode {
908+
self.strict_mode = body
909+
.stmts
910+
.first()
911+
.map(|stmt| stmt.is_use_strict())
912+
.unwrap_or(false);
913+
}
909914
// Prevent creating new scope.
910915
body.visit_mut_children_with(self);
911916
self.strict_mode = old_strict_mode;

crates/swc_ecma_transforms_base/tests/fixture.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use swc_ecma_visit::{
1010
};
1111
use testing::{fixture, run_test2, NormalizedOutput};
1212

13-
pub fn print(cm: Lrc<SourceMap>, module: &Module) -> String {
13+
pub fn print(cm: Lrc<SourceMap>, program: &Program) -> String {
1414
let mut buf = vec![];
1515
{
1616
let mut emitter = Emitter {
@@ -23,7 +23,7 @@ pub fn print(cm: Lrc<SourceMap>, module: &Module) -> String {
2323
};
2424

2525
// println!("Emitting: {:?}", module);
26-
emitter.emit_module(module).unwrap();
26+
emitter.emit_program(program).unwrap();
2727
}
2828

2929
let s = String::from_utf8_lossy(&buf);
@@ -47,15 +47,15 @@ where
4747
let lexer = Lexer::new(syntax, EsVersion::latest(), StringInput::from(&*fm), None);
4848
let mut parser = Parser::new_from(lexer);
4949

50-
let module = parser
51-
.parse_module()
50+
let program = parser
51+
.parse_program()
5252
.map_err(|err| err.into_diagnostic(&handler).emit())?;
5353

5454
let mut folder = op();
5555

56-
let module = module.fold_with(&mut folder);
56+
let program = program.fold_with(&mut folder);
5757

58-
let actual = print(cm, &module);
58+
let actual = print(cm, &program);
5959
let actual = NormalizedOutput::from(actual);
6060

6161
actual.compare_to_file(&output).unwrap();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function k() {
2+
function x() {
3+
console.log("hi");
4+
}
5+
{
6+
function x() {
7+
console.log("merong");
8+
}
9+
}
10+
return x;
11+
}
12+
k();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function k__2() {
2+
function x__3() {
3+
console.log("hi");
4+
}
5+
{
6+
function x__5() {
7+
console.log("merong");
8+
}
9+
}
10+
return x__3;
11+
}
12+
k__2();
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
switch(0){
2-
case x__3:
3-
function x__3() {}
2+
case x__2:
3+
function x__2() {}
44
}

crates/swc_ecma_transforms_base/tests/resolver/issues/7685/output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ var NaN__2 = 1;
22
{
33
let NaN__3 = 1;
44
console.log(NaN__3);
5-
}console.log(NaN__2);
5+
}console.log(NaN);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
var NaN__2;
2-
console.log(NaN__2.toString());
2+
console.log(NaN.toString());
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
var NaN__2 = 5;
2-
console.log(NaN__2.toString());
2+
console.log(NaN.toString());

0 commit comments

Comments
 (0)