1 | /**
|
---|
2 | * @fileoverview Rule to enforce spacing around embedded expressions of template strings
|
---|
3 | * @author Toru Nagashima
|
---|
4 | * @deprecated in ESLint v8.53.0
|
---|
5 | */
|
---|
6 |
|
---|
7 | "use strict";
|
---|
8 |
|
---|
9 | //------------------------------------------------------------------------------
|
---|
10 | // Requirements
|
---|
11 | //------------------------------------------------------------------------------
|
---|
12 |
|
---|
13 | const astUtils = require("./utils/ast-utils");
|
---|
14 |
|
---|
15 | //------------------------------------------------------------------------------
|
---|
16 | // Rule Definition
|
---|
17 | //------------------------------------------------------------------------------
|
---|
18 |
|
---|
19 | /** @type {import('../shared/types').Rule} */
|
---|
20 | module.exports = {
|
---|
21 | meta: {
|
---|
22 | deprecated: true,
|
---|
23 | replacedBy: [],
|
---|
24 | type: "layout",
|
---|
25 |
|
---|
26 | docs: {
|
---|
27 | description: "Require or disallow spacing around embedded expressions of template strings",
|
---|
28 | recommended: false,
|
---|
29 | url: "https://eslint.org/docs/latest/rules/template-curly-spacing"
|
---|
30 | },
|
---|
31 |
|
---|
32 | fixable: "whitespace",
|
---|
33 |
|
---|
34 | schema: [
|
---|
35 | { enum: ["always", "never"] }
|
---|
36 | ],
|
---|
37 | messages: {
|
---|
38 | expectedBefore: "Expected space(s) before '}'.",
|
---|
39 | expectedAfter: "Expected space(s) after '${'.",
|
---|
40 | unexpectedBefore: "Unexpected space(s) before '}'.",
|
---|
41 | unexpectedAfter: "Unexpected space(s) after '${'."
|
---|
42 | }
|
---|
43 | },
|
---|
44 |
|
---|
45 | create(context) {
|
---|
46 | const sourceCode = context.sourceCode;
|
---|
47 | const always = context.options[0] === "always";
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Checks spacing before `}` of a given token.
|
---|
51 | * @param {Token} token A token to check. This is a Template token.
|
---|
52 | * @returns {void}
|
---|
53 | */
|
---|
54 | function checkSpacingBefore(token) {
|
---|
55 | if (!token.value.startsWith("}")) {
|
---|
56 | return; // starts with a backtick, this is the first template element in the template literal
|
---|
57 | }
|
---|
58 |
|
---|
59 | const prevToken = sourceCode.getTokenBefore(token, { includeComments: true }),
|
---|
60 | hasSpace = sourceCode.isSpaceBetween(prevToken, token);
|
---|
61 |
|
---|
62 | if (!astUtils.isTokenOnSameLine(prevToken, token)) {
|
---|
63 | return;
|
---|
64 | }
|
---|
65 |
|
---|
66 | if (always && !hasSpace) {
|
---|
67 | context.report({
|
---|
68 | loc: {
|
---|
69 | start: token.loc.start,
|
---|
70 | end: {
|
---|
71 | line: token.loc.start.line,
|
---|
72 | column: token.loc.start.column + 1
|
---|
73 | }
|
---|
74 | },
|
---|
75 | messageId: "expectedBefore",
|
---|
76 | fix: fixer => fixer.insertTextBefore(token, " ")
|
---|
77 | });
|
---|
78 | }
|
---|
79 |
|
---|
80 | if (!always && hasSpace) {
|
---|
81 | context.report({
|
---|
82 | loc: {
|
---|
83 | start: prevToken.loc.end,
|
---|
84 | end: token.loc.start
|
---|
85 | },
|
---|
86 | messageId: "unexpectedBefore",
|
---|
87 | fix: fixer => fixer.removeRange([prevToken.range[1], token.range[0]])
|
---|
88 | });
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Checks spacing after `${` of a given token.
|
---|
94 | * @param {Token} token A token to check. This is a Template token.
|
---|
95 | * @returns {void}
|
---|
96 | */
|
---|
97 | function checkSpacingAfter(token) {
|
---|
98 | if (!token.value.endsWith("${")) {
|
---|
99 | return; // ends with a backtick, this is the last template element in the template literal
|
---|
100 | }
|
---|
101 |
|
---|
102 | const nextToken = sourceCode.getTokenAfter(token, { includeComments: true }),
|
---|
103 | hasSpace = sourceCode.isSpaceBetween(token, nextToken);
|
---|
104 |
|
---|
105 | if (!astUtils.isTokenOnSameLine(token, nextToken)) {
|
---|
106 | return;
|
---|
107 | }
|
---|
108 |
|
---|
109 | if (always && !hasSpace) {
|
---|
110 | context.report({
|
---|
111 | loc: {
|
---|
112 | start: {
|
---|
113 | line: token.loc.end.line,
|
---|
114 | column: token.loc.end.column - 2
|
---|
115 | },
|
---|
116 | end: token.loc.end
|
---|
117 | },
|
---|
118 | messageId: "expectedAfter",
|
---|
119 | fix: fixer => fixer.insertTextAfter(token, " ")
|
---|
120 | });
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (!always && hasSpace) {
|
---|
124 | context.report({
|
---|
125 | loc: {
|
---|
126 | start: token.loc.end,
|
---|
127 | end: nextToken.loc.start
|
---|
128 | },
|
---|
129 | messageId: "unexpectedAfter",
|
---|
130 | fix: fixer => fixer.removeRange([token.range[1], nextToken.range[0]])
|
---|
131 | });
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | return {
|
---|
136 | TemplateElement(node) {
|
---|
137 | const token = sourceCode.getFirstToken(node);
|
---|
138 |
|
---|
139 | checkSpacingBefore(token);
|
---|
140 | checkSpacingAfter(token);
|
---|
141 | }
|
---|
142 | };
|
---|
143 | }
|
---|
144 | };
|
---|