diff --git a/.travis.yml b/.travis.yml index 1492647..fd301d7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,5 @@ language: node_js node_js: - '12' - '10' - - '8' after_success: - './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls' diff --git a/examples/screenshot.js b/examples/screenshot.js index 37f5850..fc64030 100644 --- a/examples/screenshot.js +++ b/examples/screenshot.js @@ -4,15 +4,15 @@ const chalk = require('..'); // Generates screenshot for (const key of Object.keys(styles)) { - let ret = key; + let returnValue = key; if (key === 'reset' || key === 'hidden' || key === 'grey') { continue; } if (/^bg[^B]/.test(key)) { - ret = chalk.black(ret); + returnValue = chalk.black(returnValue); } - process.stdout.write(chalk[key](ret) + ' '); + process.stdout.write(chalk[key](returnValue) + ' '); } diff --git a/index.d.ts b/index.d.ts index 2f8271b..df96b03 100644 --- a/index.d.ts +++ b/index.d.ts @@ -91,12 +91,10 @@ declare namespace chalk { level?: Level; } - interface Instance { - /** - Return a new Chalk instance. - */ - new (options?: Options): Chalk; - } + /** + Return a new Chalk instance. + */ + type Instance = new (options?: Options) => Chalk; /** Detect whether the terminal supports color. diff --git a/index.test-d.ts b/index.test-d.ts index 0b26797..ad5dcf5 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -6,16 +6,20 @@ type colorReturn = chalk.Chalk & {supportsColor?: never}; // - supportsColor - expectType(chalk.supportsColor); -expectType((chalk.supportsColor as chalk.ColorSupport).hasBasic); -expectType((chalk.supportsColor as chalk.ColorSupport).has256); -expectType((chalk.supportsColor as chalk.ColorSupport).has16m); +if (chalk.supportsColor) { + expectType(chalk.supportsColor.hasBasic); + expectType(chalk.supportsColor.has256); + expectType(chalk.supportsColor.has16m); +} // - stderr - expectType(chalk.stderr); expectType(chalk.stderr.supportsColor); -expectType((chalk.stderr.supportsColor as chalk.ColorSupport).hasBasic); -expectType((chalk.stderr.supportsColor as chalk.ColorSupport).has256); -expectType((chalk.stderr.supportsColor as chalk.ColorSupport).has16m); +if (chalk.stderr.supportsColor) { + expectType(chalk.stderr.supportsColor.hasBasic); + expectType(chalk.stderr.supportsColor.has256); + expectType(chalk.stderr.supportsColor.has16m); +} // -- `stderr` is not a member of the Chalk interface -- expectError(chalk.reset.stderr); diff --git a/package.json b/package.json index 0a4aaa9..822c963 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "funding": "https://github.com/chalk/chalk?sponsor=1", "main": "source", "engines": { - "node": ">=8" + "node": ">=10" }, "scripts": { "test": "xo && nyc ava && tsd", @@ -47,18 +47,22 @@ "devDependencies": { "ava": "^2.4.0", "coveralls": "^3.0.7", - "execa": "^3.2.0", + "execa": "^4.0.0", "import-fresh": "^3.1.0", "matcha": "^0.7.0", "nyc": "^15.0.0", "resolve-from": "^5.0.0", "tsd": "^0.7.4", - "xo": "^0.25.3" + "xo": "^0.28.2" }, "xo": { "rules": { "unicorn/prefer-string-slice": "off", - "unicorn/prefer-includes": "off" + "unicorn/prefer-includes": "off", + "@typescript-eslint/member-ordering": "off", + "no-redeclare": "off", + "unicorn/string-content": "off", + "unicorn/better-regex": "off" } } } diff --git a/source/index.js b/source/index.js index 203b1b3..e3b2f16 100644 --- a/source/index.js +++ b/source/index.js @@ -17,7 +17,7 @@ const levelMapping = [ const styles = Object.create(null); const applyOptions = (object, options = {}) => { - if (!Number.isInteger(options.level) || options.level > 3 || options.level < 0) { + if (options.level && !(Number.isInteger(options.level) && options.level >= 0 && options.level <= 3)) { throw new Error('The `level` option should be an integer from 0 to 3'); } @@ -28,6 +28,7 @@ const applyOptions = (object, options = {}) => { class ChalkClass { constructor(options) { + // eslint-disable-next-line no-constructor-return return chalkFactory(options); } } diff --git a/source/templates.js b/source/templates.js index fe94642..b130949 100644 --- a/source/templates.js +++ b/source/templates.js @@ -2,7 +2,7 @@ const TEMPLATE_REGEX = /(?:\\(u(?:[a-f\d]{4}|\{[a-f\d]{1,6}\})|x[a-f\d]{2}|.))|(?:\{(~)?(\w+(?:\([^)]*\))?(?:\.\w+(?:\([^)]*\))?)*)(?:[ \t]|(?=\r?\n)))|(\})|((?:.|[\r\n\f])+?)/gi; const STYLE_REGEX = /(?:^|\.)(\w+)(?:\(([^)]*)\))?/g; const STRING_REGEX = /^(['"])((?:\\.|(?!\1)[^\\])*)\1$/; -const ESCAPE_REGEX = /\\(u(?:[a-f\d]{4}|\{[a-f\d]{1,6}\})|x[a-f\d]{2}|.)|([^\\])/gi; +const ESCAPE_REGEX = /\\(u(?:[a-f\d]{4}|{[a-f\d]{1,6}})|x[a-f\d]{2}|.)|([^\\])/gi; const ESCAPES = new Map([ ['n', '\n'], @@ -126,8 +126,8 @@ module.exports = (chalk, temporary) => { chunks.push(chunk.join('')); if (styles.length > 0) { - const errMsg = `Chalk template literal is missing ${styles.length} closing bracket${styles.length === 1 ? '' : 's'} (\`}\`)`; - throw new Error(errMsg); + const errMessage = `Chalk template literal is missing ${styles.length} closing bracket${styles.length === 1 ? '' : 's'} (\`}\`)`; + throw new Error(errMessage); } return chunks.join(''); diff --git a/test/template-literal.js b/test/template-literal.js index ece1428..98e01c6 100644 --- a/test/template-literal.js +++ b/test/template-literal.js @@ -121,8 +121,8 @@ test('correctly parse newline escapes (bug #177)', t => { test('correctly parse escape in parameters (bug #177 comment 318622809)', t => { const instance = new chalk.Instance({level: 0}); - const str = '\\'; - t.is(instance`{blue ${str}}`, '\\'); + const string = '\\'; + t.is(instance`{blue ${string}}`, '\\'); }); test('correctly parses unicode/hex escapes', t => {