Skip to content

Commit

Permalink
make Linter threadsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Dec 19, 2023
1 parent 9749386 commit 16e7716
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
21 changes: 10 additions & 11 deletions examples/dlint/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,21 @@ fn run_linter(
get_recommended_rules()
};
let file_diagnostics = Arc::new(Mutex::new(BTreeMap::new()));
let linter_builder = LinterBuilder::default().rules(rules.clone());

let linter = linter_builder.build();
debug!("Configured rules: {}", rules.len());

if rules.is_empty() {
bail!("There's no rule to be run!");
}

paths
.par_iter()
.try_for_each(|file_path| -> Result<(), AnyError> {
let source_code = std::fs::read_to_string(file_path)?;

debug!("Configured rules: {}", rules.len());

if rules.is_empty() {
bail!("There's no rule to be run!");
}

let linter_builder = LinterBuilder::default().rules(rules.clone());

let linter = linter_builder.build();

let (parsed_source, diagnostics) = linter.lint_file(LintFileOptions {
let (parsed_source, diagnostics) = linter.clone().lint_file(LintFileOptions {
filename: file_path.to_string_lossy().to_string(),
source_code,
media_type: MediaType::from_path(file_path),
Expand Down
8 changes: 6 additions & 2 deletions src/linter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

// Copyright 2020-2021 the Deno authors. All rights reserved. MIT license.
use crate::ast_parser::parse_program;
use crate::context::Context;
Expand Down Expand Up @@ -63,8 +65,10 @@ impl LinterBuilder {
}
}

/// A linter instance. It can be cheaply cloned and shared between threads.
#[derive(Clone)]
pub struct Linter {
ctx: LinterContext,
ctx: Arc<LinterContext>,
}

/// TODO(bartlomieju): docstring
Expand Down Expand Up @@ -114,7 +118,7 @@ impl Linter {
rules,
);

Linter { ctx }
Linter { ctx: Arc::new(ctx) }
}

pub fn lint_file(
Expand Down

0 comments on commit 16e7716

Please sign in to comment.