Skip to content

Commit

Permalink
fix: show type on invalid semver error (#559)
Browse files Browse the repository at this point in the history
Add linting rules to prevent regression in using node internals.
  • Loading branch information
tjenkinson authored May 12, 2023
1 parent 09c69e2 commit d30d25a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 20 deletions.
18 changes: 18 additions & 0 deletions .eslintrc.local.js
Original file line number Diff line number Diff line change
@@ -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'],
},
},
],
}
2 changes: 1 addition & 1 deletion classes/semver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
44 changes: 26 additions & 18 deletions test/classes/semver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion test/functions/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down
1 change: 1 addition & 0 deletions test/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const ignore = [
'.github',
'.commitlintrc.js',
'.eslintrc.js',
'.eslintrc.local.js',
'node_modules',
'coverage',
'tap-snapshots',
Expand Down

0 comments on commit d30d25a

Please sign in to comment.