Skip to content

Commit 6983d39

Browse files
committed
feat(semantic): build semantic for .d.ts files
1 parent 94796a0 commit 6983d39

File tree

1 file changed

+39
-43
lines changed

1 file changed

+39
-43
lines changed

crates/oxc_semantic/src/builder.rs

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -214,51 +214,47 @@ impl<'a> SemanticBuilder<'a> {
214214
if self.build_jsdoc {
215215
self.jsdoc = JSDocBuilder::new(self.source_text, &program.comments);
216216
}
217-
if self.source_type.is_typescript_definition() {
218-
let scope_id = self.scoping.add_scope(None, NodeId::DUMMY, ScopeFlags::Top);
219-
program.scope_id.set(Some(scope_id));
217+
218+
// Use counts of nodes, scopes, symbols, and references to pre-allocate sufficient capacity
219+
// in `AstNodes`, `ScopeTree` and `SymbolTable`.
220+
//
221+
// This means that as we traverse the AST and fill up these structures with data,
222+
// they never need to grow and reallocate - which is an expensive operation as it
223+
// involves copying all the memory from the old allocation to the new one.
224+
// For large source files, these structures are very large, so growth is very costly
225+
// as it involves copying massive chunks of memory.
226+
// Avoiding this growth produces up to 30% perf boost on our benchmarks.
227+
//
228+
// If user did not provide existing `Stats`, calculate them by visiting AST.
229+
#[cfg_attr(not(debug_assertions), expect(unused_variables))]
230+
let (stats, check_stats) = if let Some(stats) = self.stats {
231+
(stats, None)
220232
} else {
221-
// Use counts of nodes, scopes, symbols, and references to pre-allocate sufficient capacity
222-
// in `AstNodes`, `ScopeTree` and `SymbolTable`.
223-
//
224-
// This means that as we traverse the AST and fill up these structures with data,
225-
// they never need to grow and reallocate - which is an expensive operation as it
226-
// involves copying all the memory from the old allocation to the new one.
227-
// For large source files, these structures are very large, so growth is very costly
228-
// as it involves copying massive chunks of memory.
229-
// Avoiding this growth produces up to 30% perf boost on our benchmarks.
230-
//
231-
// If user did not provide existing `Stats`, calculate them by visiting AST.
232-
#[cfg_attr(not(debug_assertions), expect(unused_variables))]
233-
let (stats, check_stats) = if let Some(stats) = self.stats {
234-
(stats, None)
235-
} else {
236-
let stats = Stats::count(program);
237-
let stats_with_excess = stats.increase_by(self.excess_capacity);
238-
(stats_with_excess, Some(stats))
239-
};
240-
self.nodes.reserve(stats.nodes as usize);
241-
self.scoping.reserve(
242-
stats.symbols as usize,
243-
stats.references as usize,
244-
stats.scopes as usize,
245-
);
233+
let stats = Stats::count(program);
234+
let stats_with_excess = stats.increase_by(self.excess_capacity);
235+
(stats_with_excess, Some(stats))
236+
};
237+
self.nodes.reserve(stats.nodes as usize);
238+
self.scoping.reserve(
239+
stats.symbols as usize,
240+
stats.references as usize,
241+
stats.scopes as usize,
242+
);
246243

247-
// Visit AST to generate scopes tree etc
248-
self.visit_program(program);
249-
250-
// Check that estimated counts accurately (unless in release mode)
251-
#[cfg(debug_assertions)]
252-
if let Some(stats) = check_stats {
253-
#[expect(clippy::cast_possible_truncation)]
254-
let actual_stats = Stats::new(
255-
self.nodes.len() as u32,
256-
self.scoping.scopes_len() as u32,
257-
self.scoping.symbols_len() as u32,
258-
self.scoping.references.len() as u32,
259-
);
260-
stats.assert_accurate(actual_stats);
261-
}
244+
// Visit AST to generate scopes tree etc
245+
self.visit_program(program);
246+
247+
// Check that estimated counts accurately (unless in release mode)
248+
#[cfg(debug_assertions)]
249+
if let Some(stats) = check_stats {
250+
#[expect(clippy::cast_possible_truncation)]
251+
let actual_stats = Stats::new(
252+
self.nodes.len() as u32,
253+
self.scoping.scopes_len() as u32,
254+
self.scoping.symbols_len() as u32,
255+
self.scoping.references.len() as u32,
256+
);
257+
stats.assert_accurate(actual_stats);
262258
}
263259

264260
let comments = self.alloc(&program.comments);

0 commit comments

Comments
 (0)