Follow test references to calculate unused files#710
Conversation
Lots of notes on how it *should* work, plus some new tests. It doesn't work the way it should yet though.
Still need to look at TODOs in definition-parser and probably write more tests.
1. errors when it's already reachable (it shouldn't be in the list) 2. errors when it doesn't exist (it ... also shouldn't be in the list).
even though it is extremely confusing
sandersn
left a comment
There was a problem hiding this comment.
Explanatory comments. Let me know if you think any of it should be baked into the source as comments.
| @@ -155,28 +155,25 @@ async function getTypingDataForSingleTypesVersion( | |||
| oldMajorVersion: number | undefined, | |||
| ): Promise<TypingDataFromIndividualTypeScriptVersion> { | |||
| const tsconfig = await fs.readJson("tsconfig.json") as TsConfig; // tslint:disable-line await-promise (tslint bug) | |||
There was a problem hiding this comment.
allReferencedFiles finds all the files that are referenced transitively from the files listed in tsconfig. Then we manually reference d.ts files from OTHER_FILES.txt as references.
Next, the external dependencies are calculated. This part doesn't change too much, I just moved checkAllFilesUsed's call earlier and the no-windows-\ check earlier, into checkAllFilesUsed.
| } | ||
| } | ||
|
|
||
| return { declFiles: sort(all.keys()), dependencies, declaredModules, globals: sort(globals) }; |
There was a problem hiding this comment.
I moved the sort(all.keys()) out of this function since it didn't particularly belong.
|
|
||
| function addDependency(dependency: string): void { | ||
| function addDependency(ref: string): void { | ||
| if (ref.startsWith(".")) return; |
There was a problem hiding this comment.
this now happens for all calls to addDependency, which fixes a couple of bogus dependencies I saw on DT.
| return slash === -1 ? importText : importText.slice(0, slash); | ||
| const root = importText.slice(0, slash); | ||
| const postImport = importText.slice(slash + 1); | ||
| if (slash > -1 && postImport.match(/v\d+$/) && !typeFiles.has(postImport + ".d.ts")) { |
There was a problem hiding this comment.
this error is new, and required a couple of packages on DT to be fixed.
There was a problem hiding this comment.
Much later: This should be legal if the import is from the same package, eg "foo/v3" should be legal inside "foo".
Not sure how to detect this yet.
|
|
||
| /** Returns a map from filename (path relative to `directory`) to the SourceFile we parsed for it. */ | ||
| async function allReferencedFiles(entryFilenames: ReadonlyArray<string>, fs: FS, baseDirectory: string): Promise<Map<string, ts.SourceFile>> { | ||
| export async function allReferencedFiles( |
There was a problem hiding this comment.
allReferencedFiles now scans through .d.ts AND .ts files, instead of only looking at d.ts files. It uses extension d.ts-vs-ts to decide whether the referenced file is a type file or a test file.
This is not precisely equivalent to the old algorithm but it produces the same results on DT (so far at least).
| if (!imported.startsWith(".") && !dependencies.has(imported) && imported !== pkgName) { | ||
| testDependencies.add(imported); | ||
| if (!imported.startsWith(".")) { | ||
| const dep = rootName(imported, typeFiles); |
There was a problem hiding this comment.
this call to rootName is new, and caught several mistakes on DT.
| boring.set("boring-tests.ts", ` | ||
| import { superstor } from "super-big-fun-hus"; | ||
| import { drills } from "boring"; | ||
| import { hovercars } from "boring/secondary"; |
There was a problem hiding this comment.
need to figure out how to work a cybertruck reference in here somewhere
andrewbranch
left a comment
There was a problem hiding this comment.
This looks great. (I reviewed for intent, not correctness, since you have tests that presumably validate everything works.)
| } | ||
|
|
||
| /** In addition to dependencies found oun source code, also get dependencies from tsconfig. */ | ||
| /** In addition to dependencies found in source code, also get dependencies from tsconfig. */ |
|
I'll merge and ship once I have the related DT PR(s) ready to go. |
|
I missed a last minute change, which I just pushed to master: c169523 |
To make sure that I didn't break anything, I dumped the list of dependencies generated during parsing. In
getTypingInfo, I added the following code:I put the same code in the production version of types-publisher. This produced (1) almost-legal JSON (2) a diffable file for both. I diffed the files and found 57 differences:
types="x/v7"references that are now corrected to justtypes="x"by the additional call torootName. I added a test to show that we detect and prevent such references.types="chrome/chromecast"are corrected to just "chrome" by the additional call torootName.There were lots of unused files that I automatically added OTHER_FILES.txt entries for. That's the next step in the process I wrote up at #708.
Note: I also need to figure out how to fix the 2 references to
types="x/v7"which the tests relied on. I think I'll have to add them to the whitelist. =[Edit: I just updated the tests to newer versions of dependencies: DefinitelyTyped/DefinitelyTyped#40575