Skip to content

Commit 7718993

Browse files
feat: merge options from extends tsconfig.json (#375)
Co-authored-by: Boshen <[email protected]>
1 parent f65006a commit 7718993

File tree

7 files changed

+169
-0
lines changed

7 files changed

+169
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"experimentalDecorators": true,
4+
"jsx": "react-jsx",
5+
"jsxFactory": "h",
6+
"jsxFragmentFactory": "Fragment",
7+
"jsxImportSource": "xxx"
8+
}
9+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "./base-tsconfig"
3+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"experimentalDecorators": false,
4+
"jsx": "aaa",
5+
"jsxFactory": "bbb",
6+
"jsxFragmentFactory": "ccc",
7+
"jsxImportSource": "ddd"
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "./base-tsconfig",
3+
"compilerOptions": {
4+
"experimentalDecorators": true,
5+
"jsx": "react-jsx",
6+
"jsxFactory": "h",
7+
"jsxFragmentFactory": "Fragment",
8+
"jsxImportSource": "xxx"
9+
}
10+
}

src/tests/tsconfig_paths.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,32 @@ fn test_paths_and_base_url() {
209209
}
210210
}
211211

212+
#[test]
213+
fn test_merge_tsconfig() {
214+
let resolver = Resolver::default();
215+
let dir = super::fixture_root().join("tsconfig/cases/merge_compiler_options");
216+
let resolution = resolver.resolve_tsconfig(&dir).expect("resolved");
217+
let compiler_options = resolution.compiler_options();
218+
assert_eq!(compiler_options.experimental_decorators, Some(true));
219+
assert_eq!(compiler_options.jsx, Some("react-jsx".to_string()));
220+
assert_eq!(compiler_options.jsx_factory, Some("h".to_string()));
221+
assert_eq!(compiler_options.jsx_fragment_factory, Some("Fragment".to_string()));
222+
assert_eq!(compiler_options.jsx_import_source, Some("xxx".to_string()));
223+
}
224+
225+
#[test]
226+
fn test_no_merge_tsconfig() {
227+
let resolver = Resolver::default();
228+
let dir = super::fixture_root().join("tsconfig/cases/no_merge_compiler_options");
229+
let resolution = resolver.resolve_tsconfig(&dir).expect("resolved");
230+
let compiler_options = resolution.compiler_options();
231+
assert_eq!(compiler_options.experimental_decorators, Some(true));
232+
assert_eq!(compiler_options.jsx, Some("react-jsx".to_string()));
233+
assert_eq!(compiler_options.jsx_factory, Some("h".to_string()));
234+
assert_eq!(compiler_options.jsx_fragment_factory, Some("Fragment".to_string()));
235+
assert_eq!(compiler_options.jsx_import_source, Some("xxx".to_string()));
236+
}
237+
212238
// Template variable ${configDir} for substitution of config files directory path
213239
// https://github.com/microsoft/TypeScript/pull/58042
214240
#[test]

src/tsconfig.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,38 @@ pub trait TsConfig: Sized {
104104
compiler_options.set_base_url(base_url.to_path_buf());
105105
}
106106
}
107+
108+
if compiler_options.experimental_decorators().is_none() {
109+
if let Some(experimental_decorators) =
110+
tsconfig.compiler_options().experimental_decorators()
111+
{
112+
compiler_options.set_experimental_decorators(*experimental_decorators);
113+
}
114+
}
115+
116+
if compiler_options.jsx().is_none() {
117+
if let Some(jsx) = tsconfig.compiler_options().jsx() {
118+
compiler_options.set_jsx(jsx.to_string());
119+
}
120+
}
121+
122+
if compiler_options.jsx_factory().is_none() {
123+
if let Some(jsx_factory) = tsconfig.compiler_options().jsx_factory() {
124+
compiler_options.set_jsx_factory(jsx_factory.to_string());
125+
}
126+
}
127+
128+
if compiler_options.jsx_fragment_factory().is_none() {
129+
if let Some(jsx_fragment_factory) = tsconfig.compiler_options().jsx_fragment_factory() {
130+
compiler_options.set_jsx_fragment_factory(jsx_fragment_factory.to_string());
131+
}
132+
}
133+
134+
if compiler_options.jsx_import_source().is_none() {
135+
if let Some(jsx_import_source) = tsconfig.compiler_options().jsx_import_source() {
136+
compiler_options.set_jsx_import_source(jsx_import_source.to_string());
137+
}
138+
}
107139
}
108140

109141
/// Resolves the given `specifier` within the project configured by this
@@ -232,6 +264,46 @@ pub trait CompilerOptions {
232264

233265
/// Sets the path base.
234266
fn set_paths_base(&mut self, paths_base: PathBuf);
267+
268+
/// Whether to enable experimental decorators.
269+
fn experimental_decorators(&self) -> Option<&bool> {
270+
None
271+
}
272+
273+
/// Sets whether to enable experimental decorators.
274+
fn set_experimental_decorators(&mut self, _experimental_decorators: bool) {}
275+
276+
/// JSX.
277+
fn jsx(&self) -> Option<&str> {
278+
None
279+
}
280+
281+
/// Sets JSX.
282+
fn set_jsx(&mut self, _jsx: String) {}
283+
284+
/// JSX factory.
285+
fn jsx_factory(&self) -> Option<&str> {
286+
None
287+
}
288+
289+
/// Sets JSX factory.
290+
fn set_jsx_factory(&mut self, _jsx_factory: String) {}
291+
292+
/// JSX fragment factory.
293+
fn jsx_fragment_factory(&self) -> Option<&str> {
294+
None
295+
}
296+
297+
/// Sets JSX fragment factory.
298+
fn set_jsx_fragment_factory(&mut self, _jsx_fragment_factory: String) {}
299+
300+
/// JSX import source.
301+
fn jsx_import_source(&self) -> Option<&str> {
302+
None
303+
}
304+
305+
/// Sets JSX import source.
306+
fn set_jsx_import_source(&mut self, _jsx_import_source: String) {}
235307
}
236308

237309
/// Project Reference.

src/tsconfig_serde.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,46 @@ impl CompilerOptions for CompilerOptionsSerde {
155155
fn set_paths_base(&mut self, paths_base: PathBuf) {
156156
self.paths_base = paths_base;
157157
}
158+
159+
fn experimental_decorators(&self) -> Option<&bool> {
160+
self.experimental_decorators.as_ref()
161+
}
162+
163+
fn set_experimental_decorators(&mut self, experimental_decorators: bool) {
164+
self.experimental_decorators = Some(experimental_decorators);
165+
}
166+
167+
fn jsx(&self) -> Option<&str> {
168+
self.jsx.as_deref()
169+
}
170+
171+
fn set_jsx(&mut self, jsx: String) {
172+
self.jsx = Some(jsx);
173+
}
174+
175+
fn jsx_factory(&self) -> Option<&str> {
176+
self.jsx_factory.as_deref()
177+
}
178+
179+
fn set_jsx_factory(&mut self, jsx_factory: String) {
180+
self.jsx_factory = Some(jsx_factory);
181+
}
182+
183+
fn jsx_fragment_factory(&self) -> Option<&str> {
184+
self.jsx_fragment_factory.as_deref()
185+
}
186+
187+
fn set_jsx_fragment_factory(&mut self, jsx_fragment_factory: String) {
188+
self.jsx_fragment_factory = Some(jsx_fragment_factory);
189+
}
190+
191+
fn jsx_import_source(&self) -> Option<&str> {
192+
self.jsx_import_source.as_deref()
193+
}
194+
195+
fn set_jsx_import_source(&mut self, jsx_import_source: String) {
196+
self.jsx_import_source = Some(jsx_import_source);
197+
}
158198
}
159199

160200
/// Value for the "extends" field.

0 commit comments

Comments
 (0)