Skip to main content

import_lint/extract/
module_info.rs

1//! Owned, arena-free data model produced by [`crate::extract::extract`].
2//!
3//! Everything here is `'static`-owned (`CompactStr`/`String`/`PathBuf`/`Span` are all
4//! `Copy` or independently heap-allocated) so a `FileModuleInfo` can outlive the oxc
5//! arena it was extracted from. See PLAN-v1.md ยง2.2 for the design rationale.
6
7use std::collections::HashMap;
8use std::path::PathBuf;
9
10use oxc_span::Span;
11use oxc_str::CompactStr;
12use serde::Serialize;
13
14/// Everything ImportLint knows about a single source file after extraction.
15#[derive(Debug, Clone, Serialize)]
16pub struct FileModuleInfo {
17    /// Absolute (or caller-supplied) path to the file this was extracted from.
18    pub path: PathBuf,
19    /// Import specifiers, default imports, and `export {x} from "y"` specifiers
20    /// found in this file โ€” the entries the rule engine will check.
21    pub checked_entries: Vec<CheckedEntry>,
22    /// What this file offers to importers: exported name -> access info. This is
23    /// the "one hop" lookup target for anything that imports from this file.
24    pub export_table: HashMap<CompactStr, ExportInfo>,
25    /// Specifiers of bare `export * from "..."` statements, in source order.
26    pub star_exports: Vec<CompactStr>,
27    /// `declare module "x"` names found in this file. Collected unconditionally
28    /// during extraction regardless of file extension (D6) โ€” filtering to `.d.ts`
29    /// files only is the link phase's responsibility, not extraction's.
30    pub ambient_modules: Vec<CompactStr>,
31    /// All distinct module specifiers referenced by this file (imports,
32    /// re-exports, star exports), for the link phase to resolve.
33    pub specifiers: Vec<CompactStr>,
34    /// Line ranges silenced by `import-lint-disable-next-line` /
35    /// `import-lint-disable-line` directive comments in this file (see
36    /// `crate::extract::suppress`). The rule engine drops any diagnostic whose
37    /// span starts inside one of these ranges (subject to the rule-name filter).
38    pub suppressions: Vec<Suppression>,
39}
40
41/// One directive comment's effect: the byte range of the silenced line, plus
42/// which rules it silences (empty = every rule).
43#[derive(Debug, Clone, Serialize)]
44pub struct Suppression {
45    /// Byte range of the silenced line in the file's source text (start of line
46    /// to its newline, exclusive).
47    pub span: Span,
48    /// Rule names listed on the directive; empty means the directive applies to
49    /// all rules.
50    pub rules: Vec<CompactStr>,
51}
52
53impl Suppression {
54    /// Does this directive silence a diagnostic of rule `rule` whose span starts
55    /// at byte `offset`?
56    pub fn suppresses(&self, offset: u32, rule: &str) -> bool {
57        self.span.start <= offset
58            && offset < self.span.end
59            && (self.rules.is_empty() || self.rules.iter().any(|r| r == rule))
60    }
61}
62
63/// The kind of a checkable import/re-export site.
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
65#[serde(rename_all = "snake_case")]
66pub enum EntryKind {
67    /// `import { x } from "m"`
68    Import,
69    /// `import x from "m"` (or `import { default as x } from "m"`)
70    ImportDefault,
71    /// `export { x } from "m"`
72    ReExport,
73}
74
75/// One checkable site: an import specifier, a default import, or a re-export
76/// specifier with a module source.
77#[derive(Debug, Clone, Serialize)]
78pub struct CheckedEntry {
79    pub kind: EntryKind,
80    /// The name being imported/re-exported *at the source module* โ€” e.g. for
81    /// `import { x as y } from "m"` this is `x`, not the local alias `y`. For
82    /// default imports this is always `"default"`.
83    pub imported_name: CompactStr,
84    pub specifier: CompactStr,
85    /// Span of the whole specifier node, including any `as alias` suffix (D12).
86    pub span: Span,
87}
88
89/// What a single exported name in this file resolves to.
90#[derive(Debug, Clone, Serialize)]
91pub struct ExportInfo {
92    /// Parsed from the JSDoc on the statement that introduces this export.
93    /// `None` means no recognized JSDoc access tag was found (the rule engine
94    /// falls back to `defaultImportability` in that case).
95    pub access: Option<Access>,
96    pub span: Span,
97}
98
99/// JSDoc-declared access level (`@public`/`@package`/`@private`, or `@access <level>`).
100#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
101#[serde(rename_all = "lowercase")]
102pub enum Access {
103    Public,
104    Package,
105    Private,
106}