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.
Files changed (39) hide show
  1. package/CHANGELOG.md +95 -0
  2. package/README.md +17 -4
  3. package/docs/closures-last.md +34 -0
  4. package/docs/indent.md +102 -0
  5. package/docs/no-ambiguous-start.md +23 -0
  6. package/docs/no-loose-types.md +25 -0
  7. package/docs/no-typedef.md +24 -0
  8. package/docs/pretty-jsdoc-lines.md +30 -0
  9. package/docs/pretty-parens.md +59 -0
  10. package/docs/require-returns.md +21 -0
  11. package/docs/svelte-naming-convention.md +5 -6
  12. package/package.json +2 -7
  13. package/src/index.js +78 -19
  14. package/src/rules/ascii-order.js +113 -127
  15. package/src/rules/closures-last.js +323 -0
  16. package/src/rules/indent.js +84 -0
  17. package/src/rules/no-ambiguous-start.js +42 -0
  18. package/src/rules/no-loose-types.js +127 -0
  19. package/src/rules/no-typedef.js +44 -0
  20. package/src/rules/prefer-param-tag.js +35 -35
  21. package/src/rules/prefer-type-tag.js +29 -29
  22. package/src/rules/pretty-imports.js +23 -23
  23. package/src/rules/pretty-jsdoc-casting.js +39 -39
  24. package/src/rules/pretty-jsdoc-lines.js +104 -0
  25. package/src/rules/pretty-parens.js +280 -0
  26. package/src/rules/pretty-sequence.js +79 -95
  27. package/src/rules/pretty-ternary.js +27 -31
  28. package/src/rules/require-returns.js +190 -0
  29. package/src/rules/svelte-naming-convention.js +457 -546
  30. package/src/utils/accept_cr_eol.js +36 -0
  31. package/src/utils/indent_model.js +1596 -0
  32. package/src/utils/jsdoc_tags.js +30 -0
  33. package/src/utils/order_compare.js +17 -0
  34. package/src/utils/rule_options.js +30 -0
  35. package/src/utils/skip_unchanged_fixes.js +3 -3
  36. package/src/utils/source_text.js +80 -255
  37. package/src/utils/split_declarations.js +35 -14
  38. package/src/utils/trim_fixes.js +108 -0
  39. package/types/index.d.ts.map +1 -1
@@ -24,13 +24,6 @@ function escape_regex(text) {
24
24
  */
25
25
  function jsdoc_with(comment, indent, eol, tags) {
26
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
27
  if (rows.length < 2 || !/^\/\*\*\s*$/.test(row_at(0)) || !/^\s*\*\/$/.test(row_at(rows.length - 1))) {
35
28
  const body = comment.value.slice(1).split(line_terminator_regex)
36
29
  .map(
@@ -84,40 +77,19 @@ function jsdoc_with(comment, indent, eol, tags) {
84
77
  rows.splice(at, 0, ...added)
85
78
  }
86
79
  return rows.join(eol)
80
+ /**
81
+ * @param {number} index
82
+ * @returns {string}
83
+ */
84
+ function row_at(index) {
85
+ return /** @type {string} */(rows[index])/**/
86
+ }
87
87
  }
88
88
  /** @type {import("eslint").Rule.RuleModule} */
89
89
  export default {
90
90
  create(context) {
91
91
  const source_code = context.sourceCode
92
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
93
  return {
122
94
  FunctionDeclaration(node) {
123
95
  /** @type {{ comment: import("estree").Comment, index: number, name: string, type: string }[]} */
@@ -233,6 +205,34 @@ export default {
233
205
  )
234
206
  }
235
207
  }
208
+ /**
209
+ * @param {import("estree").Comment} comment
210
+ * @returns {import("eslint").AST.Range}
211
+ */
212
+ function comment_range(comment) {
213
+ return /** @type {import("eslint").AST.Range} */(comment.range)/**/
214
+ }
215
+ /**
216
+ * @param {number} position
217
+ * @returns {number}
218
+ */
219
+ function line_start_of(position) {
220
+ let start = position
221
+ while (start > 0 && !line_terminator_regex.test(text.charAt(start - 1))) {
222
+ start--
223
+ }
224
+ return start
225
+ }
226
+ /**
227
+ * @param {import("estree").Comment} comment
228
+ * @returns {boolean}
229
+ */
230
+ function starts_line(comment) {
231
+ const start = comment_range(comment)[0]
232
+ return space_regex.test(
233
+ text.slice(line_start_of(start), start)
234
+ )
235
+ }
236
236
  },
237
237
  meta: {
238
238
  docs: {
@@ -25,27 +25,6 @@ export default {
25
25
  const checker = program.getTypeChecker()
26
26
  const source_code = context.sourceCode
27
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
28
  return {
50
29
  VariableDeclaration(node) {
51
30
  const [ declarator, other ] = node.declarations
@@ -64,13 +43,13 @@ export default {
64
43
  const comment_range = comment?.range
65
44
  if (
66
45
  !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
- )
46
+ || !comment_range
47
+ || comment.type != "Block"
48
+ || !type_regex.test(comment.value)
49
+ || const_regex.test(comment.value)
50
+ || !space_regex.test(
51
+ text.slice(comment_range[1], open.range[0])
52
+ )
74
53
  ) {
75
54
  return
76
55
  }
@@ -92,7 +71,7 @@ export default {
92
71
  const value_type = checker.getTypeAtLocation(cast.expression)
93
72
  if (
94
73
  !checker.isTypeAssignableTo(value_type, type)
95
- || type.isUnion() && !checker.isTypeAssignableTo(type, value_type)
74
+ || type.isUnion() && !checker.isTypeAssignableTo(type, value_type)
96
75
  ) {
97
76
  return
98
77
  }
@@ -147,6 +126,27 @@ export default {
147
126
  )
148
127
  }
149
128
  }
129
+ /**
130
+ * @param {number} position
131
+ * @returns {number}
132
+ */
133
+ function line_start_of(position) {
134
+ let start = position
135
+ while (start > 0 && !line_terminator_regex.test(text.charAt(start - 1))) {
136
+ start--
137
+ }
138
+ return start
139
+ }
140
+ /**
141
+ * @param {import("estree").Comment} comment
142
+ * @returns {boolean}
143
+ */
144
+ function starts_line(comment) {
145
+ const start = /** @type {import("eslint").AST.Range} */(comment.range)/**/[0]
146
+ return space_regex.test(
147
+ text.slice(line_start_of(start), start)
148
+ )
149
+ }
150
150
  },
151
151
  meta: {
152
152
  docs: {
@@ -18,6 +18,28 @@ export default {
18
18
  const source = create_source_text(source_code)
19
19
  const text = source.text
20
20
  const eol = source.eol
21
+ /** @type {import("../../private.js").RuleListener} */
22
+ const listener = {
23
+ ExportNamedDeclaration: node => {
24
+ if (check_exports) {
25
+ verify(
26
+ /** @type {import("../../private.js").AstNode & import("estree").ExportNamedDeclaration} */(node)/**/,
27
+ "ExportSpecifier",
28
+ "export"
29
+ )
30
+ }
31
+ },
32
+ ImportDeclaration: node => {
33
+ if (check_imports) {
34
+ verify(
35
+ /** @type {import("../../private.js").AstNode & import("estree").ImportDeclaration} */(node)/**/,
36
+ "ImportSpecifier",
37
+ "import"
38
+ )
39
+ }
40
+ }
41
+ }
42
+ return /** @type {import("eslint").Rule.RuleListener} */(/** @type {unknown} */(listener))/**/
21
43
  /**
22
44
  * @param {number} start
23
45
  * @param {number} end
@@ -69,7 +91,7 @@ export default {
69
91
  const end = detached ? tail_end : node.range[1]
70
92
  const keeps_semicolon = !detached && (
71
93
  semicolon
72
- || next_token?.value != "}" && next_token?.loc.start.line == end_token.loc.end.line
94
+ || next_token?.value != "}" && next_token?.loc.start.line == end_token.loc.end.line
73
95
  )
74
96
  let length = 0
75
97
  for (const specifier of specifiers) {
@@ -129,28 +151,6 @@ export default {
129
151
  }
130
152
  )
131
153
  }
132
- /** @type {import("../../private.js").RuleListener} */
133
- const listener = {
134
- ExportNamedDeclaration: node => {
135
- if (check_exports) {
136
- verify(
137
- /** @type {import("../../private.js").AstNode & import("estree").ExportNamedDeclaration} */(node)/**/,
138
- "ExportSpecifier",
139
- "export"
140
- )
141
- }
142
- },
143
- ImportDeclaration: node => {
144
- if (check_imports) {
145
- verify(
146
- /** @type {import("../../private.js").AstNode & import("estree").ImportDeclaration} */(node)/**/,
147
- "ImportSpecifier",
148
- "import"
149
- )
150
- }
151
- }
152
- }
153
- return /** @type {import("eslint").Rule.RuleListener} */(/** @type {unknown} */(listener))/**/
154
154
  },
155
155
  meta: {
156
156
  docs: {
@@ -18,6 +18,42 @@ export default {
18
18
  const expressions = []
19
19
  /** @type {Set<string>} */
20
20
  const node_ranges = new Set()
21
+ /** @type {import("../../private.js").RuleListener} */
22
+ const listener = {
23
+ ArrayExpression: push,
24
+ ArrowFunctionExpression: push,
25
+ AssignmentExpression: push,
26
+ AwaitExpression: push,
27
+ BinaryExpression: push,
28
+ CallExpression: push,
29
+ ChainExpression: push,
30
+ ClassExpression: push,
31
+ ConditionalExpression: push,
32
+ FunctionExpression: push,
33
+ Identifier: push,
34
+ ImportExpression: push,
35
+ Literal: push,
36
+ LogicalExpression: push,
37
+ MemberExpression: push,
38
+ MetaProperty: push,
39
+ NewExpression: push,
40
+ ObjectExpression: push,
41
+ "Program:exit": () => {
42
+ for (let i = expressions.length - 1; i >= 0; i--) {
43
+ verify(
44
+ /** @type {import("../../private.js").AstNode} */(expressions[i])/**/
45
+ )
46
+ }
47
+ },
48
+ SequenceExpression: push,
49
+ TaggedTemplateExpression: push,
50
+ TemplateLiteral: push,
51
+ ThisExpression: push,
52
+ UnaryExpression: push,
53
+ UpdateExpression: push,
54
+ YieldExpression: push
55
+ }
56
+ return /** @type {import("eslint").Rule.RuleListener} */(/** @type {unknown} */(listener))/**/
21
57
  /**
22
58
  * @param {import("../../private.js").Comment} comment
23
59
  * @returns {string}
@@ -234,9 +270,9 @@ export default {
234
270
  const suffix = render_without(content_end, right, removed)
235
271
  if (
236
272
  line_break_regex.test(prefix)
237
- && restricted_regex.test(
238
- text.slice(Math.max(0, left - 7), left)
239
- )
273
+ && restricted_regex.test(
274
+ text.slice(Math.max(0, left - 7), left)
275
+ )
240
276
  ) {
241
277
  return
242
278
  }
@@ -263,42 +299,6 @@ export default {
263
299
  }
264
300
  )
265
301
  }
266
- /** @type {import("../../private.js").RuleListener} */
267
- const listener = {
268
- ArrayExpression: push,
269
- ArrowFunctionExpression: push,
270
- AssignmentExpression: push,
271
- AwaitExpression: push,
272
- BinaryExpression: push,
273
- CallExpression: push,
274
- ChainExpression: push,
275
- ClassExpression: push,
276
- ConditionalExpression: push,
277
- FunctionExpression: push,
278
- Identifier: push,
279
- ImportExpression: push,
280
- Literal: push,
281
- LogicalExpression: push,
282
- MemberExpression: push,
283
- MetaProperty: push,
284
- NewExpression: push,
285
- ObjectExpression: push,
286
- "Program:exit": () => {
287
- for (let i = expressions.length - 1; i >= 0; i--) {
288
- verify(
289
- /** @type {import("../../private.js").AstNode} */(expressions[i])/**/
290
- )
291
- }
292
- },
293
- SequenceExpression: push,
294
- TaggedTemplateExpression: push,
295
- TemplateLiteral: push,
296
- ThisExpression: push,
297
- UnaryExpression: push,
298
- UpdateExpression: push,
299
- YieldExpression: push
300
- }
301
- return /** @type {import("eslint").Rule.RuleListener} */(/** @type {unknown} */(listener))/**/
302
302
  },
303
303
  meta: {
304
304
  docs: {
@@ -0,0 +1,104 @@
1
+ import { tag_name, tag_starts } from "../utils/jsdoc_tags.js"
2
+ const blank_line_regex = /\n\s*\n/
3
+ const line_join_regex = /\s*\n\s*/g
4
+ const line_mark_regex = /^\s*\*?\s?/
5
+ const line_terminator_regex = /\r\n|[\n\r\p{Zl}\p{Zp}]/u
6
+ const space_regex = /^\s*$/
7
+ /**
8
+ * @param {string} body
9
+ * @returns {boolean}
10
+ */
11
+ function is_open_first_line(body) {
12
+ const first_line = body.split("\n", 1).join("")
13
+ return first_line.split("{").length != first_line.split("}").length
14
+ }
15
+ /** @type {import("eslint").Rule.RuleModule} */
16
+ export default {
17
+ create(context) {
18
+ const text = context.sourceCode.text
19
+ return {
20
+ Program() {
21
+ for (const comment of context.sourceCode.getAllComments()) {
22
+ if (comment.type != "Block" || !comment.value.startsWith("*") || comment.value.length < 2) {
23
+ continue
24
+ }
25
+ const range = /** @type {import("eslint").AST.Range} */(comment.range)/**/
26
+ const loc = /** @type {import("estree").SourceLocation} */(comment.loc)/**/
27
+ const raw = comment.value.slice(1)
28
+ if (line_terminator_regex.test(raw)) {
29
+ const body = raw.split(line_terminator_regex)
30
+ .map(
31
+ row => row.replace(line_mark_regex, "").trimEnd()
32
+ )
33
+ .join("\n")
34
+ .trim()
35
+ const starts = tag_starts(body)
36
+ const [ first ] = starts
37
+ if (
38
+ starts.length != 1
39
+ || first != 0
40
+ || tag_name(body) == "example"
41
+ || blank_line_regex.test(body)
42
+ || is_open_first_line(body)
43
+ ) {
44
+ continue
45
+ }
46
+ context.report(
47
+ {
48
+ fix: fixer => fixer.replaceTextRange(
49
+ range,
50
+ `/** ${body.replace(line_join_regex, " ")} */`
51
+ ),
52
+ loc,
53
+ messageId: "one"
54
+ }
55
+ )
56
+ } else {
57
+ const body = raw.trim()
58
+ const starts = tag_starts(body)
59
+ if (starts.length < 2) {
60
+ continue
61
+ }
62
+ const line_start = text.lastIndexOf("\n", range[0] - 1) + 1
63
+ const indent = text.slice(line_start, range[0])
64
+ if (!space_regex.test(indent)) {
65
+ context.report({ loc, messageId: "many" })
66
+ continue
67
+ }
68
+ const eol = text.includes("\r\n") ? "\r\n" : "\n"
69
+ const parts = [
70
+ body.slice(0, starts[0]).trim(),
71
+ ...starts.map(
72
+ (start, i) => body.slice(start, starts[i + 1]).trim()
73
+ )
74
+ ].filter(Boolean)
75
+ context.report(
76
+ {
77
+ fix: fixer => fixer.replaceTextRange(
78
+ range,
79
+ `/**${eol}${parts.map(part => `${indent} * ${part}${eol}`).join("")}${indent} */`
80
+ ),
81
+ loc,
82
+ messageId: "many"
83
+ }
84
+ )
85
+ }
86
+ }
87
+ }
88
+ }
89
+ },
90
+ meta: {
91
+ docs: {
92
+ description: "Enforces a JSDoc block of one tag on one line and a block of two or more tags with one tag a line.",
93
+ recommended: false,
94
+ url: "https://github.com/artxe/lube-series/blob/master/packages/eslint-plugin-lube/docs/pretty-jsdoc-lines.md"
95
+ },
96
+ fixable: "code",
97
+ messages: {
98
+ many: "Write each tag of this JSDoc block on a line of its own",
99
+ one: "Write this JSDoc block, which holds one tag and no description, on one line"
100
+ },
101
+ schema: [],
102
+ type: "layout"
103
+ }
104
+ }