mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
fix(codegen): print Directive original string (#2157)
> A Use Strict Directive may not contain an EscapeSequence or LineContinuation. It is `Use Strict Directive` spec, but the `expression` of `Directive` isn't original string value, it has error if using it to codegen, so here using `directive` of `Directive` to codegen and not to escape it. Here is crashed test cases. ``` js 'use str\ ict'; ``` The babel will print the original string, I follow it and avoid using `print_str` because it will escape string. I also changed some code using the `expression` of `Directive` to check `Use Strict Directive` .
This commit is contained in:
parent
ac1d318dc3
commit
989ab88bc6
6 changed files with 17 additions and 19 deletions
|
|
@ -92,8 +92,13 @@ impl<const MINIFY: bool> Gen<MINIFY> for Hashbang {
|
||||||
|
|
||||||
impl<const MINIFY: bool> Gen<MINIFY> for Directive {
|
impl<const MINIFY: bool> Gen<MINIFY> for Directive {
|
||||||
fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) {
|
fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) {
|
||||||
// Use the string value instead of the raw self.directive because it can cannot escaped values.
|
// A Use Strict Directive may not contain an EscapeSequence or LineContinuation.
|
||||||
print_str(self.expression.value.as_str(), p);
|
// So here should print original `directive` value, the `expression` value is escaped str.
|
||||||
|
// See https://github.com/babel/babel/blob/main/packages/babel-generator/src/generators/base.ts#L64
|
||||||
|
let quote = choose_quote(self.directive.as_str());
|
||||||
|
p.print(quote as u8);
|
||||||
|
p.print_str(self.directive.as_bytes());
|
||||||
|
p.print(quote as u8);
|
||||||
p.print_semicolon();
|
p.print_semicolon();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,11 +43,7 @@ impl Rule for NoAsyncClientComponent {
|
||||||
let Some(root) = ctx.nodes().iter().next() else { return };
|
let Some(root) = ctx.nodes().iter().next() else { return };
|
||||||
let AstKind::Program(program) = root.kind() else { return };
|
let AstKind::Program(program) = root.kind() else { return };
|
||||||
|
|
||||||
if program
|
if program.directives.iter().any(|directive| directive.directive.as_str() == "use client") {
|
||||||
.directives
|
|
||||||
.iter()
|
|
||||||
.any(|directive| directive.expression.value.as_str() == "use client")
|
|
||||||
{
|
|
||||||
for node in &program.body {
|
for node in &program.body {
|
||||||
let Statement::ModuleDeclaration(mod_decl) = &node else {
|
let Statement::ModuleDeclaration(mod_decl) = &node else {
|
||||||
continue;
|
continue;
|
||||||
|
|
|
||||||
|
|
@ -87,7 +87,7 @@ impl<'a> Format<'a> for Directive {
|
||||||
let mut parts = p.vec();
|
let mut parts = p.vec();
|
||||||
parts.push(Doc::Str(string::print_string(
|
parts.push(Doc::Str(string::print_string(
|
||||||
p,
|
p,
|
||||||
self.expression.value.as_str(),
|
self.directive.as_str(),
|
||||||
p.options.single_quote,
|
p.options.single_quote,
|
||||||
)));
|
)));
|
||||||
if let Some(semi) = p.semi() {
|
if let Some(semi) = p.semi() {
|
||||||
|
|
|
||||||
|
|
@ -393,7 +393,7 @@ fn check_directive<'a>(directive: &Directive, node: &AstNode<'a>, ctx: &Semantic
|
||||||
#[diagnostic()]
|
#[diagnostic()]
|
||||||
struct IllegalUseStrict(#[label] Span);
|
struct IllegalUseStrict(#[label] Span);
|
||||||
|
|
||||||
if directive.expression.value != "use strict" {
|
if directive.directive != "use strict" {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
codegen_runtime_test262 Summary:
|
codegen_runtime_test262 Summary:
|
||||||
AST Parsed : 19663/19663 (100.00%)
|
AST Parsed : 19665/19665 (100.00%)
|
||||||
Positive Passed: 19219/19663 (97.74%)
|
Positive Passed: 19220/19665 (97.74%)
|
||||||
Expect to run correctly: "annexB/built-ins/String/prototype/substr/surrogate-pairs.js"
|
Expect to run correctly: "annexB/built-ins/String/prototype/substr/surrogate-pairs.js"
|
||||||
But got a runtime error: Test262Error: start: 1 Expected SameValue(«<>», «\udf06») to be true
|
But got a runtime error: Test262Error: start: 1 Expected SameValue(«<>», «\udf06») to be true
|
||||||
|
|
||||||
|
|
@ -131,12 +131,6 @@ But got a runtime error: Test262Error: descriptor should be enumerable
|
||||||
Expect to run correctly: "annexB/language/global-code/switch-dflt-global-init.js"
|
Expect to run correctly: "annexB/language/global-code/switch-dflt-global-init.js"
|
||||||
But got a runtime error: Test262Error: descriptor should be enumerable
|
But got a runtime error: Test262Error: descriptor should be enumerable
|
||||||
|
|
||||||
Expect to run correctly: "language/directive-prologue/14.1-4-s.js"
|
|
||||||
But got a runtime error: Test262Error: Expected true but got false
|
|
||||||
|
|
||||||
Expect to run correctly: "language/directive-prologue/14.1-5-s.js"
|
|
||||||
But got a runtime error: Test262Error: Expected true but got false
|
|
||||||
|
|
||||||
Expect to run correctly: "language/eval-code/direct/async-gen-func-decl-fn-body-cntns-arguments-func-decl-declare-arguments-and-assign.js"
|
Expect to run correctly: "language/eval-code/direct/async-gen-func-decl-fn-body-cntns-arguments-func-decl-declare-arguments-and-assign.js"
|
||||||
But got a runtime error: Test262Error: Expected a SyntaxError to be thrown but no exception was thrown at all
|
But got a runtime error: Test262Error: Expected a SyntaxError to be thrown but no exception was thrown at all
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,8 @@
|
||||||
Passed: 329/1369
|
Passed: 327/1369
|
||||||
|
|
||||||
# All Passed:
|
# All Passed:
|
||||||
* babel-plugin-transform-numeric-separator
|
* babel-plugin-transform-numeric-separator
|
||||||
* babel-plugin-transform-optional-catch-binding
|
* babel-plugin-transform-optional-catch-binding
|
||||||
* babel-plugin-transform-json-strings
|
|
||||||
* babel-plugin-transform-shorthand-properties
|
* babel-plugin-transform-shorthand-properties
|
||||||
* babel-plugin-transform-sticky-regex
|
* babel-plugin-transform-sticky-regex
|
||||||
* babel-plugin-transform-instanceof
|
* babel-plugin-transform-instanceof
|
||||||
|
|
@ -608,6 +607,10 @@ Passed: 329/1369
|
||||||
* transparent-expr-wrappers/ts-as-member-expression/input.ts
|
* transparent-expr-wrappers/ts-as-member-expression/input.ts
|
||||||
* transparent-expr-wrappers/ts-parenthesized-expression-member-call/input.ts
|
* transparent-expr-wrappers/ts-parenthesized-expression-member-call/input.ts
|
||||||
|
|
||||||
|
# babel-plugin-transform-json-strings (2/4)
|
||||||
|
* json-strings/directive-line-separator/input.js
|
||||||
|
* json-strings/directive-paragraph-separator/input.js
|
||||||
|
|
||||||
# babel-plugin-transform-async-generator-functions (0/22)
|
# babel-plugin-transform-async-generator-functions (0/22)
|
||||||
* async-generators/class-method/input.js
|
* async-generators/class-method/input.js
|
||||||
* async-generators/class-private-method/input.js
|
* async-generators/class-private-method/input.js
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue