Skip to content

Commit f792e32

Browse files
committed
fmt
1 parent 321441a commit f792e32

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+4565
-2607
lines changed

crates/swc_ecma_compiler/src/common/arrow_function_converter.rs

Lines changed: 182 additions & 97 deletions
Large diffs are not rendered by default.

crates/swc_ecma_compiler/src/common/computed_key.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ use crate::{
1111
impl<'a> TransformCtx<'a> {
1212
/// Check if temp var is required for `key`.
1313
///
14-
/// `this` does not have side effects, but in this context, it needs a temp var anyway, because `this`
15-
/// in computed key and `this` within class constructor resolve to different `this` bindings.
16-
/// So we need to create a temp var outside of the class to get the correct `this`.
14+
/// `this` does not have side effects, but in this context, it needs a temp
15+
/// var anyway, because `this` in computed key and `this` within class
16+
/// constructor resolve to different `this` bindings. So we need to
17+
/// create a temp var outside of the class to get the correct `this`.
1718
/// `class C { [this] = 1; }`
18-
/// -> `let _this; _this = this; class C { constructor() { this[_this] = 1; } }`
19+
/// -> `let _this; _this = this; class C { constructor() { this[_this] = 1;
20+
/// } }`
1921
//
2022
// TODO(improve-on-babel): Can avoid the temp var if key is for a static prop/method,
2123
// as in that case the usage of `this` stays outside the class.
@@ -31,23 +33,28 @@ impl<'a> TransformCtx<'a> {
3133
| Expression::RegExpLiteral(_)
3234
| Expression::StringLiteral(_) => false,
3335
// Template literal cannot have side effects if it has no expressions.
34-
// If it *does* have expressions, but they're all literals, then also cannot have side effects,
35-
// but don't bother checking for that as it shouldn't occur in real world code.
36-
// Why would you write "`x${9}z`" when you can just write "`x9z`"?
37-
// Note: "`x${foo}`" *can* have side effects if `foo` is an object with a `toString` method.
36+
// If it *does* have expressions, but they're all literals, then also cannot have side
37+
// effects, but don't bother checking for that as it shouldn't occur in real
38+
// world code. Why would you write "`x${9}z`" when you can just write
39+
// "`x9z`"? Note: "`x${foo}`" *can* have side effects if `foo` is an object
40+
// with a `toString` method.
3841
Expression::TemplateLiteral(lit) => !lit.expressions.is_empty(),
3942
// `IdentifierReference`s can have side effects if is unbound.
4043
//
4144
// If var is mutated, it also needs a temp var, because of cases like
4245
// `let x = 1; class { [x] = 1; [++x] = 2; }`
43-
// `++x` is hoisted to before class in output, so `x` in 1st key would get the wrong value
44-
// unless it's hoisted out too.
46+
// `++x` is hoisted to before class in output, so `x` in 1st key would get the wrong
47+
// value unless it's hoisted out too.
4548
//
4649
// TODO: Add an exec test for this odd case.
4750
// TODO(improve-on-babel): That case is rare.
4851
// Test for it in first pass over class elements, and avoid temp vars where possible.
4952
Expression::Identifier(ident) => {
50-
match ctx.scoping().get_reference(ident.reference_id()).symbol_id() {
53+
match ctx
54+
.scoping()
55+
.get_reference(ident.reference_id())
56+
.symbol_id()
57+
{
5158
Some(symbol_id) => ctx.scoping().symbol_is_mutated(symbol_id),
5259
None => true,
5360
}
@@ -60,12 +67,16 @@ impl<'a> TransformCtx<'a> {
6067
}
6168

6269
/// Create `let _x;` statement and insert it.
63-
/// Return `_x = x()` assignment, and `_x` identifier referencing same temp var.
70+
/// Return `_x = x()` assignment, and `_x` identifier referencing same temp
71+
/// var.
6472
pub fn create_computed_key_temp_var(
6573
&self,
6674
key: Expression<'a>,
6775
ctx: &mut TraverseCtx<'a>,
68-
) -> (/* assignment */ Expression<'a>, /* identifier */ Expression<'a>) {
76+
) -> (
77+
/* assignment */ Expression<'a>,
78+
/* identifier */ Expression<'a>,
79+
) {
6980
let outer_scope_id = ctx.current_block_scope_id();
7081
// TODO: Handle if is a class expression defined in a function's params.
7182
let binding =

crates/swc_ecma_compiler/src/common/duplicate.rs

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ use crate::context::{TransformCtx, TraverseCtx};
1616
impl<'a> TransformCtx<'a> {
1717
/// Duplicate expression to be used twice.
1818
///
19-
/// If `expr` may have side effects, create a temp var `_expr` and assign to it.
19+
/// If `expr` may have side effects, create a temp var `_expr` and assign to
20+
/// it.
2021
///
2122
/// * `this` -> `this`, `this`
2223
/// * Bound identifier `foo` -> `foo`, `foo`
2324
/// * Unbound identifier `foo` -> `_foo = foo`, `_foo`
2425
/// * Anything else `foo()` -> `_foo = foo()`, `_foo`
2526
///
26-
/// If `mutated_symbol_needs_temp_var` is `true`, temp var will be created for a bound identifier,
27-
/// if it's mutated (assigned to) anywhere in AST.
27+
/// If `mutated_symbol_needs_temp_var` is `true`, temp var will be created
28+
/// for a bound identifier, if it's mutated (assigned to) anywhere in
29+
/// AST.
2830
///
2931
/// Returns 2 `Expression`s. The first may be an `AssignmentExpression`,
3032
/// and must be inserted into output first.
@@ -42,15 +44,17 @@ impl<'a> TransformCtx<'a> {
4244

4345
/// Duplicate expression to be used 3 times.
4446
///
45-
/// If `expr` may have side effects, create a temp var `_expr` and assign to it.
47+
/// If `expr` may have side effects, create a temp var `_expr` and assign to
48+
/// it.
4649
///
4750
/// * `this` -> `this`, `this`, `this`
4851
/// * Bound identifier `foo` -> `foo`, `foo`, `foo`
4952
/// * Unbound identifier `foo` -> `_foo = foo`, `_foo`, `_foo`
5053
/// * Anything else `foo()` -> `_foo = foo()`, `_foo`, `_foo`
5154
///
52-
/// If `mutated_symbol_needs_temp_var` is `true`, temp var will be created for a bound identifier,
53-
/// if it's mutated (assigned to) anywhere in AST.
55+
/// If `mutated_symbol_needs_temp_var` is `true`, temp var will be created
56+
/// for a bound identifier, if it's mutated (assigned to) anywhere in
57+
/// AST.
5458
///
5559
/// Returns 3 `Expression`s. The first may be an `AssignmentExpression`,
5660
/// and must be inserted into output first.
@@ -68,18 +72,20 @@ impl<'a> TransformCtx<'a> {
6872

6973
/// Duplicate expression `N + 1` times.
7074
///
71-
/// If `expr` may have side effects, create a temp var `_expr` and assign to it.
75+
/// If `expr` may have side effects, create a temp var `_expr` and assign to
76+
/// it.
7277
///
7378
/// * `this` -> `this`, [`this`; N]
7479
/// * Bound identifier `foo` -> `foo`, [`foo`; N]
7580
/// * Unbound identifier `foo` -> `_foo = foo`, [`_foo`; N]
7681
/// * Anything else `foo()` -> `_foo = foo()`, [`_foo`; N]
7782
///
78-
/// If `mutated_symbol_needs_temp_var` is `true`, temp var will be created for a bound identifier,
79-
/// if it's mutated (assigned to) anywhere in AST.
83+
/// If `mutated_symbol_needs_temp_var` is `true`, temp var will be created
84+
/// for a bound identifier, if it's mutated (assigned to) anywhere in
85+
/// AST.
8086
///
81-
/// Returns `N + 1` x `Expression`s. The first may be an `AssignmentExpression`,
82-
/// and must be inserted into output first.
87+
/// Returns `N + 1` x `Expression`s. The first may be an
88+
/// `AssignmentExpression`, and must be inserted into output first.
8389
pub(crate) fn duplicate_expression_multiple<const N: usize>(
8490
&self,
8591
expr: Expression<'a>,
@@ -102,7 +108,8 @@ impl<'a> TransformCtx<'a> {
102108
return (expr, references);
103109
}
104110

105-
// Previously `x += 1` (`x` read + write), but moving to `_x = x` (`x` read only)
111+
// Previously `x += 1` (`x` read + write), but moving to `_x = x` (`x` read
112+
// only)
106113
let reference = ctx.scoping_mut().get_reference_mut(reference_id);
107114
*reference.flags_mut() = ReferenceFlags::Read;
108115

@@ -121,10 +128,11 @@ impl<'a> TransformCtx<'a> {
121128
return (expr, references);
122129
}
123130
// Template literal cannot have side effects if it has no expressions.
124-
// If it *does* have expressions, but they're all literals, then also cannot have side effects,
125-
// but don't bother checking for that as it shouldn't occur in real world code.
126-
// Why would you write "`x${9}z`" when you can just write "`x9z`"?
127-
// Note: "`x${foo}`" *can* have side effects if `foo` is an object with a `toString` method.
131+
// If it *does* have expressions, but they're all literals, then also cannot have side
132+
// effects, but don't bother checking for that as it shouldn't occur in real
133+
// world code. Why would you write "`x${9}z`" when you can just write
134+
// "`x9z`"? Note: "`x${foo}`" *can* have side effects if `foo` is an object
135+
// with a `toString` method.
128136
Expression::TemplateLiteral(lit) if lit.expressions.is_empty() => {
129137
let references = create_array(|| {
130138
ctx.ast.expression_template_literal(
@@ -136,7 +144,9 @@ impl<'a> TransformCtx<'a> {
136144
return (expr, references);
137145
}
138146
// Anything else requires temp var
139-
_ => self.var_declarations.create_uid_var_based_on_node(&expr, ctx),
147+
_ => self
148+
.var_declarations
149+
.create_uid_var_based_on_node(&expr, ctx),
140150
};
141151

142152
let assignment = ctx.ast.expression_assignment(
@@ -152,21 +162,24 @@ impl<'a> TransformCtx<'a> {
152162
}
153163
}
154164

155-
/// Create array of length `N`, with each item initialized with provided function `init`.
165+
/// Create array of length `N`, with each item initialized with provided
166+
/// function `init`.
156167
///
157168
/// Implementation based on:
158169
/// * <https://github.com/rust-lang/rust/issues/62875#issuecomment-513834029>
159170
/// * <https://github.com/rust-lang/rust/issues/61956>
160171
//
161-
// `#[inline]` so compiler can inline `init()`, and it may unroll the loop if `init` is simple enough.
172+
// `#[inline]` so compiler can inline `init()`, and it may unroll the loop if `init` is simple
173+
// enough.
162174
#[inline]
163175
fn create_array<const N: usize, T, I: FnMut() -> T>(mut init: I) -> [T; N] {
164176
let mut array: [MaybeUninit<T>; N] = [const { MaybeUninit::uninit() }; N];
165177
for elem in &mut array {
166178
elem.write(init());
167179
}
168-
// Wrapping in `ManuallyDrop` should not be necessary because `MaybeUninit` does not impl `Drop`,
169-
// but do it anyway just to make sure, as it's mentioned in issues above.
180+
// Wrapping in `ManuallyDrop` should not be necessary because `MaybeUninit` does
181+
// not impl `Drop`, but do it anyway just to make sure, as it's mentioned in
182+
// issues above.
170183
let mut array = ManuallyDrop::new(array);
171184
// SAFETY: All elements of array are initialized.
172185
// `[MaybeUninit<T>; N]` and `[T; N]` have same layout.

0 commit comments

Comments
 (0)