refactor(linter): impl Deref<Target = Semantic> for LintContext (#6752)

`LintContext` contains many wrapper methods that invoke a method with the same
name on `Semantic`. Implementing `Deref` lets us remove those redundant methods.
This commit is contained in:
DonIsaac 2024-10-21 22:39:37 +00:00
parent 70efa47d75
commit 744aa74e81

View file

@ -1,13 +1,12 @@
#![allow(rustdoc::private_intra_doc_links)] // useful for intellisense
mod host;
use std::{path::Path, rc::Rc};
use std::{ops::Deref, path::Path, rc::Rc};
use oxc_cfg::ControlFlowGraph;
use oxc_diagnostics::{OxcDiagnostic, Severity};
use oxc_semantic::{AstNodes, JSDocFinder, ScopeTree, Semantic, SymbolTable};
use oxc_span::{GetSpan, SourceType, Span};
use oxc_syntax::module_record::ModuleRecord;
use oxc_semantic::Semantic;
use oxc_span::{GetSpan, Span};
#[cfg(debug_assertions)]
use crate::rule::RuleFixMeta;
@ -56,6 +55,15 @@ pub struct LintContext<'a> {
severity: Severity,
}
impl<'a> Deref for LintContext<'a> {
type Target = Semantic<'a>;
#[inline]
fn deref(&self) -> &Self::Target {
self.parent.semantic()
}
}
impl<'a> LintContext<'a> {
/// Base URL for the documentation, used to generate rule documentation URLs when a diagnostic is reported.
const WEBSITE_BASE_URL: &'static str = "https://oxc.rs/docs/guide/usage/linter/rules";
@ -111,24 +119,12 @@ impl<'a> LintContext<'a> {
&self.parent.disable_directives
}
/// Source code of the file being linted.
#[inline]
pub fn source_text(&self) -> &'a str {
self.semantic().source_text()
}
/// Get a snippet of source text covered by the given [`Span`]. For details,
/// see [`Span::source_text`].
pub fn source_range(&self, span: Span) -> &'a str {
span.source_text(self.semantic().source_text())
}
/// [`SourceType`] of the file currently being linted.
#[inline]
pub fn source_type(&self) -> &SourceType {
self.semantic().source_type()
}
/// Path to the file currently being linted.
#[inline]
pub fn file_path(&self) -> &Path {
@ -321,41 +317,6 @@ impl<'a> LintContext<'a> {
pub fn frameworks(&self) -> FrameworkFlags {
self.parent.frameworks
}
/// AST nodes
///
/// Shorthand for `self.semantic().nodes()`.
pub fn nodes(&self) -> &AstNodes<'a> {
self.semantic().nodes()
}
/// Scope tree
///
/// Shorthand for `ctx.semantic().scopes()`.
pub fn scopes(&self) -> &ScopeTree {
self.semantic().scopes()
}
/// Symbol table
///
/// Shorthand for `ctx.semantic().symbols()`.
pub fn symbols(&self) -> &SymbolTable {
self.semantic().symbols()
}
/// Imported modules and exported symbols
///
/// Shorthand for `ctx.semantic().module_record()`.
pub fn module_record(&self) -> &ModuleRecord {
self.semantic().module_record()
}
/// JSDoc comments
///
/// Shorthand for `ctx.semantic().jsdoc()`.
pub fn jsdoc(&self) -> &JSDocFinder<'a> {
self.semantic().jsdoc()
}
}
/// Gets the prefixed plugin name, given the short plugin name.