eslint-plugin-lube 0.6.0 → 0.8.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 +39 -0
- package/README.md +6 -1
- package/docs/prefer-param-tag.md +25 -0
- package/docs/prefer-type-tag.md +45 -0
- package/package.json +3 -3
- package/src/index.js +14 -2
- package/src/rules/ascii-order.js +165 -78
- package/src/rules/prefer-param-tag.js +252 -0
- package/src/rules/prefer-type-tag.js +165 -0
- package/src/rules/pretty-imports.js +21 -12
- package/src/rules/pretty-jsdoc-casting.js +76 -37
- package/src/rules/pretty-sequence.js +110 -42
- package/src/rules/pretty-ternary.js +43 -23
- package/src/rules/svelte-naming-convention.js +287 -134
- package/src/utils/keep_body_comments.js +53 -0
- package/src/utils/keep_comments.js +6 -2
- package/src/utils/keep_line_breaks.js +3 -1
- package/src/utils/skip_hole_commas.js +4 -4
- package/src/utils/skip_optional_patterns.js +3 -1
- package/src/utils/skip_unchanged_fixes.js +3 -1
- package/src/utils/source_text.js +57 -23
- package/types/index.d.ts.map +1 -1
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
const blank_regex = /^\s*\*?\s*$/
|
|
2
|
+
const directive_regex = /^\s*(?:eslint-disable-next-line\b|@ts-(?:expect-error|ignore)\b)/
|
|
3
|
+
const line_break_regex = /\s*(?:\r\n|[\n\r\p{Zl}\p{Zp}])\s*/gu
|
|
4
|
+
const line_terminator_regex = /\r\n|[\n\r\p{Zl}\p{Zp}]/u
|
|
5
|
+
const param_tag_regex = /^\s*\*\s*@param\b/
|
|
6
|
+
const space_regex = /^\s*$/
|
|
7
|
+
const tag_after_param_regex = /^\s*\*\s*@(?:deprecated|example|returns?|see|throws|yields?)\b/
|
|
8
|
+
const tag_regex = /^\s*\*\s*@/
|
|
9
|
+
const type_regex = /^\*\s*@type\s*(\{[^]*\})\s*$/
|
|
10
|
+
const white_space_regex = /\s/
|
|
11
|
+
/**
|
|
12
|
+
* @param {string} text
|
|
13
|
+
* @returns {string}
|
|
14
|
+
*/
|
|
15
|
+
function escape_regex(text) {
|
|
16
|
+
return text.replace(/[$()*+.?[\\\]^{|}]/g, "\\$&")
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* @param {import("estree").Comment} comment
|
|
20
|
+
* @param {string} indent
|
|
21
|
+
* @param {string} eol
|
|
22
|
+
* @param {{ index: number, tag: string }[]} tags
|
|
23
|
+
* @returns {string}
|
|
24
|
+
*/
|
|
25
|
+
function jsdoc_with(comment, indent, eol, tags) {
|
|
26
|
+
let rows = `/*${comment.value}*/`.split(line_terminator_regex)
|
|
27
|
+
/**
|
|
28
|
+
* @param {number} index
|
|
29
|
+
* @returns {string}
|
|
30
|
+
*/
|
|
31
|
+
function row_at(index) {
|
|
32
|
+
return /** @type {string} */(rows[index])/**/
|
|
33
|
+
}
|
|
34
|
+
if (rows.length < 2 || !/^\/\*\*\s*$/.test(row_at(0)) || !/^\s*\*\/$/.test(row_at(rows.length - 1))) {
|
|
35
|
+
const body = comment.value.slice(1).split(line_terminator_regex)
|
|
36
|
+
.map(
|
|
37
|
+
row => row.replace(/^\s*\*?\s?/, "").trimEnd()
|
|
38
|
+
)
|
|
39
|
+
while (body[0] === "") {
|
|
40
|
+
body.shift()
|
|
41
|
+
}
|
|
42
|
+
while (body.at(-1) === "") {
|
|
43
|
+
body.pop()
|
|
44
|
+
}
|
|
45
|
+
rows = [
|
|
46
|
+
"/**",
|
|
47
|
+
...body.map(
|
|
48
|
+
row => `${indent} * ${row}`.trimEnd()
|
|
49
|
+
),
|
|
50
|
+
`${indent} */`
|
|
51
|
+
]
|
|
52
|
+
}
|
|
53
|
+
for (const { index, tag } of tags) {
|
|
54
|
+
const rows_end = rows.length - 1
|
|
55
|
+
/** @type {{ end: number, start: number }[]} */
|
|
56
|
+
const blocks = []
|
|
57
|
+
for (let i = 1; i < rows_end; i++) {
|
|
58
|
+
if (param_tag_regex.test(row_at(i))) {
|
|
59
|
+
let end = i + 1
|
|
60
|
+
while (end < rows_end && !tag_regex.test(row_at(end)) && !blank_regex.test(row_at(end))) {
|
|
61
|
+
end++
|
|
62
|
+
}
|
|
63
|
+
blocks.push({ end, start: i })
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
let at = rows_end
|
|
67
|
+
const first = blocks[0]
|
|
68
|
+
if (first) {
|
|
69
|
+
at = index == 0
|
|
70
|
+
? first.start
|
|
71
|
+
: /** @type {{ end: number, start: number }} */(blocks[Math.min(index, blocks.length) - 1])/**/.end
|
|
72
|
+
} else {
|
|
73
|
+
const tag_at = rows.findIndex(
|
|
74
|
+
row => tag_after_param_regex.test(row)
|
|
75
|
+
)
|
|
76
|
+
if (tag_at >= 0) {
|
|
77
|
+
at = tag_at
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const added = [ `${indent} * ${tag}` ]
|
|
81
|
+
if (at == rows_end && at > 1 && !tag_regex.test(row_at(at - 1)) && !blank_regex.test(row_at(at - 1))) {
|
|
82
|
+
added.unshift(`${indent} *`)
|
|
83
|
+
}
|
|
84
|
+
rows.splice(at, 0, ...added)
|
|
85
|
+
}
|
|
86
|
+
return rows.join(eol)
|
|
87
|
+
}
|
|
88
|
+
/** @type {import("eslint").Rule.RuleModule} */
|
|
89
|
+
export default {
|
|
90
|
+
create(context) {
|
|
91
|
+
const source_code = context.sourceCode
|
|
92
|
+
const text = source_code.text
|
|
93
|
+
/**
|
|
94
|
+
* @param {import("estree").Comment} comment
|
|
95
|
+
* @returns {import("eslint").AST.Range}
|
|
96
|
+
*/
|
|
97
|
+
function comment_range(comment) {
|
|
98
|
+
return /** @type {import("eslint").AST.Range} */(comment.range)/**/
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* @param {number} position
|
|
102
|
+
* @returns {number}
|
|
103
|
+
*/
|
|
104
|
+
function line_start_of(position) {
|
|
105
|
+
let start = position
|
|
106
|
+
while (start > 0 && !line_terminator_regex.test(text.charAt(start - 1))) {
|
|
107
|
+
start--
|
|
108
|
+
}
|
|
109
|
+
return start
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* @param {import("estree").Comment} comment
|
|
113
|
+
* @returns {boolean}
|
|
114
|
+
*/
|
|
115
|
+
function starts_line(comment) {
|
|
116
|
+
const start = comment_range(comment)[0]
|
|
117
|
+
return space_regex.test(
|
|
118
|
+
text.slice(line_start_of(start), start)
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
FunctionDeclaration(node) {
|
|
123
|
+
/** @type {{ comment: import("estree").Comment, index: number, name: string, type: string }[]} */
|
|
124
|
+
const named = []
|
|
125
|
+
const data = {
|
|
126
|
+
name: node.id?.name ?? "default"
|
|
127
|
+
}
|
|
128
|
+
for (const [ index, param ] of node.params.entries()) {
|
|
129
|
+
const comment = source_code.getCommentsBefore(param).findLast(
|
|
130
|
+
item => item.type == "Block" && type_regex.test(item.value)
|
|
131
|
+
)
|
|
132
|
+
if (!comment) {
|
|
133
|
+
continue
|
|
134
|
+
}
|
|
135
|
+
const target = param.type == "AssignmentPattern" ? param.left : param
|
|
136
|
+
if (target.type == "Identifier") {
|
|
137
|
+
named.push(
|
|
138
|
+
{
|
|
139
|
+
comment,
|
|
140
|
+
index,
|
|
141
|
+
name: target.name,
|
|
142
|
+
type: /** @type {string} */(/** @type {RegExpExecArray} */(type_regex.exec(comment.value))/**/[1])/**/.replace(line_break_regex, " ")
|
|
143
|
+
}
|
|
144
|
+
)
|
|
145
|
+
} else {
|
|
146
|
+
context.report(
|
|
147
|
+
{
|
|
148
|
+
data,
|
|
149
|
+
loc: /** @type {import("estree").SourceLocation} */(comment.loc)/**/,
|
|
150
|
+
messageId: "name"
|
|
151
|
+
}
|
|
152
|
+
)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const first = named[0]
|
|
156
|
+
if (!first) {
|
|
157
|
+
return
|
|
158
|
+
}
|
|
159
|
+
const loc = /** @type {import("estree").SourceLocation} */(first.comment.loc)/**/
|
|
160
|
+
const statement = node.parent.type == "ExportNamedDeclaration" || node.parent.type == "ExportDefaultDeclaration"
|
|
161
|
+
? node.parent
|
|
162
|
+
: node
|
|
163
|
+
const leading = source_code.getCommentsBefore(statement)
|
|
164
|
+
let start = /** @type {import("eslint").AST.Range} */(statement.range)/**/[0]
|
|
165
|
+
let last = leading.length - 1
|
|
166
|
+
while (last >= 0) {
|
|
167
|
+
const item = /** @type {import("estree").Comment} */(leading[last])/**/
|
|
168
|
+
if (!starts_line(item) || !directive_regex.test(item.value)) {
|
|
169
|
+
break
|
|
170
|
+
}
|
|
171
|
+
start = comment_range(item)[0]
|
|
172
|
+
last--
|
|
173
|
+
}
|
|
174
|
+
const candidate = leading[last]
|
|
175
|
+
const jsdoc = candidate?.type == "Block" && candidate.value.startsWith("*")
|
|
176
|
+
? candidate
|
|
177
|
+
: undefined
|
|
178
|
+
const anchor = jsdoc ? comment_range(jsdoc)[0] : start
|
|
179
|
+
const indent = text.slice(line_start_of(anchor), anchor)
|
|
180
|
+
if (!space_regex.test(indent)) {
|
|
181
|
+
context.report(
|
|
182
|
+
{ data, loc, messageId: "hand" }
|
|
183
|
+
)
|
|
184
|
+
return
|
|
185
|
+
}
|
|
186
|
+
const eol = text.match(line_terminator_regex)?.[0] ?? "\n"
|
|
187
|
+
const tags = named.filter(
|
|
188
|
+
item => !jsdoc || !new RegExp(
|
|
189
|
+
`@param\\s+(?:\\{[^]*?\\}\\s+)?\\[?${escape_regex(item.name)}(?![\\w$])`
|
|
190
|
+
).test(jsdoc.value)
|
|
191
|
+
).map(
|
|
192
|
+
item => (
|
|
193
|
+
{
|
|
194
|
+
index: item.index,
|
|
195
|
+
tag: `@param ${item.type} ${item.name}`
|
|
196
|
+
}
|
|
197
|
+
)
|
|
198
|
+
)
|
|
199
|
+
context.report(
|
|
200
|
+
{
|
|
201
|
+
data,
|
|
202
|
+
fix(fixer) {
|
|
203
|
+
const fixes = named.map(
|
|
204
|
+
({ comment }) => {
|
|
205
|
+
const [ begin, close ] = comment_range(comment)
|
|
206
|
+
let end = close
|
|
207
|
+
while (white_space_regex.test(text.charAt(end))) {
|
|
208
|
+
end++
|
|
209
|
+
}
|
|
210
|
+
return fixer.removeRange([ begin, end ])
|
|
211
|
+
}
|
|
212
|
+
)
|
|
213
|
+
if (jsdoc) {
|
|
214
|
+
fixes.push(
|
|
215
|
+
fixer.replaceTextRange(
|
|
216
|
+
comment_range(jsdoc),
|
|
217
|
+
jsdoc_with(jsdoc, indent, eol, tags)
|
|
218
|
+
)
|
|
219
|
+
)
|
|
220
|
+
} else {
|
|
221
|
+
fixes.push(
|
|
222
|
+
fixer.insertTextBeforeRange(
|
|
223
|
+
[ start, start ],
|
|
224
|
+
`/**${eol}${tags.map(({ tag }) => `${indent} * ${tag}${eol}`).join("")}${indent} */${eol}${indent}`
|
|
225
|
+
)
|
|
226
|
+
)
|
|
227
|
+
}
|
|
228
|
+
return fixes
|
|
229
|
+
},
|
|
230
|
+
loc,
|
|
231
|
+
messageId: "tag"
|
|
232
|
+
}
|
|
233
|
+
)
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
},
|
|
237
|
+
meta: {
|
|
238
|
+
docs: {
|
|
239
|
+
description: "Enforces JSDoc `@param` tags on a function declaration instead of inline `/** @type {T} */` comments inside its parameter list.",
|
|
240
|
+
recommended: false,
|
|
241
|
+
url: "https://github.com/artxe/lube-series/blob/master/packages/eslint-plugin-lube/docs/prefer-param-tag.md"
|
|
242
|
+
},
|
|
243
|
+
fixable: "code",
|
|
244
|
+
messages: {
|
|
245
|
+
hand: "Type the parameters of `{{name}}` with `@param` tags on the declaration instead of inline comments; it is not fixed automatically because the declaration or its JSDoc comment does not start its line, so write the tags by hand",
|
|
246
|
+
name: "Type this parameter of `{{name}}` with a `@param` tag on the declaration instead of an inline comment; it is not fixed automatically because the parameter has no simple name to write in the tag, so document it by hand",
|
|
247
|
+
tag: "Type the parameters of `{{name}}` with `@param` tags on the declaration instead of inline `@type` comments"
|
|
248
|
+
},
|
|
249
|
+
schema: [],
|
|
250
|
+
type: "suggestion"
|
|
251
|
+
}
|
|
252
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
const const_regex = /@type\s*\{\s*const\s*\}/
|
|
2
|
+
const directive_regex = /^\s*(?:eslint-disable-next-line\b|@ts-(?:expect-error|ignore)\b)/
|
|
3
|
+
const line_terminator_regex = /\r\n|[\n\r\p{Zl}\p{Zp}]/u
|
|
4
|
+
const space_regex = /^\s*$/
|
|
5
|
+
const statement_lists = new Set(
|
|
6
|
+
[
|
|
7
|
+
"BlockStatement",
|
|
8
|
+
"Program",
|
|
9
|
+
"StaticBlock",
|
|
10
|
+
"SwitchCase",
|
|
11
|
+
"TSModuleBlock"
|
|
12
|
+
]
|
|
13
|
+
)
|
|
14
|
+
const type_regex = /^\*[^]*@type\s*\{[^]*\}/
|
|
15
|
+
const typescript_file_regex = /\.[cm]?tsx?$/
|
|
16
|
+
/** @type {import("eslint").Rule.RuleModule} */
|
|
17
|
+
export default {
|
|
18
|
+
create(context) {
|
|
19
|
+
/** @type {import("../../private.js").TypeServices | undefined} */
|
|
20
|
+
const services = context.sourceCode.parserServices
|
|
21
|
+
const program = services?.program
|
|
22
|
+
if (!services || !program || typescript_file_regex.test(context.filename)) {
|
|
23
|
+
return {}
|
|
24
|
+
}
|
|
25
|
+
const checker = program.getTypeChecker()
|
|
26
|
+
const source_code = context.sourceCode
|
|
27
|
+
const text = source_code.text
|
|
28
|
+
/**
|
|
29
|
+
* @param {number} position
|
|
30
|
+
* @returns {number}
|
|
31
|
+
*/
|
|
32
|
+
function line_start_of(position) {
|
|
33
|
+
let start = position
|
|
34
|
+
while (start > 0 && !line_terminator_regex.test(text.charAt(start - 1))) {
|
|
35
|
+
start--
|
|
36
|
+
}
|
|
37
|
+
return start
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* @param {import("estree").Comment} comment
|
|
41
|
+
* @returns {boolean}
|
|
42
|
+
*/
|
|
43
|
+
function starts_line(comment) {
|
|
44
|
+
const start = /** @type {import("eslint").AST.Range} */(comment.range)/**/[0]
|
|
45
|
+
return space_regex.test(
|
|
46
|
+
text.slice(line_start_of(start), start)
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
VariableDeclaration(node) {
|
|
51
|
+
const [ declarator, other ] = node.declarations
|
|
52
|
+
const value = declarator?.init
|
|
53
|
+
const statement = node.parent.type == "ExportNamedDeclaration" ? node.parent : node
|
|
54
|
+
if (!declarator || !value || other || !statement_lists.has(statement.parent.type)) {
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
const open = source_code.getTokenBefore(value)
|
|
58
|
+
const close = source_code.getTokenAfter(value)
|
|
59
|
+
const assign = open && source_code.getTokenBefore(open)
|
|
60
|
+
if (open?.value != "(" || close?.value != ")" || assign?.value != "=") {
|
|
61
|
+
return
|
|
62
|
+
}
|
|
63
|
+
const comment = source_code.getCommentsBefore(open).at(-1)
|
|
64
|
+
const comment_range = comment?.range
|
|
65
|
+
if (
|
|
66
|
+
!comment
|
|
67
|
+
|| !comment_range
|
|
68
|
+
|| comment.type != "Block"
|
|
69
|
+
|| !type_regex.test(comment.value)
|
|
70
|
+
|| const_regex.test(comment.value)
|
|
71
|
+
|| !space_regex.test(
|
|
72
|
+
text.slice(comment_range[1], open.range[0])
|
|
73
|
+
)
|
|
74
|
+
) {
|
|
75
|
+
return
|
|
76
|
+
}
|
|
77
|
+
const declaration = services.esTreeNodeToTSNodeMap.get(declarator)
|
|
78
|
+
const cast = /** @type {import("typescript").ParenthesizedExpression | undefined} */(declaration?.initializer)/**/
|
|
79
|
+
if (!cast?.expression) {
|
|
80
|
+
return
|
|
81
|
+
}
|
|
82
|
+
const leading = source_code.getCommentsBefore(statement)
|
|
83
|
+
const jsdoc = leading.filter(
|
|
84
|
+
item => item.type == "Block" && item.value.startsWith("*")
|
|
85
|
+
)
|
|
86
|
+
if (jsdoc.some(
|
|
87
|
+
item => type_regex.test(item.value)
|
|
88
|
+
)) {
|
|
89
|
+
return
|
|
90
|
+
}
|
|
91
|
+
const type = checker.getTypeAtLocation(cast)
|
|
92
|
+
const value_type = checker.getTypeAtLocation(cast.expression)
|
|
93
|
+
if (
|
|
94
|
+
!checker.isTypeAssignableTo(value_type, type)
|
|
95
|
+
|| type.isUnion() && !checker.isTypeAssignableTo(type, value_type)
|
|
96
|
+
) {
|
|
97
|
+
return
|
|
98
|
+
}
|
|
99
|
+
const tag = text.slice(
|
|
100
|
+
comment_range[0],
|
|
101
|
+
comment_range[1]
|
|
102
|
+
)
|
|
103
|
+
let start = /** @type {import("eslint").AST.Range} */(statement.range)/**/[0]
|
|
104
|
+
for (let i = leading.length - 1; i >= 0; i--) {
|
|
105
|
+
const item = /** @type {import("estree").Comment} */(leading[i])/**/
|
|
106
|
+
if (!starts_line(item) || !directive_regex.test(item.value)) {
|
|
107
|
+
break
|
|
108
|
+
}
|
|
109
|
+
start = /** @type {import("eslint").AST.Range} */(item.range)/**/[0]
|
|
110
|
+
}
|
|
111
|
+
const end = text.startsWith("/**/", close.range[1]) ? close.range[1] + 4 : close.range[1]
|
|
112
|
+
const data = {
|
|
113
|
+
tag,
|
|
114
|
+
value: checker.typeToString(value_type)
|
|
115
|
+
}
|
|
116
|
+
const loc = {
|
|
117
|
+
end: source_code.getLocFromIndex(end),
|
|
118
|
+
start: source_code.getLocFromIndex(comment_range[0])
|
|
119
|
+
}
|
|
120
|
+
if (jsdoc.length) {
|
|
121
|
+
context.report(
|
|
122
|
+
{ data, loc, messageId: "merge" }
|
|
123
|
+
)
|
|
124
|
+
return
|
|
125
|
+
}
|
|
126
|
+
const indent = text.slice(line_start_of(start), start)
|
|
127
|
+
const eol = text.match(line_terminator_regex)?.[0] ?? "\n"
|
|
128
|
+
const inner = text.slice(open.range[1], close.range[0])
|
|
129
|
+
context.report(
|
|
130
|
+
{
|
|
131
|
+
data,
|
|
132
|
+
fix(fixer) {
|
|
133
|
+
return [
|
|
134
|
+
fixer.insertTextBeforeRange(
|
|
135
|
+
[ start, start ],
|
|
136
|
+
space_regex.test(indent) ? tag + eol + indent : tag + " "
|
|
137
|
+
),
|
|
138
|
+
fixer.replaceTextRange(
|
|
139
|
+
[ comment_range[0], end ],
|
|
140
|
+
value.type == "SequenceExpression" ? `(${inner})` : inner
|
|
141
|
+
)
|
|
142
|
+
]
|
|
143
|
+
},
|
|
144
|
+
loc,
|
|
145
|
+
messageId: "tag"
|
|
146
|
+
}
|
|
147
|
+
)
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
meta: {
|
|
152
|
+
docs: {
|
|
153
|
+
description: "Enforces a JSDoc type tag on a declaration instead of a type cast of its value, wherever the value is assignable to the type.",
|
|
154
|
+
recommended: false,
|
|
155
|
+
url: "https://github.com/artxe/lube-series/blob/master/packages/eslint-plugin-lube/docs/prefer-type-tag.md"
|
|
156
|
+
},
|
|
157
|
+
fixable: "code",
|
|
158
|
+
messages: {
|
|
159
|
+
merge: "Type the declaration with `{{tag}}` instead of casting its value, since its type `{{value}}` is assignable to it; it is not fixed automatically because the declaration already has a JSDoc comment, so add the type to it by hand",
|
|
160
|
+
tag: "Write `{{tag}}` in front of the declaration instead of casting its value, since its type `{{value}}` is assignable to it"
|
|
161
|
+
},
|
|
162
|
+
schema: [],
|
|
163
|
+
type: "suggestion"
|
|
164
|
+
}
|
|
165
|
+
}
|
|
@@ -45,12 +45,13 @@ export default {
|
|
|
45
45
|
)
|
|
46
46
|
const first = specifiers[0]
|
|
47
47
|
const last = specifiers[specifiers.length - 1]
|
|
48
|
-
if (first?.type != specifier_type || !last || source_code.getCommentsInside(node).length)
|
|
48
|
+
if (first?.type != specifier_type || !last || source_code.getCommentsInside(node).length) {
|
|
49
|
+
return
|
|
50
|
+
}
|
|
49
51
|
const tokens = source_code.getTokens(node)
|
|
50
52
|
const open_index = tokens.findIndex(
|
|
51
53
|
token => token.range[0] == first.range[0]
|
|
52
54
|
) - 1
|
|
53
|
-
if (tokens[open_index]?.value != "{") return
|
|
54
55
|
const head = tokens.slice(0, open_index)
|
|
55
56
|
.map(token => token.value)
|
|
56
57
|
.join(" ")
|
|
@@ -94,9 +95,13 @@ export default {
|
|
|
94
95
|
: `${head} ${names}`
|
|
95
96
|
) + tail + (keeps_semicolon ? ";" : "")
|
|
96
97
|
const current = get_text(node.range[0], end)
|
|
97
|
-
if (corrected_text == current)
|
|
98
|
+
if (corrected_text == current) {
|
|
99
|
+
return
|
|
100
|
+
}
|
|
98
101
|
const same_structure = to_structure(current) == to_structure(corrected_text)
|
|
99
|
-
if (!fix_indent && same_structure)
|
|
102
|
+
if (!fix_indent && same_structure) {
|
|
103
|
+
return
|
|
104
|
+
}
|
|
100
105
|
context.report(
|
|
101
106
|
{
|
|
102
107
|
data: {
|
|
@@ -127,18 +132,22 @@ export default {
|
|
|
127
132
|
/** @type {import("../../private.js").RuleListener} */
|
|
128
133
|
const listener = {
|
|
129
134
|
ExportNamedDeclaration: node => {
|
|
130
|
-
if (check_exports)
|
|
135
|
+
if (check_exports) {
|
|
136
|
+
verify(
|
|
131
137
|
/** @type {import("../../private.js").AstNode & import("estree").ExportNamedDeclaration} */(node)/**/,
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
138
|
+
"ExportSpecifier",
|
|
139
|
+
"export"
|
|
140
|
+
)
|
|
141
|
+
}
|
|
135
142
|
},
|
|
136
143
|
ImportDeclaration: node => {
|
|
137
|
-
if (check_imports)
|
|
144
|
+
if (check_imports) {
|
|
145
|
+
verify(
|
|
138
146
|
/** @type {import("../../private.js").AstNode & import("estree").ImportDeclaration} */(node)/**/,
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
147
|
+
"ImportSpecifier",
|
|
148
|
+
"import"
|
|
149
|
+
)
|
|
150
|
+
}
|
|
142
151
|
}
|
|
143
152
|
}
|
|
144
153
|
return /** @type {import("eslint").Rule.RuleListener} */(/** @type {unknown} */(listener))/**/
|
|
@@ -8,7 +8,9 @@ const marker_regex = /\/\*\*\//g
|
|
|
8
8
|
/** @type {import("eslint").Rule.RuleModule} */
|
|
9
9
|
export default {
|
|
10
10
|
create(context) {
|
|
11
|
-
if (typescript_file_regex.test(context.filename))
|
|
11
|
+
if (typescript_file_regex.test(context.filename)) {
|
|
12
|
+
return {}
|
|
13
|
+
}
|
|
12
14
|
const source_code = context.sourceCode
|
|
13
15
|
const source = create_source_text(source_code)
|
|
14
16
|
const text = source.text
|
|
@@ -34,10 +36,11 @@ export default {
|
|
|
34
36
|
* @returns {boolean}
|
|
35
37
|
*/
|
|
36
38
|
function is_arguments(position) {
|
|
37
|
-
const node = /** @type {import("../../private.js").AstNode
|
|
38
|
-
if (!node) return false
|
|
39
|
+
const node = /** @type {import("../../private.js").AstNode} */(/** @type {unknown} */(source_code.getNodeByRangeIndex(position)))/**/
|
|
39
40
|
const type = node.type
|
|
40
|
-
if (type != "CallExpression" && type != "ImportExpression" && type != "NewExpression")
|
|
41
|
+
if (type != "CallExpression" && type != "ImportExpression" && type != "NewExpression") {
|
|
42
|
+
return false
|
|
43
|
+
}
|
|
41
44
|
const before = type == "ImportExpression"
|
|
42
45
|
? source_code.getFirstToken(
|
|
43
46
|
/** @type {import("estree").Node} */(node)/**/
|
|
@@ -57,7 +60,9 @@ export default {
|
|
|
57
60
|
*/
|
|
58
61
|
function push(node) {
|
|
59
62
|
const range = node.range[0] + " " + node.range[1]
|
|
60
|
-
if (node_ranges.has(range))
|
|
63
|
+
if (node_ranges.has(range)) {
|
|
64
|
+
return
|
|
65
|
+
}
|
|
61
66
|
node_ranges.add(range)
|
|
62
67
|
expressions.push(node)
|
|
63
68
|
}
|
|
@@ -65,23 +70,30 @@ export default {
|
|
|
65
70
|
* @param {number} start
|
|
66
71
|
* @param {number} end
|
|
67
72
|
* @param {[number, number][]} removed
|
|
68
|
-
* @returns {string
|
|
73
|
+
* @returns {string}
|
|
69
74
|
*/
|
|
70
75
|
function render_without(start, end, removed) {
|
|
71
76
|
let position = start
|
|
72
77
|
let result = ""
|
|
73
78
|
for (const [ removed_start, removed_end ] of removed) {
|
|
74
|
-
if (removed_end <= start || removed_start >= end)
|
|
75
|
-
|
|
79
|
+
if (removed_end <= start || removed_start >= end) {
|
|
80
|
+
continue
|
|
81
|
+
}
|
|
76
82
|
let chunk_end = removed_start
|
|
77
83
|
if (text[removed_start] == ")") {
|
|
78
|
-
while (chunk_end > position && is_space(text.charCodeAt(chunk_end - 1)))
|
|
79
|
-
|
|
84
|
+
while (chunk_end > position && is_space(text.charCodeAt(chunk_end - 1))) {
|
|
85
|
+
chunk_end--
|
|
86
|
+
}
|
|
87
|
+
if (source.comment_by_end.get(chunk_end)?.type == "Line") {
|
|
88
|
+
chunk_end = removed_start
|
|
89
|
+
}
|
|
80
90
|
}
|
|
81
91
|
result += source.render(position, chunk_end)
|
|
82
92
|
position = removed_end
|
|
83
93
|
if (text[removed_start] == "(") {
|
|
84
|
-
while (position < end && is_space(text.charCodeAt(position)))
|
|
94
|
+
while (position < end && is_space(text.charCodeAt(position))) {
|
|
95
|
+
position++
|
|
96
|
+
}
|
|
85
97
|
}
|
|
86
98
|
}
|
|
87
99
|
return result + source.render(position, end)
|
|
@@ -93,7 +105,9 @@ export default {
|
|
|
93
105
|
function verify(node) {
|
|
94
106
|
const node_start = node.range[0]
|
|
95
107
|
const before = source.skip_left(node_start, 0)
|
|
96
|
-
if (!before || text[before - 1] != "(" || is_arguments(before - 1))
|
|
108
|
+
if (!before || text[before - 1] != "(" || is_arguments(before - 1)) {
|
|
109
|
+
return
|
|
110
|
+
}
|
|
97
111
|
/** @type {import("../../private.js").CastingLevel[]} */
|
|
98
112
|
const levels = []
|
|
99
113
|
/** @type {[number, number][]} */
|
|
@@ -110,27 +124,39 @@ export default {
|
|
|
110
124
|
continue
|
|
111
125
|
}
|
|
112
126
|
const comment = source.comment_by_end.get(left)
|
|
113
|
-
if (!comment)
|
|
127
|
+
if (!comment) {
|
|
128
|
+
break
|
|
129
|
+
}
|
|
114
130
|
const level = levels[levels.length - 1]
|
|
115
131
|
const type = comment.type == "Block" && type_regex.test(comment.value)
|
|
116
132
|
? get_type_comment(comment)
|
|
117
133
|
: void 0
|
|
118
134
|
if (type && level && level.type == null) {
|
|
119
135
|
let comment_end = comment.range[1]
|
|
120
|
-
while (comment_end < level.open && is_space(text.charCodeAt(comment_end)))
|
|
136
|
+
while (comment_end < level.open && is_space(text.charCodeAt(comment_end))) {
|
|
137
|
+
comment_end++
|
|
138
|
+
}
|
|
121
139
|
level.type = type
|
|
122
140
|
removed.push(
|
|
123
141
|
[ comment.range[0], comment_end ]
|
|
124
142
|
)
|
|
125
|
-
} else if (type)
|
|
143
|
+
} else if (type) {
|
|
144
|
+
has_inner_type_comment = true
|
|
145
|
+
}
|
|
126
146
|
left = comment.range[0]
|
|
127
147
|
}
|
|
128
|
-
if (!left || text[left - 1] != "(" || is_arguments(left - 1))
|
|
148
|
+
if (!left || text[left - 1] != "(" || is_arguments(left - 1)) {
|
|
149
|
+
break
|
|
150
|
+
}
|
|
129
151
|
const close = source.skip_right(right, text.length)
|
|
130
152
|
right = close
|
|
131
|
-
if (text[close] != ")")
|
|
153
|
+
if (text[close] != ")") {
|
|
154
|
+
break
|
|
155
|
+
}
|
|
132
156
|
if (has_inner_type_comment) {
|
|
133
|
-
if (levels.length)
|
|
157
|
+
if (levels.length) {
|
|
158
|
+
return
|
|
159
|
+
}
|
|
134
160
|
content_end = close
|
|
135
161
|
content_start = left
|
|
136
162
|
has_inner_type_comment = false
|
|
@@ -149,16 +175,21 @@ export default {
|
|
|
149
175
|
}
|
|
150
176
|
const outer = levels[levels.length - 1]
|
|
151
177
|
const first = levels[0]
|
|
152
|
-
if (!outer || !first)
|
|
178
|
+
if (!outer || !first) {
|
|
179
|
+
return
|
|
180
|
+
}
|
|
153
181
|
const restores_outer = outer.type == null
|
|
154
182
|
for (const level of levels) {
|
|
155
|
-
if (level == outer && restores_outer)
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
183
|
+
if (level == outer && restores_outer) {
|
|
184
|
+
removed.push(
|
|
185
|
+
[ level.close + 1, level.close_end ]
|
|
186
|
+
)
|
|
187
|
+
} else {
|
|
188
|
+
removed.push(
|
|
189
|
+
[ level.open, level.open + 1 ],
|
|
190
|
+
[ level.close, level.close_end ]
|
|
191
|
+
)
|
|
192
|
+
}
|
|
162
193
|
}
|
|
163
194
|
removed.sort((a, b) => a[0] - b[0])
|
|
164
195
|
if (content_end + 4 <= first.close && source.render(content_end, content_end + 4) == "/**/") {
|
|
@@ -171,12 +202,16 @@ export default {
|
|
|
171
202
|
if (unwrapped_outer) {
|
|
172
203
|
let type_comments = 0
|
|
173
204
|
for (const level of unwrapped) {
|
|
174
|
-
if (level != unwrapped_outer && level.type != null)
|
|
205
|
+
if (level != unwrapped_outer && level.type != null) {
|
|
206
|
+
type_comments++
|
|
207
|
+
}
|
|
175
208
|
}
|
|
176
209
|
if (source.count_comments(
|
|
177
210
|
unwrapped_outer.open,
|
|
178
211
|
content_start
|
|
179
|
-
) > type_comments)
|
|
212
|
+
) > type_comments) {
|
|
213
|
+
return
|
|
214
|
+
}
|
|
180
215
|
}
|
|
181
216
|
const boundaries = [
|
|
182
217
|
left,
|
|
@@ -184,7 +219,9 @@ export default {
|
|
|
184
219
|
content_end,
|
|
185
220
|
right
|
|
186
221
|
]
|
|
187
|
-
if (boundaries.some(source.crosses_edit))
|
|
222
|
+
if (boundaries.some(source.crosses_edit)) {
|
|
223
|
+
return
|
|
224
|
+
}
|
|
188
225
|
let corrected_text = source.render(content_start, content_end)
|
|
189
226
|
for (const [ index, level ] of unwrapped.entries()) {
|
|
190
227
|
corrected_text = level.type
|
|
@@ -196,16 +233,18 @@ export default {
|
|
|
196
233
|
const prefix = render_without(left, content_start, removed)
|
|
197
234
|
const suffix = render_without(content_end, right, removed)
|
|
198
235
|
if (
|
|
199
|
-
prefix
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
236
|
+
line_break_regex.test(prefix)
|
|
237
|
+
&& restricted_regex.test(
|
|
238
|
+
text.slice(Math.max(0, left - 7), left)
|
|
239
|
+
)
|
|
240
|
+
) {
|
|
241
|
+
return
|
|
242
|
+
}
|
|
206
243
|
corrected_text = prefix + corrected_text + suffix
|
|
207
244
|
const current = source.render(left, right)
|
|
208
|
-
if (current == corrected_text)
|
|
245
|
+
if (current == corrected_text) {
|
|
246
|
+
return
|
|
247
|
+
}
|
|
209
248
|
const message_id = current.replace(marker_regex, "") == corrected_text.replace(marker_regex, "")
|
|
210
249
|
? "marker"
|
|
211
250
|
: "format"
|