Skip to content

Commit af29d00

Browse files
committed
refactor: add Tsconfig:references_resolved (#856)
This removes the `None` state from `ProjectReference::tsconfig`, which makes it hard to reason about it's existence.
1 parent 56ee26c commit af29d00

File tree

2 files changed

+21
-50
lines changed

2 files changed

+21
-50
lines changed

src/tsconfig.rs

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ const TEMPLATE_VARIABLE: &str = "${configDir}";
1515

1616
pub type CompilerOptionsPathsMap = IndexMap<String, Vec<String>, BuildHasherDefault<FxHasher>>;
1717

18+
/// Project Reference
19+
///
20+
/// <https://www.typescriptlang.org/docs/handbook/project-references.html>
21+
#[derive(Debug, Deserialize)]
22+
pub struct ProjectReference {
23+
pub path: PathBuf,
24+
}
25+
1826
#[derive(Debug, Deserialize)]
1927
#[serde(rename_all = "camelCase")]
2028
pub struct TsConfig {
@@ -42,9 +50,14 @@ pub struct TsConfig {
4250
#[serde(default)]
4351
pub compiler_options: CompilerOptions,
4452

45-
/// Bubbled up project references with a reference to their tsconfig.
4653
#[serde(default)]
4754
pub references: Vec<ProjectReference>,
55+
56+
/// Resolved project references.
57+
///
58+
/// Corresponds to each item in [TsConfig::references].
59+
#[serde(skip)]
60+
pub references_resolved: Vec<Arc<TsConfig>>,
4861
}
4962

5063
impl TsConfig {
@@ -111,26 +124,14 @@ impl TsConfig {
111124
}
112125
TsconfigReferences::Auto => {}
113126
TsconfigReferences::Paths(paths) => {
114-
self.references = paths
115-
.iter()
116-
.map(|path| ProjectReference { path: path.clone(), tsconfig: None })
117-
.collect();
127+
self.references =
128+
paths.iter().map(|path| ProjectReference { path: path.clone() }).collect();
118129
}
119130
}
120131

121132
!self.references.is_empty()
122133
}
123134

124-
/// Returns references to other tsconfig files.
125-
pub(crate) fn references(&self) -> impl Iterator<Item = &ProjectReference> {
126-
self.references.iter()
127-
}
128-
129-
/// Returns mutable references to other tsconfig files.
130-
pub(crate) fn references_mut(&mut self) -> impl Iterator<Item = &mut ProjectReference> {
131-
self.references.iter_mut()
132-
}
133-
134135
/// Returns the base path from which to resolve aliases.
135136
///
136137
/// The base path can be configured by the user as part of the
@@ -348,7 +349,7 @@ impl TsConfig {
348349
#[must_use]
349350
pub(crate) fn resolve(&self, path: &Path, specifier: &str) -> Vec<PathBuf> {
350351
let paths = self.resolve_path_alias(specifier);
351-
for tsconfig in self.references().filter_map(ProjectReference::tsconfig) {
352+
for tsconfig in &self.references_resolved {
352353
if path.starts_with(tsconfig.base_path()) {
353354
return [tsconfig.resolve_path_alias(specifier), paths].concat();
354355
}
@@ -668,36 +669,6 @@ pub enum ExtendsField {
668669
Multiple(Vec<String>),
669670
}
670671

671-
/// Project Reference
672-
///
673-
/// <https://www.typescriptlang.org/docs/handbook/project-references.html>
674-
#[derive(Debug, Deserialize)]
675-
pub struct ProjectReference {
676-
pub path: PathBuf,
677-
678-
#[serde(skip)]
679-
pub tsconfig: Option<Arc<TsConfig>>,
680-
}
681-
682-
impl ProjectReference {
683-
/// Returns the path to a directory containing a `tsconfig.json` file, or to
684-
/// the config file itself (which may have any name).
685-
#[must_use]
686-
pub fn path(&self) -> &Path {
687-
&self.path
688-
}
689-
/// Returns the resolved tsconfig, if one has been set.
690-
#[must_use]
691-
pub fn tsconfig(&self) -> Option<Arc<TsConfig>> {
692-
self.tsconfig.clone()
693-
}
694-
695-
/// Sets the resolved tsconfig.
696-
pub fn set_tsconfig(&mut self, tsconfig: Arc<TsConfig>) {
697-
self.tsconfig.replace(tsconfig);
698-
}
699-
}
700-
701672
impl TsConfig {
702673
/// Parses the tsconfig from a JSON string.
703674
///

src/tsconfig_resolver.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
9595
if tsconfig.load_references(references) {
9696
let path = tsconfig.path().to_path_buf();
9797
let directory = tsconfig.directory().to_path_buf();
98-
for reference in tsconfig.references_mut() {
99-
let reference_tsconfig_path = directory.normalize_with(reference.path());
100-
let tsconfig = self.cache.get_tsconfig(
98+
for reference in &tsconfig.references {
99+
let reference_tsconfig_path = directory.normalize_with(&reference.path);
100+
let referenced_tsconfig = self.cache.get_tsconfig(
101101
/* root */ true,
102102
&reference_tsconfig_path,
103103
|reference_tsconfig| {
@@ -114,7 +114,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
114114
Ok(())
115115
},
116116
)?;
117-
reference.set_tsconfig(tsconfig);
117+
tsconfig.references_resolved.push(referenced_tsconfig);
118118
}
119119
}
120120
Ok(())

0 commit comments

Comments
 (0)