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