|
| 1 | +#![allow(dead_code, unused_variables)] |
| 2 | + |
| 3 | +//! Store for bindings for class. |
| 4 | +//! |
| 5 | +//! This module manages identifier bindings for class transformations, |
| 6 | +//! handling both the class name and temporary variables needed during |
| 7 | +//! the transformation of class properties and private fields. |
| 8 | +
|
| 9 | +use swc_atoms::Atom; |
| 10 | +use swc_common::SyntaxContext; |
| 11 | + |
| 12 | +use super::utils::BoundIdentifier; |
| 13 | +use crate::compat::context::TransformCtx; |
| 14 | + |
| 15 | +/// Store for bindings for class. |
| 16 | +/// |
| 17 | +/// Manages three types of bindings: |
| 18 | +/// |
| 19 | +/// 1. Existing binding for class name (if class has a name). |
| 20 | +/// 2. Temp var `_Class`, which may or may not be required. |
| 21 | +/// 3. Brand variable for WeakSet (used for private methods). |
| 22 | +/// |
| 23 | +/// ## When is a temp var required? |
| 24 | +/// |
| 25 | +/// The temp var is required in the following circumstances: |
| 26 | +/// |
| 27 | +/// * Class expression has static properties. e.g., `C = class { static x = 1; |
| 28 | +/// }` |
| 29 | +/// * Class declaration has static properties and one of the static prop's |
| 30 | +/// initializers contains: a. `this` e.g., `class C { static x = this; }` b. |
| 31 | +/// Reference to class name e.g., `class C { static x = C; }` c. A private |
| 32 | +/// field referring to one of the class's static private props. e.g., `class C |
| 33 | +/// { static #x; static y = obj.#x; }` |
| 34 | +/// |
| 35 | +/// ## Private field binding usage |
| 36 | +/// |
| 37 | +/// The logic for when transpiled private fields use a reference to class name |
| 38 | +/// or class temp var is unfortunately rather complicated. |
| 39 | +/// |
| 40 | +/// Transpiled private fields referring to a static private prop use: |
| 41 | +/// |
| 42 | +/// * Class name when field is within body of class declaration e.g., `class C { |
| 43 | +/// static #x; method() { return obj.#x; } }` -> `_assertClassBrand(C, obj, |
| 44 | +/// _x)._` |
| 45 | +/// * Temp var when field is within body of class expression e.g., `C = class C |
| 46 | +/// { static #x; method() { return obj.#x; } }` -> `_assertClassBrand(_C, obj, |
| 47 | +/// _x)._` |
| 48 | +/// * Temp var when field is within a static prop initializer e.g., `class C { |
| 49 | +/// static #x; static y = obj.#x; }` -> `_assertClassBrand(_C, obj, _x)._` |
| 50 | +/// |
| 51 | +/// `static_private_fields_use_temp` is updated as transform moves through the |
| 52 | +/// class, to indicate which binding to use. |
| 53 | +pub struct ClassBindings { |
| 54 | + /// Binding for class name, if class has name |
| 55 | + pub name: Option<BoundIdentifier>, |
| 56 | + /// Temp var for class. |
| 57 | + /// e.g., `_Class` in `_Class = class {}, _Class.x = 1, _Class` |
| 58 | + pub temp: Option<BoundIdentifier>, |
| 59 | + /// Temp var for WeakSet (used for private method brands). |
| 60 | + pub brand: Option<BoundIdentifier>, |
| 61 | + /// `true` if should use temp binding for references to class in transpiled |
| 62 | + /// static private fields, `false` if can use name binding |
| 63 | + pub static_private_fields_use_temp: bool, |
| 64 | + /// `true` if temp var for class has been inserted |
| 65 | + pub temp_var_is_created: bool, |
| 66 | + /// Counter for generating unique identifiers |
| 67 | + uid_counter: usize, |
| 68 | +} |
| 69 | + |
| 70 | +impl ClassBindings { |
| 71 | + /// Create new `ClassBindings`. |
| 72 | + /// |
| 73 | + /// # Arguments |
| 74 | + /// * `name_binding` - Optional binding for the class name |
| 75 | + /// * `temp_binding` - Optional temp binding for the class |
| 76 | + /// * `brand_binding` - Optional brand binding for private methods |
| 77 | + /// * `static_private_fields_use_temp` - Whether to use temp binding for |
| 78 | + /// static private fields |
| 79 | + /// * `temp_var_is_created` - Whether the temp var has been created |
| 80 | + pub fn new( |
| 81 | + name_binding: Option<BoundIdentifier>, |
| 82 | + temp_binding: Option<BoundIdentifier>, |
| 83 | + brand_binding: Option<BoundIdentifier>, |
| 84 | + static_private_fields_use_temp: bool, |
| 85 | + temp_var_is_created: bool, |
| 86 | + ) -> Self { |
| 87 | + Self { |
| 88 | + name: name_binding, |
| 89 | + temp: temp_binding, |
| 90 | + brand: brand_binding, |
| 91 | + static_private_fields_use_temp, |
| 92 | + temp_var_is_created, |
| 93 | + uid_counter: 0, |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /// Create dummy `ClassBindings`. |
| 98 | + /// |
| 99 | + /// Used when class needs no transform, and for dummy entry at top of |
| 100 | + /// `ClassesStack`. |
| 101 | + pub fn dummy() -> Self { |
| 102 | + Self::new(None, None, None, false, false) |
| 103 | + } |
| 104 | + |
| 105 | + /// Get the name binding if it exists. |
| 106 | + pub fn name(&self) -> Option<&BoundIdentifier> { |
| 107 | + self.name.as_ref() |
| 108 | + } |
| 109 | + |
| 110 | + /// Get [`BoundIdentifier`] for class brand. |
| 111 | + /// |
| 112 | + /// Only use this method when you are sure that `brand` is not `None`, |
| 113 | + /// which happens when there is a private method in the class. |
| 114 | + /// |
| 115 | + /// # Panics |
| 116 | + /// Panics if `brand` is `None`. |
| 117 | + pub fn brand(&self) -> &BoundIdentifier { |
| 118 | + self.brand |
| 119 | + .as_ref() |
| 120 | + .expect("Brand binding should exist when private methods are present") |
| 121 | + } |
| 122 | + |
| 123 | + /// Get binding to use for referring to class in transpiled static private |
| 124 | + /// fields. |
| 125 | + /// |
| 126 | + /// Returns either: |
| 127 | + /// - Class name binding (e.g., `Class` in `_assertClassBrand(Class, object, |
| 128 | + /// _prop)._`) |
| 129 | + /// - Temp var binding (e.g., `_Class` in `_assertClassBrand(_Class, object, |
| 130 | + /// _prop)._`) |
| 131 | + /// |
| 132 | + /// The choice depends on `static_private_fields_use_temp`: |
| 133 | + /// * In class expressions, this is always temp binding. |
| 134 | + /// * In class declarations, it's the name binding when code is inside class |
| 135 | + /// body, and temp binding when code is outside class body. |
| 136 | + /// |
| 137 | + /// If a temp binding is required and one doesn't already exist, a temp |
| 138 | + /// binding is created. |
| 139 | + /// |
| 140 | + /// # Arguments |
| 141 | + /// * `ctx` - Transform context for generating unique identifiers |
| 142 | + /// |
| 143 | + /// # Returns |
| 144 | + /// Reference to the binding to use |
| 145 | + pub fn get_or_init_static_binding(&mut self, ctx: &TransformCtx) -> &BoundIdentifier { |
| 146 | + if self.static_private_fields_use_temp { |
| 147 | + // Create temp binding if it doesn't already exist |
| 148 | + if self.temp.is_none() { |
| 149 | + self.temp = Some(Self::create_temp_binding( |
| 150 | + self.name.as_ref(), |
| 151 | + &mut self.uid_counter, |
| 152 | + ctx, |
| 153 | + )); |
| 154 | + } |
| 155 | + self.temp.as_ref().unwrap() |
| 156 | + } else { |
| 157 | + // `static_private_fields_use_temp` is always `true` for class expressions. |
| 158 | + // Class declarations always have a name binding if they have any static props. |
| 159 | + // So `unwrap` here cannot panic. |
| 160 | + self.name |
| 161 | + .as_ref() |
| 162 | + .expect("Name binding should exist for class declarations with static properties") |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + /// Generate binding for temp var. |
| 167 | + /// |
| 168 | + /// # Arguments |
| 169 | + /// * `name_binding` - Optional name binding to base the temp name on |
| 170 | + /// * `uid_counter` - Counter for generating unique identifiers |
| 171 | + /// * `ctx` - Transform context |
| 172 | + /// |
| 173 | + /// # Returns |
| 174 | + /// A new bound identifier for the temp var |
| 175 | + pub fn create_temp_binding( |
| 176 | + name_binding: Option<&BoundIdentifier>, |
| 177 | + uid_counter: &mut usize, |
| 178 | + _ctx: &TransformCtx, |
| 179 | + ) -> BoundIdentifier { |
| 180 | + // Base temp binding name on class name, or "Class" if no name. |
| 181 | + // TODO(improve-on-babel): If class name var isn't mutated, no need for temp var |
| 182 | + // for class declaration. Can just use class binding. |
| 183 | + let base_name = name_binding |
| 184 | + .map(|binding| binding.name.as_ref()) |
| 185 | + .unwrap_or("Class"); |
| 186 | + |
| 187 | + *uid_counter += 1; |
| 188 | + let name = if *uid_counter > 1 { |
| 189 | + Atom::from(format!("_{}{}", base_name, uid_counter)) |
| 190 | + } else { |
| 191 | + Atom::from(format!("_{}", base_name)) |
| 192 | + }; |
| 193 | + |
| 194 | + BoundIdentifier::new(name, SyntaxContext::empty()) |
| 195 | + } |
| 196 | + |
| 197 | + /// Create a brand binding for private methods. |
| 198 | + /// |
| 199 | + /// # Arguments |
| 200 | + /// * `uid_counter` - Counter for generating unique identifiers |
| 201 | + /// * `ctx` - Transform context |
| 202 | + /// |
| 203 | + /// # Returns |
| 204 | + /// A new bound identifier for the brand |
| 205 | + pub fn create_brand_binding(uid_counter: &mut usize, _ctx: &TransformCtx) -> BoundIdentifier { |
| 206 | + *uid_counter += 1; |
| 207 | + let name = if *uid_counter > 1 { |
| 208 | + Atom::from(format!("_brand{}", uid_counter)) |
| 209 | + } else { |
| 210 | + "_brand".into() |
| 211 | + }; |
| 212 | + |
| 213 | + BoundIdentifier::new(name, SyntaxContext::empty()) |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +#[cfg(test)] |
| 218 | +mod tests { |
| 219 | + use std::path::Path; |
| 220 | + |
| 221 | + use super::*; |
| 222 | + |
| 223 | + fn create_test_ctx() -> TransformCtx { |
| 224 | + TransformCtx::from_config( |
| 225 | + Path::new("test.js"), |
| 226 | + &crate::Config { |
| 227 | + assumptions: Default::default(), |
| 228 | + ..Default::default() |
| 229 | + }, |
| 230 | + ) |
| 231 | + } |
| 232 | + |
| 233 | + #[test] |
| 234 | + fn test_class_bindings_new() { |
| 235 | + let name = BoundIdentifier::new("MyClass".into(), SyntaxContext::empty()); |
| 236 | + let bindings = ClassBindings::new(Some(name), None, None, false, false); |
| 237 | + |
| 238 | + assert!(bindings.name.is_some()); |
| 239 | + assert!(bindings.temp.is_none()); |
| 240 | + assert!(bindings.brand.is_none()); |
| 241 | + assert!(!bindings.static_private_fields_use_temp); |
| 242 | + assert!(!bindings.temp_var_is_created); |
| 243 | + } |
| 244 | + |
| 245 | + #[test] |
| 246 | + fn test_class_bindings_dummy() { |
| 247 | + let bindings = ClassBindings::dummy(); |
| 248 | + |
| 249 | + assert!(bindings.name.is_none()); |
| 250 | + assert!(bindings.temp.is_none()); |
| 251 | + assert!(bindings.brand.is_none()); |
| 252 | + assert!(!bindings.static_private_fields_use_temp); |
| 253 | + assert!(!bindings.temp_var_is_created); |
| 254 | + } |
| 255 | + |
| 256 | + #[test] |
| 257 | + fn test_get_or_init_static_binding_with_name() { |
| 258 | + let name = BoundIdentifier::new("MyClass".into(), SyntaxContext::empty()); |
| 259 | + let mut bindings = ClassBindings::new(Some(name), None, None, false, false); |
| 260 | + let ctx = create_test_ctx(); |
| 261 | + |
| 262 | + let binding = bindings.get_or_init_static_binding(&ctx); |
| 263 | + assert_eq!(binding.name.as_ref(), "MyClass"); |
| 264 | + } |
| 265 | + |
| 266 | + #[test] |
| 267 | + fn test_get_or_init_static_binding_creates_temp() { |
| 268 | + let name = BoundIdentifier::new("MyClass".into(), SyntaxContext::empty()); |
| 269 | + let mut bindings = ClassBindings::new(Some(name), None, None, true, false); |
| 270 | + let ctx = create_test_ctx(); |
| 271 | + |
| 272 | + let binding = bindings.get_or_init_static_binding(&ctx); |
| 273 | + // Should create temp binding |
| 274 | + assert!(binding.name.as_ref().starts_with("_MyClass")); |
| 275 | + } |
| 276 | + |
| 277 | + #[test] |
| 278 | + fn test_create_temp_binding_with_name() { |
| 279 | + let name = BoundIdentifier::new("MyClass".into(), SyntaxContext::empty()); |
| 280 | + let ctx = create_test_ctx(); |
| 281 | + let mut counter = 0; |
| 282 | + |
| 283 | + let temp = ClassBindings::create_temp_binding(Some(&name), &mut counter, &ctx); |
| 284 | + assert_eq!(temp.name.as_ref(), "_MyClass"); |
| 285 | + assert_eq!(counter, 1); |
| 286 | + } |
| 287 | + |
| 288 | + #[test] |
| 289 | + fn test_create_temp_binding_without_name() { |
| 290 | + let ctx = create_test_ctx(); |
| 291 | + let mut counter = 0; |
| 292 | + |
| 293 | + let temp = ClassBindings::create_temp_binding(None, &mut counter, &ctx); |
| 294 | + assert_eq!(temp.name.as_ref(), "_Class"); |
| 295 | + assert_eq!(counter, 1); |
| 296 | + } |
| 297 | + |
| 298 | + #[test] |
| 299 | + fn test_create_temp_binding_increments_counter() { |
| 300 | + let name = BoundIdentifier::new("Foo".into(), SyntaxContext::empty()); |
| 301 | + let ctx = create_test_ctx(); |
| 302 | + let mut counter = 0; |
| 303 | + |
| 304 | + let temp1 = ClassBindings::create_temp_binding(Some(&name), &mut counter, &ctx); |
| 305 | + let temp2 = ClassBindings::create_temp_binding(Some(&name), &mut counter, &ctx); |
| 306 | + |
| 307 | + assert_eq!(temp1.name.as_ref(), "_Foo"); |
| 308 | + assert_eq!(temp2.name.as_ref(), "_Foo2"); |
| 309 | + assert_eq!(counter, 2); |
| 310 | + } |
| 311 | + |
| 312 | + #[test] |
| 313 | + fn test_create_brand_binding() { |
| 314 | + let ctx = create_test_ctx(); |
| 315 | + let mut counter = 0; |
| 316 | + |
| 317 | + let brand = ClassBindings::create_brand_binding(&mut counter, &ctx); |
| 318 | + assert_eq!(brand.name.as_ref(), "_brand"); |
| 319 | + assert_eq!(counter, 1); |
| 320 | + } |
| 321 | + |
| 322 | + #[test] |
| 323 | + #[should_panic(expected = "Brand binding should exist")] |
| 324 | + fn test_brand_panics_when_none() { |
| 325 | + let bindings = ClassBindings::dummy(); |
| 326 | + let _ = bindings.brand(); |
| 327 | + } |
| 328 | +} |
0 commit comments