From 6f67755a76815b98dc65607d70d59a3d24807bd9 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Mon, 20 Apr 2020 23:42:30 +0200 Subject: [PATCH 1/2] Update: Improve report location for template-tag-spacing (refs #12334) --- lib/rules/template-tag-spacing.js | 7 +- tests/lib/rules/template-tag-spacing.js | 282 +++++++++++++++++++++--- 2 files changed, 256 insertions(+), 33 deletions(-) diff --git a/lib/rules/template-tag-spacing.js b/lib/rules/template-tag-spacing.js index 9eb6d86077d..6a4797e96da 100644 --- a/lib/rules/template-tag-spacing.js +++ b/lib/rules/template-tag-spacing.js @@ -49,7 +49,10 @@ module.exports = { if (never && hasWhitespace) { context.report({ node, - loc: tagToken.loc.start, + loc: { + start: tagToken.loc.end, + end: literalToken.loc.start + }, messageId: "unexpected", fix(fixer) { const comments = sourceCode.getCommentsBefore(node.quasi); @@ -68,7 +71,7 @@ module.exports = { } else if (!never && !hasWhitespace) { context.report({ node, - loc: tagToken.loc.start, + loc: tagToken.loc, messageId: "missing", fix(fixer) { return fixer.insertTextAfter(tagToken, " "); diff --git a/tests/lib/rules/template-tag-spacing.js b/tests/lib/rules/template-tag-spacing.js index 08859369a29..13eec577152 100644 --- a/tests/lib/rules/template-tag-spacing.js +++ b/tests/lib/rules/template-tag-spacing.js @@ -17,8 +17,6 @@ const rule = require("../../../lib/rules/template-tag-spacing"), //------------------------------------------------------------------------------ const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } }); -const unexpectedError = { messageId: "unexpected" }; -const missingError = { messageId: "missing" }; ruleTester.run("template-tag-spacing", rule, { valid: [ @@ -55,167 +53,389 @@ ruleTester.run("template-tag-spacing", rule, { { code: "tag `name`", output: "tag`name`", - errors: [unexpectedError] + errors: [{ + messageId: "unexpected", + line: 1, + column: 4, + endLine: 1, + endColumn: 5 + }] }, { code: "tag `name`", output: "tag`name`", options: ["never"], - errors: [unexpectedError] + errors: [{ + messageId: "unexpected", + line: 1, + column: 4, + endLine: 1, + endColumn: 5 + }] }, { code: "tag`name`", output: "tag `name`", options: ["always"], - errors: [missingError] + errors: [{ + messageId: "missing", + line: 1, + column: 1, + endLine: 1, + endColumn: 4 + }] }, { code: "tag /*here's a comment*/`Hello world`", output: "tag/*here's a comment*/`Hello world`", options: ["never"], - errors: [unexpectedError] + errors: [{ + messageId: "unexpected", + line: 1, + column: 4, + endLine: 1, + endColumn: 25 + }] }, { code: "tag/*here's a comment*/ `Hello world`", output: "tag/*here's a comment*/`Hello world`", options: ["never"], - errors: [unexpectedError] + errors: [{ + messageId: "unexpected", + line: 1, + column: 4, + endLine: 1, + endColumn: 25 + }] }, { code: "tag/*here's a comment*/`Hello world`", output: "tag /*here's a comment*/`Hello world`", options: ["always"], - errors: [missingError] + errors: [{ + messageId: "missing", + line: 1, + column: 1, + endLine: 1, + endColumn: 4 + }] }, { code: "tag // here's a comment \n`bar`", output: null, - errors: [unexpectedError] + errors: [{ + messageId: "unexpected", + line: 1, + column: 4, + endLine: 2, + endColumn: 1 + }] }, { code: "tag // here's a comment \n`bar`", output: null, options: ["never"], - errors: [unexpectedError] + errors: [{ + messageId: "unexpected", + line: 1, + column: 4, + endLine: 2, + endColumn: 1 + }] }, { code: "tag `hello ${name}`", output: "tag`hello ${name}`", - errors: [unexpectedError] + errors: [{ + messageId: "unexpected", + line: 1, + column: 4, + endLine: 1, + endColumn: 5 + }] }, { code: "tag `hello ${name}`", output: "tag`hello ${name}`", options: ["never"], - errors: [unexpectedError] + errors: [{ + messageId: "unexpected", + line: 1, + column: 4, + endLine: 1, + endColumn: 5 + }] }, { code: "tag`hello ${name}`", output: "tag `hello ${name}`", options: ["always"], - errors: [missingError] + errors: [{ + messageId: "missing", + line: 1, + column: 1, + endLine: 1, + endColumn: 4 + }] }, { code: "new tag `name`", output: "new tag`name`", - errors: [unexpectedError] + errors: [{ + messageId: "unexpected", + line: 1, + column: 8, + endLine: 1, + endColumn: 9 + }] }, { code: "new tag `name`", output: "new tag`name`", options: ["never"], - errors: [unexpectedError] + errors: [{ + messageId: "unexpected", + line: 1, + column: 8, + endLine: 1, + endColumn: 9 + }] }, { code: "new tag`name`", output: "new tag `name`", options: ["always"], - errors: [missingError] + errors: [{ + messageId: "missing", + line: 1, + column: 5, + endLine: 1, + endColumn: 8 + }] }, { code: "new tag `hello ${name}`", output: "new tag`hello ${name}`", - errors: [unexpectedError] + errors: [{ + messageId: "unexpected", + line: 1, + column: 8, + endLine: 1, + endColumn: 9 + }] }, { code: "new tag `hello ${name}`", output: "new tag`hello ${name}`", options: ["never"], - errors: [unexpectedError] + errors: [{ + messageId: "unexpected", + line: 1, + column: 8, + endLine: 1, + endColumn: 9 + }] }, { code: "new tag`hello ${name}`", output: "new tag `hello ${name}`", options: ["always"], - errors: [missingError] + errors: [{ + messageId: "missing", + line: 1, + column: 5, + endLine: 1, + endColumn: 8 + }] }, { code: "(tag) `name`", output: "(tag)`name`", - errors: [unexpectedError] + errors: [{ + messageId: "unexpected", + line: 1, + column: 6, + endLine: 1, + endColumn: 7 + }] }, { code: "(tag) `name`", output: "(tag)`name`", options: ["never"], - errors: [unexpectedError] + errors: [{ + messageId: "unexpected", + line: 1, + column: 6, + endLine: 1, + endColumn: 7 + }] }, { code: "(tag)`name`", output: "(tag) `name`", options: ["always"], - errors: [missingError] + errors: [{ + messageId: "missing", + line: 1, + column: 5, + endLine: 1, + endColumn: 6 + }] }, { code: "(tag) `hello ${name}`", output: "(tag)`hello ${name}`", - errors: [unexpectedError] + errors: [{ + messageId: "unexpected", + line: 1, + column: 6, + endLine: 1, + endColumn: 7 + }] }, { code: "(tag) `hello ${name}`", output: "(tag)`hello ${name}`", options: ["never"], - errors: [unexpectedError] + errors: [{ + messageId: "unexpected", + line: 1, + column: 6, + endLine: 1, + endColumn: 7 + }] }, { code: "(tag)`hello ${name}`", output: "(tag) `hello ${name}`", options: ["always"], - errors: [missingError] + errors: [{ + messageId: "missing", + line: 1, + column: 5, + endLine: 1, + endColumn: 6 + }] }, { code: "new (tag) `name`", output: "new (tag)`name`", - errors: [unexpectedError] + errors: [{ + messageId: "unexpected", + line: 1, + column: 10, + endLine: 1, + endColumn: 11 + }] }, { code: "new (tag) `name`", output: "new (tag)`name`", options: ["never"], - errors: [unexpectedError] + errors: [{ + messageId: "unexpected", + line: 1, + column: 10, + endLine: 1, + endColumn: 11 + }] }, { code: "new (tag)`name`", output: "new (tag) `name`", options: ["always"], - errors: [missingError] + errors: [{ + messageId: "missing", + line: 1, + column: 9, + endLine: 1, + endColumn: 10 + }] }, { code: "new (tag) `hello ${name}`", output: "new (tag)`hello ${name}`", - errors: [unexpectedError] + errors: [{ + messageId: "unexpected", + line: 1, + column: 10, + endLine: 1, + endColumn: 11 + }] }, { code: "new (tag) `hello ${name}`", output: "new (tag)`hello ${name}`", options: ["never"], - errors: [unexpectedError] + errors: [{ + messageId: "unexpected", + line: 1, + column: 10, + endLine: 1, + endColumn: 11 + }] }, { code: "new (tag)`hello ${name}`", output: "new (tag) `hello ${name}`", options: ["always"], - errors: [missingError] + errors: [{ + messageId: "missing", + line: 1, + column: 9, + endLine: 1, + endColumn: 10 + }] + }, + { + code: "tag `name`", + output: "tag`name`", + options: ["never"], + errors: [{ + messageId: "unexpected", + line: 1, + column: 4, + endLine: 1, + endColumn: 7 + }] + }, + { + code: "tag\n`name`", + output: "tag`name`", + options: ["never"], + errors: [{ + messageId: "unexpected", + line: 1, + column: 4, + endLine: 2, + endColumn: 1 + }] + }, + { + code: "tag \n `name`", + output: "tag`name`", + options: ["never"], + errors: [{ + messageId: "unexpected", + line: 1, + column: 4, + endLine: 2, + endColumn: 3 + }] + }, + { + code: "tag\n\n`name`", + output: "tag`name`", + options: ["never"], + errors: [{ + messageId: "unexpected", + line: 1, + column: 4, + endLine: 3, + endColumn: 1 + }] } ] }); From 576cd7603f758f6e2ebce6d9e021eda0b21c29a0 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Sun, 24 May 2020 18:36:04 +0200 Subject: [PATCH 2/2] Change always --- lib/rules/template-tag-spacing.js | 5 +++- tests/lib/rules/template-tag-spacing.js | 34 +++++++++++++++++++++---- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/lib/rules/template-tag-spacing.js b/lib/rules/template-tag-spacing.js index 6a4797e96da..16f586255af 100644 --- a/lib/rules/template-tag-spacing.js +++ b/lib/rules/template-tag-spacing.js @@ -71,7 +71,10 @@ module.exports = { } else if (!never && !hasWhitespace) { context.report({ node, - loc: tagToken.loc, + loc: { + start: node.loc.start, + end: literalToken.loc.start + }, messageId: "missing", fix(fixer) { return fixer.insertTextAfter(tagToken, " "); diff --git a/tests/lib/rules/template-tag-spacing.js b/tests/lib/rules/template-tag-spacing.js index 13eec577152..f01dbdce3a0 100644 --- a/tests/lib/rules/template-tag-spacing.js +++ b/tests/lib/rules/template-tag-spacing.js @@ -118,7 +118,7 @@ ruleTester.run("template-tag-spacing", rule, { line: 1, column: 1, endLine: 1, - endColumn: 4 + endColumn: 24 }] }, { @@ -279,7 +279,7 @@ ruleTester.run("template-tag-spacing", rule, { errors: [{ messageId: "missing", line: 1, - column: 5, + column: 1, endLine: 1, endColumn: 6 }] @@ -314,7 +314,7 @@ ruleTester.run("template-tag-spacing", rule, { errors: [{ messageId: "missing", line: 1, - column: 5, + column: 1, endLine: 1, endColumn: 6 }] @@ -349,7 +349,7 @@ ruleTester.run("template-tag-spacing", rule, { errors: [{ messageId: "missing", line: 1, - column: 9, + column: 5, endLine: 1, endColumn: 10 }] @@ -384,7 +384,7 @@ ruleTester.run("template-tag-spacing", rule, { errors: [{ messageId: "missing", line: 1, - column: 9, + column: 5, endLine: 1, endColumn: 10 }] @@ -436,6 +436,30 @@ ruleTester.run("template-tag-spacing", rule, { endLine: 3, endColumn: 1 }] + }, + { + code: "foo\n .bar`Hello world`", + output: "foo\n .bar `Hello world`", + options: ["always"], + errors: [{ + messageId: "missing", + line: 1, + column: 1, + endLine: 2, + endColumn: 7 + }] + }, + { + code: "foo(\n bar\n)`Hello world`", + output: "foo(\n bar\n) `Hello world`", + options: ["always"], + errors: [{ + messageId: "missing", + line: 1, + column: 1, + endLine: 3, + endColumn: 2 + }] } ] });