Skip to content

Commit

Permalink
refactor: Make tags strongly typed (#1367)
Browse files Browse the repository at this point in the history
* refactor: Make tags strongly typed

* fmt
  • Loading branch information
bartlomieju authored Nov 29, 2024
1 parent 718c6fb commit 3184b40
Show file tree
Hide file tree
Showing 95 changed files with 330 additions and 187 deletions.
9 changes: 5 additions & 4 deletions examples/dlint/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

use crate::color::colorize_markdown;
use deno_lint::rules::get_all_rules;
use deno_lint::tags;
use serde::Serialize;

#[derive(Clone, Copy, Serialize)]
#[derive(Clone, Serialize)]
pub struct Rule {
code: &'static str,
docs: &'static str,
tags: &'static [&'static str],
tags: Vec<&'static str>,
}

pub fn get_all_rules_metadata() -> Vec<Rule> {
Expand All @@ -17,7 +18,7 @@ pub fn get_all_rules_metadata() -> Vec<Rule> {
.map(|rule| Rule {
code: rule.code(),
docs: rule.docs(),
tags: rule.tags(),
tags: rule.tags().iter().map(|tag| tag.display()).collect(),
})
.collect()
}
Expand Down Expand Up @@ -89,7 +90,7 @@ impl RuleFormatter for PrettyFormatter {
list.push("Available rules (trailing ✔️ mark indicates it is included in the recommended rule set):".to_string());
list.extend(rules.iter().map(|r| {
let mut s = format!(" - {}", r.code);
if r.tags.contains(&"recommended") {
if r.tags.contains(&tags::RECOMMENDED.display()) {
s += " ✔️";
}
s
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub mod linter;
mod performance_mark;
pub mod rules;
pub mod swc_util;
pub mod tags;

pub use deno_ast::view::Program;
pub use deno_ast::view::ProgramRef;
Expand Down
10 changes: 7 additions & 3 deletions src/rules.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use crate::context::Context;
use crate::tags;
use crate::tags::Tags;
use crate::Program;
use crate::ProgramRef;
use std::cmp::Ordering;
Expand Down Expand Up @@ -132,7 +134,7 @@ pub trait LintRule: std::fmt::Debug + Send + Sync {
fn code(&self) -> &'static str;

/// Returns the tags this rule belongs to, e.g. `recommended`
fn tags(&self) -> &'static [&'static str] {
fn tags(&self) -> Tags {
&[]
}

Expand Down Expand Up @@ -168,7 +170,7 @@ pub fn recommended_rules(
) -> Vec<Box<dyn LintRule>> {
all_rules
.into_iter()
.filter(|r| r.tags().contains(&"recommended"))
.filter(|r| r.tags().contains(&tags::RECOMMENDED))
.collect()
}

Expand Down Expand Up @@ -367,6 +369,8 @@ fn get_all_rules_raw() -> Vec<Box<dyn LintRule>> {
mod tests {
use std::sync::Arc;

use crate::tags;

use super::*;

#[test]
Expand Down Expand Up @@ -474,7 +478,7 @@ mod tests {
let rules = Arc::clone(&rules);
spawn(move || {
for rule in rules.iter() {
assert!(rule.tags().contains(&"recommended"));
assert!(rule.tags().contains(&tags::RECOMMENDED));
}
})
})
Expand Down
5 changes: 3 additions & 2 deletions src/rules/adjacent_overload_signatures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use super::{Context, LintRule};
use crate::handler::{Handler, Traverse};
use crate::swc_util::StringRepr;
use crate::tags::{self, Tags};
use crate::Program;
use deno_ast::{view as ast_view, SourceRanged};
use derive_more::Display;
Expand All @@ -26,8 +27,8 @@ enum AdjacentOverloadSignaturesHint {
}

impl LintRule for AdjacentOverloadSignatures {
fn tags(&self) -> &'static [&'static str] {
&["recommended"]
fn tags(&self) -> Tags {
&[tags::RECOMMENDED]
}

fn code(&self) -> &'static str {
Expand Down
6 changes: 4 additions & 2 deletions src/rules/ban_ts_comment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use super::{Context, LintRule};
use crate::tags;
use crate::tags::Tags;
use crate::Program;
use deno_ast::swc::common::comments::Comment;
use deno_ast::swc::common::comments::CommentKind;
Expand Down Expand Up @@ -68,8 +70,8 @@ impl BanTsComment {
}

impl LintRule for BanTsComment {
fn tags(&self) -> &'static [&'static str] {
&["recommended"]
fn tags(&self) -> Tags {
&[tags::RECOMMENDED]
}

fn code(&self) -> &'static str {
Expand Down
5 changes: 3 additions & 2 deletions src/rules/ban_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use super::{Context, LintRule};
use crate::handler::{Handler, Traverse};
use crate::tags::{self, Tags};
use crate::Program;
use deno_ast::view::TsEntityName;
use deno_ast::{view as ast_view, SourceRanged};
Expand Down Expand Up @@ -74,8 +75,8 @@ impl TryFrom<&str> for BannedType {
}

impl LintRule for BanTypes {
fn tags(&self) -> &'static [&'static str] {
&["recommended"]
fn tags(&self) -> Tags {
&[tags::RECOMMENDED]
}

fn code(&self) -> &'static str {
Expand Down
9 changes: 6 additions & 3 deletions src/rules/ban_unknown_rule_code.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use super::{Context, LintRule};
use crate::Program;
use crate::{
tags::{self, Tags},
Program,
};

/// This is a dummy struct just for having the docs.
/// The actual implementation resides in [`Context`].
Expand All @@ -11,8 +14,8 @@ pub struct BanUnknownRuleCode;
pub(crate) const CODE: &str = "ban-unknown-rule-code";

impl LintRule for BanUnknownRuleCode {
fn tags(&self) -> &'static [&'static str] {
&["recommended"]
fn tags(&self) -> Tags {
&[tags::RECOMMENDED]
}

fn code(&self) -> &'static str {
Expand Down
9 changes: 6 additions & 3 deletions src/rules/ban_untagged_ignore.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use super::{Context, LintRule};
use crate::Program;
use crate::{
tags::{self, Tags},
Program,
};
use deno_ast::SourceRange;

#[derive(Debug)]
Expand All @@ -10,8 +13,8 @@ pub struct BanUntaggedIgnore;
const CODE: &str = "ban-untagged-ignore";

impl LintRule for BanUntaggedIgnore {
fn tags(&self) -> &'static [&'static str] {
&["recommended"]
fn tags(&self) -> Tags {
&[tags::RECOMMENDED]
}

fn code(&self) -> &'static str {
Expand Down
9 changes: 6 additions & 3 deletions src/rules/ban_unused_ignore.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use super::{Context, LintRule};
use crate::Program;
use crate::{
tags::{self, Tags},
Program,
};

/// This is a dummy struct just for having the docs.
/// The actual implementation resides in [`Context`].
#[derive(Debug)]
pub struct BanUnusedIgnore;

impl LintRule for BanUnusedIgnore {
fn tags(&self) -> &'static [&'static str] {
&["recommended"]
fn tags(&self) -> Tags {
&[tags::RECOMMENDED]
}

fn code(&self) -> &'static str {
Expand Down
3 changes: 2 additions & 1 deletion src/rules/camelcase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use super::{Context, LintRule};
use crate::handler::{Handler, Traverse};
use crate::swc_util::StringRepr;
use crate::tags::Tags;

use deno_ast::view::{Node, NodeKind, NodeTrait};
use deno_ast::{view as ast_view, SourceRange, SourceRanged};
Expand All @@ -16,7 +17,7 @@ pub struct Camelcase;
const CODE: &str = "camelcase";

impl LintRule for Camelcase {
fn tags(&self) -> &'static [&'static str] {
fn tags(&self) -> Tags {
&[]
}

Expand Down
5 changes: 3 additions & 2 deletions src/rules/constructor_super.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use super::{Context, LintRule};
use crate::handler::{Handler, Traverse};
use crate::tags::{self, Tags};
use crate::Program;
use deno_ast::{view as ast_view, SourceRange, SourceRanged};
use if_chain::if_chain;
Expand All @@ -14,8 +15,8 @@ const CODE: &str = "constructor-super";
// This rule currently differs from the ESlint implementation
// as there is currently no way of handling code paths in dlint
impl LintRule for ConstructorSuper {
fn tags(&self) -> &'static [&'static str] {
&["recommended"]
fn tags(&self) -> Tags {
&[tags::RECOMMENDED]
}

fn code(&self) -> &'static str {
Expand Down
5 changes: 3 additions & 2 deletions src/rules/for_direction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use super::{Context, LintRule};
use crate::handler::{Handler, Traverse};
use crate::tags::{self, Tags};
use crate::Program;
use deno_ast::swc::ast::AssignOp;
use deno_ast::swc::ast::BinaryOp;
Expand All @@ -15,8 +16,8 @@ use deno_ast::{view as ast_view, SourceRanged};
pub struct ForDirection;

impl LintRule for ForDirection {
fn tags(&self) -> &'static [&'static str] {
&["recommended"]
fn tags(&self) -> Tags {
&[tags::RECOMMENDED]
}

fn code(&self) -> &'static str {
Expand Down
5 changes: 3 additions & 2 deletions src/rules/fresh_handler_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use super::{Context, LintRule};
use crate::handler::{Handler, Traverse};
use crate::tags::{self, Tags};

use deno_ast::view::{Decl, Pat, Program};
use deno_ast::SourceRanged;
Expand All @@ -15,8 +16,8 @@ const MESSAGE: &str =
const HINT: &str = "Did you mean \"handler\"?";

impl LintRule for FreshHandlerExport {
fn tags(&self) -> &'static [&'static str] {
&["fresh"]
fn tags(&self) -> Tags {
&[tags::FRESH]
}

fn code(&self) -> &'static str {
Expand Down
5 changes: 3 additions & 2 deletions src/rules/fresh_server_event_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use super::{Context, LintRule};
use crate::handler::{Handler, Traverse};
use crate::tags::{self, Tags};

use deno_ast::view::{
Expr, JSXAttrName, JSXAttrValue, JSXElementName, JSXExpr, Program,
Expand All @@ -18,8 +19,8 @@ const HINT: &str =
"Remove this property or turn the enclosing component into an island";

impl LintRule for FreshServerEventHandlers {
fn tags(&self) -> &'static [&'static str] {
&["fresh"]
fn tags(&self) -> Tags {
&[tags::FRESH]
}

fn code(&self) -> &'static str {
Expand Down
5 changes: 3 additions & 2 deletions src/rules/getter_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use super::program_ref;
use super::{Context, LintRule};
use crate::swc_util::StringRepr;
use crate::tags::{self, Tags};
use crate::Program;
use crate::ProgramRef;
use deno_ast::swc::ast::{
Expand Down Expand Up @@ -39,8 +40,8 @@ enum GetterReturnHint {
}

impl LintRule for GetterReturn {
fn tags(&self) -> &'static [&'static str] {
&["recommended"]
fn tags(&self) -> Tags {
&[tags::RECOMMENDED]
}

fn code(&self) -> &'static str {
Expand Down
5 changes: 3 additions & 2 deletions src/rules/jsx_curly_braces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use super::{Context, LintRule};
use crate::diagnostic::{LintFix, LintFixChange};
use crate::handler::{Handler, Traverse};
use crate::tags::{self, Tags};
use crate::Program;
use deno_ast::view::{
Expr, JSXAttr, JSXAttrValue, JSXElement, JSXElementChild, JSXExpr, Lit,
Expand All @@ -16,8 +17,8 @@ pub struct JSXCurlyBraces;
const CODE: &str = "jsx-curly-braces";

impl LintRule for JSXCurlyBraces {
fn tags(&self) -> &'static [&'static str] {
&["recommended", "react", "jsx"]
fn tags(&self) -> Tags {
&[tags::RECOMMENDED, tags::REACT, tags::JSX]
}

fn code(&self) -> &'static str {
Expand Down
5 changes: 3 additions & 2 deletions src/rules/jsx_no_children_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use super::{Context, LintRule};
use crate::handler::{Handler, Traverse};
use crate::tags::{self, Tags};
use crate::Program;
use deno_ast::view::{JSXAttrName, JSXAttrOrSpread, JSXOpeningElement};
use deno_ast::SourceRanged;
Expand All @@ -12,8 +13,8 @@ pub struct JSXNoChildrenProp;
const CODE: &str = "jsx-no-children-prop";

impl LintRule for JSXNoChildrenProp {
fn tags(&self) -> &'static [&'static str] {
&["recommended", "react", "jsx", "fresh"]
fn tags(&self) -> Tags {
&[tags::RECOMMENDED, tags::REACT, tags::JSX, tags::FRESH]
}

fn code(&self) -> &'static str {
Expand Down
5 changes: 3 additions & 2 deletions src/rules/jsx_no_duplicate_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::collections::HashSet;

use super::{Context, LintRule};
use crate::handler::{Handler, Traverse};
use crate::tags::{self, Tags};
use crate::Program;
use deno_ast::view::{JSXAttrName, JSXAttrOrSpread, JSXOpeningElement};
use deno_ast::SourceRanged;
Expand All @@ -14,8 +15,8 @@ pub struct JSXNoDuplicateProps;
const CODE: &str = "jsx-no-duplicate-props";

impl LintRule for JSXNoDuplicateProps {
fn tags(&self) -> &'static [&'static str] {
&["recommended", "react", "jsx"]
fn tags(&self) -> Tags {
&[tags::RECOMMENDED, tags::REACT, tags::JSX]
}

fn code(&self) -> &'static str {
Expand Down
Loading

0 comments on commit 3184b40

Please sign in to comment.