no-constant-condition reported error range is too broad
See original GitHub issueTell us about your environment
- ESLint Version: 4.8.0
- Node Version: 8.3.0
- npm Version: 5.3.0
What parser (default, Babel-ESLint, etc.) are you using? default
Please show your full configuration:
Configuration
{
"rules": {
"no-constant-condition": "error"
}
}
What did you do? Please include the actual source code causing the issue.
if (true) {
something();
}
something(true ? 1 : 0);
What did you expect to happen?
The reported error ranges should only include the constant conditions themselves. In the above cases, the words “true”.
What actually happened? Please include the actual, raw output from ESLint.
The reported error ranges include the entire statement/expression that the constant condition is part of.
$ eslint test.js --format json | jq
[
{
"filePath": "/home/lydell/bugs/eslint/test.js",
"messages": [
{
"ruleId": "no-constant-condition",
"severity": 2,
"message": "Unexpected constant condition.",
"line": 1,
"column": 1,
"nodeType": "IfStatement",
"source": "if (true) {",
"endLine": 3,
"endColumn": 2
},
{
"ruleId": "no-constant-condition",
"severity": 2,
"message": "Unexpected constant condition.",
"line": 5,
"column": 11,
"nodeType": "ConditionalExpression",
"source": "something(true ? 1 : 0);",
"endLine": 5,
"endColumn": 23
}
],
"errorCount": 2,
"warningCount": 0,
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"source": "if (true) {\n something();\n}\n\nsomething(true ? 1 : 0);\n"
}
]
Expected:
@@ -7,11 +7,11 @@
"severity": 2,
"message": "Unexpected constant condition.",
"line": 1,
- "column": 1,
+ "column": 5,
"nodeType": "IfStatement",
"source": "if (true) {",
- "endLine": 3,
- "endColumn": 2
+ "endLine": 1,
+ "endColumn": 9
},
{
"ruleId": "no-constant-condition",
@@ -22,7 +22,7 @@
"nodeType": "ConditionalExpression",
"source": "something(true ? 1 : 0);",
"endLine": 5,
- "endColumn": 23
+ "endColumn": 15
}
],
"errorCount": 2,
Note: I don’t know how the reported "source"
properties are supposed to work, so I can’t tell whether they are correct or not.
Visual representation of the problem:
Notice how much more than the constant conditions are underlined.
See also: https://github.com/steelbrain/linter-ui-default/issues/398#issuecomment-331632791 and onwards.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:5 (5 by maintainers)
We can just report
node.test
as the node, and then all of that information will be automatically added to the report.would the ideal case be: just reporting the position of the constant condition (so the
true
)?so for the IfStatement, can we report with the
loc: {start: {line:nodestartPosition, column:node.test.range[0]}, end: {line:nodestartPosition, column:node.test.range[1]}}
?haven’t checked if there is a case that catches the correct position across the board, but I imagine they might be similar.