Skip to content

Commit

Permalink
refactor(linter): impl Deref<Target = Semantic> for LintContext (#…
Browse files Browse the repository at this point in the history
…6752)

`LintContext` contains many wrapper methods that invoke a method with the same
name on `Semantic`. Implementing `Deref` lets us remove those redundant methods.
  • Loading branch information
DonIsaac committed Oct 21, 2024
1 parent 70efa47 commit 744aa74
Showing 1 changed file with 12 additions and 51 deletions.
63 changes: 12 additions & 51 deletions crates/oxc_linter/src/context/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 744aa74

Please sign in to comment.