Skip to content

Commit

Permalink
[test-dist-not-imported-from-src] Add a regression test for TypeScrip…
Browse files Browse the repository at this point in the history
…t autocompletions from dist.

This is a workaround to prevent a regression for microsoft/TypeScript#47184
If microsoft/TypeScript#35395 (Exclude specific files from auto import suggestions) is implemented, we should just use that instead.
  • Loading branch information
lgarron committed Jan 5, 2022
1 parent 6f0c0f8 commit 8bd01da
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ jobs:
- run: make build-site-experiments
- run: make test-dist-experiments
- run: make test-dist-esm-parcel
- run: make test-dist-not-imported-from-src
- run: make clean
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"test-dist-esm-parcel": "node ./script/test/dist/esm/parcel/main.js",
"test-dist-esm-perf": "node script/test/dist/esm/perf/*.mjs",
"test-dist-experiments": "node ./script/test/dist/experiments/main.js",
"test-dist-not-imported-from-src": "node ./script/test/dist/not-imported-from-src/main.js",
"test-import-restrictions": "node ./script/test/import-restrictions/main.js",
"test-jest": "npx jest --collectCoverage",
"test-tsc": "npm run build-types && npx tsc --project ./tsconfig.json",
Expand Down
48 changes: 48 additions & 0 deletions script/test/dist/not-imported-from-src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { exit, stderr } from "process";
import { execPromise } from "../../../lib/execPromise.js";

// Test that the `dist` dir is not imported from the source (which causes unwanted autocompletion in VSCode).
// In theory we could test this together with import restrictions, but `npx tsc --explainFiles` lets us test directly against the completions.

await execPromise("make build"); // TODO: check for full expected build without compiling from scratch.

let output;
try {
output = await execPromise("npx tsc --explainFiles -p ./tsconfig.json");
} catch (e) {
stderr.write(
"`npx tsc --explainFiles` failed. Please run `make test-tsc` to debug.",
);
exit(1);
}
const files = {};
let currentFile = null;
for (const line of output.split("\n")) {
if (currentFile === null || !line.startsWith(" ")) {
currentFile = {
path: line,
from: [],
};
files[line] = currentFile;
} else {
if (!line.includes("from file 'dist")) {
// This theoretically has false positics, but it's good enough for us in practice.
currentFile.anyNotImportedFromDist = true;
}
currentFile.from.push(line);
}
}

for (const file of Object.values(files)) {
if (file.path.startsWith(".")) {
stderr.write(stderr, "Did not expect relative path:\n");
stderr.write(stderr, file);
exit(1);
}
if (file.path.startsWith("dist/") && file.anyNotImportedFromDist) {
stderr.write("Imports from the `dist` dir are not allowed:\n");
stderr.write(file.path + "\n");
stderr.write(file.from.join("\n"));
exit(1);
}
}

0 comments on commit 8bd01da

Please sign in to comment.