-
Notifications
You must be signed in to change notification settings - Fork 2
/
html-closing-bracket-newline.js
136 lines (127 loc) · 4.92 KB
/
html-closing-bracket-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
"use strict";
const utils = require("../utils");
const { getSourceCode } = require("eslint-compat-utils");
/**
* Get the messageId
* @param {number} expectedLineBreaks The number of expected line breaks
* @param {number} actualLineBreaks The number of actual line breaks
* @returns {string} The messageId
*/
function getMessageId(expectedLineBreaks, actualLineBreaks) {
if (expectedLineBreaks === 0) {
if (actualLineBreaks === 1) {
return "unexpected";
}
return "expectedNoLineBreaksButNLineBreaks";
}
if (actualLineBreaks === 0) {
return "missing";
}
return "expectedOneLineBreakButNLineBreaks";
}
module.exports = {
meta: {
docs: {
description:
"require or disallow a line break before tag's closing brackets",
category: "recommended-with-html",
url: "https://ota-meshi.github.io/eslint-plugin-lodash-template/rules/html-closing-bracket-newline.html",
},
fixable: "whitespace",
messages: {
unexpected:
"Expected no line breaks before closing bracket, but 1 line break found.",
expectedNoLineBreaksButNLineBreaks:
"Expected no line breaks before closing bracket, but {{n}} line breaks found.",
missing:
"Expected 1 line break before closing bracket, but no line breaks found.",
expectedOneLineBreakButNLineBreaks:
"Expected 1 line break before closing bracket, but {{n}} line breaks found.",
},
schema: [
{
type: "object",
properties: {
singleline: { enum: ["always", "never"] },
multiline: { enum: ["always", "never"] },
},
additionalProperties: false,
},
],
type: "layout",
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.getMicroTemplateService) {
return {};
}
if (!utils.isHtmlFile(context.getFilename())) {
return {};
}
const options = context.options[0] || {};
const microTemplateService =
sourceCode.parserServices.getMicroTemplateService();
/**
* Get last location
* @param {ASTNode} node The node
* @returns {object} The last location
*/
function getLastLocationInTag(node) {
const tagClose = node.tagClose;
const text = sourceCode.text.slice(
node.range[0],
tagClose.range[0],
);
const index = text.search(/\S\s*$/gu) + node.range[0] + 1;
return {
index,
loc: sourceCode.getLocFromIndex(index),
};
}
return {
"Program:exit"() {
microTemplateService.traverseDocumentNodes({
"HTMLStartTag, HTMLEndTag"(node) {
const closingBracketToken = node.tagClose;
const lastLoc = getLastLocationInTag(node);
const type =
node.loc.start.line === lastLoc.loc.line
? "singleline"
: "multiline";
const expectedLineBreaks =
options[type] === "always" ? 1 : 0;
const actualLineBreaks =
closingBracketToken.loc.start.line -
lastLoc.loc.line;
if (actualLineBreaks !== expectedLineBreaks) {
context.report({
node,
loc: {
start: lastLoc.loc,
end: closingBracketToken.loc.start,
},
messageId: getMessageId(
expectedLineBreaks,
actualLineBreaks,
),
data: {
n: actualLineBreaks,
},
fix(fixer) {
const range = [
lastLoc.index,
closingBracketToken.range[0],
];
const text = "\n".repeat(
expectedLineBreaks,
);
return fixer.replaceTextRange(range, text);
},
});
}
},
});
},
};
},
};