Skip to content

Commit

Permalink
feat(parser,linter)!: use a different ModuleRecord for linter
Browse files Browse the repository at this point in the history
The parser returns a simple `ModuleRecord` that is allocated in the
arena for performance reasons.

The linter uses a more complicated, `Send` + `Sync` `ModuleRecord` for cross-module
support, which we will add more information in the future.
  • Loading branch information
Boshen committed Nov 30, 2024
1 parent 7e42e71 commit c494062
Show file tree
Hide file tree
Showing 33 changed files with 753 additions and 248 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/oxc_language_server/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use oxc_allocator::Allocator;
use oxc_diagnostics::{Error, NamedSource, Severity};
use oxc_linter::{
loader::{JavaScriptSource, Loader},
FixKind, Linter,
FixKind, Linter, ModuleRecord,
};
use oxc_parser::{ParseOptions, Parser};
use oxc_semantic::SemanticBuilder;
Expand Down Expand Up @@ -290,7 +290,7 @@ impl IsolatedLintHandler {
return Some(Self::wrap_diagnostics(path, &source_text, reports, start));
};

let module_record = Arc::new(ret.module_record);
let module_record = Arc::new(ModuleRecord::new(path, &ret.module_record));
let mut semantic = semantic_ret.semantic;
semantic.set_irregular_whitespaces(ret.irregular_whitespaces);
let result = self.linter.run(path, Rc::new(semantic), module_record);
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/context/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use std::{cell::RefCell, path::Path, rc::Rc, sync::Arc};

use oxc_semantic::Semantic;
use oxc_span::SourceType;
use oxc_syntax::module_record::ModuleRecord;

use crate::{
config::{LintConfig, LintPlugins},
disable_directives::{DisableDirectives, DisableDirectivesBuilder},
fixer::{FixKind, Message},
frameworks,
module_record::ModuleRecord,
options::LintOptions,
utils, FrameworkFlags, RuleWithSeverity,
};
Expand Down
3 changes: 1 addition & 2 deletions crates/oxc_linter/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ use oxc_cfg::ControlFlowGraph;
use oxc_diagnostics::{OxcDiagnostic, Severity};
use oxc_semantic::Semantic;
use oxc_span::{GetSpan, Span};
use oxc_syntax::module_record::ModuleRecord;

#[cfg(debug_assertions)]
use crate::rule::RuleFixMeta;
use crate::{
disable_directives::DisableDirectives,
fixer::{FixKind, Message, RuleFix, RuleFixer},
javascript_globals::GLOBALS,
AllowWarnDeny, FrameworkFlags, OxlintEnv, OxlintGlobals, OxlintSettings,
AllowWarnDeny, FrameworkFlags, ModuleRecord, OxlintEnv, OxlintGlobals, OxlintSettings,
};

pub(crate) use host::ContextHost;
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/frameworks.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::{hash, path::Path};

use bitflags::bitflags;
use oxc_syntax::module_record::ModuleRecord;

use crate::ModuleRecord;

bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down
4 changes: 3 additions & 1 deletion crates/oxc_linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ mod fixer;
mod frameworks;
mod globals;
mod javascript_globals;
mod module_graph_visitor;
mod module_record;
mod options;
mod rule;
mod rules;
Expand All @@ -24,14 +26,14 @@ pub mod table;
use std::{io::Write, path::Path, rc::Rc, sync::Arc};

use oxc_semantic::{AstNode, Semantic};
use oxc_syntax::module_record::ModuleRecord;

pub use crate::{
builder::{LinterBuilder, LinterBuilderError},
config::{ESLintRule, LintPlugins, Oxlintrc},
context::LintContext,
fixer::FixKind,
frameworks::FrameworkFlags,
module_record::ModuleRecord,
options::{AllowWarnDeny, InvalidFilterKind, LintFilter, LintFilterKind},
rule::{RuleCategory, RuleFixMeta, RuleMeta, RuleWithSeverity},
service::{LintService, LintServiceOptions},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{marker::PhantomData, path::PathBuf, sync::Arc};
use oxc_span::CompactStr;
use rustc_hash::FxHashSet;

use crate::module_record::ModuleRecord;
use crate::ModuleRecord;

type ModulePair<'a> = (&'a CompactStr, &'a Arc<ModuleRecord>);

Expand Down Expand Up @@ -73,19 +73,19 @@ impl<'a, T> ModuleGraphVisitorBuilder<'a, T> {
self
}

/// Sets the enter module event closure.
#[must_use]
pub fn enter<F: FnMut(ModulePair, &ModuleRecord) + 'a>(mut self, enter: F) -> Self {
self.enter = Some(Box::new(enter));
self
}
// /// Sets the enter module event closure.
// #[must_use]
// pub fn enter<F: FnMut(ModulePair, &ModuleRecord) + 'a>(mut self, enter: F) -> Self {
// self.enter = Some(Box::new(enter));
// self
// }

/// Sets the leave module event closure.
#[must_use]
pub fn leave<F: FnMut(ModulePair, &ModuleRecord) + 'a>(mut self, leave: F) -> Self {
self.leave = Some(Box::new(leave));
self
}
// /// Sets the leave module event closure.
// #[must_use]
// pub fn leave<F: FnMut(ModulePair, &ModuleRecord) + 'a>(mut self, leave: F) -> Self {
// self.leave = Some(Box::new(leave));
// self
// }

/// Behaves similar to a flat fold_while iteration.
pub fn visit_fold<V: Fn(T, ModulePair, &ModuleRecord) -> VisitFoldWhile<T>>(
Expand Down Expand Up @@ -125,13 +125,13 @@ impl<T> Default for ModuleGraphVisitorBuilder<'_, T> {

pub struct ModuleGraphVisitResult<T> {
pub result: T,
pub traversed: FxHashSet<PathBuf>,
pub max_depth: u32,
pub _traversed: FxHashSet<PathBuf>,
pub _max_depth: u32,
}

impl<T> ModuleGraphVisitResult<T> {
fn with_result(result: T, visitor: ModuleGraphVisitor) -> Self {
Self { result, traversed: visitor.traversed, max_depth: visitor.max_depth }
Self { result, _traversed: visitor.traversed, _max_depth: visitor.max_depth }
}
}

Expand Down
Loading

0 comments on commit c494062

Please sign in to comment.