|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use oxc_allocator::Allocator; |
| 4 | +use oxc_ast::{ast::*, visit::walk_mut, AstBuilder, VisitMut}; |
| 5 | +use oxc_diagnostics::OxcDiagnostic; |
| 6 | +use oxc_parser::Parser; |
| 7 | +use oxc_span::SourceType; |
| 8 | +use oxc_syntax::identifier::is_identifier_name; |
| 9 | + |
| 10 | +/// Configuration for [ReplaceGlobalDefines]. |
| 11 | +/// |
| 12 | +/// Due to the usage of an arena allocator, the constructor will parse once for grammatical errors, |
| 13 | +/// and does not save the constructed expression. |
| 14 | +/// |
| 15 | +/// The data is stored in an `Arc` so this can be shared across threads. |
| 16 | +#[derive(Debug, Clone)] |
| 17 | +pub struct ReplaceGlobalDefinesConfig(Arc<ReplaceGlobalDefinesConfigImpl>); |
| 18 | + |
| 19 | +#[derive(Debug)] |
| 20 | +struct ReplaceGlobalDefinesConfigImpl { |
| 21 | + identifier_defines: Vec<(/* key */ String, /* value */ String)>, |
| 22 | + // TODO: dot defines |
| 23 | +} |
| 24 | + |
| 25 | +impl ReplaceGlobalDefinesConfig { |
| 26 | + /// # Errors |
| 27 | + /// |
| 28 | + /// * key is not an identifier |
| 29 | + /// * value has a syntax error |
| 30 | + pub fn new<S: AsRef<str>>(defines: &[(S, S)]) -> Result<Self, Vec<OxcDiagnostic>> { |
| 31 | + let allocator = Allocator::default(); |
| 32 | + let mut identifier_defines = vec![]; |
| 33 | + for (key, value) in defines { |
| 34 | + let key = key.as_ref(); |
| 35 | + let value = value.as_ref(); |
| 36 | + Self::check_key(key)?; |
| 37 | + Self::check_value(&allocator, value)?; |
| 38 | + identifier_defines.push((key.to_string(), value.to_string())); |
| 39 | + } |
| 40 | + Ok(Self(Arc::new(ReplaceGlobalDefinesConfigImpl { identifier_defines }))) |
| 41 | + } |
| 42 | + |
| 43 | + fn check_key(key: &str) -> Result<(), Vec<OxcDiagnostic>> { |
| 44 | + if !is_identifier_name(key) { |
| 45 | + return Err(vec![OxcDiagnostic::error(format!("`{key}` is not an identifier."))]); |
| 46 | + } |
| 47 | + Ok(()) |
| 48 | + } |
| 49 | + |
| 50 | + fn check_value(allocator: &Allocator, source_text: &str) -> Result<(), Vec<OxcDiagnostic>> { |
| 51 | + Parser::new(allocator, source_text, SourceType::default()).parse_expression()?; |
| 52 | + Ok(()) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/// Replace Global Defines. |
| 57 | +/// |
| 58 | +/// References: |
| 59 | +/// |
| 60 | +/// * <https://esbuild.github.io/api/#define> |
| 61 | +/// * <https://github.com/terser/terser?tab=readme-ov-file#conditional-compilation> |
| 62 | +pub struct ReplaceGlobalDefines<'a> { |
| 63 | + ast: AstBuilder<'a>, |
| 64 | + config: ReplaceGlobalDefinesConfig, |
| 65 | +} |
| 66 | + |
| 67 | +impl<'a> ReplaceGlobalDefines<'a> { |
| 68 | + pub fn new(allocator: &'a Allocator, config: ReplaceGlobalDefinesConfig) -> Self { |
| 69 | + Self { ast: AstBuilder::new(allocator), config } |
| 70 | + } |
| 71 | + |
| 72 | + pub fn build(&mut self, program: &mut Program<'a>) { |
| 73 | + self.visit_program(program); |
| 74 | + } |
| 75 | + |
| 76 | + // Construct a new expression because we don't have ast clone right now. |
| 77 | + fn parse_value(&self, source_text: &str) -> Expression<'a> { |
| 78 | + // Allocate the string lazily because replacement happens rarely. |
| 79 | + let source_text = self.ast.allocator.alloc(source_text.to_string()); |
| 80 | + // Unwrapping here, it should already be checked by [ReplaceGlobalDefinesConfig::new]. |
| 81 | + Parser::new(self.ast.allocator, source_text, SourceType::default()) |
| 82 | + .parse_expression() |
| 83 | + .unwrap() |
| 84 | + } |
| 85 | + |
| 86 | + fn replace_identifier_defines(&self, expr: &mut Expression<'a>) { |
| 87 | + for (key, value) in &self.config.0.identifier_defines { |
| 88 | + if let Expression::Identifier(ident) = expr { |
| 89 | + if ident.name.as_str() == key { |
| 90 | + let value = self.parse_value(value); |
| 91 | + *expr = value; |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl<'a> VisitMut<'a> for ReplaceGlobalDefines<'a> { |
| 100 | + fn visit_expression(&mut self, expr: &mut Expression<'a>) { |
| 101 | + self.replace_identifier_defines(expr); |
| 102 | + walk_mut::walk_expression_mut(self, expr); |
| 103 | + } |
| 104 | +} |
0 commit comments