@inthedark/dxlint 0.1.7 → 0.1.9

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.
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inthedark/dxlint",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "description": "let you and your agents own your linter",
5
5
  "license": "MIT",
6
6
  "repository": {
package/std/ts/helpers.dx CHANGED
@@ -3,6 +3,9 @@
3
3
  # A plain identifier, nothing else (`x`, `$el`, not `a.b` or `f()`).
4
4
  fn is_identifier(x) = re_test(x, /^[A-Za-z_$][\w$]*$/)
5
5
 
6
+ # TypeScript source, where type-only syntax (annotations, modifiers) is legal.
7
+ in ts_program let typescript_source re_test(file, /\.[cm]?tsx?$/)
8
+
6
9
  fn unparenthesised_node(n) = if n not ts_parenthesized then n else { value n; walk (current) if current is ts_parenthesized then current.expression.node else null; last }
7
10
  fn outer_parenthesised_node(n) = { value n; walk (current) if current.parent is ts_parenthesized then current.parent.node else null; last }
8
11
 
@@ -90,14 +90,13 @@ fn member_usage_used(declaration) = {
90
90
  }))
91
91
  }
92
92
 
93
- in ts_program let member_usage_typescript_file re_test(file, /\.[cm]?tsx?$/)
94
93
  in ts_method, ts_class_property, ts_parameter let member_usage_private
95
94
  modifier != null && contains([] modifier, "private")
96
95
  in ts_method.key, ts_class_property.key, ts_parameter.name rule es-no-unused-private-class-members {
97
96
  when is (ts_private_name, ts_identifier, ts_string, ts_number, ts_computed_key)
98
97
  when parent not ts_parameter || parent.member_usage_private
99
98
  when is ts_private_name || parent.member_usage_private
100
- let typed ts_program.member_usage_typescript_file
99
+ let typed ts_program.typescript_source
101
100
  when is ts_private_name || typed
102
101
  let declaration parent.node
103
102
  let name ts_value_key(node)
@@ -40,6 +40,7 @@ in ts_class_property rule es-explicit-member-accessibility:property {
40
40
  option accessibility ("explicit" | "no-public" | "off") = "explicit" "Required accessibility."
41
41
  option ignoredMethodNames string[] = [] "Ignored method names."
42
42
  option overrides { properties?: string; methods?: string; accessors?: string; constructors?: string; parameterProperties?: string } = {} "Requirements by member kind."
43
+ when ts_program.typescript_source
43
44
  let check overrides.properties.nonempty ?? accessibility
44
45
  let shape style_member
45
46
  when !re_test(key, /^#/) && (check == "explicit" && !{ value @modifiers; has_accessibility } || check == "no-public" && contains([] modifier, "public"))
@@ -52,6 +53,7 @@ in ts_method rule es-explicit-member-accessibility:method {
52
53
  option accessibility ("explicit" | "no-public" | "off") = "explicit" "Required accessibility."
53
54
  option ignoredMethodNames string[] = [] "Ignored method names."
54
55
  option overrides { properties?: string; methods?: string; accessors?: string; constructors?: string; parameterProperties?: string } = {} "Requirements by member kind."
56
+ when ts_program.typescript_source
55
57
  let check {
56
58
  value (if class_constructor then overrides.constructors else if kind == "get" || kind == "set" then overrides.accessors else overrides.methods)
57
59
  nonempty
@@ -68,6 +70,7 @@ in ts_parameter rule es-explicit-member-accessibility:parameter {
68
70
  option accessibility ("explicit" | "no-public" | "off") = "explicit" "Required accessibility."
69
71
  option ignoredMethodNames string[] = [] "Ignored method names."
70
72
  option overrides { properties?: string; methods?: string; accessors?: string; constructors?: string; parameterProperties?: string } = {} "Requirements by member kind."
73
+ when ts_program.typescript_source
71
74
  let check overrides.parameterProperties.nonempty ?? accessibility
72
75
  let shape style_member
73
76
  when contains([] modifier, "readonly") && parent is ts_method
@@ -67,6 +67,7 @@ in ts_function, ts_arrow, ts_method let explicit_return_shape {
67
67
  option allowHigherOrderFunctions boolean = true "Allow functions returning only functions."
68
68
  option allowIIFEs boolean = false "Allow functions directly within calls."
69
69
  option allowTypedFunctionExpressions boolean = true "Allow contextually typed function expressions."
70
+ when ts_program.typescript_source
70
71
  let shape explicit_return_shape
71
72
  when !shape.valid && !contains(allowedNames, shape.name)
72
73
  when !(allowFunctionsWithoutTypeParameters && count(type_parameter) == 0 || allowIIFEs && shape.iife || allowHigherOrderFunctions && shape.higher_order || allowConciseArrowFunctionExpressionsStartingWithVoid && shape.void_arrow)
@@ -133,6 +134,7 @@ in ts_parameter let boundary_argument {
133
134
  option allowHigherOrderFunctions boolean = true "Allow functions returning only functions."
134
135
  option allowTypedFunctionExpressions boolean = true "Allow contextually typed functions."
135
136
  option allowOverloadFunctions boolean = false "Allow implementations with overload signatures."
137
+ when ts_program.typescript_source
136
138
  let boundary_shape if parameter_check then parent.explicit_return_shape else explicit_return_shape
137
139
  let boundary_name if parameter_check then parent.boundary_function_name else boundary_function_name
138
140
  let empty_method if parameter_check then parent.boundary_empty_method else boundary_empty_method
@@ -227,8 +227,8 @@ fn type_argument_unresolved(n) = {
227
227
  value parts: value_constituents(n)
228
228
  value parts != null && !type_argument_known(parts)
229
229
  }
230
- fn type_argument_parameter_direct(n, parameter, formal) = {
231
- value annotation: first(site_field(n, parameter, "type") ?? [])
230
+ # Whether `annotation` names `formal` itself (alone or as a union member).
231
+ fn type_argument_names_formal(n, annotation, formal) = {
232
232
  value candidates: if annotation != null && annotation.kind == "ts_union"
233
233
  then site_field(n, annotation, "type") ?? [] else [annotation]
234
234
  value any(candidates, candidate => candidate != null && candidate.kind == "ts_type_reference"
@@ -237,6 +237,25 @@ fn type_argument_parameter_direct(n, parameter, formal) = {
237
237
  value found: if name != null then site_text(n, name) else null
238
238
  value found != null && found.text == formal.name })
239
239
  }
240
+ fn type_argument_parameter_direct(n, parameter, formal) =
241
+ type_argument_names_formal(n, first(site_field(n, parameter, "type") ?? []), formal)
242
+ # TypeScript infers a literal argument's own type (`E.A`, `"a"`) for a type
243
+ # parameter its signature returns at top level, and widens it elsewhere.
244
+ fn type_argument_returned(n, site, formal) = site != null
245
+ && type_argument_names_formal(n, first(site_field(n, site, "return_type") ?? []), formal)
246
+ # Whether the argument's type is a literal widening would change.
247
+ fn type_argument_literal(argument) = {
248
+ value raw: value_constituents(argument)
249
+ value widened: value_widened(argument)
250
+ value raw != null && widened != null && raw != widened
251
+ }
252
+ # Whether `parameter`'s annotation mentions `formal` anywhere (another
253
+ # inference site for it).
254
+ fn type_argument_mentions(n, parameter, formal) = {
255
+ value annotation: first(site_field(n, parameter, "type") ?? [])
256
+ value text: if annotation != null then site_text(n, annotation).text else null
257
+ value text != null && contains(split({ value text; re_replace /[^A-Za-z0-9_$]+/ ' ' }, " "), formal.name)
258
+ }
240
259
  fn type_argument_parameter_sites(n, site, signature) = if signature != null then
241
260
  map(signature.parameters ?? [], parameter => parameter.site)
242
261
  else if site == null then []
@@ -248,11 +267,16 @@ fn type_argument_parameter_sites(n, site, signature) = if signature != null then
248
267
  # default once any is written.
249
268
  fn type_argument_inferred(n, site, signature, formal) = count(n.parent.type_argument.nodes) == 1 && {
250
269
  value owner: n.parent.node
251
- value pairs: zip(type_argument_parameter_sites(n, site, signature), owner.argument.nodes)
270
+ value parameters: type_argument_parameter_sites(n, site, signature)
271
+ value pairs: zip(parameters, owner.argument.nodes)
252
272
  value pair: find(pairs, pair => pair != null && first(pair) != null && last(pair) != null
253
273
  && last(pair) not (ts_this, ts_spread)
254
274
  && type_argument_parameter_direct(n, first(pair), formal))
255
- value pair != null && type_argument_same(n, last(pair), true)
275
+ # A literal the only inference site of a returned type parameter stays
276
+ # literal: the written (wider) type argument is needed.
277
+ value literal_only: pair != null && type_argument_returned(n, site, formal) && type_argument_literal(last(pair))
278
+ && !any(parameters, parameter => parameter != null && parameter != first(pair) && type_argument_mentions(n, parameter, formal))
279
+ value pair != null && !literal_only && type_argument_same(n, last(pair), true)
256
280
  }
257
281
  fn type_argument_redundancy(n) = {
258
282
  value owner: n.parent.node