Skip to content

Commit

Permalink
Add Flow type definitions (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
saadq authored and sindresorhus committed Apr 17, 2018
1 parent 58483b5 commit 7c6f83f
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .flowconfig
@@ -0,0 +1,5 @@
[ignore]
.*/node_modules/.*

[options]
suppress_comment= \\(.\\|\n\\)*\\$ExpectError
95 changes: 95 additions & 0 deletions index.js.flow
@@ -0,0 +1,95 @@
// @flow

type TemplateStringsArray = $ReadOnlyArray<string>;

export type Level = $Values<{
None: 0,
Basic: 1,
Ansi256: 2,
TrueColor: 3
}>;

export type ChalkOptions = {|
enabled?: boolean,
level?: Level
|};

export type ColorSupport = {|
level: Level,
hasBasic: boolean,
has256: boolean,
has16m: boolean
|};

export interface Chalk {
(...text: string[]): string,
(text: TemplateStringsArray, ...placeholders: string[]): string,
constructor(options?: ChalkOptions): Chalk,
enabled: boolean,
level: Level,
rgb(r: number, g: number, b: number): Chalk,
hsl(h: number, s: number, l: number): Chalk,
hsv(h: number, s: number, v: number): Chalk,
hwb(h: number, w: number, b: number): Chalk,
bgHex(color: string): Chalk,
bgKeyword(color: string): Chalk,
bgRgb(r: number, g: number, b: number): Chalk,
bgHsl(h: number, s: number, l: number): Chalk,
bgHsv(h: number, s: number, v: number): Chalk,
bgHwb(h: number, w: number, b: number): Chalk,
hex(color: string): Chalk,
keyword(color: string): Chalk,

+reset: Chalk,
+bold: Chalk,
+dim: Chalk,
+italic: Chalk,
+underline: Chalk,
+inverse: Chalk,
+hidden: Chalk,
+strikethrough: Chalk,

+visible: Chalk,

+black: Chalk,
+red: Chalk,
+green: Chalk,
+yellow: Chalk,
+blue: Chalk,
+magenta: Chalk,
+cyan: Chalk,
+white: Chalk,
+gray: Chalk,
+grey: Chalk,
+blackBright: Chalk,
+redBright: Chalk,
+greenBright: Chalk,
+yellowBright: Chalk,
+blueBright: Chalk,
+magentaBright: Chalk,
+cyanBright: Chalk,
+whiteBright: Chalk,

+bgBlack: Chalk,
+bgRed: Chalk,
+bgGreen: Chalk,
+bgYellow: Chalk,
+bgBlue: Chalk,
+bgMagenta: Chalk,
+bgCyan: Chalk,
+bgWhite: Chalk,
+bgBlackBright: Chalk,
+bgRedBright: Chalk,
+bgGreenBright: Chalk,
+bgYellowBright: Chalk,
+bgBlueBright: Chalk,
+bgMagentaBright: Chalk,
+bgCyanBright: Chalk,
+bgWhiteBrigh: Chalk,

supportsColor: ColorSupport
};

declare var chalk: Chalk;

export default chalk;
9 changes: 7 additions & 2 deletions package.json
Expand Up @@ -8,14 +8,15 @@
"node": ">=4"
},
"scripts": {
"test": "xo && tsc --project types && nyc ava",
"test": "xo && tsc --project types && flow --max-warnings=0 && nyc ava",
"bench": "matcha benchmark.js",
"coveralls": "nyc report --reporter=text-lcov | coveralls"
},
"files": [
"index.js",
"templates.js",
"types/index.d.ts"
"types/index.d.ts",
"index.js.flow"
],
"keywords": [
"color",
Expand Down Expand Up @@ -49,6 +50,7 @@
"ava": "*",
"coveralls": "^3.0.0",
"execa": "^0.9.0",
"flow-bin": "^0.68.0",
"import-fresh": "^2.0.0",
"matcha": "^0.7.0",
"nyc": "^11.0.2",
Expand All @@ -61,6 +63,9 @@
"envs": [
"node",
"mocha"
],
"ignores": [
"test/_flow.js"
]
}
}
94 changes: 94 additions & 0 deletions test/_flow.js
@@ -0,0 +1,94 @@
// @flow
import chalk from '..';

// $ExpectError (Can't have typo in option name)
chalk.constructor({levl: 1});
chalk.constructor({level: 1});

// $ExpectError (Option must have proper type)
new chalk.constructor({enabled: 'true'});
new chalk.constructor({enabled: true});

// $ExpectError (Can't pass in null)
chalk.underline(null);
chalk.underline('foo');

// $ExpectError (Can't have typo in chalk method)
chalk.rd('foo');
chalk.red('foo');

// $ExpectError (Can't have typo in chalk method)
chalk.gren`foo`;
chalk.green`foo`;

// $ExpectError (Can't have typo in chalk method)
chalk.red.bgBlu.underline('foo');
chalk.red.bgBlue.underline('foo');

// $ExpectError (Level must be 0, 1, 2, or 3)
const badCtx = chalk.constructor({level: 4});
const ctx = chalk.constructor({level: 3});

// $ExpectError (Can't pass in null)
ctx(null);
ctx('foo');

// $ExpectError (Can't have typo in method name)
ctx.gry('foo');
ctx.grey('foo');

// $ExpectError (Can't have typo in method name)
ctx`foo`.value();
ctx`foo`.valueOf();

// $ExpectError (Can't have typo in property name)
chalk.abled = true;
chalk.enabled = true;

// $ExpectError (Can't use invalid Level for property setter)
chalk.level = 10;
chalk.level = 1;

const chalkInstance = new chalk.constructor();

// $ExpectError (Can't have typo in method name)
chalkInstance.blu('foo');
chalkInstance.blue('foo');
chalkInstance`foo`;

// $ExpectError (Can't have typo in method name)
chalk.keywrd('orange').bgBlue('foo');
chalk.keyword('orange').bgBlue('foo');

// $ExpectError (rgb should take in 3 numbers)
chalk.rgb(1, 14).bgBlue('foo');
chalk.rgb(1, 14, 9).bgBlue('foo');

// $ExpectError (hsl should take in 3 numbers)
chalk.hsl(1, 14, '9').bgBlue('foo');
chalk.hsl(1, 14, 9).bgBlue('foo');

// $ExpectError (hsv should take in 3 numbers)
chalk.hsv(1, 14).bgBlue('foo');
chalk.hsv(1, 14, 9).bgBlue('foo');

// $ExpectError (hwb should take in 3 numbers)
chalk.hwb(1, 14).bgBlue('foo');
chalk.hwb(1, 14, 9).bgBlue('foo');

// $ExpectError (Can't have typo in method name)
chalk.visibl('foo');
chalk.visible('foo');

// $ExpectError (Can't have typo in method name)
chalk.red.visibl('foo');
chalk.red.visible('foo');
chalk.visible.red('foo');

// $ExpectError (Can't write to readonly property)
chalk.black = 'foo';
chalk.black;

// $ExpectError (Can't write to readonly property)
chalk.reset = 'foo';
console.log(chalk.reset);

0 comments on commit 7c6f83f

Please sign in to comment.