mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 04:42:10 +00:00
feat(transform): sticky-regex (#968)
This commit is contained in:
parent
61bc142d95
commit
09df8e609a
5 changed files with 55 additions and 1 deletions
|
|
@ -1,3 +1,5 @@
|
|||
mod shorthand_properties;
|
||||
mod sticky_regex;
|
||||
|
||||
pub use shorthand_properties::ShorthandProperties;
|
||||
pub use sticky_regex::StickyRegex;
|
||||
|
|
|
|||
43
crates/oxc_transformer/src/es2015/sticky_regex.rs
Normal file
43
crates/oxc_transformer/src/es2015/sticky_regex.rs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
use oxc_ast::{ast::*, AstBuilder};
|
||||
use oxc_span::{Atom, Span};
|
||||
|
||||
use std::rc::Rc;
|
||||
|
||||
/// ES2015: Sticky Regex
|
||||
///
|
||||
/// References:
|
||||
/// * <https://babel.dev/docs/babel-plugin-transform-sticky-regex>
|
||||
/// * <https://github.com/babel/babel/blob/main/packages/babel-plugin-transform-sticky-regex>
|
||||
pub struct StickyRegex<'a> {
|
||||
ast: Rc<AstBuilder<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> StickyRegex<'a> {
|
||||
pub fn new(ast: Rc<AstBuilder<'a>>) -> Self {
|
||||
Self { ast }
|
||||
}
|
||||
|
||||
pub fn transform_expression<'b>(&mut self, expr: &'b mut Expression<'a>) {
|
||||
let Expression::RegExpLiteral(reg_literal) = expr else { return };
|
||||
if !reg_literal.regex.flags.contains(RegExpFlags::Y) {
|
||||
return;
|
||||
}
|
||||
|
||||
let ident = IdentifierReference::new(Span::default(), Atom::from("RegExp"));
|
||||
let callee = self.ast.identifier_expression(ident);
|
||||
let pattern_literal = self
|
||||
.ast
|
||||
.string_literal(Span::default(), Atom::from(reg_literal.regex.pattern.as_str()));
|
||||
let flags_literal = self
|
||||
.ast
|
||||
.string_literal(Span::default(), Atom::from(reg_literal.regex.flags.to_string()));
|
||||
let pattern_literal = self.ast.literal_string_expression(pattern_literal);
|
||||
let flags_literal = self.ast.literal_string_expression(flags_literal);
|
||||
|
||||
let mut arguments = self.ast.new_vec_with_capacity(2);
|
||||
arguments.push(Argument::Expression(pattern_literal));
|
||||
arguments.push(Argument::Expression(flags_literal));
|
||||
|
||||
*expr = self.ast.new_expression(Span::default(), callee, arguments, None);
|
||||
}
|
||||
}
|
||||
|
|
@ -46,6 +46,7 @@ pub struct Transformer<'a> {
|
|||
es2016_exponentiation_operator: Option<ExponentiationOperator<'a>>,
|
||||
// es2015
|
||||
es2015_shorthand_properties: Option<ShorthandProperties<'a>>,
|
||||
es2015_sticky_regex: Option<es2015::StickyRegex<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> Transformer<'a> {
|
||||
|
|
@ -78,6 +79,7 @@ impl<'a> Transformer<'a> {
|
|||
}
|
||||
if options.target < TransformTarget::ES2015 {
|
||||
t.es2015_shorthand_properties.replace(ShorthandProperties::new(Rc::clone(&ast)));
|
||||
t.es2015_sticky_regex.replace(es2015::StickyRegex::new(Rc::clone(&ast)));
|
||||
}
|
||||
t
|
||||
}
|
||||
|
|
@ -93,6 +95,7 @@ impl<'a, 'b> VisitMut<'a, 'b> for Transformer<'a> {
|
|||
// self.react_jsx.as_mut().map(|t| t.transform_expression(expr));
|
||||
self.es2021_logical_assignment_operators.as_mut().map(|t| t.transform_expression(expr));
|
||||
self.es2016_exponentiation_operator.as_mut().map(|t| t.transform_expression(expr));
|
||||
self.es2015_sticky_regex.as_mut().map(|t| t.transform_expression(expr));
|
||||
|
||||
self.visit_expression_match(expr);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
Passed: 95/1078
|
||||
Passed: 97/1080
|
||||
|
||||
# babel-plugin-transform-class-properties
|
||||
* Failed: assumption-constantSuper/complex-super-class/input.js
|
||||
|
|
@ -759,6 +759,11 @@ Passed: 95/1078
|
|||
* Passed: shorthand-properties/shorthand-multiple/input.js
|
||||
* Passed: shorthand-properties/shorthand-single/input.js
|
||||
|
||||
# babel-plugin-transform-sticky-regex
|
||||
[All passed]
|
||||
* Passed: sticky-regex/basic/input.js
|
||||
* Passed: sticky-regex/ignore-non-sticky/input.js
|
||||
|
||||
# babel-plugin-transform-typescript
|
||||
* Failed: class/abstract-class-decorated/input.ts
|
||||
* Failed: class/abstract-class-decorated-method/input.ts
|
||||
|
|
|
|||
|
|
@ -56,6 +56,7 @@ pub fn babel(options: &BabelOptions) {
|
|||
"babel-plugin-transform-exponentiation-operator",
|
||||
// ES2015
|
||||
"babel-plugin-transform-shorthand-properties",
|
||||
"babel-plugin-transform-sticky-regex",
|
||||
// TypeScript
|
||||
"babel-plugin-transform-typescript",
|
||||
// React
|
||||
|
|
|
|||
Loading…
Reference in a new issue