From d30d25a5c1fb963c3cc9178cb1769fe45e4a3cab Mon Sep 17 00:00:00 2001 From: Tom Jenkinson Date: Fri, 12 May 2023 16:50:08 +0100 Subject: [PATCH] fix: show type on invalid semver error (#559) Add linting rules to prevent regression in using node internals. --- .eslintrc.local.js | 18 +++++++++++++++++ classes/semver.js | 2 +- test/classes/semver.js | 44 ++++++++++++++++++++++++----------------- test/functions/parse.js | 2 +- test/map.js | 1 + 5 files changed, 47 insertions(+), 20 deletions(-) create mode 100644 .eslintrc.local.js diff --git a/.eslintrc.local.js b/.eslintrc.local.js new file mode 100644 index 00000000..5b7c98ea --- /dev/null +++ b/.eslintrc.local.js @@ -0,0 +1,18 @@ +'use strict' + +module.exports = { + overrides: [ + { + files: ['bin/**', 'classes/**', 'functions/**', 'internal/**', 'ranges/**'], + rules: { + 'import/no-extraneous-dependencies': [ + 'error', + { + devDependencies: false, + }, + ], + 'import/no-nodejs-modules': ['error'], + }, + }, + ], +} diff --git a/classes/semver.js b/classes/semver.js index 25ee889d..99dbe82d 100644 --- a/classes/semver.js +++ b/classes/semver.js @@ -16,7 +16,7 @@ class SemVer { version = version.version } } else if (typeof version !== 'string') { - throw new TypeError(`Invalid Version: ${require('util').inspect(version)}`) + throw new TypeError(`Invalid version. Must be a string. Got type "${typeof version}".`) } if (version.length > MAX_LENGTH) { diff --git a/test/classes/semver.js b/test/classes/semver.js index 4556434c..6be0ac8f 100644 --- a/test/classes/semver.js +++ b/test/classes/semver.js @@ -62,15 +62,19 @@ test('really big numeric prerelease value', (t) => { }) test('invalid version numbers', (t) => { - ['1.2.3.4', - 'NOT VALID', - 1.2, - null, - 'Infinity.NaN.Infinity', - ].forEach((v) => { - t.throws(() => { - new SemVer(v) // eslint-disable-line no-new - }, { name: 'TypeError', message: `Invalid Version: ${v}` }) + ['1.2.3.4', 'NOT VALID', 1.2, null, 'Infinity.NaN.Infinity'].forEach((v) => { + t.throws( + () => { + new SemVer(v) // eslint-disable-line no-new + }, + { + name: 'TypeError', + message: + typeof v === 'string' + ? `Invalid Version: ${v}` + : `Invalid version. Must be a string. Got type "${typeof v}".`, + } + ) }) t.end() @@ -113,15 +117,19 @@ test('compare main vs pre', (t) => { }) test('invalid version numbers', (t) => { - ['1.2.3.4', - 'NOT VALID', - 1.2, - null, - 'Infinity.NaN.Infinity', - ].forEach((v) => { - t.throws(() => { - new SemVer(v) // eslint-disable-line no-new - }, { name: 'TypeError', message: `Invalid Version: ${v}` }) + ['1.2.3.4', 'NOT VALID', 1.2, null, 'Infinity.NaN.Infinity'].forEach((v) => { + t.throws( + () => { + new SemVer(v) // eslint-disable-line no-new + }, + { + name: 'TypeError', + message: + typeof v === 'string' + ? `Invalid Version: ${v}` + : `Invalid version. Must be a string. Got type "${typeof v}".`, + } + ) }) t.end() diff --git a/test/functions/parse.js b/test/functions/parse.js index f3ca4cd8..dd091e94 100644 --- a/test/functions/parse.js +++ b/test/functions/parse.js @@ -20,7 +20,7 @@ t.test('throw errors if asked to', t => { parse([], null, true) }, { name: 'TypeError', - message: 'Invalid Version: []', + message: 'Invalid version. Must be a string. Got type "object".', }) t.end() }) diff --git a/test/map.js b/test/map.js index aded2454..8179e71e 100644 --- a/test/map.js +++ b/test/map.js @@ -6,6 +6,7 @@ const ignore = [ '.github', '.commitlintrc.js', '.eslintrc.js', + '.eslintrc.local.js', 'node_modules', 'coverage', 'tap-snapshots',