@lewishowles/lint-config 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/README.md +2 -2
- package/base.json +23 -4
- package/comments/plugin.js +2 -12
- package/comments/rules/configured-api-calls.js +2 -4
- package/comments/rules/formatting.js +868 -0
- package/comments/rules/variable-declarations.js +15 -5
- package/comments/rules/vue-component-documentation.js +5 -6
- package/comments/utils/documentation.js +0 -1
- package/comments/utils/jsdoc.js +78 -59
- package/comments/utils/source.js +0 -2
- package/comments/utils/wrap.js +123 -2
- package/comments.json +1 -6
- package/package.json +1 -1
- package/comments/rules/block-comments.js +0 -71
- package/comments/rules/jsdoc-tag-formatting.js +0 -71
- package/comments/rules/line-comments.js +0 -86
- package/comments/rules/max-line-length.js +0 -169
- package/comments/rules/placement.js +0 -293
- package/comments/rules/sentence-punctuation.js +0 -281
|
@@ -1,281 +0,0 @@
|
|
|
1
|
-
import { addTerminalPunctuation, capitaliseSentence, formatSentence } from "../utils/wrap.js";
|
|
2
|
-
import { formatJSDocPunctuation, isJSDoc } from "../utils/jsdoc.js";
|
|
3
|
-
import {
|
|
4
|
-
getCommentText,
|
|
5
|
-
getLineCommentGroups,
|
|
6
|
-
getLineIndent,
|
|
7
|
-
getNewline,
|
|
8
|
-
replaceMinimalComment,
|
|
9
|
-
} from "../utils/source.js";
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Replace only the value of a line comment.
|
|
13
|
-
*
|
|
14
|
-
* @param {object} sourceCode
|
|
15
|
-
* The Oxlint source code object.
|
|
16
|
-
* @param {object} comment
|
|
17
|
-
* The line comment token.
|
|
18
|
-
* @param {string} value
|
|
19
|
-
* The replacement comment value.
|
|
20
|
-
*
|
|
21
|
-
* @returns {string}
|
|
22
|
-
* The replacement comment text.
|
|
23
|
-
*/
|
|
24
|
-
function replaceLineCommentValue(sourceCode, comment, value) {
|
|
25
|
-
// The comment's raw source text.
|
|
26
|
-
const commentText = getCommentText(sourceCode, comment);
|
|
27
|
-
|
|
28
|
-
return `${commentText.slice(0, 2)}${value}`;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Format the prose text in a standard block-comment line.
|
|
33
|
-
*
|
|
34
|
-
* @param {string} line
|
|
35
|
-
* The block-comment line.
|
|
36
|
-
* @param {function} formatProse
|
|
37
|
-
* The formatter for the line's prose.
|
|
38
|
-
*
|
|
39
|
-
* @returns {string}
|
|
40
|
-
* The formatted block-comment line.
|
|
41
|
-
*/
|
|
42
|
-
function formatBlockCommentLine(line, formatProse) {
|
|
43
|
-
// The line's leading `*` decoration, when present.
|
|
44
|
-
const marker = line.match(/^\s*\*\s?/);
|
|
45
|
-
|
|
46
|
-
if (marker === null) {
|
|
47
|
-
return line;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return `${marker[0]}${formatProse(line.slice(marker[0].length).trim())}`;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Return a sentence-formatted line-comment group.
|
|
55
|
-
*
|
|
56
|
-
* @param {object} sourceCode
|
|
57
|
-
* The Oxlint source code object.
|
|
58
|
-
* @param {object[]} comments
|
|
59
|
-
* The adjacent line comments.
|
|
60
|
-
*
|
|
61
|
-
* @returns {object|null}
|
|
62
|
-
* The first and last replacements, or null when no sentence needs work.
|
|
63
|
-
*/
|
|
64
|
-
function formatLineCommentGroup(sourceCode, comments) {
|
|
65
|
-
// The group's first comment token.
|
|
66
|
-
const firstComment = comments[0];
|
|
67
|
-
// The group's last comment token.
|
|
68
|
-
const lastComment = comments.at(-1);
|
|
69
|
-
// The first comment's undecorated text.
|
|
70
|
-
const firstText = firstComment.value.trim();
|
|
71
|
-
|
|
72
|
-
if (firstText === "" || firstText.startsWith("@")) {
|
|
73
|
-
return null;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// The first comment's value, capitalised or fully sentence-formatted.
|
|
77
|
-
const firstValue =
|
|
78
|
-
comments.length === 1
|
|
79
|
-
? formatSentence(firstComment.value)
|
|
80
|
-
: capitaliseSentence(firstComment.value);
|
|
81
|
-
|
|
82
|
-
// The last comment's value, punctuated to close the sentence.
|
|
83
|
-
const lastValue = comments.length === 1 ? firstValue : addTerminalPunctuation(lastComment.value);
|
|
84
|
-
// The replacement comment text for the first and last comments.
|
|
85
|
-
const firstReplacement = replaceLineCommentValue(sourceCode, firstComment, firstValue);
|
|
86
|
-
// The replacement comment text for the last comment.
|
|
87
|
-
const lastReplacement = replaceLineCommentValue(sourceCode, lastComment, lastValue);
|
|
88
|
-
|
|
89
|
-
if (
|
|
90
|
-
firstReplacement === getCommentText(sourceCode, firstComment) &&
|
|
91
|
-
lastReplacement === getCommentText(sourceCode, lastComment)
|
|
92
|
-
) {
|
|
93
|
-
return null;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
return { firstComment, firstReplacement, lastComment, lastReplacement };
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Format prose in an ordinary block comment.
|
|
101
|
-
*
|
|
102
|
-
* @param {object} sourceCode
|
|
103
|
-
* The Oxlint source code object.
|
|
104
|
-
* @param {object} comment
|
|
105
|
-
* The block comment token.
|
|
106
|
-
*
|
|
107
|
-
* @returns {string}
|
|
108
|
-
* The sentence-formatted comment text.
|
|
109
|
-
*/
|
|
110
|
-
function formatOrdinaryBlockComment(sourceCode, comment) {
|
|
111
|
-
// The comment's raw source text.
|
|
112
|
-
const commentText = getCommentText(sourceCode, comment);
|
|
113
|
-
// The indentation the comment's lines are aligned to.
|
|
114
|
-
const indentation = getLineIndent(sourceCode, comment.range[0]);
|
|
115
|
-
// The comment body, stripped of its /* */ delimiters.
|
|
116
|
-
const content = commentText.slice(2, -2).trim();
|
|
117
|
-
|
|
118
|
-
if (indentation === null || content === "" || /^(?:eslint|oxlint|istanbul|c8)-/.test(content)) {
|
|
119
|
-
return commentText;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (!commentText.includes("\n") && !commentText.includes("\r")) {
|
|
123
|
-
return `/* ${formatSentence(content)} */`;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// The comment's individual source lines.
|
|
127
|
-
const lines = commentText.split(/\r\n|\n|\r/);
|
|
128
|
-
|
|
129
|
-
// The indexes of lines carrying prose, excluding the delimiter lines.
|
|
130
|
-
const proseLineIndexes = lines
|
|
131
|
-
.slice(1, -1)
|
|
132
|
-
.map((line, index) => ({ index: index + 1, text: line.replace(/^\s*\*?\s?/, "").trim() }))
|
|
133
|
-
.filter((line) => line.text !== "")
|
|
134
|
-
.map((line) => line.index);
|
|
135
|
-
|
|
136
|
-
if (lines[0] === "/*" && lines.at(-1).trim() === "*/" && proseLineIndexes.length > 0) {
|
|
137
|
-
// The comment lines, formatted in place.
|
|
138
|
-
const formattedLines = [...lines];
|
|
139
|
-
// The first and last prose line indexes, which start and end the
|
|
140
|
-
// sentence.
|
|
141
|
-
const firstProseLine = proseLineIndexes[0];
|
|
142
|
-
// The last prose line index, which ends the sentence.
|
|
143
|
-
const lastProseLine = proseLineIndexes.at(-1);
|
|
144
|
-
|
|
145
|
-
formattedLines[firstProseLine] = formatBlockCommentLine(
|
|
146
|
-
formattedLines[firstProseLine],
|
|
147
|
-
capitaliseSentence,
|
|
148
|
-
);
|
|
149
|
-
formattedLines[lastProseLine] = formatBlockCommentLine(
|
|
150
|
-
formattedLines[lastProseLine],
|
|
151
|
-
addTerminalPunctuation,
|
|
152
|
-
);
|
|
153
|
-
|
|
154
|
-
return formattedLines.join(getNewline(sourceCode.text));
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// The comment's prose, joined into a single paragraph.
|
|
158
|
-
const paragraphs = content
|
|
159
|
-
.split(/\r\n|\n|\r/)
|
|
160
|
-
.map((line) => line.replace(/^\s*\*?\s?/, "").trim())
|
|
161
|
-
.filter(Boolean)
|
|
162
|
-
.join(" ");
|
|
163
|
-
|
|
164
|
-
return ["/*", `${indentation} * ${formatSentence(paragraphs)}`, `${indentation} */`].join(
|
|
165
|
-
getNewline(sourceCode.text),
|
|
166
|
-
);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Create the complete-sentence comment rule.
|
|
171
|
-
*
|
|
172
|
-
* @returns {object}
|
|
173
|
-
* The Oxlint rule definition.
|
|
174
|
-
*/
|
|
175
|
-
export default {
|
|
176
|
-
meta: {
|
|
177
|
-
docs: { description: "Format existing comments as complete sentences." },
|
|
178
|
-
fixable: "code",
|
|
179
|
-
type: "layout",
|
|
180
|
-
},
|
|
181
|
-
/**
|
|
182
|
-
* Create the rule's node visitors.
|
|
183
|
-
*
|
|
184
|
-
* @param {object} context
|
|
185
|
-
* The Oxlint rule context.
|
|
186
|
-
*
|
|
187
|
-
* @returns {object}
|
|
188
|
-
* The visitor functions for this rule.
|
|
189
|
-
*/
|
|
190
|
-
createOnce(context) {
|
|
191
|
-
return {
|
|
192
|
-
/**
|
|
193
|
-
* Format every comment in the file as a complete sentence.
|
|
194
|
-
*/
|
|
195
|
-
Program() {
|
|
196
|
-
for (const commentGroup of getLineCommentGroups(context.sourceCode)) {
|
|
197
|
-
// The group's replacement text, or null when it already
|
|
198
|
-
// reads as a sentence.
|
|
199
|
-
const formattedGroup = formatLineCommentGroup(context.sourceCode, commentGroup);
|
|
200
|
-
|
|
201
|
-
if (formattedGroup) {
|
|
202
|
-
context.report({
|
|
203
|
-
/**
|
|
204
|
-
* Apply the formatted replacement to the comment
|
|
205
|
-
* group.
|
|
206
|
-
*
|
|
207
|
-
* @param {object} fixer
|
|
208
|
-
* The Oxlint fixer.
|
|
209
|
-
*
|
|
210
|
-
* @returns {object[]}
|
|
211
|
-
* The fixes to apply.
|
|
212
|
-
*/
|
|
213
|
-
fix: (fixer) => {
|
|
214
|
-
// The fixes to apply, starting with the first
|
|
215
|
-
// comment's replacement.
|
|
216
|
-
const fixes = [
|
|
217
|
-
replaceMinimalComment(
|
|
218
|
-
fixer,
|
|
219
|
-
formattedGroup.firstComment,
|
|
220
|
-
getCommentText(context.sourceCode, formattedGroup.firstComment),
|
|
221
|
-
formattedGroup.firstReplacement,
|
|
222
|
-
),
|
|
223
|
-
];
|
|
224
|
-
|
|
225
|
-
if (formattedGroup.lastComment.range[0] !== formattedGroup.firstComment.range[0]) {
|
|
226
|
-
fixes.push(
|
|
227
|
-
replaceMinimalComment(
|
|
228
|
-
fixer,
|
|
229
|
-
formattedGroup.lastComment,
|
|
230
|
-
getCommentText(context.sourceCode, formattedGroup.lastComment),
|
|
231
|
-
formattedGroup.lastReplacement,
|
|
232
|
-
),
|
|
233
|
-
);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
return fixes;
|
|
237
|
-
},
|
|
238
|
-
message: "Comment text must be a complete sentence.",
|
|
239
|
-
node: formattedGroup.firstComment,
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
for (const comment of context.sourceCode.getAllComments()) {
|
|
245
|
-
if (comment.type !== "Block") {
|
|
246
|
-
continue;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
// The comment's raw source text.
|
|
250
|
-
const commentText = getCommentText(context.sourceCode, comment);
|
|
251
|
-
|
|
252
|
-
// The comment, sentence-formatted using the JSDoc or
|
|
253
|
-
// ordinary-block formatter.
|
|
254
|
-
const formattedComment = isJSDoc(commentText)
|
|
255
|
-
? formatJSDocPunctuation(context.sourceCode, comment)
|
|
256
|
-
: formatOrdinaryBlockComment(context.sourceCode, comment);
|
|
257
|
-
|
|
258
|
-
if (formattedComment !== commentText) {
|
|
259
|
-
context.report({
|
|
260
|
-
/**
|
|
261
|
-
* Apply the sentence-formatted replacement to the
|
|
262
|
-
* comment.
|
|
263
|
-
*
|
|
264
|
-
* @param {object} fixer
|
|
265
|
-
* The Oxlint fixer.
|
|
266
|
-
*
|
|
267
|
-
* @returns {object}
|
|
268
|
-
* The fix to apply.
|
|
269
|
-
*/
|
|
270
|
-
fix: (fixer) => {
|
|
271
|
-
return replaceMinimalComment(fixer, comment, commentText, formattedComment);
|
|
272
|
-
},
|
|
273
|
-
message: "Comment text must be a complete sentence.",
|
|
274
|
-
node: comment,
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
},
|
|
279
|
-
};
|
|
280
|
-
},
|
|
281
|
-
};
|