Skip to content

Commit

Permalink
refactor(linter): add a ctx.module_record() method (#3637)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jun 12, 2024
1 parent 5cb7e6a commit 84304b4
Show file tree
Hide file tree
Showing 17 changed files with 23 additions and 18 deletions.
5 changes: 5 additions & 0 deletions crates/oxc_linter/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/import/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/import/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/import/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/import/no_cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/import/no_default_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/import/no_duplicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/import/no_named_as_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SymbolId, (Ref<'_, CompactStr, _, _>, CompactStr)> =
HashMap::default();
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/import/no_self_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/import/no_unused_modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/jest/no_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/jest/no_mocks_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/unicorn/no_process_exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/utils/nextjs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_semantic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ impl<'a> Semantic<'a> {
&self.jsdoc
}

pub fn module_record(&self) -> &Arc<ModuleRecord> {
&self.module_record
pub fn module_record(&self) -> &ModuleRecord {
self.module_record.as_ref()
}

pub fn symbols(&self) -> &SymbolTable {
Expand Down

0 comments on commit 84304b4

Please sign in to comment.