Skip to content

Commit 97c745d

Browse files
mdjermanovicbtmills
authored andcommitted
Update: Report assignment expression location in no-cond-assign (#12465)
* Update: Report assignment expression location in no-cond-assign * Report AssignmentExpression as the node
1 parent 0f01f3d commit 97c745d

2 files changed

Lines changed: 22 additions & 23 deletions

File tree

lib/rules/no-cond-assign.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ module.exports = {
116116
) {
117117

118118
context.report({
119-
node,
120-
loc: node.test.loc.start,
119+
node: node.test,
121120
messageId: "missing"
122121
});
123122
}
@@ -133,7 +132,7 @@ module.exports = {
133132

134133
if (ancestor) {
135134
context.report({
136-
node: ancestor,
135+
node,
137136
messageId: "unexpected",
138137
data: {
139138
type: NODE_DESCRIPTIONS[ancestor.type] || ancestor.type

tests/lib/rules/no-cond-assign.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,25 @@ ruleTester.run("no-cond-assign", rule, {
4747
{ code: "switch (foo) { case baz + (a = b): bar(); }", options: ["always"] }
4848
],
4949
invalid: [
50-
{ code: "var x; if (x = 0) { var b = 1; }", errors: [{ messageId: "missing", type: "IfStatement", line: 1, column: 12 }] },
51-
{ code: "var x; while (x = 0) { var b = 1; }", errors: [{ messageId: "missing", type: "WhileStatement" }] },
52-
{ code: "var x = 0, y; do { y = x; } while (x = x + 1);", errors: [{ messageId: "missing", type: "DoWhileStatement" }] },
53-
{ code: "var x; for(; x+=1 ;){};", errors: [{ messageId: "missing", type: "ForStatement" }] },
54-
{ code: "var x; if ((x) = (0));", errors: [{ messageId: "missing", type: "IfStatement" }] },
55-
{ code: "if (someNode || (someNode = parentNode)) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "an 'if' statement" }, type: "IfStatement" }] },
56-
{ code: "while (someNode || (someNode = parentNode)) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'while' statement" }, type: "WhileStatement" }] },
57-
{ code: "do { } while (someNode || (someNode = parentNode));", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'do...while' statement" }, type: "DoWhileStatement" }] },
58-
{ code: "for (; (typeof l === 'undefined' ? (l = 0) : l); i++) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'for' statement" }, type: "ForStatement" }] },
59-
{ code: "if (x = 0) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "an 'if' statement" }, type: "IfStatement" }] },
60-
{ code: "while (x = 0) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'while' statement" }, type: "WhileStatement" }] },
61-
{ code: "do { } while (x = x + 1);", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'do...while' statement" }, type: "DoWhileStatement" }] },
62-
{ code: "for(; x = y; ) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'for' statement" }, type: "ForStatement" }] },
63-
{ code: "if ((x = 0)) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "an 'if' statement" }, type: "IfStatement" }] },
64-
{ code: "while ((x = 0)) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'while' statement" }, type: "WhileStatement" }] },
65-
{ code: "do { } while ((x = x + 1));", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'do...while' statement" }, type: "DoWhileStatement" }] },
66-
{ code: "for(; (x = y); ) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'for' statement" }, type: "ForStatement" }] },
67-
{ code: "var x; var b = (x = 0) ? 1 : 0;", errors: [{ messageId: "missing", type: "ConditionalExpression" }] },
68-
{ code: "var x; var b = x && (y = 0) ? 1 : 0;", options: ["always"], errors: [{ messageId: "unexpected", type: "ConditionalExpression" }] },
69-
{ code: "(((3496.29)).bkufyydt = 2e308) ? foo : bar;", errors: [{ messageId: "missing", type: "ConditionalExpression" }] }
50+
{ code: "var x; if (x = 0) { var b = 1; }", errors: [{ messageId: "missing", type: "AssignmentExpression", line: 1, column: 12, endLine: 1, endColumn: 17 }] },
51+
{ code: "var x; while (x = 0) { var b = 1; }", errors: [{ messageId: "missing", type: "AssignmentExpression" }] },
52+
{ code: "var x = 0, y; do { y = x; } while (x = x + 1);", errors: [{ messageId: "missing", type: "AssignmentExpression" }] },
53+
{ code: "var x; for(; x+=1 ;){};", errors: [{ messageId: "missing", type: "AssignmentExpression" }] },
54+
{ code: "var x; if ((x) = (0));", errors: [{ messageId: "missing", type: "AssignmentExpression" }] },
55+
{ code: "if (someNode || (someNode = parentNode)) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "an 'if' statement" }, type: "AssignmentExpression", column: 18, endColumn: 39 }] },
56+
{ code: "while (someNode || (someNode = parentNode)) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'while' statement" }, type: "AssignmentExpression" }] },
57+
{ code: "do { } while (someNode || (someNode = parentNode));", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'do...while' statement" }, type: "AssignmentExpression" }] },
58+
{ code: "for (; (typeof l === 'undefined' ? (l = 0) : l); i++) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'for' statement" }, type: "AssignmentExpression" }] },
59+
{ code: "if (x = 0) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "an 'if' statement" }, type: "AssignmentExpression" }] },
60+
{ code: "while (x = 0) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'while' statement" }, type: "AssignmentExpression" }] },
61+
{ code: "do { } while (x = x + 1);", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'do...while' statement" }, type: "AssignmentExpression" }] },
62+
{ code: "for(; x = y; ) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'for' statement" }, type: "AssignmentExpression" }] },
63+
{ code: "if ((x = 0)) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "an 'if' statement" }, type: "AssignmentExpression" }] },
64+
{ code: "while ((x = 0)) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'while' statement" }, type: "AssignmentExpression" }] },
65+
{ code: "do { } while ((x = x + 1));", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'do...while' statement" }, type: "AssignmentExpression" }] },
66+
{ code: "for(; (x = y); ) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'for' statement" }, type: "AssignmentExpression" }] },
67+
{ code: "var x; var b = (x = 0) ? 1 : 0;", errors: [{ messageId: "missing", type: "AssignmentExpression" }] },
68+
{ code: "var x; var b = x && (y = 0) ? 1 : 0;", options: ["always"], errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] },
69+
{ code: "(((3496.29)).bkufyydt = 2e308) ? foo : bar;", errors: [{ messageId: "missing", type: "AssignmentExpression" }] }
7070
]
7171
});

0 commit comments

Comments
 (0)