-
-
Notifications
You must be signed in to change notification settings - Fork 34
feat(tsconfig): implement solution-style tsconfig resolution #815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
shulaoda
wants to merge
1
commit into
main
Choose a base branch
from
11-06-feat_tsconfig_implement_solution-style_tsconfig_resolution
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
fixtures/tsconfig/cases/solution_style/packages/pkg-a/src/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const pkgAIndex = 'pkg-a'; |
10 changes: 10 additions & 0 deletions
10
fixtures/tsconfig/cases/solution_style/packages/pkg-a/tsconfig.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "composite": true, | ||
| "baseUrl": ".", | ||
| "paths": { | ||
| "@pkg-a/*": ["./src/*"] | ||
| } | ||
| }, | ||
| "include": ["src/**/*"] | ||
| } |
1 change: 1 addition & 0 deletions
1
fixtures/tsconfig/cases/solution_style/packages/pkg-b/src/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const pkgBIndexJS = 'pkg-b-js'; |
1 change: 1 addition & 0 deletions
1
fixtures/tsconfig/cases/solution_style/packages/pkg-b/src/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const pkgBIndex = 'pkg-b'; |
11 changes: 11 additions & 0 deletions
11
fixtures/tsconfig/cases/solution_style/packages/pkg-b/tsconfig.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "composite": true, | ||
| "baseUrl": ".", | ||
| "allowJs": true, | ||
| "paths": { | ||
| "@pkg-b/*": ["./src/*"] | ||
| } | ||
| }, | ||
| "include": ["src/**/*"] | ||
| } |
1 change: 1 addition & 0 deletions
1
fixtures/tsconfig/cases/solution_style/packages/pkg-c/lib/index.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| // This file should be excluded by pkg-c's exclude pattern |
1 change: 1 addition & 0 deletions
1
fixtures/tsconfig/cases/solution_style/packages/pkg-c/lib/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const pkgCIndex = 'pkg-c'; |
11 changes: 11 additions & 0 deletions
11
fixtures/tsconfig/cases/solution_style/packages/pkg-c/tsconfig.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "composite": true, | ||
| "baseUrl": ".", | ||
| "paths": { | ||
| "@pkg-c/*": ["./lib/*"] | ||
| } | ||
| }, | ||
| "include": ["lib/**/*"], | ||
| "exclude": ["lib/**/*.test.ts"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const rootIndex = 'root'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "baseUrl": ".", | ||
| "allowJs": true, | ||
| "paths": { | ||
| "root/*": ["./src/*"] | ||
| } | ||
| }, | ||
| "include": ["src/**/*"], | ||
| "references": [ | ||
| { "path": "./packages/pkg-a" }, | ||
| { "path": "./packages/pkg-b" }, | ||
| { "path": "./packages/pkg-c" } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| //! Tests for solution-style tsconfig resolution | ||
| //! | ||
| //! Tests the `TsConfig::resolve_for_file` method which implements solution-style | ||
| //! tsconfig resolution similar to tsconfck's `resolveSolutionTSConfig`. | ||
|
|
||
| use std::{path::Path, sync::Arc}; | ||
|
|
||
| use crate::{ResolveOptions, Resolver, TsConfig}; | ||
|
|
||
| /// Helper function to load a tsconfig with references | ||
| fn load_tsconfig(path: &Path) -> Arc<TsConfig> { | ||
| let resolver = Resolver::new(ResolveOptions::default()); | ||
| resolver.resolve_tsconfig(path).unwrap() | ||
| } | ||
|
|
||
| #[test] | ||
| fn resolve_for_file_basic() { | ||
| let f = super::fixture_root().join("tsconfig/cases/solution_style"); | ||
|
|
||
| // Load root tsconfig with references | ||
| let root_tsconfig = load_tsconfig(&f.join("tsconfig.json")); | ||
|
|
||
| // File in root src/ should use root tsconfig | ||
| let file_path = f.join("src/index.ts"); | ||
| let resolved = root_tsconfig.resolve_for_file(&file_path); | ||
| assert_eq!(resolved.path(), root_tsconfig.path(), "File in src/ should use root tsconfig"); | ||
|
|
||
| // File in pkg-a should use pkg-a's tsconfig | ||
| let file_path = f.join("packages/pkg-a/src/index.ts"); | ||
| let resolved = root_tsconfig.resolve_for_file(&file_path); | ||
| assert_eq!( | ||
| resolved.path(), | ||
| f.join("packages/pkg-a/tsconfig.json"), | ||
| "File in pkg-a/src/ should use pkg-a's tsconfig" | ||
| ); | ||
|
|
||
| // File in pkg-b should use pkg-b's tsconfig | ||
| let file_path = f.join("packages/pkg-b/src/index.ts"); | ||
| let resolved = root_tsconfig.resolve_for_file(&file_path); | ||
| assert_eq!( | ||
| resolved.path(), | ||
| f.join("packages/pkg-b/tsconfig.json"), | ||
| "File in pkg-b/src/ should use pkg-b's tsconfig" | ||
| ); | ||
|
|
||
| // File in pkg-c's lib/ should use pkg-c's tsconfig | ||
| let file_path = f.join("packages/pkg-c/lib/index.ts"); | ||
| let resolved = root_tsconfig.resolve_for_file(&file_path); | ||
| assert_eq!( | ||
| resolved.path(), | ||
| f.join("packages/pkg-c/tsconfig.json"), | ||
| "File in pkg-c/lib/ should use pkg-c's tsconfig" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn resolve_for_file_exclude_patterns() { | ||
| let f = super::fixture_root().join("tsconfig/cases/solution_style"); | ||
| let root_tsconfig = load_tsconfig(&f.join("tsconfig.json")); | ||
|
|
||
| // pkg-c excludes *.test.ts files | ||
| let excluded_file = f.join("packages/pkg-c/lib/index.test.ts"); | ||
| let resolved = root_tsconfig.resolve_for_file(&excluded_file); | ||
|
|
||
| // Should fall back to root tsconfig since file is excluded from pkg-c | ||
| assert_eq!( | ||
| resolved.path(), | ||
| root_tsconfig.path(), | ||
| "Excluded files should fall back to root tsconfig" | ||
| ); | ||
|
|
||
| // Non-excluded file should still use pkg-c's tsconfig | ||
| let included_file = f.join("packages/pkg-c/lib/index.ts"); | ||
| let resolved = root_tsconfig.resolve_for_file(&included_file); | ||
| assert_eq!( | ||
| resolved.path(), | ||
| f.join("packages/pkg-c/tsconfig.json"), | ||
| "Non-excluded files should use pkg-c's tsconfig" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn resolve_for_file_allowjs() { | ||
| let f = super::fixture_root().join("tsconfig/cases/solution_style"); | ||
| let root_tsconfig = load_tsconfig(&f.join("tsconfig.json")); | ||
|
|
||
| // pkg-b has allowJs: true, so .js files should use pkg-b's tsconfig | ||
| let js_file = f.join("packages/pkg-b/src/index.js"); | ||
| let resolved = root_tsconfig.resolve_for_file(&js_file); | ||
| assert_eq!( | ||
| resolved.path(), | ||
| f.join("packages/pkg-b/tsconfig.json"), | ||
| "With allowJs, .js files should use pkg-b's tsconfig" | ||
| ); | ||
|
|
||
| // pkg-a doesn't have allowJs, so .js files should fall back to root | ||
| let js_file = f.join("packages/pkg-a/src/script.js"); | ||
| let resolved = root_tsconfig.resolve_for_file(&js_file); | ||
| assert_eq!( | ||
| resolved.path(), | ||
| root_tsconfig.path(), | ||
| "Without allowJs, .js files should fall back to root tsconfig" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn resolve_for_file_non_ts_js_files() { | ||
| let f = super::fixture_root().join("tsconfig/cases/solution_style"); | ||
| let root_tsconfig = load_tsconfig(&f.join("tsconfig.json")); | ||
|
|
||
| // Non-TS/JS files should not trigger solution-style resolution | ||
| let json_file = f.join("packages/pkg-a/src/data.json"); | ||
| let resolved = root_tsconfig.resolve_for_file(&json_file); | ||
| assert_eq!(resolved.path(), root_tsconfig.path(), "Non-TS/JS files should use root tsconfig"); | ||
|
|
||
| // .css, .html, etc. should also fall back to root | ||
| let css_file = f.join("packages/pkg-a/src/styles.css"); | ||
| let resolved = root_tsconfig.resolve_for_file(&css_file); | ||
| assert_eq!(resolved.path(), root_tsconfig.path(), "CSS files should use root tsconfig"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn resolve_for_file_no_references() { | ||
| let f = super::fixture_root().join("tsconfig/cases/solution_style"); | ||
|
|
||
| // Load pkg-a's tsconfig which has no references | ||
| let pkg_a_tsconfig = load_tsconfig(&f.join("packages/pkg-a/tsconfig.json")); | ||
|
|
||
| // When a tsconfig has no references, it should always return itself | ||
| let file_in_pkg_a = f.join("packages/pkg-a/src/index.ts"); | ||
| let resolved = pkg_a_tsconfig.resolve_for_file(&file_in_pkg_a); | ||
| assert_eq!( | ||
| resolved.path(), | ||
| pkg_a_tsconfig.path(), | ||
| "Tsconfig without references should always return itself" | ||
| ); | ||
|
|
||
| // Even for files outside its directory | ||
| let file_in_pkg_b = f.join("packages/pkg-b/src/index.ts"); | ||
| let resolved = pkg_a_tsconfig.resolve_for_file(&file_in_pkg_b); | ||
| assert_eq!( | ||
| resolved.path(), | ||
| pkg_a_tsconfig.path(), | ||
| "Tsconfig without references should return itself for any file" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn resolve_for_file_extensions() { | ||
| let f = super::fixture_root().join("tsconfig/cases/solution_style"); | ||
| let root_tsconfig = load_tsconfig(&f.join("tsconfig.json")); | ||
|
|
||
| // Test all TS extensions | ||
| for ext in ["ts", "tsx", "mts", "cts"] { | ||
| let file = f.join(format!("packages/pkg-a/src/index.{ext}")); | ||
| let resolved = root_tsconfig.resolve_for_file(&file); | ||
| assert_eq!( | ||
| resolved.path(), | ||
| f.join("packages/pkg-a/tsconfig.json"), | ||
| ".{ext} files should trigger solution-style resolution" | ||
| ); | ||
| } | ||
|
|
||
| // Test JS extensions with allowJs | ||
| for ext in ["js", "jsx", "mjs", "cjs"] { | ||
| let file = f.join(format!("packages/pkg-b/src/index.{ext}")); | ||
| let resolved = root_tsconfig.resolve_for_file(&file); | ||
| assert_eq!( | ||
| resolved.path(), | ||
| f.join("packages/pkg-b/tsconfig.json"), | ||
| ".{ext} files should trigger solution-style resolution with allowJs" | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to add a test case where
tsconfig.app.jsonextendstsconfig.jsonandtsconfig.jsonreferencestsconfig.app.json.