fix(es/resolver): Correctly check strict mode (#8851)

**Related issue:**

 - Closes #8842
This commit is contained in:
Austaras 2024-04-13 10:38:10 +08:00 committed by GitHub
parent d4b89db047
commit f6ba92b033
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 52 additions and 23 deletions

View File

@ -1,6 +1,6 @@
"use strict";
function f() {
var x, g;
var x, g1;
g();
x = 10;
throw Error("foo");

View File

@ -1,6 +1,6 @@
"use strict";
function n() {
o();
g();
n = 10;
throw new Error("foo");
if (n) {

View File

@ -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;
@ -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;

View File

@ -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 {
@ -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);
@ -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();

View File

@ -0,0 +1,12 @@
export function k() {
function x() {
console.log("hi");
}
{
function x() {
console.log("merong");
}
}
return x;
}
k();

View File

@ -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();

View File

@ -1,4 +1,4 @@
switch(0){
case x__3:
function x__3() {}
case x__2:
function x__2() {}
}

View File

@ -2,4 +2,4 @@ var NaN__2 = 1;
{
let NaN__3 = 1;
console.log(NaN__3);
}console.log(NaN__2);
}console.log(NaN);

View File

@ -1,2 +1,2 @@
var NaN__2;
console.log(NaN__2.toString());
console.log(NaN.toString());

View File

@ -1,2 +1,2 @@
var NaN__2 = 5;
console.log(NaN__2.toString());
console.log(NaN.toString());