dominikg / tsconfck

A utility to find and parse tsconfig files without depending on typescript
Other
293 stars 14 forks source link

Support for “Solution Style” `tsconfig.json` Files #2

Closed haoqunjiang closed 3 years ago

haoqunjiang commented 3 years ago

Describe the feature

TypeScript 3.9 adds this feature called “Solution Style” tsconfig.json https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-9.html#support-for-solution-style-tsconfigjson-files

In this case, the actual tsconfig may reside in files named not exactly as tsconfig.json. The found tsconfig.json is only a "solution" file that references other configuration files.

A Usecase

Some large full-stack repositories may use it. Those who put .spec.ts files alongside the source files may need it too.

Alternatives

Not sure whether this feature should be implemented in this package, or in a separate package.

If we're to support this feature, all top-level options in tsconfig.json need to be processed, including files, overwrites, include, exclude and references.

Additional context

N/A

dominikg commented 3 years ago

Thanks for pointing this out. For now your best alternative is to install ts and use parseNative which should support this ootb. (doesn't work, see below)

To implement this in parse 2 things are needed

a matcher that checks if foo/bar/input.ts file is included in the tsconfig
a loop that parses references

Possible pitfalls

Performance

Solutions tend to have large codebases making the process less than fast. Maybe it's possible to parallelize with async

Ambiguities

How to handle cases where multiple references match

References of references or extends

Is it legal to have a references b + b references c or a references b + b extends a? That could get "interesting"

dominikg commented 3 years ago

more resources: https://www.staging-typescript.org/docs/handbook/project-references.html#what-is-a-project-reference typescripts own "solution": https://github.com/microsoft/TypeScript/blob/main/src/tsconfig.json

Whats interesting here is that they use directories as paths in the references section. This case is already supported by tsconfck, as the actual closest tsconfig.json is correctly being picked up.

So as a workaround we could check for existing references that point to files not named tsconfig.json and throw an Error infoming the user that this tsconfig cannot be safely parsed without using parseNative

i'll try to setup a branch with a test fixture.

dominikg commented 3 years ago

here it is: https://github.com/dominikg/tsconfck/tree/feat/support-solution-style

unfortunately the current "native" implementation also only returns the solution tsconfig, so finding the correct one would need to be done for both parse and parseNative.

dominikg commented 3 years ago

added an initial implementation in https://github.com/dominikg/tsconfck/tree/feat/support-solution-style

It includes a pretty crude include pattern to regex approach to find the matching solution where i am not sure if thats exactly how it works inside of typescript but it's a start. https://github.com/dominikg/tsconfck/blob/db98a0f550e882b8ac1382a03d7d290bdc8d9015/src/util.ts#L118

Would love to completely drop using regex there and instead do a single pass character scan, but i need to think about how to properly implement greed for **/