eslint-plugin-lube 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.
Files changed (49) hide show
  1. package/CHANGELOG.md +260 -0
  2. package/LICENSE +21 -0
  3. package/README.md +120 -9
  4. package/docs/ascii-order.md +81 -0
  5. package/docs/pretty-imports.md +70 -59
  6. package/docs/pretty-jsdoc-casting.md +31 -23
  7. package/docs/pretty-sequence.md +129 -117
  8. package/docs/pretty-ternary.md +55 -0
  9. package/docs/svelte-naming-convention.md +59 -46
  10. package/package.json +60 -36
  11. package/src/index.js +299 -0
  12. package/src/rules/ascii-order.js +940 -0
  13. package/src/rules/pretty-imports.js +176 -0
  14. package/src/rules/pretty-jsdoc-casting.js +278 -0
  15. package/src/rules/pretty-sequence.js +489 -0
  16. package/src/rules/pretty-ternary.js +227 -0
  17. package/src/rules/svelte-naming-convention.js +903 -0
  18. package/src/utils/fix_recorder.js +50 -0
  19. package/src/utils/keep_comments.js +99 -0
  20. package/src/utils/keep_line_breaks.js +56 -0
  21. package/src/utils/skip_hole_commas.js +32 -0
  22. package/src/utils/skip_optional_patterns.js +27 -0
  23. package/src/utils/skip_unchanged_fixes.js +35 -0
  24. package/src/utils/source_text.js +442 -0
  25. package/src/utils/split_declarations.js +75 -0
  26. package/src/utils/text_width.js +9 -0
  27. package/types/index.d.ts +29 -0
  28. package/types/index.d.ts.map +1 -0
  29. package/index.js +0 -125
  30. package/private.d.ts +0 -168
  31. package/rules/pretty-imports.js +0 -147
  32. package/rules/pretty-jsdoc-casting.js +0 -236
  33. package/rules/pretty-sequence.js +0 -441
  34. package/rules/svelte-naming-convention.js +0 -361
  35. package/test/pretty-imports/ExportNamedDeclaration.js +0 -123
  36. package/test/pretty-imports/ImportDeclaration.js +0 -125
  37. package/test/pretty-imports.js +0 -20
  38. package/test/pretty-jsdoc-casting/case_by_expression.js +0 -249
  39. package/test/pretty-jsdoc-casting/various_situations.js +0 -95
  40. package/test/pretty-jsdoc-casting.js +0 -22
  41. package/test/pretty-sequence/case_by_node_type.js +0 -340
  42. package/test/pretty-sequence/case_by_option.js +0 -142
  43. package/test/pretty-sequence/various_situations.js +0 -193
  44. package/test/pretty-sequence.js +0 -23
  45. package/test/svelte-naming-convention/basic_snake_case.js +0 -54
  46. package/test/svelte-naming-convention/case_by_identifier.js +0 -878
  47. package/test/svelte-naming-convention/snake_case_with_symbols.js +0 -46
  48. package/test/svelte-naming-convention.js +0 -25
  49. package/tsconfig.json +0 -16
@@ -0,0 +1,176 @@
1
+ import { create_source_text } from "../utils/source_text.js"
2
+ import text_width from "../utils/text_width.js"
3
+ const lf_regex = /\n/g
4
+ const line_indent_regex = /\n[\t ]*/g
5
+ const semicolon_regex = /\s*;$/
6
+ /** @type {import("eslint").Rule.RuleModule} */
7
+ export default {
8
+ create(context) {
9
+ /** @type {import("../../private.js").RuleOptions["pretty-imports"]} */
10
+ const option = context.options[0]
11
+ const check_exports = option?.checkExports ?? true
12
+ const check_imports = option?.checkImports ?? true
13
+ const fix_indent = option?.fixIndent ?? false
14
+ const indent = option?.indent ?? "\t"
15
+ const max_length = option?.maxLength ?? 30
16
+ const semicolon = option?.semicolon ?? false
17
+ const source_code = context.sourceCode
18
+ const source = create_source_text(source_code)
19
+ const text = source.text
20
+ const eol = source.eol
21
+ /**
22
+ * @param {number} start
23
+ * @param {number} end
24
+ * @returns {string}
25
+ */
26
+ function get_text(start, end) {
27
+ return source.to_lf(text.slice(start, end))
28
+ }
29
+ /**
30
+ * @param {string} value
31
+ * @returns {string}
32
+ */
33
+ function to_structure(value) {
34
+ return value.replace(line_indent_regex, "\n")
35
+ }
36
+ /**
37
+ * @param {import("../../private.js").AstNode & import("../../private.js").ModuleDeclaration} node
38
+ * @param {"ExportSpecifier" | "ImportSpecifier"} specifier_type
39
+ * @param {"export" | "import"} kind
40
+ * @returns {void}
41
+ */
42
+ function verify(node, specifier_type, kind) {
43
+ const specifiers = /** @type {import("../../private.js").AstNode[]} */(node.specifiers)/**/.filter(
44
+ specifier => specifier.type == specifier_type
45
+ )
46
+ const first = specifiers[0]
47
+ const last = specifiers[specifiers.length - 1]
48
+ if (first?.type != specifier_type || !last || source_code.getCommentsInside(node).length) return
49
+ const tokens = source_code.getTokens(node)
50
+ const open_index = tokens.findIndex(
51
+ token => token.range[0] == first.range[0]
52
+ ) - 1
53
+ if (tokens[open_index]?.value != "{") return
54
+ const head = tokens.slice(0, open_index)
55
+ .map(token => token.value)
56
+ .join(" ")
57
+ .replace(" ,", ",")
58
+ const last_token = /** @type {import("eslint").AST.Token} */(tokens[tokens.length - 1])/**/
59
+ const end_token = last_token.value == ";"
60
+ ? /** @type {import("eslint").AST.Token} */(tokens[tokens.length - 2])/**/
61
+ : last_token
62
+ const tail_end = end_token.range[1]
63
+ const tail = node.source ? " from " + get_text(node.source.range[0], tail_end) : ""
64
+ const next_token = source_code.getTokenAfter(
65
+ /** @type {import("estree").Node} */(node)/**/
66
+ )
67
+ const detached = last_token != end_token && last_token.loc.start.line != end_token.loc.end.line
68
+ const end = detached ? tail_end : node.range[1]
69
+ const keeps_semicolon = !detached && (
70
+ semicolon
71
+ || next_token?.value != "}" && next_token?.loc.start.line == end_token.loc.end.line
72
+ )
73
+ let length = 0
74
+ for (const specifier of specifiers) {
75
+ length += text_width(
76
+ get_text(
77
+ specifier.range[0],
78
+ specifier.range[1]
79
+ )
80
+ )
81
+ }
82
+ const specifier_texts = specifiers.map(
83
+ specifier => get_text(
84
+ specifier.range[0],
85
+ specifier.range[1]
86
+ )
87
+ )
88
+ const line_indent = source.get_indent(node.range[0])
89
+ const item_indent = line_indent + indent
90
+ const names = `{ ${specifier_texts.join(", ")} }`
91
+ const corrected_text = (
92
+ length > max_length
93
+ ? `${head} {\n${item_indent}${specifier_texts.join(",\n" + item_indent)}\n${line_indent}}`
94
+ : `${head} ${names}`
95
+ ) + tail + (keeps_semicolon ? ";" : "")
96
+ const current = get_text(node.range[0], end)
97
+ if (corrected_text == current) return
98
+ const same_structure = to_structure(current) == to_structure(corrected_text)
99
+ if (!fix_indent && same_structure) return
100
+ context.report(
101
+ {
102
+ data: {
103
+ kind,
104
+ length: String(length),
105
+ max_length: String(max_length),
106
+ names
107
+ },
108
+ fix(fixer) {
109
+ return fixer.replaceTextRange(
110
+ [ node.range[0], end ],
111
+ eol == "\n" ? corrected_text : corrected_text.replace(lf_regex, eol)
112
+ )
113
+ },
114
+ messageId: current.replace(semicolon_regex, "") == corrected_text.replace(semicolon_regex, "")
115
+ ? keeps_semicolon
116
+ ? "semicolon"
117
+ : "no_semicolon"
118
+ : same_structure
119
+ ? "indent"
120
+ : length > max_length
121
+ ? "multiline"
122
+ : "single_line",
123
+ node: /** @type {import("estree").Node} */(node)/**/
124
+ }
125
+ )
126
+ }
127
+ /** @type {import("../../private.js").RuleListener} */
128
+ const listener = {
129
+ ExportNamedDeclaration: node => {
130
+ if (check_exports) verify(
131
+ /** @type {import("../../private.js").AstNode & import("estree").ExportNamedDeclaration} */(node)/**/,
132
+ "ExportSpecifier",
133
+ "export"
134
+ )
135
+ },
136
+ ImportDeclaration: node => {
137
+ if (check_imports) verify(
138
+ /** @type {import("../../private.js").AstNode & import("estree").ImportDeclaration} */(node)/**/,
139
+ "ImportSpecifier",
140
+ "import"
141
+ )
142
+ }
143
+ }
144
+ return /** @type {import("eslint").Rule.RuleListener} */(/** @type {unknown} */(listener))/**/
145
+ },
146
+ meta: {
147
+ docs: {
148
+ description: "Enforces proper indentation for import and export statements.",
149
+ recommended: true,
150
+ url: "https://github.com/artxe/lube-series/blob/master/packages/eslint-plugin-lube/docs/pretty-imports.md"
151
+ },
152
+ fixable: "code",
153
+ messages: {
154
+ indent: "Indent each name of this {{kind}} one level deeper than the statement",
155
+ multiline: "Put each name of this {{kind}} on a line of its own, since the names are longer than {{max_length}} characters together",
156
+ no_semicolon: "Remove the semicolon at the end of this {{kind}}",
157
+ semicolon: "Add a semicolon at the end of this {{kind}}",
158
+ single_line: "Write the names of this {{kind}} on one line as `{{names}}`, since the names are {{length}} characters together, within the {{max_length}} allowed"
159
+ },
160
+ schema: [
161
+ {
162
+ additionalProperties: false,
163
+ properties: {
164
+ checkExports: { default: true, type: "boolean" },
165
+ checkImports: { default: true, type: "boolean" },
166
+ fixIndent: { default: false, type: "boolean" },
167
+ indent: { default: "\t", type: "string" },
168
+ maxLength: { default: 30, type: "number" },
169
+ semicolon: { default: false, type: "boolean" }
170
+ },
171
+ type: "object"
172
+ }
173
+ ],
174
+ type: "layout"
175
+ }
176
+ }
@@ -0,0 +1,278 @@
1
+ import { create_source_text, is_space } from "../utils/source_text.js"
2
+ const line_break_regex = /[\n\r\u2028\u2029]/
3
+ const restricted_regex = /(?:^|[^$\w])(?:return|throw|yield)$/
4
+ const simple_type_regex = /^\*\s*@type\s*\{(.+)\}\s*$/
5
+ const type_regex = /^\*[^]*@type\s*\{[^]*\}/
6
+ const typescript_file_regex = /\.[cm]?tsx?$/
7
+ const marker_regex = /\/\*\*\//g
8
+ /** @type {import("eslint").Rule.RuleModule} */
9
+ export default {
10
+ create(context) {
11
+ if (typescript_file_regex.test(context.filename)) return {}
12
+ const source_code = context.sourceCode
13
+ const source = create_source_text(source_code)
14
+ const text = source.text
15
+ /** @type {import("../../private.js").AstNode[]} */
16
+ const expressions = []
17
+ /** @type {Set<string>} */
18
+ const node_ranges = new Set()
19
+ /**
20
+ * @param {import("../../private.js").Comment} comment
21
+ * @returns {string}
22
+ */
23
+ function get_type_comment(comment) {
24
+ const type = simple_type_regex.exec(comment.value)?.[1]
25
+ return type == null
26
+ ? text.slice(
27
+ comment.range[0],
28
+ comment.range[1]
29
+ )
30
+ : `/** @type {${type}} */`
31
+ }
32
+ /**
33
+ * @param {number} position
34
+ * @returns {boolean}
35
+ */
36
+ function is_arguments(position) {
37
+ const node = /** @type {import("../../private.js").AstNode | null} */(/** @type {unknown} */(source_code.getNodeByRangeIndex(position)))/**/
38
+ if (!node) return false
39
+ const type = node.type
40
+ if (type != "CallExpression" && type != "ImportExpression" && type != "NewExpression") return false
41
+ const before = type == "ImportExpression"
42
+ ? source_code.getFirstToken(
43
+ /** @type {import("estree").Node} */(node)/**/
44
+ )
45
+ : node.typeArguments ?? node.typeParameters ?? node.callee
46
+ let open = before && source_code.getTokenAfter(
47
+ /** @type {import("estree").Node} */(before)/**/
48
+ )
49
+ while (open && open.value != "(" && open.range[0] < node.range[1]) {
50
+ open = source_code.getTokenAfter(open)
51
+ }
52
+ return open?.range[0] == position
53
+ }
54
+ /**
55
+ * @param {import("../../private.js").AstNode} node
56
+ * @returns {void}
57
+ */
58
+ function push(node) {
59
+ const range = node.range[0] + " " + node.range[1]
60
+ if (node_ranges.has(range)) return
61
+ node_ranges.add(range)
62
+ expressions.push(node)
63
+ }
64
+ /**
65
+ * @param {number} start
66
+ * @param {number} end
67
+ * @param {[number, number][]} removed
68
+ * @returns {string | null}
69
+ */
70
+ function render_without(start, end, removed) {
71
+ let position = start
72
+ let result = ""
73
+ for (const [ removed_start, removed_end ] of removed) {
74
+ if (removed_end <= start || removed_start >= end) continue
75
+ if (source.crosses_edit(removed_start) || source.crosses_edit(removed_end)) return null
76
+ let chunk_end = removed_start
77
+ if (text[removed_start] == ")") {
78
+ while (chunk_end > position && is_space(text.charCodeAt(chunk_end - 1))) chunk_end--
79
+ if (source.comment_by_end.get(chunk_end)?.type == "Line") chunk_end = removed_start
80
+ }
81
+ result += source.render(position, chunk_end)
82
+ position = removed_end
83
+ if (text[removed_start] == "(") {
84
+ while (position < end && is_space(text.charCodeAt(position))) position++
85
+ }
86
+ }
87
+ return result + source.render(position, end)
88
+ }
89
+ /**
90
+ * @param {import("../../private.js").AstNode} node
91
+ * @returns {void}
92
+ */
93
+ function verify(node) {
94
+ const node_start = node.range[0]
95
+ const before = source.skip_left(node_start, 0)
96
+ if (!before || text[before - 1] != "(" || is_arguments(before - 1)) return
97
+ /** @type {import("../../private.js").CastingLevel[]} */
98
+ const levels = []
99
+ /** @type {[number, number][]} */
100
+ const removed = []
101
+ let content_end = node.range[1]
102
+ let content_start = node_start
103
+ let has_inner_type_comment = false
104
+ let left = node_start
105
+ let right = content_end
106
+ for (;;) {
107
+ while (left > 0) {
108
+ if (is_space(text.charCodeAt(left - 1))) {
109
+ left--
110
+ continue
111
+ }
112
+ const comment = source.comment_by_end.get(left)
113
+ if (!comment) break
114
+ const level = levels[levels.length - 1]
115
+ const type = comment.type == "Block" && type_regex.test(comment.value)
116
+ ? get_type_comment(comment)
117
+ : void 0
118
+ if (type && level && level.type == null) {
119
+ let comment_end = comment.range[1]
120
+ while (comment_end < level.open && is_space(text.charCodeAt(comment_end))) comment_end++
121
+ level.type = type
122
+ removed.push(
123
+ [ comment.range[0], comment_end ]
124
+ )
125
+ } else if (type) has_inner_type_comment = true
126
+ left = comment.range[0]
127
+ }
128
+ if (!left || text[left - 1] != "(" || is_arguments(left - 1)) break
129
+ const close = source.skip_right(right, text.length)
130
+ right = close
131
+ if (text[close] != ")") break
132
+ if (has_inner_type_comment) {
133
+ if (levels.length) return
134
+ content_end = close
135
+ content_start = left
136
+ has_inner_type_comment = false
137
+ }
138
+ const close_end = source.render(close + 1, close + 5) == "/**/" ? close + 5 : close + 1
139
+ left--
140
+ levels.push(
141
+ {
142
+ close,
143
+ close_end,
144
+ open: left,
145
+ type: void 0
146
+ }
147
+ )
148
+ right = close_end
149
+ }
150
+ const outer = levels[levels.length - 1]
151
+ const first = levels[0]
152
+ if (!outer || !first) return
153
+ const restores_outer = outer.type == null
154
+ for (const level of levels) {
155
+ if (level == outer && restores_outer) removed.push(
156
+ [ level.close + 1, level.close_end ]
157
+ )
158
+ else removed.push(
159
+ [ level.open, level.open + 1 ],
160
+ [ level.close, level.close_end ]
161
+ )
162
+ }
163
+ removed.sort((a, b) => a[0] - b[0])
164
+ if (content_end + 4 <= first.close && source.render(content_end, content_end + 4) == "/**/") {
165
+ content_end += 4
166
+ }
167
+ const unwrapped = restores_outer
168
+ ? levels.slice(0, -1)
169
+ : levels
170
+ const unwrapped_outer = unwrapped[unwrapped.length - 1]
171
+ if (unwrapped_outer) {
172
+ let type_comments = 0
173
+ for (const level of unwrapped) {
174
+ if (level != unwrapped_outer && level.type != null) type_comments++
175
+ }
176
+ if (source.count_comments(
177
+ unwrapped_outer.open,
178
+ content_start
179
+ ) > type_comments) return
180
+ }
181
+ const boundaries = [
182
+ left,
183
+ content_start,
184
+ content_end,
185
+ right
186
+ ]
187
+ if (boundaries.some(source.crosses_edit)) return
188
+ let corrected_text = source.render(content_start, content_end)
189
+ for (const [ index, level ] of unwrapped.entries()) {
190
+ corrected_text = level.type
191
+ ? unwrapped[index + 1]?.type
192
+ ? `${level.type}(${corrected_text})`
193
+ : `${level.type}(${corrected_text})/**/`
194
+ : `(${corrected_text})`
195
+ }
196
+ const prefix = render_without(left, content_start, removed)
197
+ const suffix = render_without(content_end, right, removed)
198
+ if (
199
+ prefix == null
200
+ || suffix == null
201
+ || line_break_regex.test(prefix)
202
+ && restricted_regex.test(
203
+ text.slice(Math.max(0, left - 7), left)
204
+ )
205
+ ) return
206
+ corrected_text = prefix + corrected_text + suffix
207
+ const current = source.render(left, right)
208
+ if (current == corrected_text) return
209
+ const message_id = current.replace(marker_regex, "") == corrected_text.replace(marker_regex, "")
210
+ ? "marker"
211
+ : "format"
212
+ source.edit(left, right, corrected_text)
213
+ context.report(
214
+ {
215
+ fix(fixer) {
216
+ return fixer.replaceTextRange([ left, right ], corrected_text)
217
+ },
218
+ loc: {
219
+ end: source_code.getLocFromIndex(right),
220
+ start: source_code.getLocFromIndex(left)
221
+ },
222
+ messageId: message_id,
223
+ node: /** @type {import("estree").Node} */(node)/**/
224
+ }
225
+ )
226
+ }
227
+ /** @type {import("../../private.js").RuleListener} */
228
+ const listener = {
229
+ ArrayExpression: push,
230
+ ArrowFunctionExpression: push,
231
+ AssignmentExpression: push,
232
+ AwaitExpression: push,
233
+ BinaryExpression: push,
234
+ CallExpression: push,
235
+ ChainExpression: push,
236
+ ClassExpression: push,
237
+ ConditionalExpression: push,
238
+ FunctionExpression: push,
239
+ Identifier: push,
240
+ ImportExpression: push,
241
+ Literal: push,
242
+ LogicalExpression: push,
243
+ MemberExpression: push,
244
+ MetaProperty: push,
245
+ NewExpression: push,
246
+ ObjectExpression: push,
247
+ "Program:exit": () => {
248
+ for (let i = expressions.length - 1; i >= 0; i--) {
249
+ verify(
250
+ /** @type {import("../../private.js").AstNode} */(expressions[i])/**/
251
+ )
252
+ }
253
+ },
254
+ SequenceExpression: push,
255
+ TaggedTemplateExpression: push,
256
+ TemplateLiteral: push,
257
+ ThisExpression: push,
258
+ UnaryExpression: push,
259
+ UpdateExpression: push,
260
+ YieldExpression: push
261
+ }
262
+ return /** @type {import("eslint").Rule.RuleListener} */(/** @type {unknown} */(listener))/**/
263
+ },
264
+ meta: {
265
+ docs: {
266
+ description: "Enforces additional empty comment when type casting in JSDoc.",
267
+ recommended: true,
268
+ url: "https://github.com/artxe/lube-series/blob/master/packages/eslint-plugin-lube/docs/pretty-jsdoc-casting.md"
269
+ },
270
+ fixable: "code",
271
+ messages: {
272
+ format: "Write this cast as `/** @type {T} */(value)/**/`, without white space or extra parentheses around the value",
273
+ marker: "Put one empty comment `/**/` right after the closing parenthesis of the outermost cast"
274
+ },
275
+ schema: [],
276
+ type: "layout"
277
+ }
278
+ }