Skip to content

Commit 115b33b

Browse files
authored
linter: improve new rule script (#454)
1 parent 7fb4208 commit 115b33b

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/xtask/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ enum-iterator.workspace = true
1414
reqwest = { version = "0.12.9", features = ["blocking", "json"] }
1515
serde.workspace = true
1616
convert_case.workspace = true
17+
camino.workspace = true
1718

1819
[lints]
1920
workspace = true

crates/xtask/src/new_rule.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
use anyhow::Result;
2+
use camino::Utf8PathBuf;
23
use convert_case::{Case, Casing};
34

45
use crate::NewRuleArgs;
5-
use std::{env, fs, path::PathBuf};
6+
use std::{env, fs};
67

78
fn make_lint(name: &str) -> String {
9+
let rule_name_snake = name.to_case(Case::Snake);
10+
let rule_name_pascal = name.to_case(Case::Pascal);
811
format!(
912
r###"
1013
use squawk_syntax::{{
@@ -14,11 +17,20 @@ use squawk_syntax::{{
1417
1518
use crate::{{Linter, Violation, Rule}};
1619
17-
pub(crate) fn {rule_name}(ctx: &mut Linter, parse: &Parse<SourceFile>) {{
20+
pub(crate) fn {rule_name_snake}(ctx: &mut Linter, parse: &Parse<SourceFile>) {{
1821
let file = parse.tree();
1922
for item in file.items() {{
20-
todo!();
2123
match item {{
24+
// TODO: update to the item you want to check
25+
ast::Item::CreateTable(create_table) => {{
26+
ctx.report(Violation::new(
27+
Rule::{rule_name_pascal},
28+
"todo".to_string(),
29+
create_table.syntax().text_range(),
30+
"todo or none".to_string(),
31+
));
32+
todo!();
33+
}}
2234
_ => (),
2335
}}
2436
}}
@@ -54,15 +66,13 @@ mod test {{
5466
assert_eq!(errors.len(), 0);
5567
}}
5668
}}
57-
"###,
58-
rule_name = name,
59-
rule_name_pascal = name.to_case(Case::Pascal),
69+
"###
6070
)
6171
}
6272

63-
fn root_path() -> PathBuf {
64-
let binding = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
65-
PathBuf::from(binding.parent().unwrap().parent().unwrap())
73+
fn root_path() -> Utf8PathBuf {
74+
let binding = Utf8PathBuf::from(env!("CARGO_MANIFEST_DIR"));
75+
Utf8PathBuf::from(binding.parent().unwrap().parent().unwrap())
6676
}
6777

6878
fn create_rule_file(name: &str) -> Result<()> {
@@ -71,13 +81,13 @@ fn create_rule_file(name: &str) -> Result<()> {
7181
let lint_path = root.join(format!("crates/squawk_linter/src/rules/{}.rs", name));
7282

7383
if fs::exists(&lint_path)? {
74-
println!("skipping rule definition file creation, it already exists");
84+
println!("skipping rule file creation, it already exists {lint_path}");
7585
return Ok(());
7686
}
7787

7888
let lint_data = make_lint(&name);
7989
fs::write(&lint_path, lint_data)?;
80-
println!("created rule file");
90+
println!("created rule file {lint_path}");
8191
Ok(())
8292
}
8393

@@ -211,12 +221,12 @@ fn docs_create_rule(name: &str) -> Result<()> {
211221
let name_kebab = name.to_case(Case::Kebab);
212222
let rule_doc_path = docs.join(format!("docs/{name_kebab}.md"));
213223
if fs::exists(&rule_doc_path)? {
214-
println!("skipping rule doc file creation, it already exists");
224+
println!("skipping rule doc file creation, it already exists {rule_doc_path}");
215225
return Ok(());
216226
}
217-
let doc = make_doc(&name);
227+
let doc = make_doc(name);
218228
fs::write(&rule_doc_path, doc)?;
219-
println!("created rule doc");
229+
println!("created rule doc {rule_doc_path}");
220230
Ok(())
221231
}
222232

0 commit comments

Comments
 (0)