diff --git a/lib/rules/template-tag-spacing.js b/lib/rules/template-tag-spacing.js index 9eb6d86077d..16f586255af 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,10 @@ module.exports = { } else if (!never && !hasWhitespace) { context.report({ node, - loc: tagToken.loc.start, + 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 08859369a29..f01dbdce3a0 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,413 @@ 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: 24 + }] }, { 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: 1, + 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: 1, + 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: 5, + 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: 5, + 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 + }] + }, + { + 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 + }] } ] });