Skip to content

Commit 744aa74

Browse files
committedOct 21, 2024·
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.
1 parent 70efa47 commit 744aa74

File tree

1 file changed

+12
-51
lines changed
  • crates/oxc_linter/src/context

1 file changed

+12
-51
lines changed
 

‎crates/oxc_linter/src/context/mod.rs

+12-51
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
#![allow(rustdoc::private_intra_doc_links)] // useful for intellisense
22
mod host;
33

4-
use std::{path::Path, rc::Rc};
4+
use std::{ops::Deref, path::Path, rc::Rc};
55

66
use oxc_cfg::ControlFlowGraph;
77
use oxc_diagnostics::{OxcDiagnostic, Severity};
8-
use oxc_semantic::{AstNodes, JSDocFinder, ScopeTree, Semantic, SymbolTable};
9-
use oxc_span::{GetSpan, SourceType, Span};
10-
use oxc_syntax::module_record::ModuleRecord;
8+
use oxc_semantic::Semantic;
9+
use oxc_span::{GetSpan, Span};
1110

1211
#[cfg(debug_assertions)]
1312
use crate::rule::RuleFixMeta;
@@ -56,6 +55,15 @@ pub struct LintContext<'a> {
5655
severity: Severity,
5756
}
5857

58+
impl<'a> Deref for LintContext<'a> {
59+
type Target = Semantic<'a>;
60+
61+
#[inline]
62+
fn deref(&self) -> &Self::Target {
63+
self.parent.semantic()
64+
}
65+
}
66+
5967
impl<'a> LintContext<'a> {
6068
/// Base URL for the documentation, used to generate rule documentation URLs when a diagnostic is reported.
6169
const WEBSITE_BASE_URL: &'static str = "https://oxc.rs/docs/guide/usage/linter/rules";
@@ -111,24 +119,12 @@ impl<'a> LintContext<'a> {
111119
&self.parent.disable_directives
112120
}
113121

114-
/// Source code of the file being linted.
115-
#[inline]
116-
pub fn source_text(&self) -> &'a str {
117-
self.semantic().source_text()
118-
}
119-
120122
/// Get a snippet of source text covered by the given [`Span`]. For details,
121123
/// see [`Span::source_text`].
122124
pub fn source_range(&self, span: Span) -> &'a str {
123125
span.source_text(self.semantic().source_text())
124126
}
125127

126-
/// [`SourceType`] of the file currently being linted.
127-
#[inline]
128-
pub fn source_type(&self) -> &SourceType {
129-
self.semantic().source_type()
130-
}
131-
132128
/// Path to the file currently being linted.
133129
#[inline]
134130
pub fn file_path(&self) -> &Path {
@@ -321,41 +317,6 @@ impl<'a> LintContext<'a> {
321317
pub fn frameworks(&self) -> FrameworkFlags {
322318
self.parent.frameworks
323319
}
324-
325-
/// AST nodes
326-
///
327-
/// Shorthand for `self.semantic().nodes()`.
328-
pub fn nodes(&self) -> &AstNodes<'a> {
329-
self.semantic().nodes()
330-
}
331-
332-
/// Scope tree
333-
///
334-
/// Shorthand for `ctx.semantic().scopes()`.
335-
pub fn scopes(&self) -> &ScopeTree {
336-
self.semantic().scopes()
337-
}
338-
339-
/// Symbol table
340-
///
341-
/// Shorthand for `ctx.semantic().symbols()`.
342-
pub fn symbols(&self) -> &SymbolTable {
343-
self.semantic().symbols()
344-
}
345-
346-
/// Imported modules and exported symbols
347-
///
348-
/// Shorthand for `ctx.semantic().module_record()`.
349-
pub fn module_record(&self) -> &ModuleRecord {
350-
self.semantic().module_record()
351-
}
352-
353-
/// JSDoc comments
354-
///
355-
/// Shorthand for `ctx.semantic().jsdoc()`.
356-
pub fn jsdoc(&self) -> &JSDocFinder<'a> {
357-
self.semantic().jsdoc()
358-
}
359320
}
360321

361322
/// Gets the prefixed plugin name, given the short plugin name.

0 commit comments

Comments
 (0)
Please sign in to comment.