eslint-plugin-lube 0.8.0 → 0.9.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 +95 -0
- package/README.md +17 -4
- package/docs/closures-last.md +34 -0
- package/docs/indent.md +102 -0
- package/docs/no-ambiguous-start.md +23 -0
- package/docs/no-loose-types.md +25 -0
- package/docs/no-typedef.md +24 -0
- package/docs/pretty-jsdoc-lines.md +30 -0
- package/docs/pretty-parens.md +59 -0
- package/docs/require-returns.md +21 -0
- package/docs/svelte-naming-convention.md +5 -6
- package/package.json +2 -7
- package/src/index.js +78 -19
- package/src/rules/ascii-order.js +113 -127
- package/src/rules/closures-last.js +323 -0
- package/src/rules/indent.js +84 -0
- package/src/rules/no-ambiguous-start.js +42 -0
- package/src/rules/no-loose-types.js +127 -0
- package/src/rules/no-typedef.js +44 -0
- package/src/rules/prefer-param-tag.js +35 -35
- package/src/rules/prefer-type-tag.js +29 -29
- package/src/rules/pretty-imports.js +23 -23
- package/src/rules/pretty-jsdoc-casting.js +39 -39
- package/src/rules/pretty-jsdoc-lines.js +104 -0
- package/src/rules/pretty-parens.js +280 -0
- package/src/rules/pretty-sequence.js +79 -95
- package/src/rules/pretty-ternary.js +27 -31
- package/src/rules/require-returns.js +190 -0
- package/src/rules/svelte-naming-convention.js +457 -546
- package/src/utils/accept_cr_eol.js +36 -0
- package/src/utils/indent_model.js +1596 -0
- package/src/utils/jsdoc_tags.js +30 -0
- package/src/utils/order_compare.js +17 -0
- package/src/utils/rule_options.js +30 -0
- package/src/utils/skip_unchanged_fixes.js +3 -3
- package/src/utils/source_text.js +80 -255
- package/src/utils/split_declarations.js +35 -14
- package/src/utils/trim_fixes.js +108 -0
- package/types/index.d.ts.map +1 -1
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import { compare } from "../utils/order_compare.js"
|
|
2
|
+
import { get_rule_options } from "../utils/rule_options.js"
|
|
3
|
+
import { create_source_text } from "../utils/source_text.js"
|
|
4
|
+
const function_owners = new Set(
|
|
5
|
+
[
|
|
6
|
+
"ArrowFunctionExpression",
|
|
7
|
+
"FunctionDeclaration",
|
|
8
|
+
"FunctionExpression"
|
|
9
|
+
]
|
|
10
|
+
)
|
|
11
|
+
const ends_statement_regex = /(?:Block|Body|Clause|Declaration|Statement)$/
|
|
12
|
+
const hazard_regex = /^[-+/<[(`]/
|
|
13
|
+
/** @type {import("eslint").Rule.RuleModule} */
|
|
14
|
+
export default {
|
|
15
|
+
create(context) {
|
|
16
|
+
const source_code = context.sourceCode
|
|
17
|
+
const source = create_source_text(source_code)
|
|
18
|
+
const text = source.text
|
|
19
|
+
const eol = source.eol
|
|
20
|
+
/** @type {import("../../private.js").AstNode[]} */
|
|
21
|
+
const bodies = []
|
|
22
|
+
return {
|
|
23
|
+
BlockStatement(node) {
|
|
24
|
+
if (function_owners.has(node.parent.type)) {
|
|
25
|
+
bodies.push(
|
|
26
|
+
/** @type {import("../../private.js").AstNode} */(/** @type {unknown} */(node))/**/
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"Program:exit": () => {
|
|
31
|
+
for (let i = bodies.length - 1; i >= 0; i--) {
|
|
32
|
+
verify(
|
|
33
|
+
/** @type {import("../../private.js").AstNode} */(bodies[i])/**/
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* @param {{ last: import("eslint").AST.Token, node: import("../../private.js").AstNode }} statement
|
|
40
|
+
* @returns {boolean}
|
|
41
|
+
*/
|
|
42
|
+
function continues(statement) {
|
|
43
|
+
const last = statement.last
|
|
44
|
+
if (last.value != "}") {
|
|
45
|
+
return last.value != ";"
|
|
46
|
+
}
|
|
47
|
+
let node = /** @type {import("../../private.js").AstNode} */(/** @type {unknown} */(source_code.getNodeByRangeIndex(last.range[0])))/**/
|
|
48
|
+
while (node != statement.node) {
|
|
49
|
+
if (!ends_statement_regex.test(node.type)) {
|
|
50
|
+
return true
|
|
51
|
+
}
|
|
52
|
+
node = node.parent
|
|
53
|
+
}
|
|
54
|
+
return false
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* @param {number} position
|
|
58
|
+
* @returns {import("../../private.js").Comment | undefined}
|
|
59
|
+
*/
|
|
60
|
+
function find_comment(position) {
|
|
61
|
+
let start = position
|
|
62
|
+
while (text[start] == " " || text[start] == "\t") {
|
|
63
|
+
start++
|
|
64
|
+
}
|
|
65
|
+
return source.comment_by_start.get(start)
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* @param {number} statement_end
|
|
69
|
+
* @returns {number}
|
|
70
|
+
*/
|
|
71
|
+
function get_end(statement_end) {
|
|
72
|
+
let end = statement_end
|
|
73
|
+
let comment = find_comment(end)
|
|
74
|
+
while (comment) {
|
|
75
|
+
end = comment.range[1]
|
|
76
|
+
comment = comment.type == "Line" ? undefined : find_comment(end)
|
|
77
|
+
}
|
|
78
|
+
return end
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* @param {import("../../private.js").AstNode} statement
|
|
82
|
+
* @returns {import("../../private.js").AstNode | undefined}
|
|
83
|
+
*/
|
|
84
|
+
function get_id(statement) {
|
|
85
|
+
return /** @type {{ id?: import("../../private.js").AstNode | null }} */(/** @type {unknown} */(statement))/**/.id ?? undefined
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* @param {number} position
|
|
89
|
+
* @param {number} previous_end
|
|
90
|
+
* @returns {number}
|
|
91
|
+
*/
|
|
92
|
+
function get_start(position, previous_end) {
|
|
93
|
+
const comments = /** @type {import("../../private.js").Comment[]} */(source_code.getCommentsBefore(
|
|
94
|
+
/** @type {import("eslint").AST.Token} */(source_code.getTokenByRangeStart(position))/**/
|
|
95
|
+
))/**/
|
|
96
|
+
const comment = comments.find(
|
|
97
|
+
item => item.range[0] >= previous_end && starts_line(item.range[0])
|
|
98
|
+
)
|
|
99
|
+
return comment ? comment.range[0] : position
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* @param {import("../../private.js").AstNode} statement
|
|
103
|
+
* @returns {boolean}
|
|
104
|
+
*/
|
|
105
|
+
function is_function(statement) {
|
|
106
|
+
/** @type {string} */
|
|
107
|
+
const type = statement.type
|
|
108
|
+
return type == "FunctionDeclaration" || type == "TSDeclareFunction"
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* @param {{ end: number, insert: number, skip: number, start: number }} chunk
|
|
112
|
+
* @returns {string}
|
|
113
|
+
*/
|
|
114
|
+
function render_chunk(chunk) {
|
|
115
|
+
if (chunk.insert >= 0) {
|
|
116
|
+
return `${source.render(chunk.start, chunk.insert)};${source.render(chunk.insert, chunk.end)}`
|
|
117
|
+
}
|
|
118
|
+
if (chunk.skip < 0) {
|
|
119
|
+
return source.render(chunk.start, chunk.end)
|
|
120
|
+
}
|
|
121
|
+
let resume = chunk.skip + 1
|
|
122
|
+
while (text[resume] == " " || text[resume] == "\t") {
|
|
123
|
+
resume++
|
|
124
|
+
}
|
|
125
|
+
return source.render(chunk.start, chunk.skip) + source.render(resume, chunk.end)
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* @param {number} semicolon
|
|
129
|
+
* @param {import("../../private.js").AstNode} next
|
|
130
|
+
* @returns {boolean}
|
|
131
|
+
*/
|
|
132
|
+
function starts_guard(semicolon, next) {
|
|
133
|
+
return starts_line(semicolon) && source.get_line_start(semicolon) == source.get_line_start(next.range[0])
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* @param {number} index
|
|
137
|
+
* @returns {boolean}
|
|
138
|
+
*/
|
|
139
|
+
function starts_line(index) {
|
|
140
|
+
return source.get_indent(index).length == index - source.get_line_start(index)
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* @param {import("../../private.js").AstNode} body
|
|
144
|
+
* @returns {void}
|
|
145
|
+
*/
|
|
146
|
+
function verify(body) {
|
|
147
|
+
const nodes = /** @type {import("../../private.js").AstNode[]} */(/** @type {unknown} */(
|
|
148
|
+
/** @type {import("estree").BlockStatement} */(/** @type {unknown} */(body))/**/.body
|
|
149
|
+
))/**/
|
|
150
|
+
/** @type {{ end: number, guard: number, last: import("eslint").AST.Token, node: import("../../private.js").AstNode }[]} */
|
|
151
|
+
const statements = []
|
|
152
|
+
let guard = -1
|
|
153
|
+
/** @type {Set<number>} */
|
|
154
|
+
const owned = new Set()
|
|
155
|
+
for (const [ index, node ] of nodes.entries()) {
|
|
156
|
+
const next = nodes[index + 1]
|
|
157
|
+
const guards_next = !!next && /** @type {string} */(next.type)/**/ != "EmptyStatement"
|
|
158
|
+
/** @type {string} */
|
|
159
|
+
const type = node.type
|
|
160
|
+
if (type == "EmptyStatement" && guards_next && starts_guard(
|
|
161
|
+
node.range[0],
|
|
162
|
+
/** @type {import("../../private.js").AstNode} */(next)/**/
|
|
163
|
+
)) {
|
|
164
|
+
guard = node.range[0]
|
|
165
|
+
continue
|
|
166
|
+
}
|
|
167
|
+
let last = /** @type {import("eslint").AST.Token} */(source_code.getLastToken(
|
|
168
|
+
/** @type {import("estree").Node} */(node)/**/
|
|
169
|
+
))/**/
|
|
170
|
+
let end = node.range[1]
|
|
171
|
+
let next_guard = -1
|
|
172
|
+
if (last.value == ";" && !is_function(node) && guards_next && starts_guard(
|
|
173
|
+
last.range[0],
|
|
174
|
+
/** @type {import("../../private.js").AstNode} */(next)/**/
|
|
175
|
+
)) {
|
|
176
|
+
const before = source_code.getTokenBefore(last)
|
|
177
|
+
if (before) {
|
|
178
|
+
next_guard = last.range[0]
|
|
179
|
+
owned.add(next_guard)
|
|
180
|
+
end = before.range[1]
|
|
181
|
+
last = before
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
statements.push({ end, guard, last, node })
|
|
185
|
+
guard = next_guard
|
|
186
|
+
}
|
|
187
|
+
let last = -1
|
|
188
|
+
for (const [ index, statement ] of statements.entries()) {
|
|
189
|
+
if (!is_function(statement.node)) {
|
|
190
|
+
last = index
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
const first = statements.findIndex(
|
|
194
|
+
(statement, index) => index < last && is_function(statement.node)
|
|
195
|
+
)
|
|
196
|
+
if (first < 0) {
|
|
197
|
+
return
|
|
198
|
+
}
|
|
199
|
+
let fixable = true
|
|
200
|
+
let previous_end = first
|
|
201
|
+
? /** @type {{ end: number }} */(statements[first - 1])/**/.end
|
|
202
|
+
: body.range[0] + 1
|
|
203
|
+
let previous = statements[first - 1]
|
|
204
|
+
/** @type {{ end: number, gap: string, insert: number, is_function: boolean, name: string, skip: number, start: number }[]} */
|
|
205
|
+
const chunks = []
|
|
206
|
+
for (const statement of statements.slice(first)) {
|
|
207
|
+
const node = statement.node
|
|
208
|
+
const guarded = statement.guard >= 0
|
|
209
|
+
const code_start = get_start(
|
|
210
|
+
guarded ? statement.guard : node.range[0],
|
|
211
|
+
previous_end
|
|
212
|
+
)
|
|
213
|
+
const start = source.get_line_start(code_start)
|
|
214
|
+
const end = get_end(statement.end)
|
|
215
|
+
const after = /** @type {import("eslint").AST.Token} */(source_code.getTokenAfter(statement.last))/**/
|
|
216
|
+
const token = source_code.getFirstToken(
|
|
217
|
+
/** @type {import("estree").Node} */(node)/**/
|
|
218
|
+
)
|
|
219
|
+
if (
|
|
220
|
+
!starts_line(code_start)
|
|
221
|
+
|| source_code.getLocFromIndex(end).line >= after.loc.start.line
|
|
222
|
+
) {
|
|
223
|
+
fixable = false
|
|
224
|
+
}
|
|
225
|
+
let insert = -1
|
|
226
|
+
if (!is_function(node)) {
|
|
227
|
+
if (!guarded && !!token && hazard_regex.test(token.value) && !!previous && continues(previous)) {
|
|
228
|
+
insert = token.range[0]
|
|
229
|
+
}
|
|
230
|
+
previous = statement
|
|
231
|
+
}
|
|
232
|
+
chunks.push(
|
|
233
|
+
{
|
|
234
|
+
end,
|
|
235
|
+
gap: chunks.length ? text.slice(previous_end, start) : eol,
|
|
236
|
+
insert,
|
|
237
|
+
is_function: is_function(node),
|
|
238
|
+
name: /** @type {import("estree").Identifier | undefined} */(get_id(node))/**/?.name ?? "",
|
|
239
|
+
skip: guarded && is_function(node) && owned.has(statement.guard)
|
|
240
|
+
? statement.guard
|
|
241
|
+
: -1,
|
|
242
|
+
start
|
|
243
|
+
}
|
|
244
|
+
)
|
|
245
|
+
previous_end = end
|
|
246
|
+
}
|
|
247
|
+
const region_start = /** @type {{ start: number }} */(chunks[0])/**/.start
|
|
248
|
+
const region_end = /** @type {{ end: number }} */(chunks[chunks.length - 1])/**/.end
|
|
249
|
+
const function_chunks = chunks.filter(chunk => chunk.is_function)
|
|
250
|
+
const ascii_options = get_rule_options(source_code, "ascii-order")
|
|
251
|
+
if (ascii_options && (ascii_options[0]?.checkDeclarations ?? true)) {
|
|
252
|
+
/** @type {{ chunks: typeof chunks, name: string }[]} */
|
|
253
|
+
const groups = []
|
|
254
|
+
for (const chunk of function_chunks) {
|
|
255
|
+
const group = groups[groups.length - 1]
|
|
256
|
+
if (group && group.name == chunk.name) {
|
|
257
|
+
group.chunks.push(chunk)
|
|
258
|
+
} else {
|
|
259
|
+
groups.push(
|
|
260
|
+
{
|
|
261
|
+
chunks: [ chunk ],
|
|
262
|
+
name: chunk.name
|
|
263
|
+
}
|
|
264
|
+
)
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
groups.sort(compare)
|
|
268
|
+
function_chunks.length = 0
|
|
269
|
+
for (const group of groups) {
|
|
270
|
+
function_chunks.push(...group.chunks)
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
const ordered = [
|
|
274
|
+
...chunks.filter(chunk => !chunk.is_function),
|
|
275
|
+
...function_chunks
|
|
276
|
+
]
|
|
277
|
+
let rendered = ""
|
|
278
|
+
for (const [ index, chunk ] of ordered.entries()) {
|
|
279
|
+
rendered += (index ? chunk.gap : "") + render_chunk(chunk)
|
|
280
|
+
}
|
|
281
|
+
if (fixable) {
|
|
282
|
+
source.edit(region_start, region_end, rendered)
|
|
283
|
+
}
|
|
284
|
+
for (const [ index, { node } ] of statements.entries()) {
|
|
285
|
+
const id = get_id(node)
|
|
286
|
+
if (index >= last || !is_function(node) || !id) {
|
|
287
|
+
continue
|
|
288
|
+
}
|
|
289
|
+
context.report(
|
|
290
|
+
{
|
|
291
|
+
data: {
|
|
292
|
+
name: /** @type {import("estree").Identifier} */(/** @type {unknown} */(id))/**/.name
|
|
293
|
+
},
|
|
294
|
+
fix: fixable
|
|
295
|
+
? fixer => fixer.replaceTextRange(
|
|
296
|
+
[ region_start, region_end ],
|
|
297
|
+
rendered
|
|
298
|
+
)
|
|
299
|
+
: null,
|
|
300
|
+
loc: {
|
|
301
|
+
end: source_code.getLocFromIndex(id.range[1]),
|
|
302
|
+
start: source_code.getLocFromIndex(id.range[0])
|
|
303
|
+
},
|
|
304
|
+
messageId: "last"
|
|
305
|
+
}
|
|
306
|
+
)
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
},
|
|
310
|
+
meta: {
|
|
311
|
+
docs: {
|
|
312
|
+
description: "Enforces function declarations inside a function body below its other statements, since they are hoisted.",
|
|
313
|
+
recommended: false,
|
|
314
|
+
url: "https://github.com/artxe/lube-series/blob/master/packages/eslint-plugin-lube/docs/closures-last.md"
|
|
315
|
+
},
|
|
316
|
+
fixable: "code",
|
|
317
|
+
messages: {
|
|
318
|
+
last: "Move the closure `{{name}}` below the last statement of its function, since it is hoisted"
|
|
319
|
+
},
|
|
320
|
+
schema: [],
|
|
321
|
+
type: "suggestion"
|
|
322
|
+
}
|
|
323
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import {
|
|
2
|
+
get_indent_model,
|
|
3
|
+
read_indent_options
|
|
4
|
+
} from "../utils/indent_model.js"
|
|
5
|
+
/**
|
|
6
|
+
* @param {string} indent
|
|
7
|
+
* @returns {string}
|
|
8
|
+
*/
|
|
9
|
+
function describe(indent) {
|
|
10
|
+
const tabs = indent.split("\t").length - 1
|
|
11
|
+
const spaces = indent.length - tabs
|
|
12
|
+
return tabs > 0 && spaces > 0
|
|
13
|
+
? `${plural(tabs, "tab")} and ${plural(spaces, "space")}`
|
|
14
|
+
: tabs > 0
|
|
15
|
+
? plural(tabs, "tab")
|
|
16
|
+
: plural(spaces, "space")
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* @param {number} count
|
|
20
|
+
* @param {string} name
|
|
21
|
+
* @returns {string}
|
|
22
|
+
*/
|
|
23
|
+
function plural(count, name) {
|
|
24
|
+
return `${count} ${name}${count == 1 ? "" : "s"}`
|
|
25
|
+
}
|
|
26
|
+
/** @type {import("eslint").Rule.RuleModule} */
|
|
27
|
+
export default {
|
|
28
|
+
create(context) {
|
|
29
|
+
const settings = read_indent_options(context.options)
|
|
30
|
+
const source_code = context.sourceCode
|
|
31
|
+
const text = source_code.text
|
|
32
|
+
return {
|
|
33
|
+
"Program:exit"() {
|
|
34
|
+
for (const { end, expected, line, start } of get_indent_model(source_code, settings).get_lines()) {
|
|
35
|
+
const actual = text.slice(start, end)
|
|
36
|
+
if (actual != expected) {
|
|
37
|
+
context.report(
|
|
38
|
+
{
|
|
39
|
+
data: {
|
|
40
|
+
actual: describe(actual),
|
|
41
|
+
expected: describe(expected)
|
|
42
|
+
},
|
|
43
|
+
fix: fixer => fixer.replaceTextRange([ start, end ], expected),
|
|
44
|
+
loc: {
|
|
45
|
+
end: { column: end - start, line },
|
|
46
|
+
start: { column: 0, line }
|
|
47
|
+
},
|
|
48
|
+
messageId: "wrongIndentation"
|
|
49
|
+
}
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
meta: {
|
|
57
|
+
docs: {
|
|
58
|
+
description: "Enforce consistent indentation computed from the syntax tree.",
|
|
59
|
+
url: "https://github.com/artxe/lube-series/blob/master/packages/eslint-plugin-lube/docs/indent.md"
|
|
60
|
+
},
|
|
61
|
+
fixable: "whitespace",
|
|
62
|
+
messages: {
|
|
63
|
+
wrongIndentation: "Expected indentation of {{expected}} but found {{actual}}."
|
|
64
|
+
},
|
|
65
|
+
schema: [
|
|
66
|
+
{
|
|
67
|
+
additionalProperties: false,
|
|
68
|
+
anyOf: [
|
|
69
|
+
{ enum: [ "tab" ] },
|
|
70
|
+
{ minimum: 0, type: "integer" }
|
|
71
|
+
]
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
additionalProperties: false,
|
|
75
|
+
properties: {
|
|
76
|
+
SwitchCase: { minimum: 0, type: "integer" },
|
|
77
|
+
binaryOperator: { minimum: 0, type: "integer" }
|
|
78
|
+
},
|
|
79
|
+
type: "object"
|
|
80
|
+
}
|
|
81
|
+
],
|
|
82
|
+
type: "layout"
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** @type {import("eslint").Rule.RuleModule} */
|
|
2
|
+
export default {
|
|
3
|
+
create(context) {
|
|
4
|
+
const source_code = context.sourceCode
|
|
5
|
+
return {
|
|
6
|
+
ExpressionStatement(node) {
|
|
7
|
+
const token = source_code.getFirstToken(node)
|
|
8
|
+
if (!token) {
|
|
9
|
+
return
|
|
10
|
+
}
|
|
11
|
+
const kind = token.type == "Template"
|
|
12
|
+
? "a template literal"
|
|
13
|
+
: token.type == "Punctuator" && token.value == "("
|
|
14
|
+
? "`(`"
|
|
15
|
+
: token.type == "Punctuator" && token.value == "["
|
|
16
|
+
? "`[`"
|
|
17
|
+
: undefined
|
|
18
|
+
if (kind) {
|
|
19
|
+
context.report(
|
|
20
|
+
{
|
|
21
|
+
data: { kind },
|
|
22
|
+
loc: token.loc,
|
|
23
|
+
messageId: "start"
|
|
24
|
+
}
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
meta: {
|
|
31
|
+
docs: {
|
|
32
|
+
description: "Disallows a statement that starts with `(`, `[`, a template literal or a leading type cast, which a line without a semicolon would join to the line above.",
|
|
33
|
+
recommended: false,
|
|
34
|
+
url: "https://github.com/artxe/lube-series/blob/master/packages/eslint-plugin-lube/docs/no-ambiguous-start.md"
|
|
35
|
+
},
|
|
36
|
+
messages: {
|
|
37
|
+
start: "A statement starting with {{kind}} joins the line above when it has no semicolon; assign the value to a variable first"
|
|
38
|
+
},
|
|
39
|
+
schema: [],
|
|
40
|
+
type: "problem"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
const line_star_regex = /(?<=\n[ \t]*)\*/g
|
|
2
|
+
const loose_names = new Set([ "any", "Object", "object" ])
|
|
3
|
+
const string_regex = /"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|`(?:[^`\\]|\\.)*`/g
|
|
4
|
+
const tag_regex = /(?:^|[\s*])@(?:extends|augments|param|prop|property|returns?|satisfies|template|this|throws|type|yields?)\s*\{/g
|
|
5
|
+
const token_regex = /[A-Za-z_$][\w$]*|\*/g
|
|
6
|
+
/**
|
|
7
|
+
* @param {string} type
|
|
8
|
+
* @returns {string[]}
|
|
9
|
+
*/
|
|
10
|
+
function loose_in(type) {
|
|
11
|
+
const clean = type.replace(
|
|
12
|
+
string_regex,
|
|
13
|
+
match => " ".repeat(match.length)
|
|
14
|
+
).replace(
|
|
15
|
+
line_star_regex,
|
|
16
|
+
match => " ".repeat(match.length)
|
|
17
|
+
)
|
|
18
|
+
/** @type {string[]} */
|
|
19
|
+
const found = []
|
|
20
|
+
for (const match of clean.matchAll(token_regex)) {
|
|
21
|
+
const [ token ] = match
|
|
22
|
+
const index = match.index
|
|
23
|
+
if (token != "*" && !loose_names.has(token)) {
|
|
24
|
+
continue
|
|
25
|
+
}
|
|
26
|
+
if (token != "*") {
|
|
27
|
+
const before = clean.slice(0, index).trimEnd()
|
|
28
|
+
const after = clean.slice(index + token.length).trimStart()
|
|
29
|
+
if (before.endsWith(".") || after.startsWith(":") || after.startsWith("?:")) {
|
|
30
|
+
continue
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
found.push(token)
|
|
34
|
+
}
|
|
35
|
+
return found
|
|
36
|
+
}
|
|
37
|
+
/** @type {import("eslint").Rule.RuleModule} */
|
|
38
|
+
export default {
|
|
39
|
+
create(context) {
|
|
40
|
+
const source_code = context.sourceCode
|
|
41
|
+
return {
|
|
42
|
+
Program() {
|
|
43
|
+
for (const comment of source_code.getAllComments()) {
|
|
44
|
+
if (comment.type != "Block" || !comment.value.startsWith("*")) {
|
|
45
|
+
continue
|
|
46
|
+
}
|
|
47
|
+
const range = /** @type {import("eslint").AST.Range} */(comment.range)/**/
|
|
48
|
+
for (const match of comment.value.matchAll(tag_regex)) {
|
|
49
|
+
const open = match.index + match[0].length - 1
|
|
50
|
+
let depth = 0
|
|
51
|
+
let close = open
|
|
52
|
+
for (; close < comment.value.length; close++) {
|
|
53
|
+
const char = comment.value.charAt(close)
|
|
54
|
+
depth += char == "{"
|
|
55
|
+
? 1
|
|
56
|
+
: char == "}"
|
|
57
|
+
? -1
|
|
58
|
+
: 0
|
|
59
|
+
if (depth == 0) {
|
|
60
|
+
break
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
for (const type of loose_in(
|
|
64
|
+
comment.value.slice(open + 1, close)
|
|
65
|
+
)) {
|
|
66
|
+
context.report(
|
|
67
|
+
{
|
|
68
|
+
data: { type },
|
|
69
|
+
loc: {
|
|
70
|
+
end: source_code.getLocFromIndex(range[0] + 2 + open + 1),
|
|
71
|
+
start: source_code.getLocFromIndex(range[0] + 2 + open)
|
|
72
|
+
},
|
|
73
|
+
messageId: "loose"
|
|
74
|
+
}
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
TSAnyKeyword(
|
|
81
|
+
/** @type {import("estree").Node} */ node
|
|
82
|
+
) {
|
|
83
|
+
context.report(
|
|
84
|
+
{
|
|
85
|
+
data: { type: "any" },
|
|
86
|
+
messageId: "loose",
|
|
87
|
+
node
|
|
88
|
+
}
|
|
89
|
+
)
|
|
90
|
+
},
|
|
91
|
+
TSObjectKeyword(
|
|
92
|
+
/** @type {import("estree").Node} */ node
|
|
93
|
+
) {
|
|
94
|
+
context.report(
|
|
95
|
+
{
|
|
96
|
+
data: { type: "object" },
|
|
97
|
+
messageId: "loose",
|
|
98
|
+
node
|
|
99
|
+
}
|
|
100
|
+
)
|
|
101
|
+
},
|
|
102
|
+
"TSTypeReference[typeName.name='Object']"(
|
|
103
|
+
/** @type {import("estree").Node} */ node
|
|
104
|
+
) {
|
|
105
|
+
context.report(
|
|
106
|
+
{
|
|
107
|
+
data: { type: "Object" },
|
|
108
|
+
messageId: "loose",
|
|
109
|
+
node
|
|
110
|
+
}
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
meta: {
|
|
116
|
+
docs: {
|
|
117
|
+
description: "Disallows `any`, `*`, `object` and `Object` in JSDoc type expressions and TypeScript type annotations.",
|
|
118
|
+
recommended: false,
|
|
119
|
+
url: "https://github.com/artxe/lube-series/blob/master/packages/eslint-plugin-lube/docs/no-loose-types.md"
|
|
120
|
+
},
|
|
121
|
+
messages: {
|
|
122
|
+
loose: "`{{type}}` is too loose a type; use `unknown` where the value is really arbitrary, or a precise type"
|
|
123
|
+
},
|
|
124
|
+
schema: [],
|
|
125
|
+
type: "suggestion"
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
const tag_regex = /(?:^|[\s*])@(typedef|callback)\b/g
|
|
2
|
+
/** @type {import("eslint").Rule.RuleModule} */
|
|
3
|
+
export default {
|
|
4
|
+
create(context) {
|
|
5
|
+
const source_code = context.sourceCode
|
|
6
|
+
return {
|
|
7
|
+
Program() {
|
|
8
|
+
for (const comment of source_code.getAllComments()) {
|
|
9
|
+
if (comment.type != "Block" || !comment.value.startsWith("*")) {
|
|
10
|
+
continue
|
|
11
|
+
}
|
|
12
|
+
const range = /** @type {import("eslint").AST.Range} */(comment.range)/**/
|
|
13
|
+
for (const match of comment.value.matchAll(tag_regex)) {
|
|
14
|
+
const at = range[0] + 2 + match.index + match[0].indexOf("@")
|
|
15
|
+
context.report(
|
|
16
|
+
{
|
|
17
|
+
data: { tag: `@${match[1]}` },
|
|
18
|
+
loc: {
|
|
19
|
+
end: source_code.getLocFromIndex(
|
|
20
|
+
at + 1 + String(match[1]).length
|
|
21
|
+
),
|
|
22
|
+
start: source_code.getLocFromIndex(at)
|
|
23
|
+
},
|
|
24
|
+
messageId: "typedef"
|
|
25
|
+
}
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
meta: {
|
|
33
|
+
docs: {
|
|
34
|
+
description: "Disallows the JSDoc tags `@typedef` and `@callback`.",
|
|
35
|
+
recommended: false,
|
|
36
|
+
url: "https://github.com/artxe/lube-series/blob/master/packages/eslint-plugin-lube/docs/no-typedef.md"
|
|
37
|
+
},
|
|
38
|
+
messages: {
|
|
39
|
+
typedef: "`{{tag}}` is not allowed; declare the type in a `.d.ts` file instead"
|
|
40
|
+
},
|
|
41
|
+
schema: [],
|
|
42
|
+
type: "suggestion"
|
|
43
|
+
}
|
|
44
|
+
}
|