diff --git a/crates/oxc_linter/src/context.rs b/crates/oxc_linter/src/context.rs index 9b174349d..5da45eb73 100644 --- a/crates/oxc_linter/src/context.rs +++ b/crates/oxc_linter/src/context.rs @@ -3,6 +3,7 @@ use std::{cell::RefCell, path::Path, rc::Rc, sync::Arc}; use oxc_diagnostics::{OxcDiagnostic, Severity}; use oxc_semantic::{AstNodes, JSDocFinder, ScopeTree, Semantic, SymbolTable}; use oxc_span::{SourceType, Span}; +use oxc_syntax::module_record::ModuleRecord; use crate::{ disable_directives::{DisableDirectives, DisableDirectivesBuilder}, @@ -171,6 +172,10 @@ impl<'a> LintContext<'a> { self.semantic().symbols() } + pub fn module_record(&self) -> &ModuleRecord { + self.semantic().module_record() + } + /* JSDoc */ pub fn jsdoc(&self) -> &JSDocFinder<'a> { self.semantic().jsdoc() diff --git a/crates/oxc_linter/src/rules/import/default.rs b/crates/oxc_linter/src/rules/import/default.rs index 332e4d8c0..ef17d36e0 100644 --- a/crates/oxc_linter/src/rules/import/default.rs +++ b/crates/oxc_linter/src/rules/import/default.rs @@ -38,7 +38,7 @@ declare_oxc_lint!( impl Rule for Default { fn run_once(&self, ctx: &LintContext<'_>) { - let module_record = ctx.semantic().module_record(); + let module_record = ctx.module_record(); for import_entry in &module_record.import_entries { let ImportImportName::Default(default_span) = import_entry.import_name else { continue; diff --git a/crates/oxc_linter/src/rules/import/export.rs b/crates/oxc_linter/src/rules/import/export.rs index ece148042..c8b3f9865 100644 --- a/crates/oxc_linter/src/rules/import/export.rs +++ b/crates/oxc_linter/src/rules/import/export.rs @@ -35,7 +35,7 @@ declare_oxc_lint!( impl Rule for Export { fn run_once(&self, ctx: &LintContext<'_>) { - let module_record = ctx.semantic().module_record(); + let module_record = ctx.module_record(); let named_export = &module_record.exported_bindings; let mut all_export_names = FxHashMap::default(); diff --git a/crates/oxc_linter/src/rules/import/namespace.rs b/crates/oxc_linter/src/rules/import/namespace.rs index e5ca89e94..faeba5be1 100644 --- a/crates/oxc_linter/src/rules/import/namespace.rs +++ b/crates/oxc_linter/src/rules/import/namespace.rs @@ -64,7 +64,7 @@ impl Rule for Namespace { } } fn run_once(&self, ctx: &LintContext<'_>) { - let module_record = ctx.semantic().module_record(); + let module_record = ctx.module_record(); module_record.import_entries.iter().for_each(|entry| { let (source, module) = match &entry.import_name { ImportImportName::NamespaceObject => { diff --git a/crates/oxc_linter/src/rules/import/no_cycle.rs b/crates/oxc_linter/src/rules/import/no_cycle.rs index 23e5c076c..a4d8ce8e7 100644 --- a/crates/oxc_linter/src/rules/import/no_cycle.rs +++ b/crates/oxc_linter/src/rules/import/no_cycle.rs @@ -98,7 +98,7 @@ impl Rule for NoCycle { } fn run_once(&self, ctx: &LintContext<'_>) { - let module_record = ctx.semantic().module_record(); + let module_record = ctx.module_record(); let needle = &module_record.resolved_absolute_path; let cwd = std::env::current_dir().unwrap(); diff --git a/crates/oxc_linter/src/rules/import/no_default_export.rs b/crates/oxc_linter/src/rules/import/no_default_export.rs index a99100655..9b4457fc8 100644 --- a/crates/oxc_linter/src/rules/import/no_default_export.rs +++ b/crates/oxc_linter/src/rules/import/no_default_export.rs @@ -43,7 +43,7 @@ declare_oxc_lint!( impl Rule for NoDefaultExport { fn run_once(&self, ctx: &LintContext<'_>) { - let module_record = ctx.semantic().module_record(); + let module_record = ctx.module_record(); write_diagnostic_optional(ctx, module_record.export_default); module_record.export_default_duplicated.iter().for_each(|it| write_diagnostic(ctx, *it)); write_diagnostic_optional(ctx, module_record.exported_bindings.get("default").copied()); diff --git a/crates/oxc_linter/src/rules/import/no_duplicates.rs b/crates/oxc_linter/src/rules/import/no_duplicates.rs index 9a76ce503..cd848db2f 100644 --- a/crates/oxc_linter/src/rules/import/no_duplicates.rs +++ b/crates/oxc_linter/src/rules/import/no_duplicates.rs @@ -30,7 +30,7 @@ impl Rule for NoDuplicates { } fn run_once(&self, ctx: &LintContext<'_>) { - let module_record = ctx.semantic().module_record(); + let module_record = ctx.module_record(); let groups = module_record .requested_modules diff --git a/crates/oxc_linter/src/rules/import/no_named_as_default.rs b/crates/oxc_linter/src/rules/import/no_named_as_default.rs index 577000f4d..0c3f329ac 100644 --- a/crates/oxc_linter/src/rules/import/no_named_as_default.rs +++ b/crates/oxc_linter/src/rules/import/no_named_as_default.rs @@ -43,7 +43,7 @@ declare_oxc_lint!( impl Rule for NoNamedAsDefault { fn run_once(&self, ctx: &LintContext<'_>) { - let module_record = ctx.semantic().module_record(); + let module_record = ctx.module_record(); for import_entry in &module_record.import_entries { let ImportImportName::Default(import_span) = &import_entry.import_name else { continue; diff --git a/crates/oxc_linter/src/rules/import/no_named_as_default_member.rs b/crates/oxc_linter/src/rules/import/no_named_as_default_member.rs index b0664eb1c..ca5631d36 100644 --- a/crates/oxc_linter/src/rules/import/no_named_as_default_member.rs +++ b/crates/oxc_linter/src/rules/import/no_named_as_default_member.rs @@ -62,7 +62,7 @@ fn get_symbol_id_from_ident( impl Rule for NoNamedAsDefaultMember { fn run_once(&self, ctx: &LintContext<'_>) { - let module_record = ctx.semantic().module_record(); + let module_record = ctx.module_record(); let mut has_members_map: HashMap, CompactStr)> = HashMap::default(); diff --git a/crates/oxc_linter/src/rules/import/no_self_import.rs b/crates/oxc_linter/src/rules/import/no_self_import.rs index b5213bb78..4dfdf1564 100644 --- a/crates/oxc_linter/src/rules/import/no_self_import.rs +++ b/crates/oxc_linter/src/rules/import/no_self_import.rs @@ -33,7 +33,7 @@ declare_oxc_lint!( impl Rule for NoSelfImport { fn run_once(&self, ctx: &LintContext<'_>) { - let module_record = ctx.semantic().module_record(); + let module_record = ctx.module_record(); let resolved_absolute_path = &module_record.resolved_absolute_path; for (request, requested_modules) in &module_record.requested_modules { let Some(remote_module_record_ref) = module_record.loaded_modules.get(request) else { diff --git a/crates/oxc_linter/src/rules/import/no_unused_modules.rs b/crates/oxc_linter/src/rules/import/no_unused_modules.rs index dcfe151ab..5fa5f11d1 100644 --- a/crates/oxc_linter/src/rules/import/no_unused_modules.rs +++ b/crates/oxc_linter/src/rules/import/no_unused_modules.rs @@ -42,7 +42,7 @@ impl Rule for NoUnusedModules { } fn run_once(&self, ctx: &LintContext<'_>) { - let module_record = ctx.semantic().module_record(); + let module_record = ctx.module_record(); if self.missing_exports && module_record.local_export_entries.is_empty() { ctx.diagnostic(no_exports_found(Span::new(0, 0))); } diff --git a/crates/oxc_linter/src/rules/jest/no_export.rs b/crates/oxc_linter/src/rules/jest/no_export.rs index b2c3ed897..1301660ee 100644 --- a/crates/oxc_linter/src/rules/jest/no_export.rs +++ b/crates/oxc_linter/src/rules/jest/no_export.rs @@ -43,11 +43,11 @@ impl Rule for NoExport { return; } - for span in ctx.semantic().module_record().exported_bindings.values() { + for span in ctx.module_record().exported_bindings.values() { ctx.diagnostic(no_export_diagnostic(*span)); } - if let Some(span) = ctx.semantic().module_record().export_default { + if let Some(span) = ctx.module_record().export_default { ctx.diagnostic(no_export_diagnostic(span)); } } diff --git a/crates/oxc_linter/src/rules/jest/no_mocks_import.rs b/crates/oxc_linter/src/rules/jest/no_mocks_import.rs index e48c21836..8419ae311 100644 --- a/crates/oxc_linter/src/rules/jest/no_mocks_import.rs +++ b/crates/oxc_linter/src/rules/jest/no_mocks_import.rs @@ -35,7 +35,7 @@ declare_oxc_lint!( impl Rule for NoMocksImport { fn run_once(&self, ctx: &LintContext) { - let module_records = ctx.semantic().module_record(); + let module_records = ctx.module_record(); for import_entry in &module_records.import_entries { let module_specifier = import_entry.module_request.name().as_str(); diff --git a/crates/oxc_linter/src/rules/typescript/no_useless_empty_export.rs b/crates/oxc_linter/src/rules/typescript/no_useless_empty_export.rs index 9c00edd68..5e03080d5 100644 --- a/crates/oxc_linter/src/rules/typescript/no_useless_empty_export.rs +++ b/crates/oxc_linter/src/rules/typescript/no_useless_empty_export.rs @@ -44,7 +44,7 @@ impl Rule for NoUselessEmptyExport { if decl.declaration.is_some() || !decl.specifiers.is_empty() { return; } - let module_record = ctx.semantic().module_record(); + let module_record = ctx.module_record(); if module_record.exported_bindings.is_empty() && module_record.local_export_entries.is_empty() && module_record.indirect_export_entries.is_empty() diff --git a/crates/oxc_linter/src/rules/unicorn/no_process_exit.rs b/crates/oxc_linter/src/rules/unicorn/no_process_exit.rs index 75b23d2d1..ddd90d9c5 100644 --- a/crates/oxc_linter/src/rules/unicorn/no_process_exit.rs +++ b/crates/oxc_linter/src/rules/unicorn/no_process_exit.rs @@ -75,7 +75,7 @@ fn is_inside_process_event_handler(ctx: &LintContext, node: &AstNode) -> bool { } fn is_worker_threads_imported(ctx: &LintContext) -> bool { - ctx.semantic().module_record().import_entries.iter().any(|entry| { + ctx.module_record().import_entries.iter().any(|entry| { matches!(entry.module_request.name().as_str(), "worker_threads" | "node:worker_threads") }) } diff --git a/crates/oxc_linter/src/utils/nextjs.rs b/crates/oxc_linter/src/utils/nextjs.rs index 69898c32c..78469b4e6 100644 --- a/crates/oxc_linter/src/utils/nextjs.rs +++ b/crates/oxc_linter/src/utils/nextjs.rs @@ -14,7 +14,7 @@ pub fn is_document_page(file_path: &str) -> bool { } pub fn get_next_script_import_local_name<'a>(ctx: &'a LintContext) -> Option<&'a CompactStr> { - ctx.semantic().module_record().import_entries.iter().find_map(|entry| { + ctx.module_record().import_entries.iter().find_map(|entry| { if entry.module_request.name().as_str() == "next/script" { Some(entry.local_name.name()) } else { diff --git a/crates/oxc_semantic/src/lib.rs b/crates/oxc_semantic/src/lib.rs index f2a4b08b7..5d75011d3 100644 --- a/crates/oxc_semantic/src/lib.rs +++ b/crates/oxc_semantic/src/lib.rs @@ -105,8 +105,8 @@ impl<'a> Semantic<'a> { &self.jsdoc } - pub fn module_record(&self) -> &Arc { - &self.module_record + pub fn module_record(&self) -> &ModuleRecord { + self.module_record.as_ref() } pub fn symbols(&self) -> &SymbolTable {