mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(linter): add a ctx.module_record() method (#3637)
This commit is contained in:
parent
5cb7e6ae56
commit
84304b4f8f
17 changed files with 23 additions and 18 deletions
|
|
@ -3,6 +3,7 @@ use std::{cell::RefCell, path::Path, rc::Rc, sync::Arc};
|
||||||
use oxc_diagnostics::{OxcDiagnostic, Severity};
|
use oxc_diagnostics::{OxcDiagnostic, Severity};
|
||||||
use oxc_semantic::{AstNodes, JSDocFinder, ScopeTree, Semantic, SymbolTable};
|
use oxc_semantic::{AstNodes, JSDocFinder, ScopeTree, Semantic, SymbolTable};
|
||||||
use oxc_span::{SourceType, Span};
|
use oxc_span::{SourceType, Span};
|
||||||
|
use oxc_syntax::module_record::ModuleRecord;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
disable_directives::{DisableDirectives, DisableDirectivesBuilder},
|
disable_directives::{DisableDirectives, DisableDirectivesBuilder},
|
||||||
|
|
@ -171,6 +172,10 @@ impl<'a> LintContext<'a> {
|
||||||
self.semantic().symbols()
|
self.semantic().symbols()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn module_record(&self) -> &ModuleRecord {
|
||||||
|
self.semantic().module_record()
|
||||||
|
}
|
||||||
|
|
||||||
/* JSDoc */
|
/* JSDoc */
|
||||||
pub fn jsdoc(&self) -> &JSDocFinder<'a> {
|
pub fn jsdoc(&self) -> &JSDocFinder<'a> {
|
||||||
self.semantic().jsdoc()
|
self.semantic().jsdoc()
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ declare_oxc_lint!(
|
||||||
|
|
||||||
impl Rule for Default {
|
impl Rule for Default {
|
||||||
fn run_once(&self, ctx: &LintContext<'_>) {
|
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 {
|
for import_entry in &module_record.import_entries {
|
||||||
let ImportImportName::Default(default_span) = import_entry.import_name else {
|
let ImportImportName::Default(default_span) = import_entry.import_name else {
|
||||||
continue;
|
continue;
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ declare_oxc_lint!(
|
||||||
|
|
||||||
impl Rule for Export {
|
impl Rule for Export {
|
||||||
fn run_once(&self, ctx: &LintContext<'_>) {
|
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 named_export = &module_record.exported_bindings;
|
||||||
|
|
||||||
let mut all_export_names = FxHashMap::default();
|
let mut all_export_names = FxHashMap::default();
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,7 @@ impl Rule for Namespace {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn run_once(&self, ctx: &LintContext<'_>) {
|
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| {
|
module_record.import_entries.iter().for_each(|entry| {
|
||||||
let (source, module) = match &entry.import_name {
|
let (source, module) = match &entry.import_name {
|
||||||
ImportImportName::NamespaceObject => {
|
ImportImportName::NamespaceObject => {
|
||||||
|
|
|
||||||
|
|
@ -98,7 +98,7 @@ impl Rule for NoCycle {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_once(&self, ctx: &LintContext<'_>) {
|
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 needle = &module_record.resolved_absolute_path;
|
||||||
let cwd = std::env::current_dir().unwrap();
|
let cwd = std::env::current_dir().unwrap();
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ declare_oxc_lint!(
|
||||||
|
|
||||||
impl Rule for NoDefaultExport {
|
impl Rule for NoDefaultExport {
|
||||||
fn run_once(&self, ctx: &LintContext<'_>) {
|
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);
|
write_diagnostic_optional(ctx, module_record.export_default);
|
||||||
module_record.export_default_duplicated.iter().for_each(|it| write_diagnostic(ctx, *it));
|
module_record.export_default_duplicated.iter().for_each(|it| write_diagnostic(ctx, *it));
|
||||||
write_diagnostic_optional(ctx, module_record.exported_bindings.get("default").copied());
|
write_diagnostic_optional(ctx, module_record.exported_bindings.get("default").copied());
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ impl Rule for NoDuplicates {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_once(&self, ctx: &LintContext<'_>) {
|
fn run_once(&self, ctx: &LintContext<'_>) {
|
||||||
let module_record = ctx.semantic().module_record();
|
let module_record = ctx.module_record();
|
||||||
|
|
||||||
let groups = module_record
|
let groups = module_record
|
||||||
.requested_modules
|
.requested_modules
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ declare_oxc_lint!(
|
||||||
|
|
||||||
impl Rule for NoNamedAsDefault {
|
impl Rule for NoNamedAsDefault {
|
||||||
fn run_once(&self, ctx: &LintContext<'_>) {
|
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 {
|
for import_entry in &module_record.import_entries {
|
||||||
let ImportImportName::Default(import_span) = &import_entry.import_name else {
|
let ImportImportName::Default(import_span) = &import_entry.import_name else {
|
||||||
continue;
|
continue;
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,7 @@ fn get_symbol_id_from_ident(
|
||||||
|
|
||||||
impl Rule for NoNamedAsDefaultMember {
|
impl Rule for NoNamedAsDefaultMember {
|
||||||
fn run_once(&self, ctx: &LintContext<'_>) {
|
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)> =
|
let mut has_members_map: HashMap<SymbolId, (Ref<'_, CompactStr, _, _>, CompactStr)> =
|
||||||
HashMap::default();
|
HashMap::default();
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ declare_oxc_lint!(
|
||||||
|
|
||||||
impl Rule for NoSelfImport {
|
impl Rule for NoSelfImport {
|
||||||
fn run_once(&self, ctx: &LintContext<'_>) {
|
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;
|
let resolved_absolute_path = &module_record.resolved_absolute_path;
|
||||||
for (request, requested_modules) in &module_record.requested_modules {
|
for (request, requested_modules) in &module_record.requested_modules {
|
||||||
let Some(remote_module_record_ref) = module_record.loaded_modules.get(request) else {
|
let Some(remote_module_record_ref) = module_record.loaded_modules.get(request) else {
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ impl Rule for NoUnusedModules {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_once(&self, ctx: &LintContext<'_>) {
|
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() {
|
if self.missing_exports && module_record.local_export_entries.is_empty() {
|
||||||
ctx.diagnostic(no_exports_found(Span::new(0, 0)));
|
ctx.diagnostic(no_exports_found(Span::new(0, 0)));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,11 +43,11 @@ impl Rule for NoExport {
|
||||||
return;
|
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));
|
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));
|
ctx.diagnostic(no_export_diagnostic(span));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ declare_oxc_lint!(
|
||||||
|
|
||||||
impl Rule for NoMocksImport {
|
impl Rule for NoMocksImport {
|
||||||
fn run_once(&self, ctx: &LintContext) {
|
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 {
|
for import_entry in &module_records.import_entries {
|
||||||
let module_specifier = import_entry.module_request.name().as_str();
|
let module_specifier = import_entry.module_request.name().as_str();
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ impl Rule for NoUselessEmptyExport {
|
||||||
if decl.declaration.is_some() || !decl.specifiers.is_empty() {
|
if decl.declaration.is_some() || !decl.specifiers.is_empty() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let module_record = ctx.semantic().module_record();
|
let module_record = ctx.module_record();
|
||||||
if module_record.exported_bindings.is_empty()
|
if module_record.exported_bindings.is_empty()
|
||||||
&& module_record.local_export_entries.is_empty()
|
&& module_record.local_export_entries.is_empty()
|
||||||
&& module_record.indirect_export_entries.is_empty()
|
&& module_record.indirect_export_entries.is_empty()
|
||||||
|
|
|
||||||
|
|
@ -75,7 +75,7 @@ fn is_inside_process_event_handler(ctx: &LintContext, node: &AstNode) -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_worker_threads_imported(ctx: &LintContext) -> 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")
|
matches!(entry.module_request.name().as_str(), "worker_threads" | "node:worker_threads")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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> {
|
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" {
|
if entry.module_request.name().as_str() == "next/script" {
|
||||||
Some(entry.local_name.name())
|
Some(entry.local_name.name())
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -105,8 +105,8 @@ impl<'a> Semantic<'a> {
|
||||||
&self.jsdoc
|
&self.jsdoc
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn module_record(&self) -> &Arc<ModuleRecord> {
|
pub fn module_record(&self) -> &ModuleRecord {
|
||||||
&self.module_record
|
self.module_record.as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn symbols(&self) -> &SymbolTable {
|
pub fn symbols(&self) -> &SymbolTable {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue