Skip to content

Commit

Permalink
fix(es/resolver): Correctly check strict mode (#8851)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8842
  • Loading branch information
Austaras committed Apr 13, 2024
1 parent d4b89db commit f6ba92b
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
function f() {
var x, g;
var x, g1;
g();
x = 10;
throw Error("foo");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
function n() {
o();
g();
n = 10;
throw new Error("foo");
if (n) {
Expand Down
25 changes: 15 additions & 10 deletions crates/swc_ecma_transforms_base/src/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,11 +558,14 @@ impl<'a> VisitMut for Resolver<'a> {
child.mark_block(&mut s.span);

let old_strict_mode = child.strict_mode;
child.strict_mode = s
.stmts
.first()
.map(|stmt| stmt.is_use_strict())
.unwrap_or(false);

if !child.strict_mode {
child.strict_mode = s
.stmts
.first()
.map(|stmt| stmt.is_use_strict())
.unwrap_or(false);
}
// Prevent creating new scope.
s.stmts.visit_mut_with(child);
child.strict_mode = old_strict_mode;
Expand Down Expand Up @@ -901,11 +904,13 @@ impl<'a> VisitMut for Resolver<'a> {
Some(body) => {
self.mark_block(&mut body.span);
let old_strict_mode = self.strict_mode;
self.strict_mode = body
.stmts
.first()
.map(|stmt| stmt.is_use_strict())
.unwrap_or(false);
if !self.strict_mode {
self.strict_mode = body
.stmts
.first()
.map(|stmt| stmt.is_use_strict())
.unwrap_or(false);
}
// Prevent creating new scope.
body.visit_mut_children_with(self);
self.strict_mode = old_strict_mode;
Expand Down
12 changes: 6 additions & 6 deletions crates/swc_ecma_transforms_base/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use swc_ecma_visit::{
};
use testing::{fixture, run_test2, NormalizedOutput};

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

// println!("Emitting: {:?}", module);
emitter.emit_module(module).unwrap();
emitter.emit_program(program).unwrap();
}

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

let module = parser
.parse_module()
let program = parser
.parse_program()
.map_err(|err| err.into_diagnostic(&handler).emit())?;

let mut folder = op();

let module = module.fold_with(&mut folder);
let program = program.fold_with(&mut folder);

let actual = print(cm, &module);
let actual = print(cm, &program);
let actual = NormalizedOutput::from(actual);

actual.compare_to_file(&output).unwrap();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function k() {
function x() {
console.log("hi");
}
{
function x() {
console.log("merong");
}
}
return x;
}
k();
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function k__2() {
function x__3() {
console.log("hi");
}
{
function x__5() {
console.log("merong");
}
}
return x__3;
}
k__2();
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
switch(0){
case x__3:
function x__3() {}
case x__2:
function x__2() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ var NaN__2 = 1;
{
let NaN__3 = 1;
console.log(NaN__3);
}console.log(NaN__2);
}console.log(NaN);
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
var NaN__2;
console.log(NaN__2.toString());
console.log(NaN.toString());
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
var NaN__2 = 5;
console.log(NaN__2.toString());
console.log(NaN.toString());

0 comments on commit f6ba92b

Please sign in to comment.