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