/
html-comment-content-newline.js
254 lines (239 loc) · 9.59 KB
/
html-comment-content-newline.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
"use strict";
const utils = require("../utils");
const { getSourceCode } = require("eslint-compat-utils");
/**
* Check whether the given node is a multiline
* @param {object} contentFirstLoc The comment content first location.
* @param {object} contentLastLoc The comment content last location.
* @returns {boolean} `true` if the node is a multiline.
*/
function isMultiline(contentFirstLoc, contentLastLoc) {
if (contentFirstLoc.line < contentLastLoc.line) {
// multiline contents
return true;
}
return false;
}
/**
* Normalize options.
* @param {object} options The options user configured.
* @returns {object} The normalized options.
*/
function parseOptions(options) {
return Object.assign(
{
singleline: "never",
multiline: "always",
},
options,
);
}
/**
* Get the messageId for before tag open
* @param {number} expectedLineBreaks The number of expected line breaks
* @param {number} actualLineBreaks The number of actual line breaks
* @returns {string} The messageId
*/
function getMessageIdForTagOpen(expectedLineBreaks, actualLineBreaks) {
if (expectedLineBreaks === 0) {
if (actualLineBreaks === 1) {
return "unexpectedAfterTagOpen";
}
return "expectedNoLineBreaksAfterTagOpenButNLineBreaks";
}
if (actualLineBreaks === 0) {
return "missingAfterTagOpen";
}
return "expectedOneLineBreakAfterTagOpenButNLineBreaks";
}
/**
* Get the messageId for after tag close
* @param {number} expectedLineBreaks The number of expected line breaks
* @param {number} actualLineBreaks The number of actual line breaks
* @returns {string} The messageId
*/
function getMessageIdForTagClose(expectedLineBreaks, actualLineBreaks) {
if (expectedLineBreaks === 0) {
if (actualLineBreaks === 1) {
return "unexpectedBeforeTagClose";
}
return "expectedNoLineBreaksBeforeTagCloseButNLineBreaks";
}
if (actualLineBreaks === 0) {
return "missingBeforeTagClose";
}
return "expectedOneLineBreakBeforeTagCloseButNLineBreaks";
}
module.exports = {
meta: {
docs: {
description:
"require or disallow a line break before and after HTML comment contents",
category: "recommended-with-html",
url: "https://ota-meshi.github.io/eslint-plugin-lodash-template/rules/html-comment-content-newline.html",
},
fixable: "whitespace",
messages: {
unexpectedAfterTagOpen:
"Expected no line breaks after `<!--`, but 1 line break found.",
expectedNoLineBreaksAfterTagOpenButNLineBreaks:
"Expected no line breaks after `<!--`, but {{n}} line breaks found.",
missingAfterTagOpen:
"Expected 1 line break after `<!--`, but no line breaks found.",
expectedOneLineBreakAfterTagOpenButNLineBreaks:
"Expected 1 line break after `<!--`, but {{n}} line breaks found.",
unexpectedBeforeTagClose:
"Expected no line breaks before `-->`, but 1 line break found.",
expectedNoLineBreaksBeforeTagCloseButNLineBreaks:
"Expected no line breaks before `-->`, but {{n}} line breaks found.",
missingBeforeTagClose:
"Expected 1 line break before `-->`, but no line breaks found.",
expectedOneLineBreakBeforeTagCloseButNLineBreaks:
"Expected 1 line break before `-->`, but {{n}} line breaks found.",
},
schema: [
{
type: "object",
properties: {
singleline: { enum: ["ignore", "always", "never"] },
multiline: { enum: ["ignore", "always", "never"] },
},
additionalProperties: false,
},
],
type: "layout",
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.getMicroTemplateService) {
return {};
}
if (!utils.isHtmlFile(context.getFilename())) {
return {};
}
const microTemplateService =
sourceCode.parserServices.getMicroTemplateService();
const options = parseOptions(context.options[0]);
/**
* Get the comments locations.
* @param {ASTNode} node The HTML comment.
* @returns {object} The comments locations.
*/
function getCommentLocations(node) {
const contentStartIndex = node.commentOpen.range[1];
const contentEndIndex = node.commentClose.range[0];
const contentText = sourceCode.text.slice(
contentStartIndex,
contentEndIndex,
);
const contentFirstIndex = (() => {
const index = contentText.search(/\S/u);
if (index >= 0) {
return index + contentStartIndex;
}
return contentEndIndex;
})();
const contentLastIndex = (() => {
const index = contentText.search(/\S\s*$/gu);
if (index >= 0) {
return index + contentStartIndex + 1;
}
return contentStartIndex;
})();
return {
first: {
index: contentFirstIndex,
loc: sourceCode.getLocFromIndex(contentFirstIndex),
},
last: {
index: contentLastIndex,
loc: sourceCode.getLocFromIndex(contentLastIndex),
},
};
}
return {
"Program:exit"() {
microTemplateService.traverseDocumentNodes({
HTMLComment(node) {
const commentOpen = node.commentOpen;
const commentClose = node.commentClose;
if (!commentOpen || !commentClose) {
return;
}
const commentLocs = getCommentLocations(node);
const type = isMultiline(
commentLocs.first.loc,
commentLocs.last.loc,
)
? options.multiline
: options.singleline;
if (type === "ignore") {
// 'ignore' option
return;
}
const beforeLineBreaks =
commentLocs.first.loc.line -
commentOpen.loc.end.line;
const afterLineBreaks =
commentClose.loc.start.line -
commentLocs.last.loc.line;
const expectedLineBreaks = type === "always" ? 1 : 0;
if (expectedLineBreaks !== beforeLineBreaks) {
context.report({
loc: {
start: commentOpen.loc.end,
end: commentLocs.first.loc,
},
messageId: getMessageIdForTagOpen(
expectedLineBreaks,
beforeLineBreaks,
),
data: {
n: beforeLineBreaks,
},
fix(fixer) {
const range = [
commentOpen.range[1],
commentLocs.first.index,
];
const text = "\n".repeat(
expectedLineBreaks,
);
return fixer.replaceTextRange(range, text);
},
});
}
if (commentOpen.range[1] === commentLocs.last.index) {
return;
}
if (expectedLineBreaks !== afterLineBreaks) {
context.report({
loc: {
start: commentLocs.last.loc,
end: commentClose.loc.start,
},
messageId: getMessageIdForTagClose(
expectedLineBreaks,
afterLineBreaks,
),
data: {
n: afterLineBreaks,
},
fix(fixer) {
const range = [
commentLocs.last.index,
commentClose.range[0],
];
const text = "\n".repeat(
expectedLineBreaks,
);
return fixer.replaceTextRange(range, text);
},
});
}
},
});
},
};
},
};