From 2349e2770a041ccb44b559e8128da3b1ea61767c Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Mon, 21 Oct 2019 22:54:16 +0200 Subject: [PATCH] Report AssignmentExpression as the node --- lib/rules/no-cond-assign.js | 6 ++--- tests/lib/rules/no-cond-assign.js | 40 +++++++++++++++---------------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/lib/rules/no-cond-assign.js b/lib/rules/no-cond-assign.js index 77d54d0a1dc..3843a7ac2e3 100644 --- a/lib/rules/no-cond-assign.js +++ b/lib/rules/no-cond-assign.js @@ -116,8 +116,7 @@ module.exports = { ) { context.report({ - node, - loc: node.test.loc, + node: node.test, messageId: "missing" }); } @@ -133,8 +132,7 @@ module.exports = { if (ancestor) { context.report({ - node: ancestor, - loc: node.loc, + node, messageId: "unexpected", data: { type: NODE_DESCRIPTIONS[ancestor.type] || ancestor.type diff --git a/tests/lib/rules/no-cond-assign.js b/tests/lib/rules/no-cond-assign.js index 0556f768f30..77e4c785e72 100644 --- a/tests/lib/rules/no-cond-assign.js +++ b/tests/lib/rules/no-cond-assign.js @@ -47,25 +47,25 @@ ruleTester.run("no-cond-assign", rule, { { code: "switch (foo) { case baz + (a = b): bar(); }", options: ["always"] } ], invalid: [ - { code: "var x; if (x = 0) { var b = 1; }", errors: [{ messageId: "missing", type: "IfStatement", line: 1, column: 12, endLine: 1, endColumn: 17 }] }, - { code: "var x; while (x = 0) { var b = 1; }", errors: [{ messageId: "missing", type: "WhileStatement" }] }, - { code: "var x = 0, y; do { y = x; } while (x = x + 1);", errors: [{ messageId: "missing", type: "DoWhileStatement" }] }, - { code: "var x; for(; x+=1 ;){};", errors: [{ messageId: "missing", type: "ForStatement" }] }, - { code: "var x; if ((x) = (0));", errors: [{ messageId: "missing", type: "IfStatement" }] }, - { code: "if (someNode || (someNode = parentNode)) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "an 'if' statement" }, type: "IfStatement", column: 18, endColumn: 39 }] }, - { code: "while (someNode || (someNode = parentNode)) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'while' statement" }, type: "WhileStatement" }] }, - { code: "do { } while (someNode || (someNode = parentNode));", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'do...while' statement" }, type: "DoWhileStatement" }] }, - { code: "for (; (typeof l === 'undefined' ? (l = 0) : l); i++) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'for' statement" }, type: "ForStatement" }] }, - { code: "if (x = 0) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "an 'if' statement" }, type: "IfStatement" }] }, - { code: "while (x = 0) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'while' statement" }, type: "WhileStatement" }] }, - { code: "do { } while (x = x + 1);", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'do...while' statement" }, type: "DoWhileStatement" }] }, - { code: "for(; x = y; ) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'for' statement" }, type: "ForStatement" }] }, - { code: "if ((x = 0)) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "an 'if' statement" }, type: "IfStatement" }] }, - { code: "while ((x = 0)) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'while' statement" }, type: "WhileStatement" }] }, - { code: "do { } while ((x = x + 1));", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'do...while' statement" }, type: "DoWhileStatement" }] }, - { code: "for(; (x = y); ) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'for' statement" }, type: "ForStatement" }] }, - { code: "var x; var b = (x = 0) ? 1 : 0;", errors: [{ messageId: "missing", type: "ConditionalExpression" }] }, - { code: "var x; var b = x && (y = 0) ? 1 : 0;", options: ["always"], errors: [{ messageId: "unexpected", type: "ConditionalExpression" }] }, - { code: "(((3496.29)).bkufyydt = 2e308) ? foo : bar;", errors: [{ messageId: "missing", type: "ConditionalExpression" }] } + { code: "var x; if (x = 0) { var b = 1; }", errors: [{ messageId: "missing", type: "AssignmentExpression", line: 1, column: 12, endLine: 1, endColumn: 17 }] }, + { code: "var x; while (x = 0) { var b = 1; }", errors: [{ messageId: "missing", type: "AssignmentExpression" }] }, + { code: "var x = 0, y; do { y = x; } while (x = x + 1);", errors: [{ messageId: "missing", type: "AssignmentExpression" }] }, + { code: "var x; for(; x+=1 ;){};", errors: [{ messageId: "missing", type: "AssignmentExpression" }] }, + { code: "var x; if ((x) = (0));", errors: [{ messageId: "missing", type: "AssignmentExpression" }] }, + { code: "if (someNode || (someNode = parentNode)) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "an 'if' statement" }, type: "AssignmentExpression", column: 18, endColumn: 39 }] }, + { code: "while (someNode || (someNode = parentNode)) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'while' statement" }, type: "AssignmentExpression" }] }, + { code: "do { } while (someNode || (someNode = parentNode));", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'do...while' statement" }, type: "AssignmentExpression" }] }, + { code: "for (; (typeof l === 'undefined' ? (l = 0) : l); i++) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'for' statement" }, type: "AssignmentExpression" }] }, + { code: "if (x = 0) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "an 'if' statement" }, type: "AssignmentExpression" }] }, + { code: "while (x = 0) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'while' statement" }, type: "AssignmentExpression" }] }, + { code: "do { } while (x = x + 1);", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'do...while' statement" }, type: "AssignmentExpression" }] }, + { code: "for(; x = y; ) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'for' statement" }, type: "AssignmentExpression" }] }, + { code: "if ((x = 0)) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "an 'if' statement" }, type: "AssignmentExpression" }] }, + { code: "while ((x = 0)) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'while' statement" }, type: "AssignmentExpression" }] }, + { code: "do { } while ((x = x + 1));", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'do...while' statement" }, type: "AssignmentExpression" }] }, + { code: "for(; (x = y); ) { }", options: ["always"], errors: [{ messageId: "unexpected", data: { type: "a 'for' statement" }, type: "AssignmentExpression" }] }, + { code: "var x; var b = (x = 0) ? 1 : 0;", errors: [{ messageId: "missing", type: "AssignmentExpression" }] }, + { code: "var x; var b = x && (y = 0) ? 1 : 0;", options: ["always"], errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] }, + { code: "(((3496.29)).bkufyydt = 2e308) ? foo : bar;", errors: [{ messageId: "missing", type: "AssignmentExpression" }] } ] });