@@ -3,10 +3,11 @@ use std::{
3
3
sync:: Arc ,
4
4
} ;
5
5
6
- use crate :: PathUtil ;
7
6
use serde:: Deserialize ;
8
7
use typescript_tsconfig_json:: { CompilerOptionsPathsMap , ExtendsField } ;
9
8
9
+ use crate :: PathUtil ;
10
+
10
11
#[ derive( Debug , Deserialize ) ]
11
12
#[ serde( rename_all = "camelCase" ) ]
12
13
pub struct TsConfig {
@@ -71,24 +72,25 @@ impl TsConfig {
71
72
Ok ( tsconfig)
72
73
}
73
74
74
- /// Directory to `package.json`
75
+ fn base_path ( & self ) -> & Path {
76
+ self . compiler_options
77
+ . base_url
78
+ . as_ref ( )
79
+ . map_or_else ( || self . directory ( ) , |path| path. as_ref ( ) )
80
+ }
81
+
82
+ /// Directory to `tsconfig.json`
75
83
///
76
84
/// # Panics
77
85
///
78
- /// * When the package .json path is misconfigured.
86
+ /// * When the `tsconfig .json` path is misconfigured.
79
87
pub fn directory ( & self ) -> & Path {
80
88
debug_assert ! ( self . path. file_name( ) . is_some( ) ) ;
81
89
self . path . parent ( ) . unwrap ( )
82
90
}
83
91
84
- fn base_path ( & self ) -> & Path {
85
- self . compiler_options
86
- . base_url
87
- . as_ref ( )
88
- . map_or_else ( || self . directory ( ) , |path| path. as_ref ( ) )
89
- }
90
-
91
92
pub fn extend_tsconfig ( & mut self , tsconfig : & Self ) {
93
+ let dir = self . directory ( ) . to_path_buf ( ) ;
92
94
let compiler_options = & mut self . compiler_options ;
93
95
if compiler_options. paths . is_none ( ) {
94
96
compiler_options. paths_base = compiler_options
@@ -100,6 +102,29 @@ impl TsConfig {
100
102
if compiler_options. base_url . is_none ( ) {
101
103
compiler_options. base_url = tsconfig. compiler_options . base_url . clone ( ) ;
102
104
}
105
+ if let Some ( paths) = & mut compiler_options. paths {
106
+ Self :: normalize_paths ( & dir, paths) ;
107
+ }
108
+ }
109
+
110
+ fn normalize_paths ( directory : & Path , paths : & mut CompilerOptionsPathsMap ) {
111
+ for paths in paths. values_mut ( ) {
112
+ for path in paths {
113
+ Self :: substitute_template_variable ( directory, path) ;
114
+ }
115
+ }
116
+ }
117
+
118
+ /// Template variable `${configDir}` for substitution of config files directory path
119
+ ///
120
+ /// NOTE: All tests cases are just a head replacement of `${configDir}`, so we are constrained as such.
121
+ ///
122
+ /// See <https://github.com/microsoft/TypeScript/pull/58042>
123
+ fn substitute_template_variable ( directory : & Path , path : & mut String ) {
124
+ const TEMPLATE_VARIABLE : & str = "${configDir}/" ;
125
+ if let Some ( stripped_path) = path. strip_prefix ( TEMPLATE_VARIABLE ) {
126
+ * path = directory. join ( stripped_path) . to_string_lossy ( ) . to_string ( ) ;
127
+ }
103
128
}
104
129
105
130
pub fn resolve ( & self , path : & Path , specifier : & str ) -> Vec < PathBuf > {
0 commit comments