eslint-plugin-lube 0.5.1 → 0.7.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 +283 -0
- package/LICENSE +21 -0
- package/README.md +123 -9
- package/docs/ascii-order.md +81 -0
- package/docs/prefer-type-tag.md +45 -0
- package/docs/pretty-imports.md +70 -59
- package/docs/pretty-jsdoc-casting.md +31 -23
- package/docs/pretty-sequence.md +129 -117
- package/docs/pretty-ternary.md +55 -0
- package/docs/svelte-naming-convention.md +59 -46
- package/package.json +60 -38
- package/src/index.js +308 -0
- package/src/rules/ascii-order.js +1032 -0
- package/src/rules/prefer-type-tag.js +165 -0
- package/src/rules/pretty-imports.js +188 -0
- package/src/rules/pretty-jsdoc-casting.js +325 -0
- package/src/rules/pretty-sequence.js +562 -0
- package/src/rules/pretty-ternary.js +255 -0
- package/src/rules/svelte-naming-convention.js +1062 -0
- package/src/utils/fix_recorder.js +50 -0
- package/src/utils/keep_body_comments.js +53 -0
- package/src/utils/keep_comments.js +103 -0
- package/src/utils/keep_line_breaks.js +58 -0
- package/src/utils/skip_hole_commas.js +34 -0
- package/src/utils/skip_optional_patterns.js +29 -0
- package/src/utils/skip_unchanged_fixes.js +37 -0
- package/src/utils/source_text.js +476 -0
- package/src/utils/split_declarations.js +75 -0
- package/src/utils/text_width.js +9 -0
- package/types/index.d.ts +27 -13
- package/types/index.d.ts.map +1 -1
- package/index.js +0 -126
- package/private.d.ts +0 -168
- package/rules/pretty-imports.js +0 -147
- package/rules/pretty-jsdoc-casting.js +0 -236
- package/rules/pretty-sequence.js +0 -441
- package/rules/svelte-naming-convention.js +0 -361
- package/test/pretty-imports/ExportNamedDeclaration.js +0 -123
- package/test/pretty-imports/ImportDeclaration.js +0 -125
- package/test/pretty-imports.js +0 -20
- package/test/pretty-jsdoc-casting/case_by_expression.js +0 -289
- package/test/pretty-jsdoc-casting/various_situations.js +0 -111
- package/test/pretty-jsdoc-casting.js +0 -22
- package/test/pretty-sequence/case_by_node_type.js +0 -360
- package/test/pretty-sequence/case_by_option.js +0 -142
- package/test/pretty-sequence/various_situations.js +0 -204
- package/test/pretty-sequence.js +0 -23
- package/test/svelte-naming-convention/basic_snake_case.js +0 -67
- package/test/svelte-naming-convention/case_by_identifier.js +0 -1394
- package/test/svelte-naming-convention/snake_case_with_symbols.js +0 -67
- package/test/svelte-naming-convention.js +0 -25
- package/tsconfig.json +0 -21
- package/types/rules/pretty-imports.d.ts +0 -3
- package/types/rules/pretty-imports.d.ts.map +0 -1
- package/types/rules/pretty-jsdoc-casting.d.ts +0 -3
- package/types/rules/pretty-jsdoc-casting.d.ts.map +0 -1
- package/types/rules/pretty-sequence.d.ts +0 -3
- package/types/rules/pretty-sequence.d.ts.map +0 -1
- package/types/rules/svelte-naming-convention.d.ts +0 -3
- package/types/rules/svelte-naming-convention.d.ts.map +0 -1
- package/types/test/pretty-imports/ExportNamedDeclaration.d.ts +0 -2
- package/types/test/pretty-imports/ExportNamedDeclaration.d.ts.map +0 -1
- package/types/test/pretty-imports/ImportDeclaration.d.ts +0 -2
- package/types/test/pretty-imports/ImportDeclaration.d.ts.map +0 -1
- package/types/test/pretty-imports.d.ts +0 -2
- package/types/test/pretty-imports.d.ts.map +0 -1
- package/types/test/pretty-jsdoc-casting/case_by_expression.d.ts +0 -2
- package/types/test/pretty-jsdoc-casting/case_by_expression.d.ts.map +0 -1
- package/types/test/pretty-jsdoc-casting/various_situations.d.ts +0 -2
- package/types/test/pretty-jsdoc-casting/various_situations.d.ts.map +0 -1
- package/types/test/pretty-jsdoc-casting.d.ts +0 -2
- package/types/test/pretty-jsdoc-casting.d.ts.map +0 -1
- package/types/test/pretty-sequence/case_by_node_type.d.ts +0 -2
- package/types/test/pretty-sequence/case_by_node_type.d.ts.map +0 -1
- package/types/test/pretty-sequence/case_by_option.d.ts +0 -2
- package/types/test/pretty-sequence/case_by_option.d.ts.map +0 -1
- package/types/test/pretty-sequence/various_situations.d.ts +0 -2
- package/types/test/pretty-sequence/various_situations.d.ts.map +0 -1
- package/types/test/pretty-sequence.d.ts +0 -2
- package/types/test/pretty-sequence.d.ts.map +0 -1
- package/types/test/svelte-naming-convention/basic_snake_case.d.ts +0 -2
- package/types/test/svelte-naming-convention/basic_snake_case.d.ts.map +0 -1
- package/types/test/svelte-naming-convention/case_by_identifier.d.ts +0 -2
- package/types/test/svelte-naming-convention/case_by_identifier.d.ts.map +0 -1
- package/types/test/svelte-naming-convention/snake_case_with_symbols.d.ts +0 -2
- package/types/test/svelte-naming-convention/snake_case_with_symbols.d.ts.map +0 -1
- package/types/test/svelte-naming-convention.d.ts +0 -2
- package/types/test/svelte-naming-convention.d.ts.map +0 -1
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { create_source_text } from "../utils/source_text.js"
|
|
2
|
+
const line_break_regex = /[\n\r\u2028\u2029]/
|
|
3
|
+
/** @type {import("eslint").Rule.RuleModule} */
|
|
4
|
+
export default {
|
|
5
|
+
create(context) {
|
|
6
|
+
/** @type {import("../../private.js").RuleOptions["pretty-ternary"]} */
|
|
7
|
+
const option = context.options[0]
|
|
8
|
+
const ignore_template_literal = option?.ignoreTemplateLiteral ?? true
|
|
9
|
+
const indent = option?.indent ?? "\t"
|
|
10
|
+
const max_length = option?.maxLength ?? 80
|
|
11
|
+
const source_code = context.sourceCode
|
|
12
|
+
const source = create_source_text(source_code)
|
|
13
|
+
const text = source.text
|
|
14
|
+
const eol = source.eol
|
|
15
|
+
/** @type {import("../../private.js").TernaryNode[]} */
|
|
16
|
+
const nodes = []
|
|
17
|
+
let template_end = -1
|
|
18
|
+
/**
|
|
19
|
+
* @param {import("../../private.js").TernaryNode} node
|
|
20
|
+
* @param {number} depth
|
|
21
|
+
* @param {import("../../private.js").TernaryGap[]} gaps
|
|
22
|
+
* @returns {boolean}
|
|
23
|
+
*/
|
|
24
|
+
function collect(node, depth, gaps) {
|
|
25
|
+
const parts = get_parts(node)
|
|
26
|
+
const question = source_code.getTokenAfter(
|
|
27
|
+
/** @type {import("estree").Node} */(/** @type {unknown} */(parts.test))/**/,
|
|
28
|
+
{
|
|
29
|
+
filter: token => token.value == "?"
|
|
30
|
+
}
|
|
31
|
+
)
|
|
32
|
+
const colon = question && source_code.getTokenAfter(
|
|
33
|
+
/** @type {import("estree").Node} */(/** @type {unknown} */(parts.consequent))/**/,
|
|
34
|
+
{
|
|
35
|
+
filter: token => token.value == ":"
|
|
36
|
+
}
|
|
37
|
+
)
|
|
38
|
+
if (
|
|
39
|
+
!question
|
|
40
|
+
|| !colon
|
|
41
|
+
|| question.range[1] > parts.consequent.range[0]
|
|
42
|
+
|| colon.range[1] > parts.alternate.range[0]
|
|
43
|
+
) {
|
|
44
|
+
return false
|
|
45
|
+
}
|
|
46
|
+
for (const operator of [ question, colon ]) {
|
|
47
|
+
const before = /** @type {import("eslint").AST.Token | import("estree").Comment} */(source_code.getTokenBefore(
|
|
48
|
+
operator,
|
|
49
|
+
{ includeComments: true }
|
|
50
|
+
))/**/
|
|
51
|
+
const after = /** @type {import("eslint").AST.Token | import("estree").Comment} */(source_code.getTokenAfter(
|
|
52
|
+
operator,
|
|
53
|
+
{ includeComments: true }
|
|
54
|
+
))/**/
|
|
55
|
+
if (after.type == "Line") {
|
|
56
|
+
return false
|
|
57
|
+
}
|
|
58
|
+
gaps.push(
|
|
59
|
+
{
|
|
60
|
+
depth,
|
|
61
|
+
end: operator.range[0],
|
|
62
|
+
line_break: true,
|
|
63
|
+
start: /** @type {[number, number]} */(before.range)/**/[1]
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
depth,
|
|
67
|
+
end: /** @type {[number, number]} */(after.range)/**/[0],
|
|
68
|
+
line_break: false,
|
|
69
|
+
start: operator.range[1]
|
|
70
|
+
}
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
for (const branch of [
|
|
74
|
+
parts.consequent,
|
|
75
|
+
parts.alternate
|
|
76
|
+
]) {
|
|
77
|
+
if (is_chained(branch) && !collect(branch, depth + 1, gaps)) {
|
|
78
|
+
return false
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return true
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* @param {import("../../private.js").TernaryNode} node
|
|
85
|
+
* @returns {{ alternate: import("../../private.js").TernaryNode, consequent: import("../../private.js").TernaryNode, test: import("../../private.js").TernaryNode }}
|
|
86
|
+
*/
|
|
87
|
+
function get_parts(node) {
|
|
88
|
+
return node.type == "TSConditionalType"
|
|
89
|
+
? {
|
|
90
|
+
alternate: node.falseType,
|
|
91
|
+
consequent: node.trueType,
|
|
92
|
+
test: node.extendsType
|
|
93
|
+
}
|
|
94
|
+
: {
|
|
95
|
+
alternate: node.alternate,
|
|
96
|
+
consequent: node.consequent,
|
|
97
|
+
test: node.test
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* @param {import("../../private.js").TernaryNode} node
|
|
102
|
+
* @returns {boolean}
|
|
103
|
+
*/
|
|
104
|
+
function is_chained(node) {
|
|
105
|
+
const parent = node.parent
|
|
106
|
+
if (!parent || !is_ternary(node) || parent.type != node.type) {
|
|
107
|
+
return false
|
|
108
|
+
}
|
|
109
|
+
const parts = get_parts(parent)
|
|
110
|
+
if (node != parts.consequent && node != parts.alternate) {
|
|
111
|
+
return false
|
|
112
|
+
}
|
|
113
|
+
const before = source_code.getTokenBefore(
|
|
114
|
+
/** @type {import("estree").Node} */(/** @type {unknown} */(node))/**/
|
|
115
|
+
)
|
|
116
|
+
return before?.value == "?" || before?.value == ":"
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* @param {import("../../private.js").TernaryNode} node
|
|
120
|
+
* @returns {boolean}
|
|
121
|
+
*/
|
|
122
|
+
function is_ternary(node) {
|
|
123
|
+
return node.type == "ConditionalExpression" || node.type == "TSConditionalType"
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* @param {import("../../private.js").TernaryNode} node
|
|
127
|
+
* @returns {void}
|
|
128
|
+
*/
|
|
129
|
+
function push(node) {
|
|
130
|
+
if (!ignore_template_literal || node.range[0] >= template_end) {
|
|
131
|
+
nodes.push(node)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* @param {import("../../private.js").TernaryNode} node
|
|
136
|
+
* @returns {void}
|
|
137
|
+
*/
|
|
138
|
+
function verify(node) {
|
|
139
|
+
if (is_chained(node)) {
|
|
140
|
+
return
|
|
141
|
+
}
|
|
142
|
+
/** @type {import("../../private.js").TernaryGap[]} */
|
|
143
|
+
const gaps = []
|
|
144
|
+
if (!collect(node, 0, gaps)) {
|
|
145
|
+
return
|
|
146
|
+
}
|
|
147
|
+
let length = node.range[1] - node.range[0]
|
|
148
|
+
for (const gap of gaps) {
|
|
149
|
+
length -= gap.end - gap.start - 1
|
|
150
|
+
}
|
|
151
|
+
const reason = gaps.length > 4
|
|
152
|
+
? "a branch is another ternary"
|
|
153
|
+
: length > max_length
|
|
154
|
+
? `it is longer than ${max_length} characters`
|
|
155
|
+
: line_break_regex.test(
|
|
156
|
+
text.slice(node.range[0], node.range[1])
|
|
157
|
+
)
|
|
158
|
+
? "it spans lines"
|
|
159
|
+
: ""
|
|
160
|
+
const multiline = reason != ""
|
|
161
|
+
const line_indent = source.get_line_indent(
|
|
162
|
+
/** @type {import("../../private.js").AstNode} */(/** @type {unknown} */(node))/**/,
|
|
163
|
+
node.range[0],
|
|
164
|
+
indent
|
|
165
|
+
)
|
|
166
|
+
/** @type {import("eslint").AST.Range[]} */
|
|
167
|
+
const ranges = []
|
|
168
|
+
/** @type {string[]} */
|
|
169
|
+
const values = []
|
|
170
|
+
for (const gap of gaps) {
|
|
171
|
+
const current = text.slice(gap.start, gap.end)
|
|
172
|
+
const breaks = multiline && gap.line_break
|
|
173
|
+
if (breaks && line_break_regex.test(current)) {
|
|
174
|
+
continue
|
|
175
|
+
}
|
|
176
|
+
const value = breaks
|
|
177
|
+
? eol + line_indent + indent.repeat(gap.depth + 1)
|
|
178
|
+
: " "
|
|
179
|
+
if (current == value) {
|
|
180
|
+
continue
|
|
181
|
+
}
|
|
182
|
+
ranges.push([ gap.start, gap.end ])
|
|
183
|
+
values.push(value)
|
|
184
|
+
}
|
|
185
|
+
if (!ranges.length) {
|
|
186
|
+
return
|
|
187
|
+
}
|
|
188
|
+
context.report(
|
|
189
|
+
{
|
|
190
|
+
data: { reason },
|
|
191
|
+
fix(fixer) {
|
|
192
|
+
return ranges.map(
|
|
193
|
+
(range, index) => fixer.replaceTextRange(
|
|
194
|
+
range,
|
|
195
|
+
/** @type {string} */(values[index])/**/
|
|
196
|
+
)
|
|
197
|
+
)
|
|
198
|
+
},
|
|
199
|
+
messageId: values.some(value => value != " ") ? "multiline" : "spacing",
|
|
200
|
+
node: /** @type {import("estree").Node} */(/** @type {unknown} */(node))/**/
|
|
201
|
+
}
|
|
202
|
+
)
|
|
203
|
+
}
|
|
204
|
+
return {
|
|
205
|
+
/** @param {import("estree").ConditionalExpression} node */
|
|
206
|
+
ConditionalExpression(node) {
|
|
207
|
+
push(
|
|
208
|
+
/** @type {import("../../private.js").TernaryNode} */(/** @type {unknown} */(node))/**/
|
|
209
|
+
)
|
|
210
|
+
},
|
|
211
|
+
"Program:exit": () => {
|
|
212
|
+
for (const node of nodes) {
|
|
213
|
+
verify(node)
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
/** @param {import("estree").Node} node */
|
|
217
|
+
TSConditionalType(node) {
|
|
218
|
+
push(
|
|
219
|
+
/** @type {import("../../private.js").TernaryNode} */(/** @type {unknown} */(node))/**/
|
|
220
|
+
)
|
|
221
|
+
},
|
|
222
|
+
/** @param {import("estree").TemplateLiteral} node */
|
|
223
|
+
TemplateLiteral(node) {
|
|
224
|
+
const end = /** @type {[number, number]} */(node.range)/**/[1]
|
|
225
|
+
if (end > template_end) {
|
|
226
|
+
template_end = end
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
meta: {
|
|
232
|
+
docs: {
|
|
233
|
+
description: "Enforces nested and long ternaries to break before `?` and `:` with one more indent per level.",
|
|
234
|
+
recommended: true,
|
|
235
|
+
url: "https://github.com/artxe/lube-series/blob/master/packages/eslint-plugin-lube/docs/pretty-ternary.md"
|
|
236
|
+
},
|
|
237
|
+
fixable: "code",
|
|
238
|
+
messages: {
|
|
239
|
+
multiline: "Break this ternary before `?` and `:`, since {{reason}}",
|
|
240
|
+
spacing: "Put one space before and after `?` and `:` of this ternary"
|
|
241
|
+
},
|
|
242
|
+
schema: [
|
|
243
|
+
{
|
|
244
|
+
additionalProperties: false,
|
|
245
|
+
properties: {
|
|
246
|
+
ignoreTemplateLiteral: { default: true, type: "boolean" },
|
|
247
|
+
indent: { default: "\t", type: "string" },
|
|
248
|
+
maxLength: { default: 80, type: "number" }
|
|
249
|
+
},
|
|
250
|
+
type: "object"
|
|
251
|
+
}
|
|
252
|
+
],
|
|
253
|
+
type: "layout"
|
|
254
|
+
}
|
|
255
|
+
}
|