Skip to content
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

Add Program.isSourceFileDefaultLibrary to public API #25225

Closed
4 tasks done
timocov opened this issue Jun 26, 2018 · 8 comments
Closed
4 tasks done

Add Program.isSourceFileDefaultLibrary to public API #25225

timocov opened this issue Jun 26, 2018 · 8 comments
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue Help Wanted You can do this

Comments

@timocov
Copy link
Contributor

timocov commented Jun 26, 2018

Search Terms

API, Program

Suggestion

Is there a chance to add isSourceFileDefaultLibrary to Programs API?

For now, it is marked as @internal and this method is removed from declaration file https://github.com/Microsoft/TypeScript/blob/70975cd08f48497f3ae6538ff69bf50cac23706b/src/compiler/types.ts#L2777

Use Cases

I working on https://github.com/timocov/dts-bundle-generator and there I need to know what files are default lib files. I need to know it to skip these files from preparing. It seems that is the right way to detect it, but it is not public.

If there is another way to check that some file is default lib file - please let me know.

Related to timocov/dts-bundle-generator#39.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript / JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. new expression-level syntax)
@ajafff
Copy link
Contributor

ajafff commented Jun 26, 2018

I had a similar requirement when I wanted to exclude all external files from linting.
There is a lot more that needs to be considered though:

  • other library files referenced by the default lib file
  • @types/<package> implicitly added
  • tslib that somehow is added to the Program if you symlink multiple projects' source folders
  • files from external packages

This is my implementation that handles all of these cases: https://github.com/fimbullinter/wotan/blob/a3cfe28ef9162dacf451c839efa2f26be30d1ce1/packages/wotan/src/runner.ts#L196-L216

@timocov
Copy link
Contributor Author

timocov commented Jun 26, 2018

@ajafff In my case, I need to know only if a file is default lib file, but anyway thanks for the additional information!

@ajafff
Copy link
Contributor

ajafff commented Jun 26, 2018

In that case you could simply compare to ts.getDefaultLibFilePath(program.getCompilerOptions())

@timocov
Copy link
Contributor Author

timocov commented Jun 26, 2018

It seems that ts.getDefaultLibFilePath(program.getCompilerOptions()) returns only one default lib file - lib.d.ts, but I need to check every lib file (e.g. lib.es2015.d.ts or lib.es2017.d.ts).

@ajafff
Copy link
Contributor

ajafff commented Jun 26, 2018

isSourceFileDefaultLibrary is probably not what you are looking for. It only handles the default library file and those declared in compilerOptions.lib.

You might want to do the path comparison yourself:

// may need some special handling to normalize slashes and file casing
path.dirname(sourceFile.fileName) === path.dirname(ts.getDefaultLibFilePath(program.getCompilerOptions()));

@timocov
Copy link
Contributor Author

timocov commented Jun 26, 2018

I guess there is some misunderstanding - I need to filter all lib files (I thought it called "default lib files"). Program.isSourceFileDefaultLibrary does exactly what I need and I believe any other solution is not entirely correct because only compiler knows what is default library file and what is not.

Of course, we can create one more solution (with checking the path of the file or something else), but compiler knows it already, so why I need to do its work? 🙂

@timocov
Copy link
Contributor Author

timocov commented Jun 26, 2018

You might want to do the path comparison yourself:

But in this case, we'll filter typescript.d.ts file and this is not what I actually want.

is probably not what you are looking for

Why not? As far as I understand it checks that file is lib.xxx.d.ts file, doesn't it?

https://github.com/Microsoft/TypeScript/blob/bd97e12f760773aac87d62159fa26ea1474a2337/src/compiler/program.ts#L1291-L1309

@mhegazy mhegazy added Bug A bug in TypeScript Help Wanted You can do this labels Jun 26, 2018
@mhegazy mhegazy added this to the TypeScript 3.0 milestone Jun 26, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Jun 26, 2018

A PR would be appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

3 participants