-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dfd7f38
commit d9a29da
Showing
9 changed files
with
795 additions
and
721 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
use std::{fmt, ops::Deref}; | ||
|
||
use drop_bomb::DropBomb; | ||
use ruff_db::{ | ||
diagnostic::{DiagnosticId, Severity}, | ||
files::File, | ||
}; | ||
use ruff_python_ast::AnyNodeRef; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::{ | ||
lint::{LintId, LintMetadata}, | ||
Db, | ||
}; | ||
|
||
use super::{TypeCheckDiagnostic, TypeCheckDiagnostics}; | ||
|
||
/// Context for infering types for a specific file. | ||
pub(crate) struct TyContext<'db> { | ||
db: &'db dyn Db, | ||
file: File, | ||
diagnostics: std::cell::RefCell<TypeCheckDiagnostics>, | ||
defused: DropBomb, | ||
} | ||
|
||
impl<'db> TyContext<'db> { | ||
pub(crate) fn new(db: &'db dyn Db, file: File) -> Self { | ||
Self { | ||
db, | ||
file, | ||
diagnostics: std::cell::RefCell::new(TypeCheckDiagnostics::default()), | ||
defused: DropBomb::new("Failed to consume typing context"), | ||
} | ||
} | ||
|
||
/// The file for which the types are inferred. | ||
pub(crate) fn file(&self) -> File { | ||
self.file | ||
} | ||
|
||
pub(crate) fn db(&self) -> &'db dyn Db { | ||
self.db | ||
} | ||
|
||
pub(crate) fn extend<T>(&mut self, other: &T) | ||
where | ||
T: WithDiagnostics, | ||
{ | ||
self.diagnostics | ||
.get_mut() | ||
.extend(other.diagnostics().iter().cloned()); | ||
} | ||
|
||
/// Reports a lint located at `node`. | ||
pub(super) fn report_lint( | ||
&self, | ||
lint: &'static LintMetadata, | ||
node: AnyNodeRef, | ||
message: std::fmt::Arguments, | ||
) { | ||
// Skip over diagnostics if the rule is disabled. | ||
let Some(severity) = self.db.rule_selection().severity(LintId::of(lint)) else { | ||
return; | ||
}; | ||
|
||
self.report_diagnostic(node, DiagnosticId::Lint(lint.name()), severity, message); | ||
} | ||
|
||
/// Adds a new diagnostic. | ||
/// | ||
/// The diagnostic does not get added if the rule isn't enabled for this file. | ||
pub(super) fn report_diagnostic( | ||
&self, | ||
node: AnyNodeRef, | ||
id: DiagnosticId, | ||
severity: Severity, | ||
message: std::fmt::Arguments, | ||
) { | ||
if !self.db.is_file_open(self.file) { | ||
return; | ||
} | ||
|
||
// TODO: Don't emit the diagnostic if: | ||
// * The enclosing node contains any syntax errors | ||
// * The rule is disabled for this file. We probably want to introduce a new query that | ||
// returns a rule selector for a given file that respects the package's settings, | ||
// any global pragma comments in the file, and any per-file-ignores. | ||
// * Check for suppression comments, bump a counter if the diagnostic is suppressed. | ||
|
||
self.diagnostics.borrow_mut().push(TypeCheckDiagnostic { | ||
file: self.file, | ||
id, | ||
message: message.to_string(), | ||
range: node.range(), | ||
severity, | ||
}); | ||
} | ||
|
||
pub(crate) fn finish(mut self) -> TypeCheckDiagnostics { | ||
self.defused.defuse(); | ||
let mut diagnostics = self.diagnostics.into_inner(); | ||
diagnostics.shrink_to_fit(); | ||
diagnostics | ||
} | ||
} | ||
|
||
impl Deref for TyContext<'_> { | ||
type Target = dyn Db; | ||
fn deref(&self) -> &Self::Target { | ||
self.db | ||
} | ||
} | ||
|
||
impl fmt::Debug for TyContext<'_> { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
f.debug_struct("TyContext") | ||
.field("file", &self.file) | ||
.field("diagnostics", &self.diagnostics) | ||
.field("defused", &self.defused) | ||
.finish() | ||
} | ||
} | ||
|
||
pub(crate) trait WithDiagnostics { | ||
fn diagnostics(&self) -> &TypeCheckDiagnostics; | ||
} |
Oops, something went wrong.