Skip to content

Commit

Permalink
Require Node.js 10
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 2, 2020
1 parent f0f4638 commit 61999a4
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 26 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -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'
6 changes: 3 additions & 3 deletions examples/screenshot.js
Expand Up @@ -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) + ' ');
}
10 changes: 4 additions & 6 deletions index.d.ts
Expand Up @@ -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.
Expand Down
16 changes: 10 additions & 6 deletions index.test-d.ts
Expand Up @@ -6,16 +6,20 @@ type colorReturn = chalk.Chalk & {supportsColor?: never};

// - supportsColor -
expectType<chalk.ColorSupport | false>(chalk.supportsColor);
expectType<boolean>((chalk.supportsColor as chalk.ColorSupport).hasBasic);
expectType<boolean>((chalk.supportsColor as chalk.ColorSupport).has256);
expectType<boolean>((chalk.supportsColor as chalk.ColorSupport).has16m);
if (chalk.supportsColor) {
expectType<boolean>(chalk.supportsColor.hasBasic);
expectType<boolean>(chalk.supportsColor.has256);
expectType<boolean>(chalk.supportsColor.has16m);
}

// - stderr -
expectType<chalk.Chalk>(chalk.stderr);
expectType<chalk.ColorSupport | false>(chalk.stderr.supportsColor);
expectType<boolean>((chalk.stderr.supportsColor as chalk.ColorSupport).hasBasic);
expectType<boolean>((chalk.stderr.supportsColor as chalk.ColorSupport).has256);
expectType<boolean>((chalk.stderr.supportsColor as chalk.ColorSupport).has16m);
if (chalk.stderr.supportsColor) {
expectType<boolean>(chalk.stderr.supportsColor.hasBasic);
expectType<boolean>(chalk.stderr.supportsColor.has256);
expectType<boolean>(chalk.stderr.supportsColor.has16m);
}

// -- `stderr` is not a member of the Chalk interface --
expectError(chalk.reset.stderr);
Expand Down
12 changes: 8 additions & 4 deletions package.json
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
}
}
}
3 changes: 2 additions & 1 deletion source/index.js
Expand Up @@ -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');
}

Expand All @@ -28,6 +28,7 @@ const applyOptions = (object, options = {}) => {

class ChalkClass {
constructor(options) {
// eslint-disable-next-line no-constructor-return
return chalkFactory(options);
}
}
Expand Down
6 changes: 3 additions & 3 deletions source/templates.js
Expand Up @@ -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'],
Expand Down Expand Up @@ -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('');
Expand Down
4 changes: 2 additions & 2 deletions test/template-literal.js
Expand Up @@ -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 => {
Expand Down

0 comments on commit 61999a4

Please sign in to comment.