Skip to content

Commit

Permalink
Merge pull request #100 from webpack/type-and-lint
Browse files Browse the repository at this point in the history
Type and lint
  • Loading branch information
sokra authored Jun 14, 2019
2 parents af06702 + 3debffd commit 2b321a4
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 82 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
sudo: false
language: node_js
node_js:
- "12"
- "10"
- "8"
- "6"
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License

Copyright (c) Tobias Koppers @sokra
Copyright JS Foundation and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
28 changes: 10 additions & 18 deletions lib/__tests__/AsyncParallelHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,21 @@ const AsyncParallelHook = require("../AsyncParallelHook");
const AsyncParallelBailHook = require("../AsyncParallelBailHook");

describe("AsyncParallelHook", () => {
it(
"should have to correct behavior",
async () => {
const tester = new HookTester(args => new AsyncParallelHook(args));
it("should have to correct behavior", async () => {
const tester = new HookTester(args => new AsyncParallelHook(args));

const result = await tester.run();
const result = await tester.run();

expect(result).toMatchSnapshot();
},
15000
);
expect(result).toMatchSnapshot();
}, 15000);
});

describe("AsyncParallelBailHook", () => {
it(
"should have to correct behavior",
async () => {
const tester = new HookTester(args => new AsyncParallelBailHook(args));
it("should have to correct behavior", async () => {
const tester = new HookTester(args => new AsyncParallelBailHook(args));

const result = await tester.run();
const result = await tester.run();

expect(result).toMatchSnapshot();
},
15000
);
expect(result).toMatchSnapshot();
}, 15000);
});
56 changes: 20 additions & 36 deletions lib/__tests__/SyncHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,41 @@ const SyncWaterfallHook = require("../SyncWaterfallHook");
const SyncLoopHook = require("../SyncLoopHook");

describe("SyncHook", () => {
it(
"should have to correct behavior",
async () => {
const tester = new HookTester(args => new SyncHook(args));
it("should have to correct behavior", async () => {
const tester = new HookTester(args => new SyncHook(args));

const result = await tester.run(true);
const result = await tester.run(true);

expect(result).toMatchSnapshot();
},
15000
);
expect(result).toMatchSnapshot();
}, 15000);
});

describe("SyncBailHook", () => {
it(
"should have to correct behavior",
async () => {
const tester = new HookTester(args => new SyncBailHook(args));
it("should have to correct behavior", async () => {
const tester = new HookTester(args => new SyncBailHook(args));

const result = await tester.run(true);
const result = await tester.run(true);

expect(result).toMatchSnapshot();
},
15000
);
expect(result).toMatchSnapshot();
}, 15000);
});

describe("SyncWaterfallHook", () => {
it(
"should have to correct behavior",
async () => {
const tester = new HookTester(args => new SyncWaterfallHook(args));
it("should have to correct behavior", async () => {
const tester = new HookTester(args => new SyncWaterfallHook(args));

const result = await tester.run(true);
const result = await tester.run(true);

expect(result).toMatchSnapshot();
},
15000
);
expect(result).toMatchSnapshot();
}, 15000);
});

describe("SyncLoopHook", () => {
it(
"should have to correct behavior",
async () => {
const tester = new HookTester(args => new SyncLoopHook(args));
it("should have to correct behavior", async () => {
const tester = new HookTester(args => new SyncLoopHook(args));

const result = await tester.runForLoop(true);
const result = await tester.runForLoop(true);

expect(result).toMatchSnapshot();
},
15000
);
expect(result).toMatchSnapshot();
}, 15000);
});
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
"types": "./tapable.d.ts",
"scripts": {
"test": "jest",
"travis": "jest --coverage && codecov",
"pretty": "prettier --write lib/*.js lib/__tests__/*.js"
"travis": "yarn pretty-lint && jest --coverage && codecov",
"pretty-lint": "prettier --check lib/*.js lib/__tests__/*.js",
"pretty": "prettier --loglevel warn --write lib/*.js lib/__tests__/*.js"
},
"jest": {
"transform": {
Expand Down
48 changes: 23 additions & 25 deletions tapable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Append<T extends any[], U> = {
7: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], U];
8: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], T[7], U];
}[Measure<T["length"]>];
type AsArray<T> = T extends any[] ? T : [T];

type Callback<E, T> = (error?: E, result?: T) => void;

Expand All @@ -40,44 +41,41 @@ interface HookInterceptor<H> {

type ArgumentNames<T extends any[]> = FixedSizeArray<T["length"], string>;

declare class Hook<T extends any[], R> {
constructor(args?: ArgumentNames<T>);
declare class Hook<T, R> {
constructor(args?: ArgumentNames<AsArray<T>>);
intercept(interceptor: HookInterceptor<Hook<T, R>>): void;
isUsed(): boolean;
callAsync(...args: Append<T, Callback<Error, R>>): void;
promise(...args: T): Promise<R>;
tap(options: string | Tap, fn: (...args: T) => R): void;
callAsync(...args: Append<AsArray<T>, Callback<Error, R>>): void;
promise(...args: AsArray<T>): Promise<R>;
tap(options: string | Tap, fn: (...args: AsArray<T>) => R): void;
withOptions(options: TapOptions): Hook<T, R>;
}

export class SyncHook<T extends any[], R = void> extends Hook<T, R> {
call(...args: T): R;
export class SyncHook<T, R = void> extends Hook<T, R> {
call(...args: AsArray<T>): R;
}

export class SyncBailHook<T extends any[], R> extends SyncHook<T, R> {}
export class SyncLoopHook<T extends any[]> extends SyncHook<T, void> {}
export class SyncWaterfallHook<T extends any[]> extends SyncHook<T, T[0]> {}
export class SyncBailHook<T, R> extends SyncHook<T, R> {}
export class SyncLoopHook<T> extends SyncHook<T, void> {}
export class SyncWaterfallHook<T> extends SyncHook<T, AsArray<T>[0]> {}

declare class AsyncHook<T extends any[], R> extends Hook<T, R> {
declare class AsyncHook<T, R> extends Hook<T, R> {
tapAsync(
options: string | Tap,
fn: (...args: Append<T, Callback<Error, R>>) => void
fn: (...args: Append<AsArray<T>, Callback<Error, R>>) => void
): void;
tapPromise(
options: string | Tap,
fn: (...args: AsArray<T>) => Promise<R>
): void;
tapPromise(options: string | Tap, fn: (...args: T) => Promise<R>): void;
}

export class AsyncParallelHook<T extends any[]> extends AsyncHook<T, void> {}
export class AsyncParallelBailHook<T extends any[], R> extends AsyncHook<
T,
R
> {}
export class AsyncSeriesHook<T extends any[]> extends AsyncHook<T, void> {}
export class AsyncSeriesBailHook<T extends any[], R> extends AsyncHook<T, R> {}
export class AsyncSeriesLoopHook<T extends any[]> extends AsyncHook<T, void> {}
export class AsyncSeriesWaterfallHook<T extends any[]> extends AsyncHook<
T,
T[0]
> {}
export class AsyncParallelHook<T> extends AsyncHook<T, void> {}
export class AsyncParallelBailHook<T, R> extends AsyncHook<T, R> {}
export class AsyncSeriesHook<T> extends AsyncHook<T, void> {}
export class AsyncSeriesBailHook<T, R> extends AsyncHook<T, R> {}
export class AsyncSeriesLoopHook<T> extends AsyncHook<T, void> {}
export class AsyncSeriesWaterfallHook<T> extends AsyncHook<T, AsArray<T>[0]> {}

type HookFactory<H> = (key: any, hook?: H) => H;

Expand Down

0 comments on commit 2b321a4

Please sign in to comment.