refactor(linter): add a ctx.module_record() method (#3637)

This commit is contained in:
Boshen 2024-06-12 04:32:38 +00:00
parent 5cb7e6ae56
commit 84304b4f8f
17 changed files with 23 additions and 18 deletions

View file

@ -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()

View file

@ -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;

View file

@ -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();

View file

@ -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 => {

View file

@ -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();

View file

@ -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());

View file

@ -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

View file

@ -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;

View file

@ -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();

View file

@ -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 {

View file

@ -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)));
}

View file

@ -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));
}
}

View file

@ -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();

View file

@ -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()

View file

@ -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")
})
}

View file

@ -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 {

View file

@ -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 {