dsherret / ts-morph

TypeScript Compiler API wrapper for static analysis and programmatic code changes.
https://ts-morph.com
MIT License
4.97k stars 195 forks source link

Why doesn't `ts-morph` have a `isSourceFileDefaultLibrary` function like `typescript`? #1283

Open hamirmahal opened 2 years ago

hamirmahal commented 2 years ago

microsoft/TypeScript/src/compiler/program.ts has a isSourceFileDefaultLibrary function.

        function isSourceFileDefaultLibrary(file: SourceFile): boolean {
            if (!file.isDeclarationFile) {
                return false;
            }

            if (file.hasNoDefaultLib) {
                return true;
            }

            if (!options.noLib) {
                return false;
            }

            // If '--lib' is not specified, include default library file according to '--target'
            // otherwise, using options specified in '--lib' instead of '--target' default library file
            const equalityComparer = host.useCaseSensitiveFileNames() ? equateStringsCaseSensitive : equateStringsCaseInsensitive;
            if (!options.lib) {
                return equalityComparer(file.fileName, getDefaultLibraryFileName());
            }
            else {
                return some(options.lib, libFileName => equalityComparer(file.fileName, pathForLibFile(libFileName)));
            }
        }

I was just curious: why does this repository have isSourceFileFromExternalLibrary, but not isSourceFileDefaultLibrary?

On a sidenote, how do you feel about creating a Discussions section for more discussion-based questions like this one?

dsherret commented 2 years ago

It doesn't have it because it was never implemented. You could try out project.getProgram().compilerObject.isSourceFileDefaultLibrary(sourceFile.compilerNode) and see if it works, but I'm not sure if it will because ts-morph does some custom stuff. It might be more reliable to see if getFilePath() contains "/node_modules/typescript/lib/".

On a sidenote, how do you feel about creating a Discussions section for more discussion-based questions like this one?

I tried discussions and I don't like it. It creates two places to check instead of just one.

menocomp commented 1 year ago

Thanks @dsherret

I just tried:

identifier.getProject().getProgram().compilerObject.isSourceFileDefaultLibrary(identifier.getType().getSymbol().getDeclarations()[0].getSourceFile().compilerNode)

and it is working fine. Hopefully this function would have native support by ts-morph.