From 7c6f83f719b241b7a1a1cd54b256d123e53eab4a Mon Sep 17 00:00:00 2001 From: Saad Quadri Date: Tue, 17 Apr 2018 00:26:17 -0400 Subject: [PATCH] Add Flow type definitions (#260) --- .flowconfig | 5 +++ index.js.flow | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 9 +++-- test/_flow.js | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 201 insertions(+), 2 deletions(-) create mode 100644 .flowconfig create mode 100644 index.js.flow create mode 100644 test/_flow.js diff --git a/.flowconfig b/.flowconfig new file mode 100644 index 0000000..2318f0d --- /dev/null +++ b/.flowconfig @@ -0,0 +1,5 @@ +[ignore] +.*/node_modules/.* + +[options] +suppress_comment= \\(.\\|\n\\)*\\$ExpectError diff --git a/index.js.flow b/index.js.flow new file mode 100644 index 0000000..a55cf1d --- /dev/null +++ b/index.js.flow @@ -0,0 +1,95 @@ +// @flow + +type TemplateStringsArray = $ReadOnlyArray; + +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; diff --git a/package.json b/package.json index 3edc94a..05ab3df 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -61,6 +63,9 @@ "envs": [ "node", "mocha" + ], + "ignores": [ + "test/_flow.js" ] } } diff --git a/test/_flow.js b/test/_flow.js new file mode 100644 index 0000000..2cf3cf2 --- /dev/null +++ b/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);