aboutsummaryrefslogtreecommitdiff
path: root/mingling_cli/pre/lint_registry.rs
blob: e69592b71b838fbc960954605f8cef9cba21d311 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use std::{fs, io::Error};

use just_template::{Template, tmpl};

/// Generate lint module registry file (src/lints/mod.rs)
///
/// Read all Rust source files in the src/lints/ directory (excluding mod.rs itself),
/// automatically generate module declarations and pub use export statements, and write them to mod.rs.
pub fn gen_mod_file() -> Result<(), Error> {
    let root = std::env::current_dir()?;
    let lints_dir = root.join("src").join("lints");
    let tmpl_file = root.join("tmpls").join("lints.tmpl");
    let mod_file = root.join("src").join("lints.rs");

    let mut template = Template::from(fs::read_to_string(tmpl_file).unwrap());

    // Collect all .rs file names (without extension), excluding mod
    let mut entries: Vec<String> = fs::read_dir(&lints_dir)?
        .filter_map(|e| e.ok())
        .filter(|e| e.path().is_file())
        .filter_map(|e| {
            e.path()
                .file_stem()
                .and_then(|s| s.to_str())
                .map(|s| s.to_string())
        })
        .filter(|name| name != "mod")
        .collect();

    entries.sort();

    // Generate module declarations and re-export statements
    for name in &entries {
        tmpl!(template, impls {
            mod_name = name
        })
    }

    // Generate run_all_lints call arms
    for name in &entries {
        tmpl!(template, calls {
            name = name
        })
    }

    // Generate file-level lint calls (lints that have `pub fn check_file`)
    for name in &entries {
        let file_path = lints_dir.join(format!("{name}.rs"));
        let has_check_file = fs::read_to_string(&file_path)
            .map(|c| c.contains("pub fn check_file("))
            .unwrap_or(false);
        if has_check_file {
            tmpl!(template, file_calls {
                name = name
            });
            tmpl!(template, file_lints {
                name = name
            });
        }
    }

    fs::write(&mod_file, template.to_string())?;

    Ok(())
}

/// Generate lint metadata registry
///
/// Parses each `.rs` file in `src/lints/` (excluding mod.rs and _init.rs),
/// extracts doc-comment metadata, and writes the result as JSON.
pub fn gen_lint_registry() -> Result<(), Error> {
    let root = std::env::current_dir()?;
    let lints_dir = root.join("src").join("lints");

    let mut lints: Vec<serde_json::Value> = Vec::new();

    for entry in fs::read_dir(&lints_dir)? {
        let entry = entry?;
        let path = entry.path();
        if !path.is_file() || path.extension().and_then(|e| e.to_str()) != Some("rs") {
            continue;
        }
        let stem = match path.file_stem().and_then(|s| s.to_str()) {
            Some(s) => s.to_string(),
            None => continue,
        };
        if stem == "mod" {
            continue;
        }

        let content = fs::read_to_string(&path)?;
        if let Some(meta) = parse_lint_file(&content, &stem) {
            lints.push(meta);
        }
    }

    lints.sort_by(|a, b| {
        a["name"]
            .as_str()
            .unwrap_or("")
            .cmp(b["name"].as_str().unwrap_or(""))
    });

    let json = serde_json::json!({ "lints": lints });
    let out_path = root.join("registry.json");
    fs::write(&out_path, serde_json::to_string_pretty(&json).unwrap())?;
    Ok(())
}

/// Parse a single lint `.rs` file and return its metadata as JSON.
fn parse_lint_file(content: &str, name: &str) -> Option<serde_json::Value> {
    // Collect leading doc comment lines (//!)
    let mut doc_lines: Vec<String> = Vec::new();
    for line in content.lines() {
        let trimmed = line.trim();
        if let Some(rest) = trimmed.strip_prefix("//!") {
            doc_lines.push(rest.trim().to_string());
        } else if !doc_lines.is_empty() {
            break;
        }
    }

    // Title: first non-empty doc line
    let title = doc_lines
        .iter()
        .find(|l| !l.is_empty())
        .cloned()
        .unwrap_or_default();

    // Summary: between ## Summary and ## Metadata
    let mut summary = String::new();
    let mut in_summary = false;
    for line in &doc_lines {
        if line.contains("## Summary") {
            in_summary = true;
            continue;
        }
        if line.contains("## Metadata") {
            in_summary = false;
        }
        if in_summary && !line.starts_with("## ") {
            if !summary.is_empty() {
                summary.push('\n');
            }
            summary.push_str(line.trim());
        }
    }

    // Metadata section
    let mut author = String::new();
    let mut default_level = String::from("warn");
    let mut active_on = String::from("File");
    let mut in_metadata = false;

    for line in &doc_lines {
        if line.contains("## Metadata") {
            in_metadata = true;
            continue;
        }
        if in_metadata {
            if line.starts_with("## ") {
                break;
            }
            if let Some(val) = line
                .strip_prefix("Author:")
                .or_else(|| line.strip_prefix("Author:"))
            {
                author = val.trim().trim_matches('`').to_string();
            }
            if let Some(val) = line
                .strip_prefix("Default:")
                .or_else(|| line.strip_prefix("Default:"))
            {
                let raw = val.trim().trim_matches('`');
                if raw == "warn" || raw == "allow" || raw == "deny" {
                    default_level = raw.to_string();
                }
            }
        }
    }

    // Extract active_on from function signature (last occurrence = actual fn)
    if let Some(pos) = content.rfind("pub fn linter(") {
        let after = &content[pos..];
        if let Some(colon) = after.find(':') {
            let type_part = &after[colon + 1..].trim();
            let raw = type_part
                .trim_start_matches("syn::")
                .split([' ', ')', ',', '\n'])
                .next()
                .unwrap_or("File");
            // Strip "Item" prefix: ItemFn → Fn, ItemStruct → Struct, etc.
            active_on = raw.strip_prefix("Item").unwrap_or(raw).to_string();
        }
    }

    let mut meta = serde_json::Map::new();
    meta.insert("author".into(), serde_json::Value::String(author));
    meta.insert("default".into(), serde_json::Value::String(default_level));
    meta.insert("active_on".into(), serde_json::Value::String(active_on));

    Some(serde_json::json!({
        "name": name,
        "title": title,
        "summary": summary.trim(),
        "metadata": meta,
    }))
}