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) {
52 loc: tagToken.loc.start,
53 messageId: "unexpected",
55 const comments = sourceCode.getCommentsBefore(node.quasi);
57 // Don't fix anything if there's a single line comment after the template tag
58 if (comments.some(comment => comment.type === "Line")) {
62 return fixer.replaceTextRange(
63 [tagToken.range[1], literalToken.range[0]],
64 comments.reduce((text, comment) => text + sourceCode.getText(comment), "")
68 } else if (!never && !hasWhitespace) {
71 loc: tagToken.loc.start,
74 return fixer.insertTextAfter(tagToken, " ");
81 TaggedTemplateExpression: checkSpacing