@inthedark/dxlint 0.1.0 → 0.1.2

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.
@@ -3,7 +3,7 @@
3
3
  define {
4
4
 
5
5
  # ---- comments --------------------------------------------------------------------
6
- fn tslint_comment_edit(source, n) = {
6
+ fn linter_comment_edit(source, n) = {
7
7
  value line_start: position(n.offset).js_line_start
8
8
  value prefix: byte_slice(source, line_start, n.offset)
9
9
  value tail: n.after
@@ -24,12 +24,20 @@ fn tslint_comment_edit(source, n) = {
24
24
  text: if re_test(prefix, /\S$/) && re_test(tail, /^\S/) && !re_test(tail, /^[;,)}\]]/) then " " else ""
25
25
  }
26
26
  }
27
- in ts_comment rule es-ban-tslint-comment {
27
+ # Patterns read the whole comment, `//` or `/* */` included: ESLint takes its
28
+ # config comments (`/* eslint ... */`, `/* global x */`) from block comments only.
29
+ in ts_comment rule es-ban-linter-comment {
30
+ option formats { name: string; pattern: regex }[] = [
31
+ { name: "tslint", pattern: /^(?:\/\/|\/\*)\s*tslint:(?:enable|disable)(?:-(?:line|next-line))?(?::|\s|\*\/|$)/ },
32
+ { name: "eslint", pattern: /^(?:\/\/|\/\*)\s*eslint-(?:disable|enable)(?:-(?:line|next-line))?(?:\s|\*\/|$)/ },
33
+ { name: "eslint", pattern: /^\/\*\s*(?:eslint|eslint-env|globals?|exported)(?:\s|\*\/)/ }
34
+ ] "linter comments to report: a name for the message and a pattern over the whole comment"
35
+ let format first(filter(formats, format => re_test(text, format.pattern)))
36
+ when format != null
28
37
  let content re_replace(text, /^\/\/|^\/\*|\*\/$/, "")
29
38
  let normalised re_replace(content, /^\s+|\s+$/, "")
30
- when re_test(content, /^\s*tslint:(enable|disable)(?:-(line|next-line))?(:|\s|$)/)
31
- let edit tslint_comment_edit(source, node)
32
- warn "tslint comment detected: \"{if node is ts_line_comment then '// ' normalised else '/* ' normalised ' */'}\""
39
+ let edit linter_comment_edit(source, node)
40
+ warn "{format.name} comment detected: \"{if node is ts_line_comment then '// ' normalised else '/* ' normalised ' */'}\""
33
41
  fix at edit if edit != null as edit.text
34
42
  }
35
43
 
@@ -1,147 +1,147 @@
1
- # Floating promise values and immediate promise elements in arrays or tuples.
2
-
3
- define {
4
-
5
- in ts_node let floating_promise_arms value_parts(node)
6
- in ts_await let floating_promise_arms value_parts(ts_awaited_value(node, argument.node))
7
- in ts_node let floating_promise_safe_call false
8
- in ts_node let floating_promise_safe_types []
9
- in ts_node let floating_promises_checked true
10
- fn floating_promise_safe(n, safe_calls) = {
11
- value expression: unparenthesised_node(n)
12
- value expression.floating_promise_safe_call
13
- || len(safe_calls) > 0 && expression is ts_call && {
14
- value callee: unparenthesised_node(expression.callee.node)
15
- value callee is ts_identifier && contains(safe_calls, callee.text)
16
- }
17
- }
18
- fn floating_promise_subject_checked(n, ignore_iife) = {
19
- value n.floating_promises_checked
20
- && (!(n.parent.node is ts_arrow) || n is ts_unary)
21
- && !(ignore_iife && n.parent.node is ts_expression_statement && {
22
- value expression: unparenthesised_node(n)
23
- value expression is ts_call && unparenthesised_node(expression.callee.node) is (ts_function, ts_arrow)
24
- })
25
- }
26
- fn floating_rejection_handler(n) = {
27
- value callee: unparenthesised_node(n.callee.node)
28
- value position: if callee.static_name == "catch" then 0
29
- else if callee.static_name == "then" then 1 else -1
30
- value handler: if callee.static_name == "catch" then first(n.argument.nodes)
31
- else if callee.static_name == "then" then { value n.argument.nodes; slice 1 2; first } else null
32
- value checked_count: position + 1
33
- value preceding: slice(n.argument.nodes, 0, checked_count)
34
- value if handler == null || any(preceding, argument => argument is ts_spread) then "missing"
35
- else if handler could callable && handler always callable then "handled" else "useless"
36
- }
37
- fn floating_promise_nonnull(n) = {
38
- value arms: value_constituents(n)
39
- value arms != null && len(arms) > 0
40
- && all(arms, arm => any(arm, part => any(part.traits, trait =>
41
- contains(["boolean", "number", "string", "bigint", "symbol", "object", "truthy"], trait))))
42
- }
43
- fn floating_promise_logical_leaves(n) =
44
- if n.operator == "&&" && n.left.node could falsy && n.left.node always falsy
45
- || n.operator == "||" && n.left.node could truthy && n.left.node always truthy
46
- || n.operator == "??" && floating_promise_nonnull(n.left.node) then [n.left.node]
47
- else [n.left.node, n.right.node]
48
- fn floating_promise_leaves(n, ignore_void) = {
49
- value unparenthesised_node(n)
50
- walk (current) if current is ts_parenthesized then current.expression.node
51
- else if current is ts_sequence then current.expression.nodes
52
- else if current is ts_logical then floating_promise_logical_leaves(current)
53
- else if current is ts_conditional then [current.consequent.node, current.alternate.node]
54
- else if !ignore_void && current is ts_unary && current.operator == "void" then current.argument.node
55
- else if current is ts_call && current.callee.node.static_name == "finally" then current.callee.node.object.node else null
56
- filter (part) part not (ts_parenthesized, ts_sequence, ts_logical, ts_conditional)
57
- && !(part is ts_call && part.callee.node.static_name == "finally")
58
- && !(!ignore_void && part is ts_unary && part.operator == "void")
59
- }
60
- fn floating_promise_alias_allowed(n, safe_promises) = len(safe_promises) > 0 && {
61
- value walk(value_sources(n).sites ?? [], site => {
62
- value sources: site_sources(n, site)
63
- value if sources.combine == "union" then [] else sources.sites ?? []
64
- })
65
- any (site) site.kind == "ts_type_alias" && contains(safe_promises, site.name)
66
- && any(site_sources(n, site).sites, body =>
67
- contains(["ts_type_literal", "ts_union", "ts_intersection", "ts_array_type", "ts_tuple_type", "ts_readonly_type"], body.kind))
68
- }
69
- fn floating_promise_arm_allowed(arm, n, safe_promises) = {
70
- value entries: n.floating_promise_safe_types
71
- value directory: if any(entries, entry => entry.from == "file") then program_directory(n) else null
72
- value len(safe_promises) > 0 && any(arm.parts, part => part.carrier != null
73
- && any(ts_value_ancestry(part).parts, base => contains(safe_promises, base.carrier.description.name)))
74
- || any(entries, entry => any(arm.parts, part => type_specifier_part(entry, n, part, directory) != false))
75
- }
76
- fn floating_promise_arm(arm, n, check_thenables, safe_promises) =
77
- !floating_promise_arm_allowed(arm, n, safe_promises)
78
- && (any(arm.parts, part => part.carrier != null
79
- && ts_value_builtin_ancestor(part, n, ["Promise"]) == true)
80
- || check_thenables && {
81
- value member: value_lookup(arm.handle, "then").member
82
- value headers: value_signatures(member)
83
- value any(headers ?? [], alternatives => any(alternatives, header => count(header.parameters) >= 2
84
- && all(slice(header.parameters, 0, 2), parameter => floating_callable_arm(parameter))))
85
- })
86
- fn floating_promise_failure(n, check_thenables, ignore_void, safe_promises, safe_calls) = {
87
- value floating_promise_leaves(n, ignore_void)
88
- map (part) if part is ts_assignment || floating_promise_safe(part, safe_calls)
89
- || floating_promise_alias_allowed(part, safe_promises)
90
- || type_value_allowed(part.floating_promise_safe_types, part) != false then null else {
91
- value arms: part.floating_promise_arms
92
- value if floating_promise_array(arms, part, check_thenables, safe_promises) then "array"
93
- else if part not ts_await
94
- && any(arms ?? [], arm => floating_promise_arm(arm, part, check_thenables, safe_promises))
95
- then if part is ts_call then {
96
- value handler: floating_rejection_handler(part)
97
- value if handler == "handled" then null else if handler == "useless" then "useless" else "promise"
98
- } else "promise" else null
99
- }
100
- filter (failure) failure != null
101
- first
102
- }
103
- in ts_expression_statement.expression, ts_arrow.body rule es-no-floating-promises {
104
- option checkThenables boolean = false "Check promise-like types with fulfilment and rejection callbacks"
105
- option ignoreVoid boolean = true "Treat the void operator as explicit promise handling"
106
- option ignoreIIFE boolean = false "Ignore a directly invoked function expression"
107
- option allowForKnownSafePromises string[] = [] "Promise type names whose rejection is handled elsewhere"
108
- option allowForKnownSafeCalls string[] = [] "Function names whose returned promise is handled elsewhere"
109
- when floating_promise_subject_checked(node, ignoreIIFE)
110
- when node not ts_assignment
111
- let failure floating_promise_failure(node, checkThenables, ignoreVoid, allowForKnownSafePromises, allowForKnownSafeCalls)
112
- when contains(["promise", "useless"], failure)
113
- report at if parent.node is ts_expression_statement then parent.node else node
114
- warn (if ignoreVoid then "Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator."
115
- else "Promises must be awaited, end with a call to .catch, or end with a call to .then with a rejection handler.") (if failure == "useless"
116
- then " A rejection handler that is not a function will be ignored." else "")
117
- }
118
-
119
- fn floating_callable_arm(handle) = any(value_alternatives(handle) ?? [], arm => {
120
- value signatures: value_signatures(arm)
121
- value signatures != null && any(signatures, headers => len(headers) > 0)
122
- })
123
- fn floating_promise_array(arms, n, check_thenables, safe_promises) = any(arms ?? [], arm =>
124
- !any(arm.parts, part => contains(part.traits, "intersection"))
125
- && !floating_promise_arm_allowed(arm, n, safe_promises) && any(arm.parts, part => {
126
- value shape: ts_value_array_part(part, n)
127
- value elements: if shape.tuple then value_children(part.handle, true, [])
128
- else if shape.array then value_children(part.handle, false, [0]) else []
129
- value any(elements ?? [], element => any(value_parts(element.handle) ?? [],
130
- item => floating_promise_arm(item, n, check_thenables, safe_promises)))
131
- }))
132
-
133
- in ts_expression_statement.expression, ts_arrow.body rule es-no-floating-promises:array {
134
- option checkThenables boolean = false "Check promise-like types with fulfilment and rejection callbacks"
135
- option ignoreVoid boolean = true "Treat the void operator as explicit promise handling"
136
- option ignoreIIFE boolean = false "Ignore a directly invoked function expression"
137
- option allowForKnownSafePromises string[] = [] "Promise type names whose rejection is handled elsewhere"
138
- option allowForKnownSafeCalls string[] = [] "Function names whose returned promise is handled elsewhere"
139
- when floating_promise_subject_checked(node, ignoreIIFE)
140
- when node not ts_assignment
141
- when floating_promise_failure(node, checkThenables, ignoreVoid, allowForKnownSafePromises, allowForKnownSafeCalls) == "array"
142
- report at if parent.node is ts_expression_statement then parent.node else node
143
- warn if ignoreVoid then "An array of Promises may be unintentional. Consider handling the promises' fulfillment or rejection with Promise.all or similar, or explicitly marking the expression as ignored with the `void` operator."
144
- else "An array of Promises may be unintentional. Consider handling the promises' fulfillment or rejection with Promise.all or similar."
145
- }
146
-
147
- }
1
+ # Floating promise values and immediate promise elements in arrays or tuples.
2
+
3
+ define {
4
+
5
+ in ts_node let floating_promise_arms value_parts(node)
6
+ in ts_await let floating_promise_arms value_parts(ts_awaited_value(node, argument.node))
7
+ in ts_node let floating_promise_safe_call false
8
+ in ts_node let floating_promise_safe_types []
9
+ in ts_node let floating_promises_checked true
10
+ fn floating_promise_safe(n, safe_calls) = {
11
+ value expression: unparenthesised_node(n)
12
+ value expression.floating_promise_safe_call
13
+ || len(safe_calls) > 0 && expression is ts_call && {
14
+ value callee: unparenthesised_node(expression.callee.node)
15
+ value callee is ts_identifier && contains(safe_calls, callee.text)
16
+ }
17
+ }
18
+ fn floating_promise_subject_checked(n, ignore_iife) = {
19
+ value n.floating_promises_checked
20
+ && (!(n.parent.node is ts_arrow) || n is ts_unary)
21
+ && !(ignore_iife && n.parent.node is ts_expression_statement && {
22
+ value expression: unparenthesised_node(n)
23
+ value expression is ts_call && unparenthesised_node(expression.callee.node) is (ts_function, ts_arrow)
24
+ })
25
+ }
26
+ fn floating_rejection_handler(n) = {
27
+ value callee: unparenthesised_node(n.callee.node)
28
+ value position: if callee.static_name == "catch" then 0
29
+ else if callee.static_name == "then" then 1 else -1
30
+ value handler: if callee.static_name == "catch" then first(n.argument.nodes)
31
+ else if callee.static_name == "then" then { value n.argument.nodes; slice 1 2; first } else null
32
+ value checked_count: position + 1
33
+ value preceding: slice(n.argument.nodes, 0, checked_count)
34
+ value if handler == null || any(preceding, argument => argument is ts_spread) then "missing"
35
+ else if handler could callable && handler always callable then "handled" else "useless"
36
+ }
37
+ fn floating_promise_nonnull(n) = {
38
+ value arms: value_constituents(n)
39
+ value arms != null && len(arms) > 0
40
+ && all(arms, arm => any(arm, part => any(part.traits, trait =>
41
+ contains(["boolean", "number", "string", "bigint", "symbol", "object", "truthy"], trait))))
42
+ }
43
+ fn floating_promise_logical_leaves(n) =
44
+ if n.operator == "&&" && n.left.node could falsy && n.left.node always falsy
45
+ || n.operator == "||" && n.left.node could truthy && n.left.node always truthy
46
+ || n.operator == "??" && floating_promise_nonnull(n.left.node) then [n.left.node]
47
+ else [n.left.node, n.right.node]
48
+ fn floating_promise_leaves(n, ignore_void) = {
49
+ value unparenthesised_node(n)
50
+ walk (current) if current is ts_parenthesized then current.expression.node
51
+ else if current is ts_sequence then current.expression.nodes
52
+ else if current is ts_logical then floating_promise_logical_leaves(current)
53
+ else if current is ts_conditional then [current.consequent.node, current.alternate.node]
54
+ else if !ignore_void && current is ts_unary && current.operator == "void" then current.argument.node
55
+ else if current is ts_call && current.callee.node.static_name == "finally" then current.callee.node.object.node else null
56
+ filter (part) part not (ts_parenthesized, ts_sequence, ts_logical, ts_conditional)
57
+ && !(part is ts_call && part.callee.node.static_name == "finally")
58
+ && !(!ignore_void && part is ts_unary && part.operator == "void")
59
+ }
60
+ fn floating_promise_alias_allowed(n, safe_promises) = len(safe_promises) > 0 && {
61
+ value walk(value_sources(n).sites ?? [], site => {
62
+ value sources: site_sources(n, site)
63
+ value if sources.combine == "union" then [] else sources.sites ?? []
64
+ })
65
+ any (site) site.kind == "ts_type_alias" && contains(safe_promises, site.name)
66
+ && any(site_sources(n, site).sites, body =>
67
+ contains(["ts_type_literal", "ts_union", "ts_intersection", "ts_array_type", "ts_tuple_type", "ts_readonly_type"], body.kind))
68
+ }
69
+ fn floating_promise_arm_allowed(arm, n, safe_promises) = {
70
+ value entries: n.floating_promise_safe_types
71
+ value directory: if any(entries, entry => entry.from == "file") then program_directory(n) else null
72
+ value len(safe_promises) > 0 && any(arm.parts, part => part.carrier != null
73
+ && any(ts_value_ancestry(part).parts, base => contains(safe_promises, base.carrier.description.name)))
74
+ || any(entries, entry => any(arm.parts, part => type_specifier_part(entry, n, part, directory) != false))
75
+ }
76
+ fn floating_promise_arm(arm, n, check_thenables, safe_promises) =
77
+ !floating_promise_arm_allowed(arm, n, safe_promises)
78
+ && (any(arm.parts, part => part.carrier != null
79
+ && ts_value_builtin_ancestor(part, n, ["Promise"]) == true)
80
+ || check_thenables && {
81
+ value member: value_lookup(arm.handle, "then").member
82
+ value headers: value_signatures(member)
83
+ value any(headers ?? [], alternatives => any(alternatives, header => count(header.parameters) >= 2
84
+ && all(slice(header.parameters, 0, 2), parameter => floating_callable_arm(parameter))))
85
+ })
86
+ fn floating_promise_failure(n, check_thenables, ignore_void, safe_promises, safe_calls) = {
87
+ value floating_promise_leaves(n, ignore_void)
88
+ map (part) if part is ts_assignment || floating_promise_safe(part, safe_calls)
89
+ || floating_promise_alias_allowed(part, safe_promises)
90
+ || type_value_allowed(part.floating_promise_safe_types, part) != false then null else {
91
+ value arms: part.floating_promise_arms
92
+ value if floating_promise_array(arms, part, check_thenables, safe_promises) then "array"
93
+ else if part not ts_await
94
+ && any(arms ?? [], arm => floating_promise_arm(arm, part, check_thenables, safe_promises))
95
+ then if part is ts_call then {
96
+ value handler: floating_rejection_handler(part)
97
+ value if handler == "handled" then null else if handler == "useless" then "useless" else "promise"
98
+ } else "promise" else null
99
+ }
100
+ filter (failure) failure != null
101
+ first
102
+ }
103
+ in ts_expression_statement.expression, ts_arrow.body rule es-no-floating-promises {
104
+ option checkThenables boolean = false "Check promise-like types with fulfilment and rejection callbacks"
105
+ option ignoreVoid boolean = true "Treat the void operator as explicit promise handling"
106
+ option ignoreIIFE boolean = false "Ignore a directly invoked function expression"
107
+ option allowForKnownSafePromises string[] = [] "Promise type names whose rejection is handled elsewhere"
108
+ option allowForKnownSafeCalls string[] = [] "Function names whose returned promise is handled elsewhere"
109
+ when floating_promise_subject_checked(node, ignoreIIFE)
110
+ when node not ts_assignment
111
+ let failure floating_promise_failure(node, checkThenables, ignoreVoid, allowForKnownSafePromises, allowForKnownSafeCalls)
112
+ when contains(["promise", "useless"], failure)
113
+ report at if parent.node is ts_expression_statement then parent.node else node
114
+ warn (if ignoreVoid then "Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator."
115
+ else "Promises must be awaited, end with a call to .catch, or end with a call to .then with a rejection handler.") (if failure == "useless"
116
+ then " A rejection handler that is not a function will be ignored." else "")
117
+ }
118
+
119
+ fn floating_callable_arm(handle) = any(value_alternatives(handle) ?? [], arm => {
120
+ value signatures: value_signatures(arm)
121
+ value signatures != null && any(signatures, headers => len(headers) > 0)
122
+ })
123
+ fn floating_promise_array(arms, n, check_thenables, safe_promises) = any(arms ?? [], arm =>
124
+ !any(arm.parts, part => contains(part.traits, "intersection"))
125
+ && !floating_promise_arm_allowed(arm, n, safe_promises) && any(arm.parts, part => {
126
+ value shape: ts_value_array_part(part, n)
127
+ value elements: if shape.tuple then value_children(part.handle, true, [])
128
+ else if shape.array then value_children(part.handle, false, [0]) else []
129
+ value any(elements ?? [], element => any(value_parts(element.handle) ?? [],
130
+ item => floating_promise_arm(item, n, check_thenables, safe_promises)))
131
+ }))
132
+
133
+ in ts_expression_statement.expression, ts_arrow.body rule es-no-floating-promises:array {
134
+ option checkThenables boolean = false "Check promise-like types with fulfilment and rejection callbacks"
135
+ option ignoreVoid boolean = true "Treat the void operator as explicit promise handling"
136
+ option ignoreIIFE boolean = false "Ignore a directly invoked function expression"
137
+ option allowForKnownSafePromises string[] = [] "Promise type names whose rejection is handled elsewhere"
138
+ option allowForKnownSafeCalls string[] = [] "Function names whose returned promise is handled elsewhere"
139
+ when floating_promise_subject_checked(node, ignoreIIFE)
140
+ when node not ts_assignment
141
+ when floating_promise_failure(node, checkThenables, ignoreVoid, allowForKnownSafePromises, allowForKnownSafeCalls) == "array"
142
+ report at if parent.node is ts_expression_statement then parent.node else node
143
+ warn if ignoreVoid then "An array of Promises may be unintentional. Consider handling the promises' fulfillment or rejection with Promise.all or similar, or explicitly marking the expression as ignored with the `void` operator."
144
+ else "An array of Promises may be unintentional. Consider handling the promises' fulfillment or rejection with Promise.all or similar."
145
+ }
146
+
147
+ }