tsz-cli 0.1.9

CLI binaries for the tsz TypeScript compiler
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
use anyhow::{Context, Result, bail};
use globset::{Glob, GlobSet, GlobSetBuilder};
use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
use walkdir::{DirEntry, WalkDir};

use crate::config::TsConfig;

pub(crate) const DEFAULT_EXCLUDES: [&str; 3] =
    ["node_modules", "bower_components", "jspm_packages"];

#[derive(Debug, Clone)]
pub struct FileDiscoveryOptions {
    pub base_dir: PathBuf,
    pub files: Vec<PathBuf>,
    pub include: Option<Vec<String>>,
    pub exclude: Option<Vec<String>>,
    pub out_dir: Option<PathBuf>,
    pub follow_links: bool,
    pub allow_js: bool,
}

impl FileDiscoveryOptions {
    pub fn from_tsconfig(config_path: &Path, config: &TsConfig, out_dir: Option<&Path>) -> Self {
        let base_dir = config_path
            .parent()
            .map_or_else(|| PathBuf::from("."), Path::to_path_buf);

        let files = config
            .files
            .as_ref()
            .map(|list| list.iter().map(PathBuf::from).collect())
            .unwrap_or_default();

        Self {
            base_dir,
            files,
            include: config.include.clone(),
            exclude: config.exclude.clone(),
            out_dir: out_dir.map(Path::to_path_buf),
            follow_links: false,
            allow_js: false,
        }
    }
}

pub fn discover_ts_files(options: &FileDiscoveryOptions) -> Result<Vec<PathBuf>> {
    let mut files = BTreeSet::new();

    for file in &options.files {
        let path = resolve_file_path(&options.base_dir, file);
        ensure_file_exists(&path)?;
        // Explicitly listed files (from CLI positional args or tsconfig "files" array)
        // are always compiled, including .js/.jsx/.mjs/.cjs files, regardless of
        // the allowJs setting. This matches tsc behavior where allowJs only controls
        // pattern-matched file discovery (include/exclude), not explicit file lists.
        if is_ts_file(&path) || is_js_file(&path) {
            files.insert(path);
        }
    }

    let include_patterns = build_include_patterns(options);
    if !include_patterns.is_empty() {
        let include_set =
            build_globset(&include_patterns).context("failed to build include globset")?;
        let exclude_patterns = build_exclude_patterns(options);
        let exclude_set = if exclude_patterns.is_empty() {
            None
        } else {
            Some(build_globset(&exclude_patterns).context("failed to build exclude globset")?)
        };

        let walker = WalkDir::new(&options.base_dir)
            .follow_links(options.follow_links)
            .into_iter()
            .filter_entry(|entry| allow_entry(entry, &options.base_dir, exclude_set.as_ref()));

        for entry in walker {
            let entry = entry.context("failed to read directory entry")?;
            if !entry.file_type().is_file() {
                continue;
            }

            let path = entry.path();
            if !(is_ts_file(path) || (options.allow_js && is_js_file(path))) {
                continue;
            }

            let rel_path = path.strip_prefix(&options.base_dir).unwrap_or(path);
            if !include_set.is_match(rel_path) {
                continue;
            }

            if let Some(exclude) = exclude_set.as_ref()
                && exclude.is_match(rel_path)
            {
                continue;
            }

            // Avoid canonicalizing unless following links; canonicalizing can change
            // the base prefix (e.g., /var -> /private/var on macOS) which breaks
            // relative path expectations in the CLI.
            let resolved = if options.follow_links {
                std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf())
            } else {
                path.to_path_buf()
            };
            files.insert(resolved);
        }
    }

    let mut list: Vec<PathBuf> = files.into_iter().collect();
    list.sort_by(|a, b| a.to_string_lossy().cmp(&b.to_string_lossy()));
    Ok(list)
}

fn build_include_patterns(options: &FileDiscoveryOptions) -> Vec<String> {
    match options.include.as_ref() {
        Some(patterns) if patterns.is_empty() => Vec::new(),
        Some(patterns) => expand_include_patterns(&normalize_patterns(patterns)),
        None => {
            if options.files.is_empty() {
                vec!["**/*".to_string()]
            } else {
                Vec::new()
            }
        }
    }
}

/// Expand include patterns to match files in directories.
///
/// TypeScript's include patterns work as follows:
/// - `src` matches `src/` directory and expands to `src/**/*`
/// - `src/*` matches files directly in src, but for directories, adds `/**/*`
/// - Patterns with extensions (e.g., `*.ts`) are used as-is
fn expand_include_patterns(patterns: &[String]) -> Vec<String> {
    let mut expanded = Vec::new();
    for pattern in patterns {
        // If pattern already has glob metacharacters with extensions, use as-is
        if pattern.ends_with(".ts")
            || pattern.ends_with(".tsx")
            || pattern.ends_with(".js")
            || pattern.ends_with(".jsx")
            || pattern.ends_with(".mts")
            || pattern.ends_with(".cts")
        {
            expanded.push(pattern.clone());
            continue;
        }

        // If pattern ends with /**/* or /**/*.*, it's already expanded
        if pattern.ends_with("/**/*") || pattern.ends_with("/**/*.*") {
            expanded.push(pattern.clone());
            continue;
        }

        // Directory pattern (no extension or glob at end) - expand to match all files
        let base = pattern.trim_end_matches('/');
        expanded.push(format!("{base}/**/*"));
    }
    expanded
}

fn build_exclude_patterns(options: &FileDiscoveryOptions) -> Vec<String> {
    let mut patterns = match options.exclude.as_ref() {
        Some(patterns) => normalize_patterns(patterns),
        None => normalize_patterns(
            &DEFAULT_EXCLUDES
                .iter()
                .map(std::string::ToString::to_string)
                .collect::<Vec<_>>(),
        ),
    };

    if options.exclude.is_none()
        && let Some(out_dir) = options.out_dir.as_ref()
        && let Some(out_pattern) = path_to_pattern(&options.base_dir, out_dir)
    {
        patterns.push(out_pattern);
    }

    expand_exclude_patterns(&patterns)
}

fn normalize_patterns(patterns: &[String]) -> Vec<String> {
    patterns
        .iter()
        .filter_map(|pattern| {
            let trimmed = pattern.trim();
            if trimmed.is_empty() {
                return None;
            }
            // Normalize path separators and strip leading "./" prefix
            // TypeScript treats "./**/*.ts" the same as "**/*.ts"
            let normalized = trimmed.replace('\\', "/");
            let stripped = normalized.strip_prefix("./").unwrap_or(&normalized);
            Some(stripped.to_string())
        })
        .collect()
}

fn expand_exclude_patterns(patterns: &[String]) -> Vec<String> {
    let mut expanded = Vec::new();
    for pattern in patterns {
        expanded.push(pattern.clone());
        if !contains_glob_meta(pattern) && !pattern.ends_with("/**") {
            expanded.push(format!("{}/**", pattern.trim_end_matches('/')));
        }
    }
    expanded
}

fn contains_glob_meta(pattern: &str) -> bool {
    pattern.contains('*') || pattern.contains('?') || pattern.contains('[') || pattern.contains(']')
}

fn build_globset(patterns: &[String]) -> Result<GlobSet> {
    let mut builder = GlobSetBuilder::new();
    for pattern in patterns {
        let glob =
            Glob::new(pattern).with_context(|| format!("invalid glob pattern '{pattern}'"))?;
        builder.add(glob);
    }

    Ok(builder.build()?)
}

fn allow_entry(entry: &DirEntry, base_dir: &Path, exclude: Option<&GlobSet>) -> bool {
    let Some(exclude) = exclude else {
        return true;
    };

    let path = entry.path();
    if path == base_dir {
        return true;
    }

    // Use safe path handling instead of unwrap_or for panic hardening
    let rel_path = match path.strip_prefix(base_dir) {
        Ok(stripped) => stripped,
        Err(_) => {
            // If path is not under base_dir, use the path itself for matching
            return !exclude.is_match(path);
        }
    };
    !exclude.is_match(rel_path)
}

fn resolve_file_path(base_dir: &Path, file: &Path) -> PathBuf {
    if file.is_absolute() {
        file.to_path_buf()
    } else {
        base_dir.join(file)
    }
}

fn ensure_file_exists(path: &Path) -> Result<()> {
    if !path.exists() {
        bail!("file not found: {}", path.display());
    }

    if !path.is_file() {
        bail!("path is not a file: {}", path.display());
    }

    Ok(())
}

pub(crate) fn is_js_file(path: &Path) -> bool {
    matches!(
        path.extension().and_then(|ext| ext.to_str()),
        Some("js") | Some("jsx") | Some("mjs") | Some("cjs")
    )
}

pub(crate) fn is_ts_file(path: &Path) -> bool {
    let name = match path.file_name().and_then(|name| name.to_str()) {
        Some(name) => name,
        None => return false,
    };

    if name.ends_with(".d.ts") || name.ends_with(".d.mts") || name.ends_with(".d.cts") {
        return true;
    }

    matches!(
        path.extension().and_then(|ext| ext.to_str()),
        Some("ts") | Some("tsx") | Some("mts") | Some("cts")
    )
}

/// Check if a path is a valid module file for module resolution purposes.
/// This includes TypeScript files AND .json files (which can be imported with resolveJsonModule).
pub(crate) fn is_valid_module_file(path: &Path) -> bool {
    let name = match path.file_name().and_then(|name| name.to_str()) {
        Some(name) => name,
        None => return false,
    };

    if name.ends_with(".d.ts") || name.ends_with(".d.mts") || name.ends_with(".d.cts") {
        return true;
    }

    matches!(
        path.extension().and_then(|ext| ext.to_str()),
        Some("ts") | Some("tsx") | Some("mts") | Some("cts") | Some("json")
    )
}

fn path_to_pattern(base_dir: &Path, path: &Path) -> Option<String> {
    let rel = if path.is_absolute() {
        path.strip_prefix(base_dir).ok()?.to_path_buf()
    } else {
        path.to_path_buf()
    };
    let value = rel.to_string_lossy().replace('\\', "/");
    if value.is_empty() { None } else { Some(value) }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    #[test]
    fn test_discover_explicitly_listed_js_file_without_allow_js() {
        // Explicitly listed .js files should be included even when allow_js is false.
        // This matches tsc behavior where CLI positional args and tsconfig "files"
        // entries are always compiled regardless of the allowJs setting.
        let dir = std::env::temp_dir().join("tsz_fs_test_explicit_js");
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(&dir).unwrap();
        fs::write(dir.join("app.ts"), "const x = 1;").unwrap();
        fs::write(dir.join("lib.js"), "var y = 2;").unwrap();

        let options = FileDiscoveryOptions {
            base_dir: dir.clone(),
            files: vec![PathBuf::from("app.ts"), PathBuf::from("lib.js")],
            include: None,
            exclude: None,
            out_dir: None,
            follow_links: false,
            allow_js: false, // NOT set, but .js should still be included
        };

        let result = discover_ts_files(&options).unwrap();
        assert!(
            result.iter().any(|p| p.ends_with("app.ts")),
            "explicitly listed .ts file should be included"
        );
        assert!(
            result.iter().any(|p| p.ends_with("lib.js")),
            "explicitly listed .js file should be included even without allowJs"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn test_discover_pattern_matched_js_file_requires_allow_js() {
        // Pattern-matched .js files (from include/exclude) should NOT be included
        // when allow_js is false. This is the correct tsc behavior.
        let dir = std::env::temp_dir().join("tsz_fs_test_pattern_js");
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(dir.join("src")).unwrap();
        fs::write(dir.join("src/app.ts"), "const x = 1;").unwrap();
        fs::write(dir.join("src/lib.js"), "var y = 2;").unwrap();

        // Without allowJs, pattern-matched .js files are excluded
        let options = FileDiscoveryOptions {
            base_dir: dir.clone(),
            files: vec![],
            include: Some(vec!["src".to_string()]),
            exclude: None,
            out_dir: None,
            follow_links: false,
            allow_js: false,
        };

        let result = discover_ts_files(&options).unwrap();
        assert!(
            result.iter().any(|p| p.ends_with("app.ts")),
            ".ts file should be included from pattern"
        );
        assert!(
            !result.iter().any(|p| p.ends_with("lib.js")),
            ".js file should NOT be included from pattern without allowJs"
        );

        // With allowJs, pattern-matched .js files are included
        let options_with_js = FileDiscoveryOptions {
            base_dir: dir.clone(),
            files: vec![],
            include: Some(vec!["src".to_string()]),
            exclude: None,
            out_dir: None,
            follow_links: false,
            allow_js: true,
        };

        let result_with_js = discover_ts_files(&options_with_js).unwrap();
        assert!(
            result_with_js.iter().any(|p| p.ends_with("lib.js")),
            ".js file should be included from pattern with allowJs"
        );

        let _ = fs::remove_dir_all(&dir);
    }
}