Skip to content

Commit 80a6179

Browse files
authored
feat(lint): change behavior of --rules flag (#27245)
This commit changes how `deno lint --rules` behaves: 1. All available rules are now printed and rules enabled are marked as such 2. `deno lint --rules --json` doesn't include markdown documentation for rules but rather a link to the docs. This should allow us to save around 400kB of the final `deno` binary size
1 parent 563a7c2 commit 80a6179

File tree

5 files changed

+59
-50
lines changed

5 files changed

+59
-50
lines changed

cli/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ deno_doc = { version = "=0.164.0", features = ["rust", "comrak"] }
7474
deno_error.workspace = true
7575
deno_graph = { version = "=0.87.0" }
7676
deno_lib.workspace = true
77-
deno_lint = { version = "=0.68.2", features = ["docs"] }
77+
deno_lint = { version = "=0.68.2" }
7878
deno_lockfile.workspace = true
7979
deno_media_type = { workspace = true, features = ["data_url", "decoding", "module_specifier"] }
8080
deno_npm.workspace = true

cli/tools/lint/mod.rs

+28-19
Original file line numberDiff line numberDiff line change
@@ -458,27 +458,27 @@ fn collect_lint_files(
458458
#[allow(clippy::print_stdout)]
459459
pub fn print_rules_list(json: bool, maybe_rules_tags: Option<Vec<String>>) {
460460
let rule_provider = LintRuleProvider::new(None, None);
461-
let lint_rules = rule_provider
462-
.resolve_lint_rules(
463-
LintRulesConfig {
464-
tags: maybe_rules_tags.clone(),
465-
include: None,
466-
exclude: None,
467-
},
468-
None,
469-
)
470-
.rules;
461+
let all_rules = rule_provider.all_rules();
462+
let configured_rules = rule_provider.resolve_lint_rules(
463+
LintRulesConfig {
464+
tags: maybe_rules_tags.clone(),
465+
include: None,
466+
exclude: None,
467+
},
468+
None,
469+
);
471470

472471
if json {
473472
let json_output = serde_json::json!({
474473
"version": JSON_SCHEMA_VERSION,
475-
"rules": lint_rules
474+
"rules": all_rules
476475
.iter()
477476
.map(|rule| {
477+
// TODO(bartlomieju): print if rule enabled
478478
serde_json::json!({
479479
"code": rule.code(),
480480
"tags": rule.tags(),
481-
"docs": rule.docs(),
481+
"docs": rule.help_docs_url(),
482482
})
483483
})
484484
.collect::<Vec<serde_json::Value>>(),
@@ -488,17 +488,26 @@ pub fn print_rules_list(json: bool, maybe_rules_tags: Option<Vec<String>>) {
488488
// The rules should still be printed even if `--quiet` option is enabled,
489489
// so use `println!` here instead of `info!`.
490490
println!("Available rules:");
491-
for rule in lint_rules.iter() {
492-
print!(" - {}", colors::cyan(rule.code()));
493-
if rule.tags().is_empty() {
494-
println!();
491+
for rule in all_rules.iter() {
492+
// TODO(bartlomieju): this is O(n) search, fix before landing
493+
let enabled = if configured_rules.rules.contains(rule) {
494+
"✓"
495495
} else {
496-
println!(" [{}]", colors::gray(rule.tags().join(", ")))
497-
}
496+
" "
497+
};
498+
println!("- {} {}", rule.code(), colors::green(enabled),);
498499
println!(
499500
"{}",
500-
colors::gray(format!(" help: {}", rule.help_docs_url()))
501+
colors::gray(format!(" help: {}", rule.help_docs_url()))
501502
);
503+
if rule.tags().is_empty() {
504+
println!(" {}", colors::gray("tags:"));
505+
} else {
506+
println!(
507+
" {}",
508+
colors::gray(format!("tags: {}", rule.tags().join(", ")))
509+
);
510+
}
502511
println!();
503512
}
504513
}

cli/tools/lint/rules/mod.rs

+22-24
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ pub trait PackageLintRule: std::fmt::Debug + Send + Sync {
2929
&[]
3030
}
3131

32-
fn docs(&self) -> &'static str;
33-
3432
fn help_docs_url(&self) -> Cow<'static, str>;
3533

3634
fn lint_package(
@@ -64,6 +62,12 @@ enum CliLintRuleKind {
6462
#[derive(Debug)]
6563
pub struct CliLintRule(CliLintRuleKind);
6664

65+
impl PartialEq for CliLintRule {
66+
fn eq(&self, other: &Self) -> bool {
67+
self.code() == other.code()
68+
}
69+
}
70+
6771
impl CliLintRule {
6872
pub fn code(&self) -> &'static str {
6973
use CliLintRuleKind::*;
@@ -83,15 +87,6 @@ impl CliLintRule {
8387
}
8488
}
8589

86-
pub fn docs(&self) -> &'static str {
87-
use CliLintRuleKind::*;
88-
match &self.0 {
89-
DenoLint(rule) => rule.docs(),
90-
Extended(rule) => rule.docs(),
91-
Package(rule) => rule.docs(),
92-
}
93-
}
94-
9590
pub fn help_docs_url(&self) -> Cow<'static, str> {
9691
use CliLintRuleKind::*;
9792
match &self.0 {
@@ -171,11 +166,7 @@ impl LintRuleProvider {
171166
Ok(lint_rules)
172167
}
173168

174-
pub fn resolve_lint_rules(
175-
&self,
176-
rules: LintRulesConfig,
177-
maybe_config_file: Option<&ConfigFile>,
178-
) -> ConfiguredRules {
169+
pub fn all_rules(&self) -> Vec<CliLintRule> {
179170
let deno_lint_rules = deno_lint::rules::get_all_rules();
180171
let cli_lint_rules = vec![CliLintRule(CliLintRuleKind::Extended(
181172
Box::new(no_sloppy_imports::NoSloppyImportsRule::new(
@@ -186,19 +177,26 @@ impl LintRuleProvider {
186177
let cli_graph_rules = vec![CliLintRule(CliLintRuleKind::Package(
187178
Box::new(no_slow_types::NoSlowTypesRule),
188179
))];
189-
let mut all_rule_names = HashSet::with_capacity(
190-
deno_lint_rules.len() + cli_lint_rules.len() + cli_graph_rules.len(),
191-
);
192-
let all_rules = deno_lint_rules
180+
deno_lint_rules
193181
.into_iter()
194182
.map(|rule| CliLintRule(CliLintRuleKind::DenoLint(rule)))
195183
.chain(cli_lint_rules)
196184
.chain(cli_graph_rules)
197-
.inspect(|rule| {
198-
all_rule_names.insert(rule.code());
199-
});
185+
.collect()
186+
}
187+
188+
pub fn resolve_lint_rules(
189+
&self,
190+
rules: LintRulesConfig,
191+
maybe_config_file: Option<&ConfigFile>,
192+
) -> ConfiguredRules {
193+
let all_rules = self.all_rules();
194+
let mut all_rule_names = HashSet::with_capacity(all_rules.len());
195+
for rule in &all_rules {
196+
all_rule_names.insert(rule.code());
197+
}
200198
let rules = filtered_rules(
201-
all_rules,
199+
all_rules.into_iter(),
202200
rules
203201
.tags
204202
.or_else(|| Some(get_default_tags(maybe_config_file))),

cli/tools/lint/rules/no_sloppy_imports.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,10 @@ impl LintRule for NoSloppyImportsRule {
161161
CODE
162162
}
163163

164-
fn docs(&self) -> &'static str {
165-
include_str!("no_sloppy_imports.md")
166-
}
164+
// TODO(bartlomieju): this document needs to be exposed to `https://lint.deno.land`.
165+
// fn docs(&self) -> &'static str {
166+
// include_str!("no_sloppy_imports.md")
167+
// }
167168

168169
fn tags(&self) -> &'static [&'static str] {
169170
&["recommended"]

cli/tools/lint/rules/no_slow_types.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ impl PackageLintRule for NoSlowTypesRule {
2626
&["jsr"]
2727
}
2828

29-
fn docs(&self) -> &'static str {
30-
include_str!("no_slow_types.md")
31-
}
29+
// TODO(bartlomieju): these docs need to be hosted somewhere.
30+
// fn docs(&self) -> &'static str {
31+
// include_str!("no_slow_types.md")
32+
// }
3233

3334
fn help_docs_url(&self) -> Cow<'static, str> {
3435
Cow::Borrowed("https://jsr.io/docs/about-slow-types")

0 commit comments

Comments
 (0)