Skip to content

Commit 4dc10a9

Browse files
committed
refactor(semantic)!: AstNodes::program return &Program not Option<&Program>
1 parent e882a45 commit 4dc10a9

File tree

11 files changed

+13
-17
lines changed

11 files changed

+13
-17
lines changed

crates/oxc_linter/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ impl Linter {
248248
let external_linter = self.external_linter.as_ref().unwrap();
249249

250250
// Write offset of `Program` in metadata at end of buffer
251-
let program = semantic.nodes().program().unwrap();
251+
let program = semantic.nodes().program();
252252
let program_offset = ptr::from_ref(program) as u32;
253253

254254
let metadata = RawTransferMetadata::new(program_offset);

crates/oxc_linter/src/rules/eslint/sort_imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl Rule for SortImports {
134134
}
135135

136136
fn run_once(&self, ctx: &LintContext) {
137-
let program = ctx.nodes().program().unwrap();
137+
let program = ctx.nodes().program();
138138

139139
let mut import_declarations = vec![];
140140

crates/oxc_linter/src/rules/import/exports_last.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ declare_oxc_lint!(
5151
impl Rule for ExportsLast {
5252
fn run_once(&self, ctx: &LintContext<'_>) {
5353
// find last non export declaration index
54-
let program = ctx.nodes().program().unwrap();
54+
let program = ctx.nodes().program();
5555
let body = &program.body;
5656
let find_res =
5757
body.iter().rev().find_position(|statement| !is_exports_declaration(statement));

crates/oxc_linter/src/rules/import/first.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl Rule for First {
110110
let mut non_import_count = 0;
111111
let mut any_relative = false;
112112

113-
let program = ctx.nodes().program().unwrap();
113+
let program = ctx.nodes().program();
114114

115115
for statement in &program.body {
116116
match statement {

crates/oxc_linter/src/rules/nextjs/no_async_client_component.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ declare_oxc_lint!(
9090

9191
impl Rule for NoAsyncClientComponent {
9292
fn run_once(&self, ctx: &LintContext) {
93-
let program = ctx.nodes().program().unwrap();
93+
let program = ctx.nodes().program();
9494

9595
if program.directives.iter().any(|directive| directive.directive.as_str() == "use client") {
9696
for node in &program.body {

crates/oxc_linter/src/rules/typescript/consistent_type_imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ fn get_type_only_named_import<'a>(
576576
ctx: &LintContext<'a>,
577577
source: &str,
578578
) -> Option<&'a ImportDeclaration<'a>> {
579-
let program = ctx.nodes().program().unwrap();
579+
let program = ctx.nodes().program();
580580

581581
for stmt in &program.body {
582582
let Statement::ImportDeclaration(import_decl) = stmt else {

crates/oxc_linter/src/rules/typescript/triple_slash_reference.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl Rule for TripleSlashReference {
106106
}
107107

108108
fn run_once(&self, ctx: &LintContext) {
109-
let program = ctx.nodes().program().unwrap();
109+
let program = ctx.nodes().program();
110110

111111
// We don't need to iterate over all comments since Triple-slash directives are only valid at the top of their containing file.
112112
// We are trying to get the first statement start potioin, falling back to the program end if statement does not exist

crates/oxc_linter/src/rules/unicorn/no_empty_file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ declare_oxc_lint!(
4343

4444
impl Rule for NoEmptyFile {
4545
fn run_once(&self, ctx: &LintContext) {
46-
let program = ctx.nodes().program().unwrap();
46+
let program = ctx.nodes().program();
4747
if program.body.iter().any(|node| !is_empty_stmt(node)) {
4848
return;
4949
}

crates/oxc_linter/src/rules/unicorn/no_process_exit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl Rule for NoProcessExit {
6363
}
6464

6565
fn has_hashbang(ctx: &LintContext) -> bool {
66-
ctx.nodes().program().unwrap().hashbang.is_some()
66+
ctx.nodes().program().hashbang.is_some()
6767
}
6868

6969
fn is_inside_process_event_handler(ctx: &LintContext, node: &AstNode) -> bool {

crates/oxc_semantic/src/node.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,10 @@ impl<'a> AstNodes<'a> {
185185
}
186186

187187
/// Get the [`Program`] that's also the root of the AST.
188-
///
189-
/// Returns [`None`] if root node isn't set. This will never happen if you
190-
/// are obtaining an [`AstNodes`] that has already been constructed.
191188
#[inline]
192-
pub fn program(&self) -> Option<&'a Program<'a>> {
193-
self.program
189+
pub fn program(&self) -> &'a Program<'a> {
190+
#[expect(clippy::missing_panics_doc, reason = "self.program is always `Some`")]
191+
self.program.as_ref().unwrap()
194192
}
195193

196194
/// Create and add an [`AstNode`] to the [`AstNodes`] tree and get its [`NodeId`].

0 commit comments

Comments
 (0)