Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: show type on invalid semver error #559

Merged
merged 8 commits into from May 12, 2023
26 changes: 26 additions & 0 deletions .eslintrc.local.js
@@ -0,0 +1,26 @@
'use strict'

module.exports = {
rules: {
'import/no-extraneous-dependencies': [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this may not be required. checking

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yep. I think this config is useful though? The default would allow importing from devDependencies

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file is already imported as a plugin, so the extra dependency isn't needed. I think a single new rule is all we need here with an allow of ./test and map.js.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then this needs to be addressed in template-oss, that's not a rule that we'd want to only apply to a single one of our over 80 repos.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wraithgar so given this doesn't follow the standard structure npm/eslint-config#67 doesn't have any effect here, so is the custom config in this PR good to go?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we'll need a custom config but this one looks to be a bit different in approach than the one we landed on in eslint-config. We should pattern it after the one there but instead of lib and bin it should be bin, classes, functions, internal, and ranges (and now we begin to see why we standardized on lib).

The way this PR currently does it is by completely bypassing the no-extraneous-dependencies rule for the test files, which is a regression.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. done!

'error',
{
devDependencies: false,
optionalDependencies: false,
peerDependencies: false,
bundledDependencies: false,
includeInternal: true,
},
],
'import/no-nodejs-modules': ['error'],
},
overrides: [
{
files: ['**/test/**', '.eslintrc.js', '.eslintrc.local.js'],
rules: {
'import/no-extraneous-dependencies': 0,
'import/no-nodejs-modules': 0,
},
},
],
}
2 changes: 1 addition & 1 deletion classes/semver.js
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}".`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"must be a string or a SemVer" would be more accurate

}

if (version.length > MAX_LENGTH) {
Expand Down
144 changes: 79 additions & 65 deletions test/classes/semver.js
Expand Up @@ -5,52 +5,60 @@ const comparisons = require('../fixtures/comparisons.js')
const equality = require('../fixtures/equality.js')
const invalidVersions = require('../fixtures/invalid-versions')

test('comparisons', t => {
test('comparisons', (t) => {
t.plan(comparisons.length)
comparisons.forEach(([v0, v1, opt]) => t.test(`${v0} ${v1}`, t => {
const s0 = new SemVer(v0, opt)
const s1 = new SemVer(v1, opt)
t.equal(s0.compare(s1), 1)
t.equal(s0.compare(v1), 1)
t.equal(s1.compare(s0), -1)
t.equal(s1.compare(v0), -1)
t.equal(s0.compare(v0), 0)
t.equal(s1.compare(v1), 0)
t.end()
}))
comparisons.forEach(([v0, v1, opt]) =>
tjenkinson marked this conversation as resolved.
Show resolved Hide resolved
t.test(`${v0} ${v1}`, (t) => {
const s0 = new SemVer(v0, opt)
const s1 = new SemVer(v1, opt)
t.equal(s0.compare(s1), 1)
t.equal(s0.compare(v1), 1)
t.equal(s1.compare(s0), -1)
t.equal(s1.compare(v0), -1)
t.equal(s0.compare(v0), 0)
t.equal(s1.compare(v1), 0)
t.end()
})
)
})

test('equality', t => {
test('equality', (t) => {
t.plan(equality.length)
equality.forEach(([v0, v1, loose]) => t.test(`${v0} ${v1} ${loose}`, t => {
const s0 = new SemVer(v0, loose)
const s1 = new SemVer(v1, loose)
t.equal(s0.compare(s1), 0)
t.equal(s1.compare(s0), 0)
t.equal(s0.compare(v1), 0)
t.equal(s1.compare(v0), 0)
t.equal(s0.compare(s0), 0)
t.equal(s1.compare(s1), 0)
t.equal(s0.comparePre(s1), 0, 'comparePre just to hit that code path')
t.end()
}))
equality.forEach(([v0, v1, loose]) =>
t.test(`${v0} ${v1} ${loose}`, (t) => {
const s0 = new SemVer(v0, loose)
const s1 = new SemVer(v1, loose)
t.equal(s0.compare(s1), 0)
t.equal(s1.compare(s0), 0)
t.equal(s0.compare(v1), 0)
t.equal(s1.compare(v0), 0)
t.equal(s0.compare(s0), 0)
t.equal(s1.compare(s1), 0)
t.equal(s0.comparePre(s1), 0, 'comparePre just to hit that code path')
t.end()
})
)
})

test('toString equals parsed version', t => {
test('toString equals parsed version', (t) => {
t.equal(String(new SemVer('v1.2.3')), '1.2.3')
t.end()
})

test('throws when presented with garbage', t => {
test('throws when presented with garbage', (t) => {
t.plan(invalidVersions.length)
invalidVersions.forEach(([v, msg, opts]) =>
t.throws(() => new SemVer(v, opts), msg))
t.throws(() => new SemVer(v, opts), msg)
)
})

test('return SemVer arg to ctor if options match', t => {
test('return SemVer arg to ctor if options match', (t) => {
const s = new SemVer('1.2.3', { loose: true, includePrerelease: true })
t.equal(new SemVer(s, { loose: true, includePrerelease: true }), s,
'get same object when options match')
t.equal(
new SemVer(s, { loose: true, includePrerelease: true }),
s,
'get same object when options match'
)
t.not(new SemVer(s), s, 'get new object when options match')
t.end()
})
Expand All @@ -62,37 +70,39 @@ 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()
})

test('incrementing', t => {
test('incrementing', (t) => {
t.plan(increments.length)
increments.forEach(([
version,
inc,
expect,
options,
id,
base,
]) => t.test(`${version} ${inc} ${id || ''}`.trim(), t => {
t.plan(1)
if (expect === null) {
t.throws(() => new SemVer(version, options).inc(inc, id, base))
} else {
t.equal(new SemVer(version, options).inc(inc, id, base).version, expect)
}
}))
increments.forEach(([version, inc, expect, options, id, base]) =>
t.test(`${version} ${inc} ${id || ''}`.trim(), (t) => {
t.plan(1)
if (expect === null) {
t.throws(() => new SemVer(version, options).inc(inc, id, base))
} else {
t.equal(
new SemVer(version, options).inc(inc, id, base).version,
expect
)
}
})
)
})

test('compare main vs pre', (t) => {
Expand All @@ -113,15 +123,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
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
Expand Up @@ -6,6 +6,7 @@ const ignore = [
'.github',
'.commitlintrc.js',
'.eslintrc.js',
'.eslintrc.local.js',
'node_modules',
'coverage',
'tap-snapshots',
Expand Down