2 * @fileoverview Rule to check spacing between template tags and their literals
3 * @author Jonathan Wilsson
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
17 description: "require or disallow spacing between template tags and their literals",
18 category: "Stylistic Issues",
20 url: "https://eslint.org/docs/rules/template-tag-spacing"
23 fixable: "whitespace",
26 { enum: ["always", "never"] }
29 unexpected: "Unexpected space between template tag and template literal.",
30 missing: "Missing space between template tag and template literal."
35 const never = context.options[0] !== "always";
36 const sourceCode = context.getSourceCode();
39 * Check if a space is present between a template tag and its literal
40 * @param {ASTNode} node node to evaluate
44 function checkSpacing(node) {
45 const tagToken = sourceCode.getTokenBefore(node.quasi);
46 const literalToken = sourceCode.getFirstToken(node.quasi);
47 const hasWhitespace = sourceCode.isSpaceBetweenTokens(tagToken, literalToken);
49 if (never && hasWhitespace) {
53 start: tagToken.loc.end,
54 end: literalToken.loc.start
56 messageId: "unexpected",
58 const comments = sourceCode.getCommentsBefore(node.quasi);
60 // Don't fix anything if there's a single line comment after the template tag
61 if (comments.some(comment => comment.type === "Line")) {
65 return fixer.replaceTextRange(
66 [tagToken.range[1], literalToken.range[0]],
67 comments.reduce((text, comment) => text + sourceCode.getText(comment), "")
71 } else if (!never && !hasWhitespace) {
75 start: node.loc.start,
76 end: literalToken.loc.start
80 return fixer.insertTextAfter(tagToken, " ");
87 TaggedTemplateExpression: checkSpacing