Skip to content

Commit bc54ec6

Browse files
demurgosphated
authored andcommitted
Update: Add TypeScript definitions (closes #5) (#6)
1 parent 2fabb9e commit bc54ec6

File tree

4 files changed

+234
-3
lines changed

4 files changed

+234
-3
lines changed

index.d.ts

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
declare namespace PluginError {
2+
export interface Constructor {
3+
/**
4+
* @param options Options with plugin name and message
5+
*/
6+
new(options: Options & {plugin: string, message: string}): PluginError;
7+
8+
/**
9+
* @param plugin Plugin name
10+
* @param message Error message
11+
* @param options Error options
12+
*/
13+
new (plugin: string, message: string, options?: Options): PluginError;
14+
15+
/**
16+
* @param plugin Plugin name
17+
* @param error Base error
18+
* @param options Error options
19+
*/
20+
new <E extends Error>(plugin: string, error: E, options?: Options): PluginError<E>;
21+
22+
/**
23+
* @param plugin Plugin name
24+
* @param options Options with message
25+
*/
26+
new(plugin: string, options: Options & {message: string}): PluginError;
27+
}
28+
29+
interface Options {
30+
/**
31+
* Error name
32+
*/
33+
name?: string;
34+
35+
/**
36+
* Error message
37+
*/
38+
message?: any;
39+
40+
/**
41+
* File name where the error occurred
42+
*/
43+
fileName?: string;
44+
45+
46+
/**
47+
* Line number where the error occurred
48+
*/
49+
lineNumber?: number;
50+
51+
/**
52+
* Error properties will be included in err.toString(). Can be omitted by
53+
* setting this to false.
54+
*
55+
* Default: `true`
56+
*/
57+
showProperties?: boolean;
58+
59+
/**
60+
* By default the stack will not be shown. Set this to true if you think the
61+
* stack is important for your error.
62+
*
63+
* Default: `false`
64+
*/
65+
showStack?: boolean;
66+
67+
/**
68+
* Error stack to use for `err.toString()` if `showStack` is `true`.
69+
* By default it uses the `stack` of the original error if you used one, otherwise it captures a new stack.
70+
*/
71+
stack?: string;
72+
}
73+
74+
75+
/**
76+
* The `Base` interface defines the properties available on all the the instances of `PluginError`.
77+
*/
78+
export interface Base extends Error {
79+
/**
80+
* Plugin name
81+
*/
82+
plugin: string;
83+
84+
/**
85+
* Boolean controlling if the stack will be shown in `err.toString()`.
86+
*/
87+
showStack: boolean;
88+
89+
/**
90+
* Boolean controlling if properties will be shown in `err.toString()`.
91+
*/
92+
showProperties: boolean;
93+
94+
/**
95+
* File name where the error occurred
96+
*/
97+
fileName?: string;
98+
99+
/**
100+
* Line number where the error occurred
101+
*/
102+
lineNumber?: number;
103+
}
104+
}
105+
106+
/**
107+
* Abstraction for error handling for Vinyl plugins
108+
*/
109+
type PluginError<T = {}> = PluginError.Base & T;
110+
111+
declare const PluginError: PluginError.Constructor;
112+
113+
export = PluginError;

package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
},
1717
"license": "MIT",
1818
"files": [
19+
"index.d.ts",
1920
"index.js"
2021
],
2122
"main": "index.js",
2223
"engines": {
2324
"node": ">=0.10.0"
2425
},
2526
"scripts": {
26-
"test": "mocha"
27+
"test": "mocha test.js && tsc -p test/types"
2728
},
2829
"dependencies": {
2930
"ansi-cyan": "^0.1.1",
@@ -33,8 +34,9 @@
3334
"extend-shallow": "^1.1.2"
3435
},
3536
"devDependencies": {
36-
"mocha": "*",
37-
"should": "*"
37+
"mocha": "^2.5.3",
38+
"should": "^13.2.0",
39+
"typescript": "^2.6.2"
3840
},
3941
"keywords": [
4042
"error",

test/types/test.ts

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import PluginError = require("plugin-error");
2+
3+
{
4+
// Check constructor signatures
5+
// See: https://github.com/gulpjs/gulp-util#new-pluginerrorpluginname-message-options
6+
{
7+
const err = new PluginError("test", {
8+
message: "something broke",
9+
});
10+
}
11+
12+
{
13+
const err = new PluginError({
14+
plugin: "test",
15+
message: "something broke",
16+
});
17+
}
18+
19+
{
20+
const err = new PluginError("test", "something broke");
21+
}
22+
23+
{
24+
const err = new PluginError("test", "something broke", {showStack: true});
25+
}
26+
27+
{
28+
const existingError = new Error("OMG");
29+
const err = new PluginError("test", existingError, {showStack: true});
30+
}
31+
}
32+
33+
{
34+
{
35+
// Check available properties
36+
const realErr = Object.assign(new Error("something broke"), {fileName: "original.js"});
37+
const err = new PluginError("test", realErr, {showStack: true, fileName: "override.js"});
38+
const plugin: string = err.plugin;
39+
const message: string = err.message;
40+
const fileName: string = err.fileName;
41+
const showStack: boolean = err.showStack;
42+
const showProperties: boolean = err.showProperties;
43+
}
44+
{
45+
// Inference of custom properties from `error` argument,
46+
const realErr = Object.assign(new Error("something broke"), {abstractProperty: "abstract"});
47+
const err = new PluginError("test", realErr, realErr);
48+
const plugin: string = err.plugin;
49+
const message: string = err.message;
50+
const abstractProperty: string = err.abstractProperty;
51+
}
52+
}
53+

test/types/tsconfig.json

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": false,
4+
"allowSyntheticDefaultImports": false,
5+
"allowUnreachableCode": false,
6+
"allowUnusedLabels": false,
7+
"alwaysStrict": true,
8+
"baseUrl": "../..",
9+
"charset": "utf8",
10+
"checkJs": false,
11+
"declaration": true,
12+
"disableSizeLimit": false,
13+
"downlevelIteration": false,
14+
"emitBOM": false,
15+
"emitDecoratorMetadata": true,
16+
"experimentalDecorators": true,
17+
"forceConsistentCasingInFileNames": true,
18+
"importHelpers": false,
19+
"inlineSourceMap": false,
20+
"inlineSources": false,
21+
"isolatedModules": false,
22+
"lib": [
23+
"es2017"
24+
],
25+
"locale": "en-us",
26+
"module": "commonjs",
27+
"moduleResolution": "node",
28+
"newLine": "lf",
29+
"noEmit": true,
30+
"noEmitHelpers": false,
31+
"noEmitOnError": true,
32+
"noErrorTruncation": true,
33+
"noFallthroughCasesInSwitch": true,
34+
"noImplicitAny": true,
35+
"noImplicitReturns": true,
36+
"noImplicitThis": true,
37+
"noStrictGenericChecks": false,
38+
"noUnusedLocals": false,
39+
"noUnusedParameters": false,
40+
"noLib": false,
41+
"noResolve": false,
42+
"paths": {
43+
"plugin-error": [
44+
"index.d.ts"
45+
]
46+
},
47+
"preserveConstEnums": true,
48+
"removeComments": false,
49+
"rootDir": "",
50+
"skipLibCheck": false,
51+
"sourceMap": true,
52+
"strict": true,
53+
"strictNullChecks": true,
54+
"suppressExcessPropertyErrors": false,
55+
"suppressImplicitAnyIndexErrors": false,
56+
"target": "es2017",
57+
"traceResolution": false
58+
},
59+
"include": [
60+
"./**/*.ts"
61+
],
62+
"exclude": []
63+
}

0 commit comments

Comments
 (0)