Skip to content
This repository was archived by the owner on Jun 11, 2020. It is now read-only.

Follow test references to calculate unused files#710

Merged
sandersn merged 16 commits intomasterfrom
follow-test-references-to-calculate-unused-files
Nov 26, 2019
Merged

Follow test references to calculate unused files#710
sandersn merged 16 commits intomasterfrom
follow-test-references-to-calculate-unused-files

Conversation

@sandersn
Copy link
Member

@sandersn sandersn commented Nov 21, 2019

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:

    console.log(`"${packageName}": {`)
    let first = true
    for (const k in res) {
        if (first)
            first = false
        else
            console.log(",")
        console.log(`  "${k}": ${JSON.stringify(res[k].dependencies.map(d => d.name))}`)
    }
    console.log("},")

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:

  • 30: order differences from sorting or not.
  • 2: incorrect types="x/v7" references that are now corrected to just types="x" by the additional call to rootName. I added a test to show that we detect and prevent such references.
  • 2: incorrect references "chrome/chromecast" from types="chrome/chromecast" are corrected to just "chrome" by the additional call to rootName.
  • 1: incorrect reference to "@ember/services", from @types/ember__services itself.
  • 22: missing references from untested files. I added entries to OTHER_FILES.txt to add these missing references.

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

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).
Copy link
Member Author

@sandersn sandersn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) };
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this error is new, and required a couple of packages on DT to be fixed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to figure out how to work a cybertruck reference in here somewhere

Copy link
Member

@andrewbranch andrewbranch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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. */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😐

@sandersn
Copy link
Member Author

I'll merge and ship once I have the related DT PR(s) ready to go.

@sandersn sandersn merged commit 85b12f6 into master Nov 26, 2019
@sandersn sandersn deleted the follow-test-references-to-calculate-unused-files branch November 26, 2019 18:12
@sandersn
Copy link
Member Author

I missed a last minute change, which I just pushed to master: c169523

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants