-
-
Notifications
You must be signed in to change notification settings - Fork 480
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transfrom): transform-json-strings (#2168)
The pr intends to implement the plugin `babel-plugin-transform-json-strings`. But here is only mutate `Directive`, the `StringLiteral` is not implement. It need to changed the `StringLiteral` printer. I'm intend to add the raw of `StringLiteral`, it will be mutate at plugin, and using the `raw` to print `StringLiteral`. If you other ideas, please let me know. --------- Co-authored-by: Boshen <[email protected]>
- Loading branch information
Showing
7 changed files
with
91 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use oxc_ast::ast::*; | ||
use oxc_span::Atom; | ||
use oxc_syntax::identifier::{LS, PS}; | ||
|
||
use crate::options::{TransformOptions, TransformTarget}; | ||
|
||
/// ES2019: Json Strings | ||
/// | ||
/// References: | ||
/// * <https://babeljs.io/docs/babel-plugin-transform-json-strings> | ||
/// * <https://github.com/babel/babel/tree/main/packages/babel-plugin-transform-json-strings> | ||
pub struct JsonStrings; | ||
|
||
impl JsonStrings { | ||
pub fn new(options: &TransformOptions) -> Option<Self> { | ||
(options.target < TransformTarget::ES2019 || options.json_strings).then(|| Self {}) | ||
} | ||
|
||
// Allow `U+2028` and `U+2029` in string literals | ||
// <https://tc39.es/proposal-json-superset> | ||
// <https://github.com/tc39/proposal-json-superset> | ||
fn normalize_str(str: &str) -> Option<Atom> { | ||
if !str.contains(LS) && !str.contains(PS) { | ||
return None; | ||
} | ||
let mut buf = String::new(); | ||
let mut is_escaped = false; | ||
for c in str.chars() { | ||
match (is_escaped, c) { | ||
(false, LS) => buf.push_str("\\u2028"), | ||
(false, PS) => buf.push_str("\\u2029"), | ||
_ => buf.push(c), | ||
} | ||
is_escaped = !is_escaped && matches!(c, '\\'); | ||
} | ||
Some(buf.into()) | ||
} | ||
|
||
#[allow(clippy::unused_self)] | ||
// TODO oxc_codegen currently prints json strings correctly, | ||
// but we need a way to turn off this behaviour from codegen | ||
// and do the transformation here. | ||
pub fn transform_string_literal(&mut self, _literal: &mut StringLiteral) { | ||
// let str = &self.ctx.semantic().source_text()[literal.span.start as usize + 1..literal.span.end as usize - 1]; | ||
// if let Some(value) = Self::normalize_str(str) { | ||
// literal.value = value; | ||
// } | ||
} | ||
|
||
#[allow(clippy::unused_self)] | ||
pub fn transform_directive(&mut self, directive: &mut Directive) { | ||
if let Some(value) = Self::normalize_str(directive.directive.as_str()) { | ||
directive.directive = value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod json_strings; | ||
mod optional_catch_binding; | ||
|
||
pub use json_strings::JsonStrings; | ||
pub use optional_catch_binding::OptionalCatchBinding; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters