Skip to content

Commit da7ce27

Browse files
committed
refactor(transformer): use Option::get_or_insert_with
1 parent 8729755 commit da7ce27

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

crates/oxc_transformer/src/plugins/inject_global_variables.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,12 @@ impl<'a> InjectGlobalVariables<'a> {
236236
if ReplaceGlobalDefines::is_dot_define(ctx.symbols(), dot_define, member) {
237237
// If this is first replacement made for this dot define,
238238
// create `Atom` for replacement, and record in `replaced_dot_defines`
239-
if value_atom.is_none() {
240-
*value_atom = Some(self.ast.atom(dot_define.value.as_str()));
241-
239+
let value_atom = value_atom.get_or_insert_with(|| {
242240
self.replaced_dot_defines
243241
.push((dot_define.parts[0].clone(), dot_define.value.clone()));
244-
}
245-
let value_atom = value_atom.as_ref().unwrap().clone();
242+
self.ast.atom(dot_define.value.as_str())
243+
});
244+
let value_atom = value_atom.clone();
246245

247246
let value = self.ast.expression_identifier_reference(SPAN, value_atom);
248247
*expr = value;

crates/oxc_transformer/src/react/jsx_source.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,9 @@ impl<'a, 'ctx> Traverse<'a> for ReactJsxSource<'a, 'ctx> {
7777

7878
impl<'a, 'ctx> ReactJsxSource<'a, 'ctx> {
7979
pub fn get_line_column(&mut self, offset: u32) -> (usize, usize) {
80-
if self.source_rope.is_none() {
81-
self.source_rope = Some(Rope::from_str(self.ctx.source_text));
82-
}
83-
get_line_column(self.source_rope.as_ref().unwrap(), offset, self.ctx.source_text)
80+
let source_rope =
81+
self.source_rope.get_or_insert_with(|| Rope::from_str(self.ctx.source_text));
82+
get_line_column(source_rope, offset, self.ctx.source_text)
8483
}
8584

8685
pub fn get_object_property_kind_for_jsx_plugin(
@@ -221,11 +220,8 @@ impl<'a, 'ctx> ReactJsxSource<'a, 'ctx> {
221220
}
222221

223222
fn get_filename_var(&mut self, ctx: &mut TraverseCtx<'a>) -> &BoundIdentifier<'a> {
224-
if self.filename_var.is_none() {
225-
self.filename_var = Some(
226-
ctx.generate_uid_in_root_scope(FILE_NAME_VAR, SymbolFlags::FunctionScopedVariable),
227-
);
228-
}
229-
self.filename_var.as_ref().unwrap()
223+
self.filename_var.get_or_insert_with(|| {
224+
ctx.generate_uid_in_root_scope(FILE_NAME_VAR, SymbolFlags::FunctionScopedVariable)
225+
})
230226
}
231227
}

0 commit comments

Comments
 (0)