From 2a210499288ed14ec9a6fd72decabfb77504c197 Mon Sep 17 00:00:00 2001 From: Anix Date: Thu, 11 Jun 2020 23:10:25 +0530 Subject: [PATCH] Update: key-spacing loc changes for extra space (refs #12334) (#13362) * Update: key-spacing loc changes for extra (refs #12334) * Chore: Fixed typo in variable name. Co-authored-by: Kai Cataldo Co-authored-by: Kai Cataldo --- lib/rules/key-spacing.js | 8 +- tests/lib/rules/key-spacing.js | 200 ++++++++++++++++++++++++--------- 2 files changed, 153 insertions(+), 55 deletions(-) diff --git a/lib/rules/key-spacing.js b/lib/rules/key-spacing.js index 57abb00b06e..fc885a117a1 100644 --- a/lib/rules/key-spacing.js +++ b/lib/rules/key-spacing.js @@ -433,11 +433,15 @@ module.exports = { tokenBeforeColon = sourceCode.getTokenBefore(nextColon, { includeComments: true }), tokenAfterColon = sourceCode.getTokenAfter(nextColon, { includeComments: true }), isKeySide = side === "key", - locStart = isKeySide ? tokenBeforeColon.loc.start : tokenAfterColon.loc.start, isExtra = diff > 0, diffAbs = Math.abs(diff), spaces = Array(diffAbs + 1).join(" "); + const locStart = isKeySide ? tokenBeforeColon.loc.end : nextColon.loc.start; + const locEnd = isKeySide ? nextColon.loc.start : tokenAfterColon.loc.start; + const missingLoc = isKeySide ? tokenBeforeColon.loc : tokenAfterColon.loc; + const loc = isExtra ? { start: locStart, end: locEnd } : missingLoc; + if (( diff && mode === "strict" || diff < 0 && mode === "minimum" || @@ -482,7 +486,7 @@ module.exports = { context.report({ node: property[side], - loc: locStart, + loc, messageId, data: { computed: property.computed ? "computed " : "", diff --git a/tests/lib/rules/key-spacing.js b/tests/lib/rules/key-spacing.js index 29bbbe85781..849ce619949 100644 --- a/tests/lib/rules/key-spacing.js +++ b/tests/lib/rules/key-spacing.js @@ -892,16 +892,110 @@ ruleTester.run("key-spacing", rule, { on: "value" } }] - }], - + } + ], invalid: [{ + code: "var a ={'key' : value };", + output: "var a ={'key':value };", + options: [{ + beforeColon: false, + afterColon: false + }], + errors: [ + { + + type: "Literal", + messageId: "extraKey", + data: { computed: "", key: "key" }, + line: 1, + column: 14, + endLine: 1, + endColumn: 15 + }, + { + type: "Identifier", + messageId: "extraValue", + data: { computed: "", key: "key" }, + line: 1, + column: 15, + endLine: 1, + endColumn: 17 + } + ] + }, { + code: "var a ={'key' :value };", + output: "var a ={'key': value };", + options: [{ + beforeColon: false, + afterColon: true + }], + errors: [ + { + + type: "Literal", + messageId: "extraKey", + data: { computed: "", key: "key" }, + line: 1, + column: 14, + endLine: 1, + endColumn: 15 + }, + { + type: "Identifier", + messageId: "missingValue", + data: { computed: "", key: "key" }, + line: 1, + column: 16, + endLine: 1, + endColumn: 21 + } + ] + }, { + code: "var a ={'key'\n : \nvalue };", + output: "var a ={'key':value };", + options: [{ + beforeColon: false, + afterColon: false + }], + errors: [ + { + + type: "Literal", + messageId: "extraKey", + data: { computed: "", key: "key" }, + line: 1, + column: 14, + endLine: 2, + endColumn: 2 + }, + { + type: "Identifier", + messageId: "extraValue", + data: { computed: "", key: "key" }, + line: 2, + column: 2, + endLine: 3, + endColumn: 1 + } + ] + }, { code: "var bat = function() { return { foo:bar, 'key': value }; };", output: "var bat = function() { return { foo:bar, 'key':value }; };", options: [{ beforeColon: false, afterColon: false }], - errors: [{ messageId: "extraValue", data: { computed: "", key: "key" }, type: "Identifier", line: 1, column: 49 }] + errors: [ + { + messageId: "extraValue", + data: { computed: "", key: "key" }, + type: "Identifier", + line: 1, + column: 47, + endLine: 1, + endColumn: 49 + } + ] }, { code: "var obj = { [ (a + b) ]:value };", output: "var obj = { [ (a + b) ]: value };", @@ -915,7 +1009,7 @@ ruleTester.run("key-spacing", rule, { beforeColon: false, afterColon: false }], - errors: [{ messageId: "extraKey", data: { computed: "", key: "key" }, type: "Literal", line: 1, column: 15 }] + errors: [{ messageId: "extraKey", data: { computed: "", key: "key" }, type: "Literal", line: 1, column: 20, endLine: 1, endColumn: 21 }] }, { code: "var obj = {prop :(42)};", output: "var obj = {prop : (42)};", @@ -940,10 +1034,10 @@ ruleTester.run("key-spacing", rule, { afterColon: true }], errors: [ - { messageId: "extraKey", data: { computed: "", key: "a" }, type: "Literal", line: 1, column: 3 }, - { messageId: "missingValue", data: { computed: "", key: "a" }, type: "CallExpression", line: 1, column: 9 }, - { messageId: "missingKey", data: { computed: "", key: "b" }, type: "Identifier", line: 1, column: 16 }, - { messageId: "extraValue", data: { computed: "", key: "b" }, type: "CallExpression", line: 1, column: 20 } + { messageId: "extraKey", data: { computed: "", key: "a" }, type: "Literal", line: 1, column: 6, endLine: 1, endColumn: 8 }, + { messageId: "missingValue", data: { computed: "", key: "a" }, type: "CallExpression", line: 1, column: 9, endLine: 1, endColumn: 12 }, + { messageId: "missingKey", data: { computed: "", key: "b" }, type: "Identifier", line: 1, column: 16, endLine: 1, endColumn: 17 }, + { messageId: "extraValue", data: { computed: "", key: "b" }, type: "CallExpression", line: 1, column: 17, endLine: 1, endColumn: 20 } ] }, { code: "bar = { key:value };", @@ -973,7 +1067,7 @@ ruleTester.run("key-spacing", rule, { }], errors: [ { messageId: "missingKey", data: { computed: "", key: "key" }, type: "Identifier", line: 2, column: 5 }, - { messageId: "extraValue", data: { computed: "", key: "key" }, type: "Identifier", line: 2, column: 12 }, + { messageId: "extraValue", data: { computed: "", key: "key" }, type: "Identifier", line: 2, column: 8 }, { messageId: "missingValue", data: { computed: "", key: "foobar" }, type: "CallExpression", line: 3, column: 12 } ] }, { @@ -999,9 +1093,9 @@ ruleTester.run("key-spacing", rule, { afterColon: false }], errors: [ - { messageId: "extraValue", data: { computed: "", key: "a" }, type: "Identifier", line: 2, column: 11 }, + { messageId: "extraValue", data: { computed: "", key: "a" }, type: "Identifier", line: 2, column: 9 }, { messageId: "missingKey", data: { computed: "", key: "foo" }, type: "Identifier", line: 3, column: 5 }, - { messageId: "extraKey", data: { computed: "", key: "b" }, type: "Identifier", line: 4, column: 5 } + { messageId: "extraKey", data: { computed: "", key: "b" }, type: "Identifier", line: 4, column: 6 } ] }, { code: [ @@ -1027,10 +1121,10 @@ ruleTester.run("key-spacing", rule, { }], parserOptions: { ecmaVersion: 6 }, errors: [ - { messageId: "extraValue", data: { computed: "", key: "a" }, type: "CallExpression", line: 2, column: 11 }, - { messageId: "extraKey", data: { computed: "", key: "b" }, type: "Literal", line: 3, column: 5 }, + { messageId: "extraValue", data: { computed: "", key: "a" }, type: "CallExpression", line: 2, column: 6 }, + { messageId: "extraKey", data: { computed: "", key: "b" }, type: "Literal", line: 3, column: 8 }, { messageId: "missingValue", data: { computed: "", key: "foo" }, type: "Identifier", line: 4, column: 9 }, - { messageId: "extraKey", data: { computed: "computed ", key: "a" }, type: "Identifier", line: 6, column: 7 } + { messageId: "extraKey", data: { computed: "computed ", key: "a" }, type: "Identifier", line: 6, column: 8 } ] }, { code: [ @@ -1056,7 +1150,7 @@ ruleTester.run("key-spacing", rule, { }], errors: [ { messageId: "missingKey", data: { computed: "", key: "a" }, type: "Identifier", line: 2, column: 5 }, - { messageId: "extraValue", data: { computed: "", key: "bar" }, type: "CallExpression", line: 5, column: 11 } + { messageId: "extraValue", data: { computed: "", key: "bar" }, type: "CallExpression", line: 5, column: 9 } ] }, { code: [ @@ -1105,8 +1199,8 @@ ruleTester.run("key-spacing", rule, { afterColon: false }], errors: [ - { messageId: "extraValue", data: { computed: "", key: "key" }, type: "Identifier", line: 3, column: 9 }, - { messageId: "extraKey", data: { computed: "", key: "key2" }, type: "Identifier", line: 4, column: 5 } + { messageId: "extraValue", data: { computed: "", key: "key" }, type: "Identifier", line: 2, column: 8 }, + { messageId: "extraKey", data: { computed: "", key: "key2" }, type: "Identifier", line: 4, column: 9 } ] }, { code: [ @@ -1153,7 +1247,7 @@ ruleTester.run("key-spacing", rule, { output: "var obj = {a: 'foo', bar: 'bam'};", options: [{ align: "colon" }], errors: [ - { messageId: "extraKey", data: { computed: "", key: "a" }, line: 1, column: 12, type: "Identifier" } + { messageId: "extraKey", data: { computed: "", key: "a" }, line: 1, column: 13, type: "Identifier" } ] }, { code: [ @@ -1170,7 +1264,7 @@ ruleTester.run("key-spacing", rule, { ].join("\n"), options: [{ align: "colon" }], errors: [ - { messageId: "extraKey", data: { computed: "", key: "b" }, line: 3, column: 5, type: "Identifier" } + { messageId: "extraKey", data: { computed: "", key: "b" }, line: 3, column: 6, type: "Identifier" } ] }, { code: [ @@ -1203,7 +1297,7 @@ ruleTester.run("key-spacing", rule, { afterColon: true }], errors: [ - { messageId: "extraValue", data: { computed: "", key: "key" }, line: 2, column: 8, type: "Identifier" } + { messageId: "extraValue", data: { computed: "", key: "key" }, line: 2, column: 2, type: "Identifier" } ] }, { code: [ @@ -1243,7 +1337,7 @@ ruleTester.run("key-spacing", rule, { options: [{ align: "value" }], parserOptions: { ecmaVersion: 6 }, errors: [ - { messageId: "extraValue", data: { computed: "", key: "foobar" }, line: 2, column: 14, type: "Literal" } + { messageId: "extraValue", data: { computed: "", key: "foobar" }, line: 2, column: 11, type: "Literal" } ] }, { code: [ @@ -1283,7 +1377,7 @@ ruleTester.run("key-spacing", rule, { options: [{ align: "value" }], parserOptions: { ecmaVersion: 6 }, errors: [ - { messageId: "extraValue", data: { computed: "", key: "foobar" }, line: 2, column: 14, type: "Literal" } + { messageId: "extraValue", data: { computed: "", key: "foobar" }, line: 2, column: 11, type: "Literal" } ] }, { code: [ @@ -1307,7 +1401,7 @@ ruleTester.run("key-spacing", rule, { options: [{ align: "value" }], parserOptions: { ecmaVersion: 6 }, errors: [ - { messageId: "extraValue", data: { computed: "", key: "baz" }, line: 6, column: 13, type: "Literal" } + { messageId: "extraValue", data: { computed: "", key: "baz" }, line: 6, column: 8, type: "Literal" } ] }, { code: [ @@ -1341,7 +1435,7 @@ ruleTester.run("key-spacing", rule, { ].join("\n"), options: [{ align: "colon" }], errors: [ - { messageId: "extraValue", data: { computed: "", key: "cats" }, line: 3, column: 12, type: "Identifier" } + { messageId: "extraValue", data: { computed: "", key: "cats" }, line: 3, column: 9, type: "Identifier" } ] }, { code: [ @@ -1371,7 +1465,7 @@ ruleTester.run("key-spacing", rule, { ].join("\n"), options: [{ align: "colon" }], errors: [ - { messageId: "extraKey", data: { computed: "", key: "foo" }, line: 1, column: 13, type: "Identifier" } + { messageId: "extraKey", data: { computed: "", key: "foo" }, line: 1, column: 16, type: "Identifier" } ] }, { code: [ @@ -1401,7 +1495,7 @@ ruleTester.run("key-spacing", rule, { ].join("\n"), options: [{ align: "colon" }], errors: [ - { messageId: "extraValue", data: { computed: "", key: "foo" }, line: 1, column: 20, type: "Identifier" } + { messageId: "extraValue", data: { computed: "", key: "foo" }, line: 1, column: 17, type: "Identifier" } ] }, { code: [ @@ -1416,7 +1510,7 @@ ruleTester.run("key-spacing", rule, { ].join("\n"), options: [{ align: "colon" }], errors: [ - { messageId: "extraValue", data: { computed: "", key: "cats" }, line: 2, column: 20, type: "Identifier" } + { messageId: "extraValue", data: { computed: "", key: "cats" }, line: 2, column: 17, type: "Identifier" } ] }, @@ -1456,7 +1550,7 @@ ruleTester.run("key-spacing", rule, { parserOptions: { ecmaVersion: 2018 }, errors: [ { messageId: "missingKey", data: { computed: "", key: "a" }, line: 3, column: 5, type: "Identifier" }, - { messageId: "extraKey", data: { computed: "", key: "f" }, line: 12, column: 5, type: "Identifier" } + { messageId: "extraKey", data: { computed: "", key: "f" }, line: 12, column: 6, type: "Identifier" } ] }, @@ -1478,7 +1572,7 @@ ruleTester.run("key-spacing", rule, { align: "colon" }], errors: [ - { messageId: "extraKey", data: { computed: "", key: "a" }, line: 2, column: 5, type: "Identifier" } + { messageId: "extraKey", data: { computed: "", key: "a" }, line: 2, column: 6, type: "Identifier" } ] }, { code: [ @@ -1497,7 +1591,7 @@ ruleTester.run("key-spacing", rule, { align: "value" }], errors: [ - { messageId: "extraKey", data: { computed: "", key: "c" }, line: 3, column: 5, type: "Identifier" } + { messageId: "extraKey", data: { computed: "", key: "c" }, line: 3, column: 6, type: "Identifier" } ] }, { code: [ @@ -1554,7 +1648,7 @@ ruleTester.run("key-spacing", rule, { } }], errors: [ - { messageId: "extraValue", data: { computed: "", key: "a1" }, line: 6, column: 17, type: "Literal" } + { messageId: "extraValue", data: { computed: "", key: "a1" }, line: 6, column: 15, type: "Literal" } ] }, { code: [ @@ -1635,7 +1729,7 @@ ruleTester.run("key-spacing", rule, { mode: "minimum" }], errors: [ - { messageId: "extraKey", data: { computed: "", key: "ex" }, line: 4, column: 4, type: "Identifier" } + { messageId: "extraKey", data: { computed: "", key: "ex" }, line: 4, column: 6, type: "Identifier" } ] }, { code: [ @@ -1674,7 +1768,7 @@ ruleTester.run("key-spacing", rule, { parserOptions: { ecmaVersion: 2018 }, errors: [ { messageId: "missingValue", data: { computed: "", key: "a" }, line: 1, column: 6, type: "Identifier" }, - { messageId: "extraKey", data: { computed: "", key: "c" }, line: 1, column: 20, type: "Identifier" } + { messageId: "extraKey", data: { computed: "", key: "c" }, line: 1, column: 21, type: "Identifier" } ] }, @@ -1755,8 +1849,8 @@ ruleTester.run("key-spacing", rule, { { messageId: "missingValue", data: { computed: "", key: "func" }, line: 2, column: 10, type: "FunctionExpression" }, { messageId: "missingKey", data: { computed: "", key: "longName" }, line: 5, column: 5, type: "Identifier" }, { messageId: "missingKey", data: { computed: "", key: "small" }, line: 6, column: 5, type: "Identifier" }, - { messageId: "extraKey", data: { computed: "", key: "xs" }, line: 7, column: 5, type: "Identifier" }, - { messageId: "extraKey", data: { computed: "", key: "singleLine" }, line: 11, column: 5, type: "Identifier" } + { messageId: "extraKey", data: { computed: "", key: "xs" }, line: 7, column: 7, type: "Identifier" }, + { messageId: "extraKey", data: { computed: "", key: "singleLine" }, line: 11, column: 15, type: "Identifier" } ] }, { code: [ @@ -1803,10 +1897,10 @@ ruleTester.run("key-spacing", rule, { errors: [ { messageId: "missingValue", data: { computed: "", key: "func" }, line: 2, column: 10, type: "FunctionExpression" }, { messageId: "missingKey", data: { computed: "", key: "small" }, line: 6, column: 5, type: "Identifier" }, - { messageId: "extraKey", data: { computed: "", key: "xs" }, line: 7, column: 5, type: "Identifier" }, - { messageId: "extraValue", data: { computed: "", key: "xs" }, line: 7, column: 21, type: "Literal" }, - { messageId: "extraValue", data: { computed: "", key: "func2" }, line: 8, column: 16, type: "FunctionExpression" }, - { messageId: "extraKey", data: { computed: "", key: "singleLine" }, line: 11, column: 5, type: "Identifier" } + { messageId: "extraKey", data: { computed: "", key: "xs" }, line: 7, column: 7, type: "Identifier" }, + { messageId: "extraValue", data: { computed: "", key: "xs" }, line: 7, column: 19, type: "Literal" }, + { messageId: "extraValue", data: { computed: "", key: "func2" }, line: 8, column: 14, type: "FunctionExpression" }, + { messageId: "extraKey", data: { computed: "", key: "singleLine" }, line: 11, column: 15, type: "Identifier" } ] }, { code: [ @@ -1843,8 +1937,8 @@ ruleTester.run("key-spacing", rule, { }], parserOptions: { ecmaVersion: 6 }, errors: [ - { messageId: "extraValue", data: { computed: "", key: "key2" }, line: 4, column: 14, type: "Literal" }, - { messageId: "extraValue", data: { computed: "", key: "key3" }, line: 5, column: 14, type: "Literal" } + { messageId: "extraValue", data: { computed: "", key: "key2" }, line: 4, column: 9, type: "Literal" }, + { messageId: "extraValue", data: { computed: "", key: "key3" }, line: 5, column: 9, type: "Literal" } ] }, { code: [ @@ -1881,20 +1975,20 @@ ruleTester.run("key-spacing", rule, { }], parserOptions: { ecmaVersion: 6 }, errors: [ - { messageId: "extraValue", data: { computed: "", key: "key2" }, line: 4, column: 14, type: "Literal" }, - { messageId: "extraValue", data: { computed: "", key: "key3" }, line: 5, column: 14, type: "Literal" } + { messageId: "extraValue", data: { computed: "", key: "key2" }, line: 4, column: 9, type: "Literal" }, + { messageId: "extraValue", data: { computed: "", key: "key3" }, line: 5, column: 9, type: "Literal" } ] }, { // https://github.com/eslint/eslint/issues/7603 code: "({ foo/* comment */ : bar })", output: "({ foo/* comment */: bar })", - errors: [{ messageId: "extraKey", data: { computed: "", key: "foo" }, line: 1, column: 7, type: "Identifier" }] + errors: [{ messageId: "extraKey", data: { computed: "", key: "foo" }, line: 1, column: 20, type: "Identifier" }] }, { code: "({ foo: /* comment */bar })", output: "({ foo:/* comment */bar })", options: [{ afterColon: false }], - errors: [{ messageId: "extraValue", data: { computed: "", key: "foo" }, line: 1, column: 9, type: "Identifier" }] + errors: [{ messageId: "extraValue", data: { computed: "", key: "foo" }, line: 1, column: 7, type: "Identifier" }] }, { code: "({ foo/*comment*/:/*comment*/bar })", @@ -1951,10 +2045,10 @@ ruleTester.run("key-spacing", rule, { } }], errors: [ - { messageId: "extraKey", data: { computed: "", key: "foo" }, line: 2, column: 5, type: "Identifier" }, - { messageId: "extraKey", data: { computed: "", key: "bar" }, line: 2, column: 14, type: "Literal" }, - { messageId: "extraKey", data: { computed: "", key: "baz" }, line: 2, column: 25, type: "Identifier" }, - { messageId: "extraKey", data: { computed: "", key: "longlonglong" }, line: 2, column: 34, type: "Identifier" } + { messageId: "extraKey", data: { computed: "", key: "foo" }, line: 2, column: 8, type: "Identifier" }, + { messageId: "extraKey", data: { computed: "", key: "bar" }, line: 2, column: 19, type: "Literal" }, + { messageId: "extraKey", data: { computed: "", key: "baz" }, line: 2, column: 28, type: "Identifier" }, + { messageId: "extraKey", data: { computed: "", key: "longlonglong" }, line: 2, column: 46, type: "Identifier" } ] }, { code: [ @@ -2015,10 +2109,10 @@ ruleTester.run("key-spacing", rule, { } }], errors: [ - { messageId: "extraValue", data: { computed: "", key: "foo" }, line: 1, column: 18, type: "Literal" }, - { messageId: "extraValue", data: { computed: "", key: "bar" }, line: 1, column: 28, type: "Literal" }, - { messageId: "extraKey", data: { computed: "", key: "baz" }, line: 1, column: 31, type: "Identifier" }, - { messageId: "extraKey", data: { computed: "", key: "longlonglong" }, line: 1, column: 39, type: "Identifier" } + { messageId: "extraValue", data: { computed: "", key: "foo" }, line: 1, column: 16, type: "Literal" }, + { messageId: "extraValue", data: { computed: "", key: "bar" }, line: 1, column: 26, type: "Literal" }, + { messageId: "extraKey", data: { computed: "", key: "baz" }, line: 1, column: 34, type: "Identifier" }, + { messageId: "extraKey", data: { computed: "", key: "longlonglong" }, line: 1, column: 51, type: "Identifier" } ] }, { code: [