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

chore: migrate jest-haste-map to TypeScript #7854

Merged
merged 16 commits into from
Feb 12, 2019
Prev Previous commit
Next Next commit
some more types slayed
SimenB committed Feb 11, 2019

Partially verified

This commit is signed with the committer’s verified signature.
the-mikedavis’s contribution has been verified via SSH key.
We cannot verify signatures from co-authors, and some of the co-authors attributed to this commit require their commits to be signed.
commit 5e2774ff87487ae806660b150d372ed4831020dc
4 changes: 2 additions & 2 deletions packages/jest-haste-map/src/ModuleMap.ts
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ export type SerializableModuleMap = {

export default class ModuleMap {
private readonly _raw: RawModuleMap;
static DuplicateHasteCandidatesError: DuplicateHasteCandidatesError;
static DuplicateHasteCandidatesError: typeof DuplicateHasteCandidatesError;

constructor(raw: RawModuleMap) {
this._raw = raw;
@@ -187,7 +187,7 @@ export default class ModuleMap {
}
}

export class DuplicateHasteCandidatesError extends Error {
class DuplicateHasteCandidatesError extends Error {
hasteName: string;
platform: string | null;
supportsNativePlatform: boolean;
7 changes: 5 additions & 2 deletions packages/jest-haste-map/src/crawlers/watchman.ts
Original file line number Diff line number Diff line change
@@ -41,6 +41,7 @@ export = async function watchmanCrawl(
let clientError;
client.on('error', error => (clientError = WatchmanError(error)));

// TODO: type better than `any`
const cmd = (...args: Array<any>): Promise<any> =>
new Promise((resolve, reject) =>
client.command(args, (error, result) =>
@@ -138,7 +139,7 @@ export = async function watchmanCrawl(
}

let files = data.files;
let watchmanFiles;
let watchmanFiles: Map<string, any>;
try {
const watchmanRoots = await getWatchmanRoots(roots);
const watchmanFileResults = await queryWatchmanForDirs(watchmanRoots);
@@ -158,7 +159,8 @@ export = async function watchmanCrawl(
throw clientError;
}

for (const [watchRoot, response] of watchmanFiles) {
// TODO: remove non-null
for (const [watchRoot, response] of watchmanFiles!) {
const fsRoot = normalizePathSep(watchRoot);
const relativeFsRoot = fastPath.relative(rootDir, fsRoot);
clocks.set(relativeFsRoot, response.clock);
@@ -191,6 +193,7 @@ export = async function watchmanCrawl(
sha1hex &&
existingFileData[H.SHA1] === sha1hex
) {
// @ts-ignore: TODO why is this wrong?
nextData = [...existingFileData];
nextData[1] = mtime;
} else {
15 changes: 10 additions & 5 deletions packages/jest-haste-map/src/index.ts
Original file line number Diff line number Diff line change
@@ -509,7 +509,7 @@ class HasteMap extends EventEmitter {
};

// Callback called when the response from the worker is an error.
const workerError = (error: Error | unknown) => {
const workerError = (error: Error | any) => {
if (typeof error !== 'object' || !error.message || !error.stack) {
error = new Error(error);
error.stack = ''; // Remove stack for stack-less errors.
@@ -759,7 +759,7 @@ class HasteMap extends EventEmitter {
const ignorePattern = this._options.ignorePattern;
const rootDir = this._options.rootDir;

let changeQueue = Promise.resolve();
let changeQueue: Promise<null | void> = Promise.resolve();
let eventsQueue: Array<{
filePath: Config.Path;
stat: fs.Stats | undefined;
@@ -769,6 +769,7 @@ class HasteMap extends EventEmitter {
let mustCopy = true;

const createWatcher = (root: Config.Path): Promise<Watcher> => {
// @ts-ignore: TODO how? "Cannot use 'new' with an expression whose type lacks a call or construct signature."
const watcher = new Watcher(root, {
dot: false,
glob: extensions.map(extension => '**/*.' + extension),
@@ -852,7 +853,10 @@ class HasteMap extends EventEmitter {
};
}

const add = () => eventsQueue.push({filePath, stat, type});
const add = () => {
eventsQueue.push({filePath, stat, type});
return null;
};

const relativeFilePath = fastPath.relative(rootDir, filePath);
const fileMetadata = hasteMap.files.get(relativeFilePath);
@@ -898,8 +902,8 @@ class HasteMap extends EventEmitter {
);
const fileMetadata: FileMetaData = [
'',
stat.mtime.getTime(),
stat.size,
stat ? stat.mtime.getTime() : -1,
stat ? stat.size : 0,
0,
[],
null,
@@ -998,6 +1002,7 @@ class HasteMap extends EventEmitter {
}

end(): Promise<void> {
// @ts-ignore: TODO TS cannot decide if `setInterval` and `clearInterval` comes from NodeJS or the DOM
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 is super weird - setInterval returns NodeJS.Timeout, but clearInterval expects number. Not sure what's up?

clearInterval(this._changeInterval);
if (!this._watchers.length) {
return Promise.resolve();