Function drawSettings
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
function drawSettings () {
document.getElementById('wpm').value = options.getProp('wpm')
document.getElementById('slowStartCount').value = options.getProp('slowStartCount')
document.getElementById('sentenceDelay').value = options.getProp('sentenceDelay')
document.getElementById('otherPuncDelay').value = options.getProp('otherPuncDelay')
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function listenForKey
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
function listenForKey (keyboardEvent) {
keyboardEvent.stopPropagation()
var key = keyboardEvent.key || ''
if (['Alt', 'OS', 'Control', 'Meta', 'Shift'].some(s => key === s)) return
var modifier = ''
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Unnecessary use of conditional expression for default assignment. Open
modEl.value = modifier ? modifier : ''
- Read upRead up
- Exclude checks
disallow ternary operators when simpler alternatives exist (no-unneeded-ternary)
It's a common mistake in JavaScript to use a conditional expression to select between two Boolean values instead of using ! to convert the test to a Boolean. Here are some examples:
// Bad
var isYes = answer === 1 ? true : false;
// Good
var isYes = answer === 1;
// Bad
var isNo = answer === 1 ? false : true;
// Good
var isNo = answer !== 1;
Another common mistake is using a single variable as both the conditional test and the consequent. In such cases, the logical OR
can be used to provide the same functionality.
Here is an example:
// Bad
foo(bar ? bar : 1);
// Good
foo(bar || 1);
Rule Details
This rule disallow ternary operators when simpler alternatives exist.
Examples of incorrect code for this rule:
/*eslint no-unneeded-ternary: "error"*/
var a = x === 2 ? true : false;
var a = x ? true : false;
Examples of correct code for this rule:
/*eslint no-unneeded-ternary: "error"*/
var a = x === 2 ? "Yes" : "No";
var a = x !== false;
var a = x ? "Yes" : "No";
var a = x ? y : x;
f(x ? x : 1); // default assignment - would be disallowed if defaultAssignment option set to false. See option details below.
Options
This rule has an object option:
-
"defaultAssignment": true
(default) allows the conditional expression as a default assignment pattern -
"defaultAssignment": false
disallows the conditional expression as a default assignment pattern
defaultAssignment
When set to true
, which it is by default, The defaultAssignment option allows expressions of the form x ? x : expr
(where x
is any identifier and expr
is any expression).
Examples of additional incorrect code for this rule with the { "defaultAssignment": false }
option:
/*eslint no-unneeded-ternary: ["error", { "defaultAssignment": false }]*/
var a = x ? x : 1;
f(x ? x : 1);
Note that defaultAssignment: false
still allows expressions of the form x ? expr : x
(where the identifier is on the right hand side of the ternary).
When Not To Use It
You can turn this rule off if you are not concerned with unnecessary complexity in conditional expressions.
Related Rules
- [no-ternary](no-ternary.md)
- [no-nested-ternary](no-nested-ternary.md) Source: http://eslint.org/docs/rules/