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

Enforce type checking on babel-{cli,node} #14765

Merged
merged 9 commits into from Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/fs-readdir-recursive.d.ts
@@ -0,0 +1,7 @@
declare module "fs-readdir-recursive" {
function read(
root: string,
filter?: (filename: string, index: number, dir: string) => boolean
): string[];
export = read;
}
Copy link
Member

Choose a reason for hiding this comment

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

As a follow-up, could you open a PR to update https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/fs-readdir-recursive/index.d.ts so that then we can use @types/fs-readdir-recursive?

Copy link
Member Author

Choose a reason for hiding this comment

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

DefinitelyTyped/DefinitelyTyped#61344
Finish. I think we might be able to remove this package at some point.

4 changes: 4 additions & 0 deletions lib/kexec.d.ts
@@ -0,0 +1,4 @@
declare module "kexec" {
function execvp(cmd: string, args: string[]): string;
export = execvp;
}
4 changes: 4 additions & 0 deletions lib/slash.d.ts
@@ -0,0 +1,4 @@
declare module "slash" {
function slash(path: string): string;
export = slash;
}
liuxingbaoyu marked this conversation as resolved.
Show resolved Hide resolved
1 change: 0 additions & 1 deletion packages/babel-cli/src/babel/dir.ts
@@ -1,4 +1,3 @@
// @ts-expect-error
import slash from "slash";
liuxingbaoyu marked this conversation as resolved.
Show resolved Hide resolved
import path from "path";
import fs from "fs";
Expand Down
1 change: 0 additions & 1 deletion packages/babel-cli/src/babel/file.ts
@@ -1,7 +1,6 @@
import convertSourceMap from "convert-source-map";
import { AnyMap, encodedMap } from "@jridgewell/trace-mapping";
import type { Section } from "@jridgewell/trace-mapping/dist/types/types";
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not comfortable with the undeclared package sub imports. @jridgewell Can you make the Section type public?

Copy link
Member

Choose a reason for hiding this comment

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

Sure. Does this serve the need for now?

import type { SectionedSourceMap } from '@jridgewell/trace-mapping';
type Section = SectionedSourceMap['sections'][0]

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, Thanks!

// @ts-expect-error
import slash from "slash";
import path from "path";
import fs from "fs";
Expand Down
23 changes: 9 additions & 14 deletions packages/babel-cli/src/babel/util.ts
@@ -1,4 +1,3 @@
// @ts-expect-error
import readdirRecursive from "fs-readdir-recursive";
import * as babel from "@babel/core";
import path from "path";
Expand All @@ -21,19 +20,15 @@ export function readdir(
includeDotfiles: boolean,
filter?: ReaddirFilter,
): Array<string> {
return readdirRecursive(
dirname,
(filename: string, index: number, currentDirectory: string) => {
const stat = fs.statSync(path.join(currentDirectory, filename));

if (stat.isDirectory()) return true;

return (
(includeDotfiles || filename[0] !== ".") &&
(!filter || filter(filename))
);
},
);
return readdirRecursive(dirname, (filename, index, currentDirectory) => {
const stat = fs.statSync(path.join(currentDirectory, filename));

if (stat.isDirectory()) return true;

return (
(includeDotfiles || filename[0] !== ".") && (!filter || filter(filename))
);
});
}

export function readdirForCompilable(
Expand Down
5 changes: 3 additions & 2 deletions packages/babel-cli/src/babel/watcher.ts
Expand Up @@ -15,15 +15,16 @@ export function enable({ enableGlobbing }: { enableGlobbing: boolean }) {

const { FSWatcher } = requireChokidar();

watcher = new FSWatcher({
const options: WatchOptions = {
disableGlobbing: !enableGlobbing,
persistent: true,
ignoreInitial: true,
awaitWriteFinish: {
stabilityThreshold: 50,
pollInterval: 10,
},
} as WatchOptions);
};
watcher = new FSWatcher(options);

watcher.on("unlink", unwatchFile);
}
Expand Down
1 change: 0 additions & 1 deletion packages/babel-node/src/babel-node.ts
Expand Up @@ -73,7 +73,6 @@ getV8Flags(async function (err, v8Flags) {
}

try {
// @ts-expect-error
const { default: kexec } = await import("kexec");
kexec(process.argv[0], args);
} catch (err) {
Expand Down
3 changes: 3 additions & 0 deletions scripts/generators/tsconfig.js
Expand Up @@ -129,6 +129,9 @@ fs.writeFileSync(
"to-fast-properties",
["./node_modules/to-fast-properties-BABEL_8_BREAKING-true"],
],
["slash", ["./lib/slash.d.ts"]],
["fs-readdir-recursive", ["./lib/fs-readdir-recursive.d.ts"]],
["kexec", ["./lib/kexec.d.ts"]],
]),
},
},
Expand Down
9 changes: 9 additions & 0 deletions tsconfig.json
Expand Up @@ -718,6 +718,15 @@
],
"to-fast-properties": [
"./node_modules/to-fast-properties-BABEL_8_BREAKING-true"
],
"slash": [
"./lib/slash.d.ts"
],
"fs-readdir-recursive": [
"./lib/fs-readdir-recursive.d.ts"
],
"kexec": [
"./lib/kexec.d.ts"
]
}
}
Expand Down