Skip to content

Commit

Permalink
Fix: ensure "version" always has a valid value in parseOptions (#109)
Browse files Browse the repository at this point in the history
The rule no-unsupported-features would sometimes fail with a TypeError:
```
Error while loading rule 'node/no-unsupported-features': Cannot read property 'split' of null
TypeError: Error while loading rule 'node/no-unsupported-features': Cannot read property 'split' of null
    at new Range (.../node_modules/semver/semver.js:780:20)
    at Function.intersects (.../node_modules/semver/semver.js:1305:8)
    at Object.freeze.features.Object.freeze.OPTIONS.reduce (.../node_modules/eslint-plugin-node/lib/rules/no-unsupported-features.js:191:44)
    at Array.reduce (<anonymous>)
    at parseOptions (.../node_modules/eslint-plugin-node/lib/rules/no-unsupported-features.js:175:41)
    at Object.create (.../node_modules/eslint-plugin-node/lib/rules/no-unsupported-features.js:281:25)
```

This happens, when neither "engines" are set in the "package.json",
nor "version" is set in the rule configuration but the rule still has
configuration options, because version will then be set to
`options.version`, even if that is `undefined`.

This commit ensures that `version` always has a valid value before
trying to construct a semver range from it.
  • Loading branch information
ZauberNerd authored and mysticatea committed Feb 23, 2018
1 parent bbf4b60 commit 234703c
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/rules/no-unsupported-features.js
Expand Up @@ -104,7 +104,7 @@ function getDefaultVersion(filename) {
const info = getPackageJson(filename)
const nodeVersion = info && info.engines && info.engines.node

return nodeVersion ? semver.validRange(nodeVersion) : null
return semver.validRange(nodeVersion) || DEFAULT_VERSION
}

/**
Expand Down Expand Up @@ -147,7 +147,7 @@ function isIgnored(key, ignores) {
* @returns {object} Parsed value.
*/
function parseOptions(options, defaultVersion) {
let version = defaultVersion ? null : DEFAULT_VERSION
let version = null
let range = null
let ignores = []

Expand Down

0 comments on commit 234703c

Please sign in to comment.