fallow-engine 3.48.0

Typed analysis engine facade for fallow consumers
Documentation
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Config actions for unused exports that a file edit cannot fix well.
//!
//! - A generated file: its generator writes the file again, so a removed
//!   export comes back. The finding keeps its `remove-export` action, but
//!   that action is not auto-fixable, and the inline suppression is dropped,
//!   because the generator removes the comment too.
//! - A shadcn/ui component folder (the `ui` alias of `components.json`): the
//!   files are copied as complete sets, and many projects keep each named
//!   part before it has a consumer.
//!
//! Each such finding gets an `add-to-config` action with the `ignoreExports`
//! rule that hides it. The findings stay in the report. The human report
//! prints one hint per group from these actions.

use std::io::Read;
use std::path::{Path, PathBuf};

use fallow_config::{MergedTsconfig, ResolvedConfig};
use fallow_types::output::{
    AddToConfigAction, AddToConfigKind, AddToConfigValue, GENERATED_FILE_EXPORTS_HINT,
    IgnoreExportsRule, IssueAction, UI_FOLDER_EXPORTS_HINT,
};
use fallow_types::output_dead_code::IGNORE_EXPORTS_VALUE_SCHEMA;
use fallow_types::results::AnalysisResults;
use rustc_hash::FxHashMap;
use serde_json::Value;

/// The header length that the generated-file check needs.
const HEADER_BYTES: u64 = 4_096;

const COMPONENTS_FILE: &str = "components.json";

/// The tsconfig files that can hold the path alias of the `ui` folder.
const TSCONFIG_FILES: &[&str] = &["tsconfig.json", "tsconfig.app.json", "jsconfig.json"];

/// Add the config action to each unused-export and unused-type finding in a
/// generated file, and to each unused-export finding in a shadcn/ui folder.
///
/// The pass can run more than one time on the same results, and another pass
/// can set the fix action of a finding between two runs. So each run sets the
/// actions of a generated file again, and adds a config action only when the
/// finding has none.
pub fn add_unused_export_config_actions(results: &mut AnalysisResults, config: &ResolvedConfig) {
    if results.unused_exports.is_empty() && results.unused_types.is_empty() {
        return;
    }
    let mut files = FileFacts {
        root: config.root.as_path(),
        generated: FxHashMap::default(),
        ui_folders: FxHashMap::default(),
    };
    for finding in &mut results.unused_exports {
        let path = finding.export.path.clone();
        if files.is_generated(&path) {
            mark_generated(&mut finding.actions, &files.relative(&path));
        } else if !has_ignore_exports_action(&finding.actions)
            && let Some(folder) = files.ui_folder(&path)
        {
            let pattern = format!("{}/**", forward_slashes(&folder));
            finding
                .actions
                .push(ignore_exports_action(UI_FOLDER_EXPORTS_HINT, pattern));
        }
    }
    for finding in &mut results.unused_types {
        let path = finding.export.path.clone();
        if files.is_generated(&path) {
            mark_generated(&mut finding.actions, &files.relative(&path));
        }
    }
}

/// What the pass knows about each file and folder, read one time.
struct FileFacts<'a> {
    root: &'a Path,
    generated: FxHashMap<PathBuf, bool>,
    ui_folders: FxHashMap<PathBuf, Option<PathBuf>>,
}

impl FileFacts<'_> {
    fn relative(&self, path: &Path) -> String {
        forward_slashes(path.strip_prefix(self.root).unwrap_or(path))
    }

    fn is_generated(&mut self, path: &Path) -> bool {
        let Ok(relative) = path.strip_prefix(self.root) else {
            return false;
        };
        *self
            .generated
            .entry(path.to_path_buf())
            .or_insert_with(|| is_generated_file(relative, path))
    }

    fn ui_folder(&mut self, path: &Path) -> Option<PathBuf> {
        ui_folder_of(self.root, path, &mut self.ui_folders)
    }
}

fn has_ignore_exports_action(actions: &[IssueAction]) -> bool {
    actions.iter().any(
        |action| matches!(action, IssueAction::AddToConfig(add) if add.config_key == "ignoreExports"),
    )
}

/// Set the actions of a finding in a generated file: the code fix is not
/// auto-fixable, the inline suppression is gone, and the config action is
/// present one time.
fn mark_generated(actions: &mut Vec<IssueAction>, file: &str) {
    actions.retain(|action| !matches!(action, IssueAction::SuppressLine(_)));
    for action in actions.iter_mut() {
        if let IssueAction::Fix(fix) = action {
            fix.auto_fixable = false;
            fix.note = Some(GENERATED_FILE_EXPORTS_HINT.to_string());
        }
    }
    if !has_ignore_exports_action(actions) {
        actions.push(ignore_exports_action(
            GENERATED_FILE_EXPORTS_HINT,
            file.to_string(),
        ));
    }
}

fn ignore_exports_action(description: &str, file: String) -> IssueAction {
    IssueAction::AddToConfig(AddToConfigAction {
        kind: AddToConfigKind::AddToConfig,
        auto_fixable: false,
        description: description.to_string(),
        config_key: "ignoreExports".to_string(),
        value: AddToConfigValue::ExportsRules(vec![IgnoreExportsRule {
            file,
            exports: vec!["*".to_string()],
        }]),
        value_schema: Some(IGNORE_EXPORTS_VALUE_SCHEMA.to_string()),
    })
}

fn forward_slashes(path: &Path) -> String {
    path.to_string_lossy().replace('\\', "/")
}

/// Whether the file is generated code, from its path below the project root
/// and from the start of its text.
fn is_generated_file(relative: &Path, path: &Path) -> bool {
    let mut header = String::new();
    let read = std::fs::File::open(path)
        .map(|file| file.take(HEADER_BYTES))
        .and_then(|mut head| {
            let mut bytes = Vec::new();
            head.read_to_end(&mut bytes).map(|_| bytes)
        });
    if let Ok(bytes) = read {
        header = String::from_utf8_lossy(&bytes).into_owned();
    }
    crate::source::is_generated_file(relative, &header)
}

/// The shadcn/ui folder that holds `path`, relative to the project root.
///
/// The nearest `components.json` at or above the file, up to the project
/// root, gives the folder. `folders` keeps the answer for each directory
/// that holds, or does not hold, such a file.
fn ui_folder_of(
    root: &Path,
    path: &Path,
    folders: &mut FxHashMap<PathBuf, Option<PathBuf>>,
) -> Option<PathBuf> {
    let mut current = path.parent();
    while let Some(dir) = current {
        if !dir.starts_with(root) {
            return None;
        }
        let folder = folders
            .entry(dir.to_path_buf())
            .or_insert_with(|| ui_folder_from_components_file(dir));
        if let Some(folder) = folder {
            return path
                .starts_with(&*folder)
                .then(|| folder.strip_prefix(root).ok().map(Path::to_path_buf))
                .flatten();
        }
        if dir == root {
            return None;
        }
        current = dir.parent();
    }
    None
}

/// The absolute `ui` folder of the `components.json` in `dir`, when the file
/// is a shadcn/ui config and its `ui` alias names an existing folder.
fn ui_folder_from_components_file(dir: &Path) -> Option<PathBuf> {
    let components = read_jsonc(&dir.join(COMPONENTS_FILE))?;
    alias_folder(dir, &ui_alias(&components)?)
}

/// The import path of the `ui` folder: `aliases.ui`, or `aliases.components`
/// with `/ui`. Only a file with the shadcn/ui schema URL counts.
fn ui_alias(components: &Value) -> Option<String> {
    let is_shadcn = components
        .get("$schema")
        .and_then(Value::as_str)
        .is_some_and(|schema| schema.contains("ui.shadcn.com"));
    if !is_shadcn {
        return None;
    }
    let aliases = components.get("aliases")?;
    let ui = aliases.get("ui").and_then(Value::as_str);
    let components = aliases.get("components").and_then(Value::as_str);
    ui.map(str::to_string)
        .or_else(|| components.map(|components| format!("{components}/ui")))
}

/// Resolve an import path such as `~/components/ui` to an existing folder,
/// through the `paths` of a tsconfig file in `dir`. Like TypeScript, the
/// pattern with the longest prefix wins. A pattern whose target is not a
/// folder gives way to the next pattern.
fn alias_folder(dir: &Path, alias: &str) -> Option<PathBuf> {
    TSCONFIG_FILES.iter().find_map(|name| {
        let tsconfig = MergedTsconfig::read(&dir.join(name))?;
        let paths = tsconfig.paths()?;
        let mut candidates: Vec<(usize, PathBuf)> = paths
            .patterns
            .iter()
            .filter_map(|(pattern, targets)| {
                let prefix = pattern.strip_suffix('*')?;
                let rest = alias.strip_prefix(prefix)?;
                let target = targets.as_array()?.first()?.as_str()?.replace('*', rest);
                Some((prefix.len(), target))
            })
            .flat_map(|(prefix_len, target)| {
                paths
                    .base_dirs()
                    .map(move |base| (prefix_len, normalize(&base.join(&target))))
                    .collect::<Vec<_>>()
            })
            .collect();
        // The longest prefix wins. A stable sort keeps the `baseUrl` folder
        // before the `paths` config folder for one pattern.
        candidates.sort_by_key(|candidate| std::cmp::Reverse(candidate.0));
        candidates
            .into_iter()
            .map(|(_, folder)| folder)
            .find(|folder| folder.is_dir())
    })
}

/// Remove the `.` and `..` components of a path without a file system read.
fn normalize(path: &Path) -> PathBuf {
    let mut normalized = PathBuf::new();
    for component in path.components() {
        match component {
            std::path::Component::CurDir => {}
            std::path::Component::ParentDir => {
                normalized.pop();
            }
            other => normalized.push(other),
        }
    }
    normalized
}

fn read_jsonc(path: &Path) -> Option<Value> {
    let content = std::fs::read_to_string(path).ok()?;
    fallow_config::jsonc::parse_to_value(content.trim_start_matches('\u{FEFF}')).ok()
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn the_ui_alias_needs_the_shadcn_schema() {
        let shadcn = json!({
            "$schema": "https://ui.shadcn.com/schema.json",
            "aliases": { "components": "@/components", "ui": "~/components/ui" },
        });
        assert_eq!(ui_alias(&shadcn).as_deref(), Some("~/components/ui"));

        let without_ui = json!({
            "$schema": "https://ui.shadcn.com/schema.json",
            "aliases": { "components": "@/components" },
        });
        assert_eq!(ui_alias(&without_ui).as_deref(), Some("@/components/ui"));

        let other = json!({ "aliases": { "ui": "~/components/ui" } });
        assert_eq!(ui_alias(&other), None);
    }

    #[test]
    fn an_alias_resolves_through_the_longest_tsconfig_path_that_is_a_folder() {
        let project = tempfile::tempdir().expect("temp dir");
        let dir = project.path();
        std::fs::create_dir_all(dir.join("src/components/ui")).expect("ui folder");
        std::fs::write(
            dir.join("tsconfig.json"),
            r#"{
              // The app aliases.
              "compilerOptions": {
                "paths": { "@/*": ["./*"], "@/components/*": ["./src/components/*"] },
              },
            }"#,
        )
        .expect("tsconfig");

        assert_eq!(
            alias_folder(dir, "@/components/ui"),
            Some(dir.join("src/components/ui")),
            "the longer pattern wins"
        );
        assert_eq!(
            alias_folder(dir, "@/src/components/ui"),
            Some(dir.join("src/components/ui")),
            "the short pattern applies when no longer pattern matches"
        );
        assert_eq!(alias_folder(dir, "@workspace/ui/components"), None);
        assert_eq!(alias_folder(dir, "@/missing"), None);
    }

    #[test]
    fn an_alias_resolves_through_paths_that_the_tsconfig_extends() {
        let project = tempfile::tempdir().expect("temp dir");
        let dir = project.path();
        std::fs::create_dir_all(dir.join("src/components/ui")).expect("ui folder");
        std::fs::write(
            dir.join("tsconfig.base.json"),
            r#"{"compilerOptions":{"baseUrl":".","paths":{"@/*":["./src/*"]}}}"#,
        )
        .expect("base tsconfig");
        std::fs::write(
            dir.join("tsconfig.json"),
            r#"{"extends":"./tsconfig.base.json"}"#,
        )
        .expect("tsconfig");

        assert_eq!(
            alias_folder(dir, "@/components/ui"),
            Some(dir.join("src/components/ui"))
        );
    }

    #[test]
    fn an_alias_folder_under_base_url_wins_over_the_paths_config_folder() {
        let project = tempfile::tempdir().expect("temp dir");
        let root = project.path();
        let app = root.join("app");
        for folder in [
            root.join("src/components/ui"),
            app.join("src/components/ui"),
        ] {
            std::fs::create_dir_all(folder).expect("ui folder");
        }
        std::fs::write(
            root.join("base.json"),
            r#"{"compilerOptions":{"baseUrl":"."}}"#,
        )
        .expect("base tsconfig");
        std::fs::write(
            app.join("tsconfig.json"),
            r#"{"extends":"../base.json","compilerOptions":{"paths":{"@/*":["./src/*"]}}}"#,
        )
        .expect("tsconfig");

        assert_eq!(
            alias_folder(&app, "@/components/ui"),
            Some(root.join("src/components/ui"))
        );
    }

    fn remove_export_actions() -> Vec<IssueAction> {
        use fallow_types::output_dead_code::UnusedExportFinding;
        use fallow_types::results::UnusedExport;
        UnusedExportFinding::with_actions(UnusedExport {
            path: PathBuf::from("src/a.graphql.ts"),
            export_name: "operation".to_string(),
            is_type_only: false,
            line: 1,
            col: 0,
            span_start: 0,
            is_re_export: false,
            deprecated: false,
            deprecated_reason: None,
        })
        .actions
    }

    #[test]
    fn a_generated_finding_stays_not_auto_fixable_after_another_pass_sets_the_fix() {
        let mut actions = remove_export_actions();
        mark_generated(&mut actions, "src/a.graphql.ts");
        assert_eq!(actions.len(), 2, "the fix and the config action");
        assert!(!actions.iter().any(IssueAction::is_auto_fixable));

        // The type-aware pass can set the fix action of the finding again.
        if let IssueAction::Fix(fix) = &mut actions[0] {
            fix.auto_fixable = true;
            fix.note = None;
        }
        mark_generated(&mut actions, "src/a.graphql.ts");

        assert_eq!(
            actions.len(),
            2,
            "a second run adds no second config action"
        );
        assert!(!actions.iter().any(IssueAction::is_auto_fixable));
    }
}