Skip to content

Commit dd42a64

Browse files
authored
refactor(lint): manage schema files for linter in Deno repo (#27324)
This commit provides schema files for lint rules and lint tags in this repo instead of pulling them from `deno_lint` repository. A unit test was added to ensure all available rules are listed in the schema file. A unit test for tags can be done once #27162 lands.
1 parent 7c8b55b commit dd42a64

File tree

4 files changed

+184
-3
lines changed

4 files changed

+184
-3
lines changed

cli/schemas/config-file.v1.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@
291291
"type": "array",
292292
"description": "List of tag names that will be run. Empty list disables all tags and will only use rules from `include`.",
293293
"items": {
294-
"$ref": "https://raw.githubusercontent.com/denoland/deno_lint/main/schemas/tags.v1.json"
294+
"$ref": "lint-tags.v1.json"
295295
},
296296
"minItems": 0,
297297
"uniqueItems": true
@@ -300,7 +300,7 @@
300300
"type": "array",
301301
"description": "List of rule names that will be excluded from configured tag sets. If the same rule is in `include` it will be run.",
302302
"items": {
303-
"$ref": "https://raw.githubusercontent.com/denoland/deno_lint/main/schemas/rules.v1.json"
303+
"$ref": "lint-rules.v1.json"
304304
},
305305
"minItems": 0,
306306
"uniqueItems": true
@@ -309,7 +309,7 @@
309309
"type": "array",
310310
"description": "List of rule names that will be run. Even if the same rule is in `exclude` it will be run.",
311311
"items": {
312-
"$ref": "https://raw.githubusercontent.com/denoland/deno_lint/main/schemas/rules.v1.json"
312+
"$ref": "lint-rules.v1.json"
313313
},
314314
"minItems": 0,
315315
"uniqueItems": true

cli/schemas/lint-rules.v1.json

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"enum": [
4+
"adjacent-overload-signatures",
5+
"ban-ts-comment",
6+
"ban-types",
7+
"ban-unknown-rule-code",
8+
"ban-untagged-ignore",
9+
"ban-untagged-todo",
10+
"ban-unused-ignore",
11+
"camelcase",
12+
"constructor-super",
13+
"default-param-last",
14+
"eqeqeq",
15+
"explicit-function-return-type",
16+
"explicit-module-boundary-types",
17+
"for-direction",
18+
"fresh-handler-export",
19+
"fresh-server-event-handlers",
20+
"getter-return",
21+
"guard-for-in",
22+
"no-array-constructor",
23+
"no-async-promise-executor",
24+
"no-await-in-loop",
25+
"no-await-in-sync-fn",
26+
"no-boolean-literal-for-arguments",
27+
"no-case-declarations",
28+
"no-class-assign",
29+
"no-compare-neg-zero",
30+
"no-cond-assign",
31+
"no-console",
32+
"no-const-assign",
33+
"no-constant-condition",
34+
"no-control-regex",
35+
"no-debugger",
36+
"no-delete-var",
37+
"no-deprecated-deno-api",
38+
"no-dupe-args",
39+
"no-dupe-class-members",
40+
"no-dupe-else-if",
41+
"no-dupe-keys",
42+
"no-duplicate-case",
43+
"no-empty",
44+
"no-empty-character-class",
45+
"no-empty-enum",
46+
"no-empty-interface",
47+
"no-empty-pattern",
48+
"no-eval",
49+
"no-ex-assign",
50+
"no-explicit-any",
51+
"no-external-import",
52+
"no-extra-boolean-cast",
53+
"no-extra-non-null-assertion",
54+
"no-fallthrough",
55+
"no-func-assign",
56+
"no-global-assign",
57+
"no-implicit-declare-namespace-export",
58+
"no-import-assertions",
59+
"no-import-assign",
60+
"no-inferrable-types",
61+
"no-inner-declarations",
62+
"no-invalid-regexp",
63+
"no-invalid-triple-slash-reference",
64+
"no-irregular-whitespace",
65+
"no-misused-new",
66+
"no-namespace",
67+
"no-new-symbol",
68+
"no-node-globals",
69+
"no-non-null-asserted-optional-chain",
70+
"no-non-null-assertion",
71+
"no-obj-calls",
72+
"no-octal",
73+
"no-process-globals",
74+
"no-prototype-builtins",
75+
"no-redeclare",
76+
"no-regex-spaces",
77+
"no-self-assign",
78+
"no-self-compare",
79+
"no-setter-return",
80+
"no-shadow-restricted-names",
81+
"no-sloppy-imports",
82+
"no-slow-types",
83+
"no-sparse-arrays",
84+
"no-sync-fn-in-async-fn",
85+
"no-this-alias",
86+
"no-this-before-super",
87+
"no-throw-literal",
88+
"no-top-level-await",
89+
"no-undef",
90+
"no-unreachable",
91+
"no-unsafe-finally",
92+
"no-unsafe-negation",
93+
"no-unused-labels",
94+
"no-unused-vars",
95+
"no-var",
96+
"no-window",
97+
"no-window-prefix",
98+
"no-with",
99+
"prefer-as-const",
100+
"prefer-ascii",
101+
"prefer-const",
102+
"prefer-namespace-keyword",
103+
"prefer-primordials",
104+
"require-await",
105+
"require-yield",
106+
"single-var-declarator",
107+
"triple-slash-reference",
108+
"use-isnan",
109+
"valid-typeof",
110+
"verbatim-module-syntax"
111+
]
112+
}

cli/schemas/lint-tags.v1.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"enum": ["fresh", "jsr", "jsx", "react", "recommended"]
4+
}

cli/tools/lint/mod.rs

+65
Original file line numberDiff line numberDiff line change
@@ -556,3 +556,68 @@ struct LintError {
556556
file_path: String,
557557
message: String,
558558
}
559+
560+
#[cfg(test)]
561+
mod tests {
562+
use super::*;
563+
use pretty_assertions::assert_eq;
564+
use serde::Deserialize;
565+
use test_util as util;
566+
567+
#[derive(Serialize, Deserialize)]
568+
struct RulesSchema {
569+
#[serde(rename = "$schema")]
570+
schema: String,
571+
572+
#[serde(rename = "enum")]
573+
rules: Vec<String>,
574+
}
575+
576+
fn get_all_rules() -> Vec<String> {
577+
let rule_provider = LintRuleProvider::new(None, None);
578+
let configured_rules =
579+
rule_provider.resolve_lint_rules(Default::default(), None);
580+
let mut all_rules = configured_rules
581+
.all_rule_codes
582+
.into_iter()
583+
.map(|s| s.to_string())
584+
.collect::<Vec<String>>();
585+
all_rules.sort();
586+
587+
all_rules
588+
}
589+
590+
// TODO(bartlomieju): do the same for tags, once https://github.com/denoland/deno/pull/27162 lands
591+
#[test]
592+
fn all_lint_rules_are_listed_in_schema_file() {
593+
let all_rules = get_all_rules();
594+
595+
let rules_schema_path =
596+
util::root_path().join("cli/schemas/lint-rules.v1.json");
597+
let rules_schema_file =
598+
std::fs::read_to_string(&rules_schema_path).unwrap();
599+
600+
let schema: RulesSchema = serde_json::from_str(&rules_schema_file).unwrap();
601+
602+
const UPDATE_ENV_VAR_NAME: &str = "UPDATE_EXPECTED";
603+
604+
if std::env::var(UPDATE_ENV_VAR_NAME).ok().is_none() {
605+
assert_eq!(
606+
schema.rules, all_rules,
607+
"Lint rules schema file not up to date. Run again with {}=1 to update the expected output",
608+
UPDATE_ENV_VAR_NAME
609+
);
610+
return;
611+
}
612+
613+
std::fs::write(
614+
&rules_schema_path,
615+
serde_json::to_string_pretty(&RulesSchema {
616+
schema: schema.schema,
617+
rules: all_rules,
618+
})
619+
.unwrap(),
620+
)
621+
.unwrap();
622+
}
623+
}

0 commit comments

Comments
 (0)