@inthedark/dxlint 0.1.13 → 0.1.15

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 (61) hide show
  1. package/README.md +16 -9
  2. package/bin/dxlint-darwin-arm64 +0 -0
  3. package/bin/dxlint-linux-x64 +0 -0
  4. package/bin/dxlint-win32-x64.exe +0 -0
  5. package/esbuild.mjs +106 -0
  6. package/package.json +20 -2
  7. package/std/ts/calls.dx +9 -7
  8. package/std/ts/conditions.dx +2 -0
  9. package/std/ts/constants.dx +2 -1
  10. package/std/ts/content-mapper.dx +48 -0
  11. package/std/ts/deprecation.dx +12 -5
  12. package/std/ts/effects.dx +8 -1
  13. package/std/ts/format.dx +1 -1
  14. package/std/ts/grammar.dx +23 -13
  15. package/std/ts/import-no-deprecated.dx +2 -0
  16. package/std/ts/javascript.dx +496 -0
  17. package/std/ts/layout/literals.dx +14 -7
  18. package/std/ts/layout/separators.dx +1 -1
  19. package/std/ts/layout/shared.dx +36 -37
  20. package/std/ts/layout/types.dx +4 -0
  21. package/std/ts/literals.dx +1 -0
  22. package/std/ts/methods.dx +3 -0
  23. package/std/ts/modules.dx +8 -0
  24. package/std/ts/names.dx +25 -13
  25. package/std/ts/problems/control-flow.dx +1 -0
  26. package/std/ts/problems/numeric-precision.dx +1 -0
  27. package/std/ts/problems/regexp.dx +1 -0
  28. package/std/ts/regex/checks.dx +1 -0
  29. package/std/ts/regex/findings.dx +10 -6
  30. package/std/ts/regex/values.dx +4 -15
  31. package/std/ts/restrictions.dx +76 -0
  32. package/std/ts/scoping/member-uses.dx +5 -3
  33. package/std/ts/scoping/shadowing.dx +1 -0
  34. package/std/ts/scoping/usage.dx +2 -2
  35. package/std/ts/spreads.dx +1 -1
  36. package/std/ts/structure/padding.dx +10 -8
  37. package/std/ts/style/expressions.dx +8 -4
  38. package/std/ts/style/returns.dx +3 -1
  39. package/std/ts/style/signatures.dx +5 -0
  40. package/std/ts/style/type-exports.dx +1 -0
  41. package/std/ts/suggestions/builtins.dx +1 -1
  42. package/std/ts/suggestions/comparisons.dx +1 -0
  43. package/std/ts/suggestions/expressions.dx +1 -0
  44. package/std/ts/suggestions/object-spread.dx +1 -1
  45. package/std/ts/tokens.dx +11 -5
  46. package/std/ts/typed/awaitability.dx +1 -0
  47. package/std/ts/typed/floating-promises.dx +45 -14
  48. package/std/ts/typed/includes.dx +3 -4
  49. package/std/ts/typed/promises.dx +10 -10
  50. package/std/ts/typed/string-boundaries.dx +1 -0
  51. package/std/ts/typed/switches.dx +1 -0
  52. package/std/ts/typed/type-arguments.dx +11 -19
  53. package/std/ts/typed/type-conversions.dx +1 -0
  54. package/std/ts/typed/type-parameters.dx +1 -1
  55. package/std/ts/typed/using-disposable.dx +90 -0
  56. package/std/ts/typed.dx +1 -0
  57. package/std/ts/values/arguments.dx +1 -1
  58. package/std/ts/values/enums.dx +3 -0
  59. package/std/ts/values/inspection.dx +1 -4
  60. package/std/ts/values.dx +41 -26
  61. package/std/ts.dx +10 -4
package/README.md CHANGED
@@ -23,7 +23,7 @@ warn: Promises must be awaited, end with a call to .catch, end with a call to .t
23
23
 
24
24
  - **it's over 200× faster than eslint+typescript-eslint.** because apples to apples requires eslint *without* its caching enabled, and dxlint's cache is intrinsic. (even without that, it's also twice as fast and significantly less memory hungry)
25
25
  - **and it has almost perfect eslint parity.** 99.4% of eslint's and typescript-eslint's test cases give the same result. type-aware rules read your `tsconfig.json` *without running typescript*
26
- - **everything is overrideable, you own it all.** built-in rules are `.dx` files: override them, tweak them, write your own, or describe a whole new language
26
+ - **everything is overrideable, you own it all.** built-in rules, and even the typescript parser, are `.dx` files: override them, tweak them, extend the grammar with [syntax of your own](docs/advanced.md#transforms-syntax-of-your-own) that typescript and esbuild see through, or describe a whole new language
27
27
  - **plus a high quality vscode extension too!** extremely fast, with diagnostics, with a proper `.dx` lsp
28
28
 
29
29
  ## getting started
@@ -47,8 +47,9 @@ ignore "dist" "src/generated/**"
47
47
  | `dxlint --fix` | apply fixes |
48
48
  | `dxlint rules` | list every rule with its options |
49
49
  | `dxlint describe <name>` | explain a rule, option or anything else in the config |
50
+ | `dxlint map <files>` | show what files using your own syntax become ([transforms](docs/advanced.md#transforms-syntax-of-your-own)) |
50
51
 
51
- exit code 1 means errors were found, 2 means the config is broken.&nbsp;
52
+ `dx` works as a shorter name for `dxlint`. exit code 1 means errors were found, 2 means the config is broken.&nbsp;
52
53
 
53
54
  don't forget to add `.dxcache` to `.gitignore`!!!
54
55
 
@@ -70,14 +71,20 @@ in ts_call.callee rule no-alert-calls error "use the notification service instea
70
71
 
71
72
  ## performance
72
73
 
73
- one core (`--jobs 1`, both pinned to the same core, wall time, measured in one session), on a project with 2,094 .ts files, its complex custom eslint config ported, type-aware rules included:
74
+ a project with 2,161 .ts files and its complex custom eslint config ported, type-aware rules included. eslint 9.39 with typescript-eslint 8.58, wall time, measured in one session on a 16-core machine:
74
75
 
75
- | | ESLint | dxlint |
76
+ | one core (`--jobs 1`) | ESLint | dxlint |
76
77
  | --- | --- | --- |
77
- | first run | 164 s | 72 s |
78
- | cached run, nothing changed | 4.4 s | 0.8 s |
79
- | peak memory (working set) | 2.0 GB | 0.7 GB |
78
+ | first run | 143 s | 58 s |
79
+ | cached run, nothing changed | 2.5 s | 2.0 s |
80
+ | peak memory (working set) | 3.6 GB | 0.60 GB |
80
81
 
81
- and remember: this isn't a fair comparison because [eslint --cache doesn't work with typescript-eslint](https://typescript-eslint.io/troubleshooting/faqs/eslint/?utm_source=chatgpt.com#can-i-use-eslints---cache-with-typescript-eslint)!&nbsp;
82
+ | every core (dxlint's default; ESLint `--concurrency auto`) | ESLint | dxlint |
83
+ | --- | --- | --- |
84
+ | first run | 119 s | 11 s |
85
+ | cached run, nothing changed | 3.0 s | 1.5 s |
86
+ | peak memory (working set) | 18 GB | 1.07 GB |
87
+
88
+ and remember: the cached rows aren't a fair comparison because [eslint --cache doesn't work with typescript-eslint](https://typescript-eslint.io/troubleshooting/faqs/eslint/?utm_source=chatgpt.com#can-i-use-eslints---cache-with-typescript-eslint)! eslint's cache skips files whose own text didn't change, even when a type they use did, while dxlint's cache tracks what each file read&nbsp;
82
89
 
83
- also note that by default dxlint uses every core, and is significantly better at that than eslint+typescript-eslint due to no reliance on typescript itself
90
+ eslint's threads each build their own typescript program, so more cores mostly buy it memory; dxlint doesn't rely on typescript, so its work splits across cores
Binary file
Binary file
Binary file
package/esbuild.mjs ADDED
@@ -0,0 +1,106 @@
1
+ // An esbuild plugin mapping files through a project's `.dx` transforms, the
2
+ // same way `tsc` sees them: one long-lived `dxlint map` for the whole build,
3
+ // spoken to in its own protocol (`map { path, text, sourceMap }`).
4
+ import { spawn } from "node:child_process";
5
+ import { readFile } from "node:fs/promises";
6
+ import { dirname, join } from "node:path";
7
+ import { fileURLToPath } from "node:url";
8
+
9
+ const launcher = join(dirname(fileURLToPath(import.meta.url)), "bin", "dxlint");
10
+
11
+ function server() {
12
+ const child = spawn(process.execPath, [launcher, "map"], { stdio: ["pipe", "pipe", "inherit"] });
13
+ const waiting = new Map();
14
+ let next = 1;
15
+ let buffer = Buffer.alloc(0);
16
+ child.stdout.on("data", chunk => {
17
+ buffer = Buffer.concat([buffer, chunk]);
18
+ for (;;) {
19
+ const header = buffer.indexOf("\r\n\r\n");
20
+ if (header === -1) return;
21
+ const length = Number(/content-length:\s*(\d+)/i.exec(buffer.toString("latin1", 0, header))[1]);
22
+ if (buffer.length < header + 4 + length) return;
23
+ const message = JSON.parse(buffer.toString("utf8", header + 4, header + 4 + length));
24
+ buffer = buffer.subarray(header + 4 + length);
25
+ const { resolve, reject } = waiting.get(message.id);
26
+ waiting.delete(message.id);
27
+ if (message.error) reject(new Error(message.error.message));
28
+ else resolve(message.result);
29
+ }
30
+ });
31
+ child.on("exit", () => {
32
+ for (const { reject } of waiting.values()) reject(new Error("dxlint map exited"));
33
+ waiting.clear();
34
+ });
35
+ return {
36
+ request(method, params) {
37
+ return new Promise((resolve, reject) => {
38
+ const id = next++;
39
+ waiting.set(id, { resolve, reject });
40
+ const body = Buffer.from(JSON.stringify({ jsonrpc: "2.0", id, method, params }));
41
+ child.stdin.write(`Content-Length: ${body.length}\r\n\r\n`);
42
+ child.stdin.write(body);
43
+ });
44
+ },
45
+ close() {
46
+ child.stdin.end();
47
+ },
48
+ };
49
+ }
50
+
51
+ /** Byte offset of a problem as esbuild's line and column. */
52
+ function location(file, content, start, end) {
53
+ const bytes = Buffer.from(content);
54
+ const before = bytes.subarray(0, start).toString();
55
+ const line = before.split("\n").length;
56
+ const column = before.length - (before.lastIndexOf("\n") + 1);
57
+ const length = bytes.subarray(start, end).toString().length;
58
+ return { file, line, column, length, lineText: content.split("\n")[line - 1] ?? "" };
59
+ }
60
+
61
+ /**
62
+ * The project's `.dx` transforms as a function, for builds whose own `onLoad`
63
+ * does more with a file: one long-lived `dxlint map`, started on first use.
64
+ * `transform` gives TypeScript `contents` with an inline source map, or esbuild
65
+ * `errors` (and no `contents`) when the transform reports problems.
66
+ * @returns {{ transform(fileName: string, content: string): Promise<{ contents?: string, errors: { text: string, location: object }[] }>, close(): void }}
67
+ */
68
+ export function dxlintMapper() {
69
+ let mapper;
70
+ return {
71
+ async transform(fileName, content) {
72
+ mapper ??= server();
73
+ const result = await mapper.request("map", { path: fileName, text: content, sourceMap: true });
74
+ const errors = result.problems.map(problem => ({ text: problem.message, location: location(fileName, content, problem.offset, problem.end_offset) }));
75
+ if (errors.length > 0) return { errors };
76
+ const map = Buffer.from(result.sourceMap).toString("base64");
77
+ return { contents: `${result.text}\n//# sourceMappingURL=data:application/json;base64,${map}`, errors };
78
+ },
79
+ close() {
80
+ mapper?.close();
81
+ mapper = undefined;
82
+ },
83
+ };
84
+ }
85
+
86
+ /**
87
+ * @param {{ filter?: RegExp, readFile?: (path: string) => string | Uint8Array | Promise<string | Uint8Array> }} [options]
88
+ * `filter`: files to map, by default `.wts`. `readFile`: a file's content as
89
+ * the build should see it (an editor buffer, another checkout's view); by
90
+ * default the disk's.
91
+ */
92
+ export function dxlintTransform({ filter = /\.wts$/, readFile: readContent = path => readFile(path, "utf8") } = {}) {
93
+ return {
94
+ name: "dxlint-transform",
95
+ setup(build) {
96
+ const mapper = dxlintMapper();
97
+ build.onDispose(() => mapper.close());
98
+ build.onLoad({ filter }, async args => {
99
+ const read = await readContent(args.path);
100
+ const content = typeof read === "string" ? read : Buffer.from(read).toString("utf8");
101
+ const { contents, errors } = await mapper.transform(args.path, content);
102
+ return contents === undefined ? { errors } : { contents, loader: "ts", resolveDir: dirname(args.path) };
103
+ });
104
+ },
105
+ };
106
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inthedark/dxlint",
3
- "version": "0.1.13",
3
+ "version": "0.1.15",
4
4
  "description": "let you and your agents own your linter",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -32,12 +32,30 @@
32
32
  "arm64"
33
33
  ],
34
34
  "bin": {
35
- "dxlint": "bin/dxlint"
35
+ "dxlint": "bin/dxlint",
36
+ "dx": "bin/dxlint"
37
+ },
38
+ "exports": {
39
+ "./esbuild": "./esbuild.mjs",
40
+ "./package.json": "./package.json"
41
+ },
42
+ "typescript": {
43
+ "contentMapper": {
44
+ "exec": [
45
+ "node",
46
+ "bin/dxlint",
47
+ "map",
48
+ "--protocol",
49
+ ".ts/content-mapper"
50
+ ],
51
+ "dynamicConfig": true
52
+ }
36
53
  },
37
54
  "files": [
38
55
  "bin",
39
56
  "std",
40
57
  "rules",
58
+ "esbuild.mjs",
41
59
  "README.md",
42
60
  "LICENSE"
43
61
  ]
package/std/ts/calls.dx CHANGED
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  # .ts/calls: signatures and invocation syntax.
2
3
  import .ts/effects
3
4
 
@@ -44,15 +45,16 @@ define {
44
45
  }
45
46
  }
46
47
 
48
+ in ts_class let calls_bodiless_methods filter(member.nodes, declaration => declaration is ts_method && declaration.body == null)
49
+ in ts_arrow, ts_function_type, ts_method_signature, ts_call_signature let calls_overload_implementation false
50
+ in ts_function let calls_overload_implementation node.body != null && any(name_binding(node.name.node).declarations, declaration =>
51
+ declaration.parent.node is ts_function && declaration.parent.node.body == null)
52
+ in ts_method let calls_overload_implementation node.body != null && parent is ts_class && count(parent.calls_bodiless_methods) > 0
53
+ && ts_value_key(key.node) != null
54
+ && any(parent.calls_bodiless_methods, declaration => ts_value_key(declaration.key.node) == ts_value_key(key.node)
55
+ && ts_value_static(declaration.modifier.nodes) == ts_value_static(modifier.nodes))
47
56
  in ts_function, ts_arrow, ts_method, ts_function_type, ts_method_signature, ts_call_signature {
48
57
  overloads infer last
49
- let calls_overload_implementation node.body != null && (
50
- node is ts_function && any(name_binding(node.name.node).declarations, declaration =>
51
- declaration.parent.node is ts_function && declaration.parent.node.body == null)
52
- || node is ts_method && parent is ts_class && ts_value_key(key.node) != null
53
- && any(parent.member.nodes, declaration => declaration is ts_method && declaration.body == null
54
- && ts_value_key(declaration.key.node) == ts_value_key(key.node)
55
- && ts_value_static(declaration.modifier.nodes) == ts_value_static(modifier.nodes)))
56
58
  let calls_receivers filter(parameter.nodes, p => p.name is ts_this || p.name is ts_identifier && p.name == "this")
57
59
  let calls_parameters filter(parameter.nodes, p => !(p.name is ts_this || p.name is ts_identifier && p.name == "this"))
58
60
  let calls_concise_result if node is ts_arrow && node.body not ts_block then node.body.node else null
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  # .ts/conditions: type-aware conditional policy.
2
3
 
3
4
  import .ts/values/inspection
@@ -207,6 +208,7 @@ fn condition_callback(n) = if n is (ts_arrow, ts_function) then when {
207
208
  else then null
208
209
  } else null
209
210
  fn condition_array_method(n) = n.callee is (ts_member, ts_index) && contains(["every", "filter", "find", "findIndex", "findLast", "findLastIndex", "some"], n.callee.static_name)
211
+ in ts_call let condition_array_call condition_array_method(node)
210
212
  fn condition_array_callback(n) = condition_array_method(n) && strict_boolean_array(n.callee.object.node)
211
213
  fn condition_literal_arm(arm) = {
212
214
  value parts: filter(arm, part => part.identity.kind == "value")
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  import .ts/globals
2
3
  import .ts/expression-fixes
3
4
 
@@ -9,7 +10,7 @@ fn ts_runtime_binding_init(n) = {
9
10
  value owner: declaration.parent.node
10
11
  value if binding == null || count(binding.declarations) != 1 || owner not ts_declarator
11
12
  || owner.name.node != declaration || !contains(["const", "let", "var"], declaration.declaration_kind)
12
- || !all(binding.references, reference => reference.access != null
13
+ || !all(name_references(n), reference => reference.access != null
13
14
  && !contains(["write", "read_write"], reference.access))
14
15
  then null else owner.init.node
15
16
  }
@@ -0,0 +1,48 @@
1
+ # TypeScript's content-mapper protocol (TypeScript 7: `contentMappers` in
2
+ # tsconfig, `tsc --runExternalCode`), as an adapter over dxlint's neutral
3
+ # mapping. Served by `dxlint map --protocol .ts/content-mapper`, which the
4
+ # npm package's manifest names. Kept text and copies are verbatim segments
5
+ # (mirrors take part in no language feature, so tools land on the copy),
6
+ # renamed text is an alias; segments split where another piece's span
7
+ # starts or ends, as TypeScript wants original spans equal or disjoint.
8
+
9
+ protocol typescript {
10
+ method initialize (params) = { protocolVersion: 1, positionEncoding: "utf-8", diagnosticSource: "dxlint" }
11
+ method openProject (params) = { configIdentity: config_identity(params.configFileName), watchedFiles: config_files(params.configFileName) }
12
+ method closeProject (params) = null
13
+ method transform (params) = ts_content_mapping(map_file(params.fileName, params.content))
14
+ }
15
+
16
+ define {
17
+
18
+ fn ts_mapper_cuts(pieces) = { value pieces; flat_map (p) [p.source.offset, p.source.end_offset]; unique; sort }
19
+
20
+ # A same-text piece as segments split at every cut inside its span:
21
+ # `[virtual start, length, original start, length]`.
22
+ fn ts_mapper_segments(p, cuts) = {
23
+ value inside: { value cuts; filter (c) c > p.source.offset && c < p.source.end_offset }
24
+ value starts: [p.source.offset] inside
25
+ value { value starts; zip (inside [p.source.end_offset]) (start, end) [p.output.offset + start - p.source.offset, end - start, start, end - start] }
26
+ }
27
+
28
+ # Renamed text maps as a whole; across a cut it maps nowhere.
29
+ fn ts_mapper_alias(p, cuts) = if any(cuts, c => c > p.source.offset && c < p.source.end_offset) then []
30
+ else [[p.output.offset, p.output.end_offset - p.output.offset, p.source.offset, p.source.end_offset - p.source.offset, 2]]
31
+
32
+ # Kind 0 is verbatim; a sixth element 0 turns every language feature off.
33
+ fn ts_mapper_tuple(p, segment) = if p.kind == "mirror" then segment [0, 0] else segment [0]
34
+
35
+ fn ts_mapper_mappings(pieces) = {
36
+ value cuts: ts_mapper_cuts(pieces)
37
+ value pieces
38
+ flat_map (p) if p.kind == "renamed" then ts_mapper_alias(p, cuts) else map(ts_mapper_segments(p, cuts), s => ts_mapper_tuple(p, s))
39
+ }
40
+
41
+ fn ts_content_mapping(mapped) = {
42
+ text: mapped.text,
43
+ extension: ".ts",
44
+ mappings: ts_mapper_mappings(mapped.pieces),
45
+ diagnostics: map(mapped.problems, p => { messageText: p.message, start: p.offset, length: p.end_offset - p.offset }),
46
+ }
47
+
48
+ }
@@ -54,7 +54,7 @@ fn import_deprecation_doc(n, site) = {
54
54
  fn import_remote_sites(n, sites) = filter(sites ?? [], site => import_remote_site(n, site))
55
55
  # `export { /** @deprecated */ X }`: the specifier carries the documentation,
56
56
  # not `X`'s declaration. Found among the statements of the declaring file.
57
- fn import_specifier_docs(n, site, name) = {
57
+ fn import_specifier_docs(n, site, name) = if !site_mentions(n, site, "@deprecated") then [] else {
58
58
  value root: last(walk([site], current => site_parent(n, current)))
59
59
  value exports: filter(site_field(n, root, "body") ?? [], statement => statement.kind == "ts_export")
60
60
  value specifiers: flat_map(exports, statement => site_field(n, statement, "specifier") ?? [])
@@ -67,15 +67,22 @@ fn import_specifier_docs(n, site, name) = {
67
67
  fn import_deprecation_tag(n) = {
68
68
  value values: import_remote_sites(n, name_definitions(n, "value"))
69
69
  value types: if count(values) > 0 then [] else import_remote_sites(n, name_definitions(n, "type"))
70
- value sites: if count(values) > 0 then values else types
71
- value imported: name_binding(n).node.imported_name ?? n.text
72
- value deprecation_first(flat_map(reverse(sites), site => [import_deprecation_doc(n, site)] import_specifier_docs(n, site, imported)))
70
+ value sites: filter(if count(values) > 0 then values else types, site => site_mentions(n, site, "@deprecated"))
71
+ value if count(sites) == 0 then null else {
72
+ value imported: name_binding(n).node.imported_name ?? n.text
73
+ value deprecation_first(flat_map(reverse(sites), site => [import_deprecation_doc(n, site)] import_specifier_docs(n, site, imported)))
74
+ }
73
75
  }
74
76
  fn deprecation_local_doc(n) = {
75
77
  value owner: find(walk([] n.declaration, current =>
76
78
  if current is (ts_declarator, ts_identifier) then current.parent.node else null),
77
79
  current => current is ts_variable_declaration)
78
- value if owner == null then null else deprecation_leading_doc(owner.gap_before ?? owner.text_before)
80
+ value if owner == null then null else deprecation_leading_doc(owner.deprecation_leading)
81
+ }
82
+ # From the code before it: comments are children only under some configs.
83
+ in ts_variable_declaration let deprecation_leading {
84
+ value lead: token(node, "before")
85
+ value byte_slice(source, if lead == null then 0 else lead.end, offset)
79
86
  }
80
87
  fn deprecation_subject(n) = if n is (ts_member.property, ts_qualified_name.right) then n.parent.node
81
88
  else if n.deprecation_shorthand then n.declaration else n
package/std/ts/effects.dx CHANGED
@@ -1,3 +1,4 @@
1
+ import .ts/javascript
1
2
  # .ts/effects: evaluation order and separate execution entries.
2
3
 
3
4
  define {
@@ -96,7 +97,13 @@ in ts_identifier, ts_member, ts_index let flow_assignment_scalar true
96
97
  in ts_parenthesized, ts_as, ts_satisfies, ts_non_null, ts_type_assertion, ts_instantiation let flow_assignment_scalar expression.flow_assignment_scalar
97
98
  in ts_property, ts_pattern_property let flow_shorthand_default value is ts_assignment_pattern && value.left == null
98
99
  in ts_node let flow_destructuring_target
99
- if is (ts_for_in.left, ts_for_of.left) then true
100
+ if !(is (ts_for_in.left, ts_for_of.left, ts_declarator.name, ts_assignment.left,
101
+ ts_array.element, ts_array_pattern.element, ts_object.property, ts_object_pattern.property,
102
+ ts_object_pattern.rest, ts_rest.argument, ts_spread.argument, ts_assignment_pattern.left,
103
+ ts_parenthesized.expression, ts_as.expression, ts_satisfies.expression, ts_non_null.expression,
104
+ ts_type_assertion.expression, ts_instantiation.expression, ts_property.value, ts_pattern_property.value,
105
+ ts_property.key, ts_pattern_property.key)) then false
106
+ else if is (ts_for_in.left, ts_for_of.left) then true
100
107
  else if is ts_declarator.name then
101
108
  (node.parent.init != null && flow_assignment_scalar == false)
102
109
  || node.parent.parent is (ts_for_in.left, ts_for_of.left)
package/std/ts/format.dx CHANGED
@@ -12,7 +12,7 @@ define {
12
12
  # ---- blank lines ---------------------------------------------------------
13
13
 
14
14
  # No blank lines between a header and its single-statement body.
15
- in ts_if.consequent, ts_while.body, ts_for.body, ts_for_in.body, ts_for_of.body rule es-no-blank-lines-in-optional-blocks warn "Avoid blank lines between a conditional or loop statement and its single non-block statement." {
15
+ in ts_if.consequent, ts_while.body, ts_do_while.body, ts_for.body, ts_for_in.body, ts_for_of.body rule es-no-blank-lines-in-optional-blocks warn "Avoid blank lines between a conditional or loop statement and its single non-block statement." {
16
16
  when not ts_block && !space_before.comments && space_before.blank_lines > 0
17
17
  let optional_header { value space_before.text; re_find /^[ \t]*/; nonempty } ?? ""
18
18
  let optional_indent { value space_before.text; re_find /[ \t]*\z/; nonempty } ?? ""
package/std/ts/grammar.dx CHANGED
@@ -5,31 +5,36 @@
5
5
  @macro ts_parser_signature (type_parameter:type_parameters)? parameter:parameters (":" return_type:return_type)?
6
6
  @macro ts_parser_tuple_member (name:type_identifier optional:"?"? ":" type:type_expression | type:type_expression optional:"?"?)
7
7
  @macro ts_parser_method_header (kind:/get|set/ peek(accessor_next))? key:property_key optional:"?"? (type_parameter:goal(types, type_parameters))? parameter:parameters (":" return_type:goal(types, return_type))?
8
- @macro ts_parser_class_header modifier:/declare|abstract/* "class" name:(not(/extends|implements/) type_identifier)? (type_parameter:goal(types, type_parameters))? ("extends" extends:lhs_expression (type_argument:goal(types, type_arguments))?)? ("implements" implements:goal(types, type_expression) ("," implements:goal(types, type_expression))*)?
9
- @macro ts_parser_class_modifiers (modifier:(text("static") peek(accessor_next) | /public|private|protected|readonly|abstract|override|declare|accessor|async/ same_line peek(accessor_next)))*
8
+ @macro ts_parser_class_header modifier:/declare|abstract/* "class" name:(not(/extends|implements/) type_identifier)? (type_parameter:goal(types, type_parameters))? ("extends" extends:set(heritage, lhs_expression) (type_argument:goal(types, type_arguments))?)? ("implements" implements:goal(types, type_expression) ("," implements:goal(types, type_expression))*)?
9
+ @macro ts_parser_class_modifiers (modifier:class_modifier)*
10
10
  @macro ts_parser_import_kind (kind:"type" peek("{" | "*" | not("from") type_identifier))?
11
11
  @macro ts_parser_specifier_kind (kind:"type" peek(not("as") (type_identifier | ts_string)))?
12
12
  @macro ts_parser_semi terminator(";", before("}"), newline, eof)
13
13
  @macro ts_parser_instantiation_end not(/<|>|\+|-/) (not(same_line) | peek(/instanceof|in|as|satisfies|\?\?|\|\||&&|\||\^|&|==|!=|===|!==|<=|>=|<<|>>|>>>|\*|\/|%|\*\*/) | not(peek(type_identifier | ts_number | ts_bigint | ts_string | ts_template | /\(|\[|\{|!|~|\+\+|--|\/|\/=|@|\.\.\.|#/)))
14
14
  @macro ts_parser_arrow_tail same_line operator:"=>" cut body:clear(generator, (clear(no_in, ts_block) | assignment))
15
- parser ts_syntax in js {
15
+ parser ts in js {
16
16
  trivia /\A\u{feff}?#![^\n]*/
17
17
  trivia /[ \t\r\n\x0b\x0c\u{00a0}\u{1680}\u{2000}-\u{200a}\u{2028}\u{2029}\u{202f}\u{205f}\u{3000}\u{feff}]+/
18
18
  trivia /\/\/[^\r\n\u{2028}\u{2029}]*/
19
19
  trivia /\/\*(?:[^*]|\*+[^*/])*\*+\//
20
20
  line_break /[\r\n\u{2028}\u{2029}]/
21
+ # `a` and `\u{61}` in a name are `a`.
22
+ name_escape /\\u\{([0-9a-fA-F]+)\}|\\u([0-9a-fA-F]{4})/
21
23
 
22
24
  flag jsx option
23
25
  flag generator
24
26
  flag async
25
27
  flag no_in
26
28
  flag no_cond
29
+ flag heritage
27
30
 
28
31
  token keyword /(?:break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|false|finally|for|function|if|import|in|instanceof|new|null|return|super|switch|this|throw|true|try|typeof|var|void|while|with|yield|let|static|implements|interface|package|private|protected|public|await|async|abstract|declare|type|namespace|module|as|satisfies|keyof|infer|readonly|is|asserts|of|using)/ as ts_keyword
29
- token word /(?:[A-Za-z_$]|[^\x00-\x7f\u{00a0}\u{1680}\u{2000}-\u{200a}\u{2028}\u{2029}\u{202f}\u{205f}\u{3000}\u{feff}]|\\u(?:[0-9a-fA-F]{4}|\{[0-9a-fA-F]+\}))(?:[A-Za-z0-9_$]|[^\x00-\x7f\u{00a0}\u{1680}\u{2000}-\u{200a}\u{2028}\u{2029}\u{202f}\u{205f}\u{3000}\u{feff}]|\\u(?:[0-9a-fA-F]{4}|\{[0-9a-fA-F]+\}))*/
30
- token private_name /#(?:[A-Za-z_$]|[^\x00-\x7f])(?:[A-Za-z0-9_$]|[^\x00-\x7f])*/
32
+ token word /(?:[A-Za-z_$]|[^\x00-\x7f\u{00a0}\u{1680}\u{2000}-\u{200a}\u{2028}\u{2029}\u{202f}\u{205f}\u{3000}\u{feff}]|\\u(?:[0-9a-fA-F]{4}|\{[0-9a-fA-F]+\}))(?:[A-Za-z0-9_$]|[^\x00-\x7f\u{00a0}\u{1680}\u{2000}-\u{200a}\u{2028}\u{2029}\u{202f}\u{205f}\u{3000}\u{feff}]|\\u(?:[0-9a-fA-F]{4}|\{[0-9a-fA-F]+\}))*/ as ts_keyword
33
+ token private_name /#(?:[A-Za-z_$]|[^\x00-\x7f]|\\u(?:[0-9a-fA-F]{4}|\{[0-9a-fA-F]+\}))(?:[A-Za-z0-9_$]|[^\x00-\x7f]|\\u(?:[0-9a-fA-F]{4}|\{[0-9a-fA-F]+\}))*/
31
34
  token bigint /(?:0[xX][0-9a-fA-F_]+|0[bB][01_]+|0[oO][0-7_]+|[0-9][0-9_]*)n/
32
- token number /(?:0[xX][0-9a-fA-F_]+|0[bB][01_]+|0[oO][0-7_]+|(?:[0-9][0-9_]*(?:\.[0-9_]*)?|\.[0-9][0-9_]*)(?:[eE][+-]?[0-9][0-9_]*)?)/
35
+ # A legacy octal (`0123`) takes no fraction or exponent: `0123.a` reads a
36
+ # member. Alternatives are tried in order.
37
+ token number /(?:0[xX][0-9a-fA-F_]+|0[bB][01_]+|0[oO][0-7_]+|(?:(?:0[0-7]*[89][0-9_]*|[1-9][0-9_]*)(?:\.[0-9_]*)?|\.[0-9][0-9_]*)(?:[eE][+-]?[0-9][0-9_]*)?|0[0-7]+|0(?:\.[0-9_]*)?(?:[eE][+-]?[0-9][0-9_]*)?)/
33
38
  token string /(?:"(?:\\\r\n|\\[\s\S]|[^"\\\n])*"|'(?:\\\r\n|\\[\s\S]|[^'\\\n])*')/
34
39
  token regexp /\/(?:\\[\s\S]|\[(?:\\[\s\S]|[^\]\\\n])*\]|[^\/\\\[\n])+\/(?:[A-Za-z0-9_$\\]|[^\x00-\x7f])*/
35
40
  token template /`(?:\\[\s\S]|[^\\`$]|\$+(?:\\[\s\S]|[^\\`{$]))*\$*`/
@@ -37,7 +42,7 @@ parser ts_syntax in js {
37
42
  token template_middle /\}(?:\\[\s\S]|[^\\`$]|\$+(?:\\[\s\S]|[^\\`{$]))*\$*\$\{/
38
43
  token template_tail /\}(?:\\[\s\S]|[^\\`$]|\$+(?:\\[\s\S]|[^\\`{$]))*\$*`/
39
44
  token jsx_name /(?:[A-Za-z_$]|[^\x00-\x7f\u{00a0}\u{1680}\u{2000}-\u{200a}\u{2028}\u{2029}\u{202f}\u{205f}\u{3000}\u{feff}])(?:[A-Za-z0-9_$-]|[^\x00-\x7f\u{00a0}\u{1680}\u{2000}-\u{200a}\u{2028}\u{2029}\u{202f}\u{205f}\u{3000}\u{feff}])*/
40
- token jsx_text /[^<{]+/
45
+ token jsx_text /[^<{]+/ lines
41
46
  token jsx_string /(?:"[^"]*"|'[^']*')/
42
47
  token jsx_value_comment /[ \t\r\n\x0b\x0c\u{00a0}\u{1680}\u{2000}-\u{200a}\u{2028}\u{2029}\u{202f}\u{205f}\u{3000}\u{feff}]*(?:\/\/|\/\*)/
43
48
 
@@ -195,7 +200,7 @@ parser ts_syntax in js {
195
200
  node ts_property_signature { modifier?: text, key: property_key, optional?: text, type?: type_expression }
196
201
  node ts_method_signature { modifier?: text, kind?: text, key: property_key, optional?: text, type_parameter?: ts_type_parameter*, parameter?: ts_parameter*, return_type?: return_type }
197
202
  node ts_call_signature { kind?: text, type_parameter?: ts_type_parameter*, parameter?: ts_parameter*, return_type?: return_type }
198
- node ts_index_signature { decorator?: ts_decorator*, modifier?: text*, parameter: ts_parameter, type?: type_expression }
203
+ node ts_index_signature { decorator?: ts_decorator*, modifier?: text*, parameter: ts_parameter+, type?: type_expression }
199
204
  node ts_tuple_type { element?: ts_tuple_member | ts_rest* }
200
205
  node ts_tuple_member { name?: type_identifier, optional?: text, type: type_expression }
201
206
  node ts_type_query { name: entity_query | ts_import_type, type_argument?: type_expression* }
@@ -340,9 +345,10 @@ parser ts_syntax in js {
340
345
  suffix 1 (same_line "!") => ts_non_null(expression)
341
346
  suffix 1 quasi:ts_template => ts_tagged_template(tag)
342
347
  suffix 1 try(type_argument:goal(types, type_arguments) quasi:ts_template) => ts_tagged_template(tag)
343
- suffix 1 try(type_argument:goal(types, type_arguments) @ts_parser_instantiation_end) => ts_instantiation(expression)
348
+ # A class heritage's `<...>` are its type arguments, never an instantiation.
349
+ suffix 1 try(type_argument:goal(types, type_arguments) if(heritage, not(()), (@ts_parser_instantiation_end))) => ts_instantiation(expression)
344
350
  }
345
- production arguments = "(" (argument:(ts_spread | clear(no_in, assignment)) ("," argument:(ts_spread | clear(no_in, assignment)))* ","?)? ")"
351
+ production arguments = "(" clear(heritage, (argument:(ts_spread | clear(no_in, assignment)) ("," argument:(ts_spread | clear(no_in, assignment)))* ","?)?) ")"
346
352
  production new_callee precedence goal(primary, primary_expression) {
347
353
  suffix 1 ("." property:property_name) => ts_member(object)
348
354
  suffix 1 ("[" index:clear(no_in, expression) "]") => ts_index(object)
@@ -431,7 +437,7 @@ parser ts_syntax in js {
431
437
  production type_members = "{" (type_member (";" | "," | terminator(";", before("}"), newline)))* "}"
432
438
  production type_member = try(ts_call_signature) | try(ts_index_signature) | try(ts_method_signature) | ts_property_signature
433
439
  production ts_call_signature = kind:"new"? @ts_parser_signature
434
- production ts_index_signature = (modifier:"readonly" same_line peek("["))? "[" parameter:emit(ts_parameter, name:type_identifier optional:"?"? ":" type:type_expression) "]" (":" type:type_expression)?
440
+ production ts_index_signature = (modifier:"readonly" same_line peek("["))? "[" parameter:emit(ts_parameter, name:type_identifier optional:"?"? ":" type:type_expression) ("," parameter:emit(ts_parameter, name:type_identifier optional:"?"? ":" type:type_expression))* "]" (":" type:type_expression)?
435
441
  production ts_method_signature = (modifier:"readonly" same_line peek(property_key))? (kind:/get|set/ same_line peek(property_key))? key:property_key optional:"?"? (type_parameter:type_parameters)? parameter:parameters (":" return_type:return_type)?
436
442
  production ts_property_signature = (modifier:"readonly" same_line peek(property_key))? (discard(/get|set/) same_line peek(property_key))? key:property_key optional:"?"? (":" type:type_expression)?
437
443
 
@@ -445,7 +451,7 @@ parser ts_syntax in js {
445
451
  ))
446
452
  production class_member = try(ts_static_block) | try(class_index_signature) | try(class_method) | ts_class_property
447
453
  production ts_static_block = discard(ts_decorator*) "static" body:clear(no_in, ts_block)
448
- production class_index_signature = emit(ts_index_signature, decorator:ts_decorator* @ts_parser_class_modifiers "[" parameter:emit(ts_parameter, name:type_identifier optional:"?"? ":" type:goal(types, type_expression)) "]" (":" type:goal(types, type_expression))?) discard(@ts_parser_semi)
454
+ production class_index_signature = emit(ts_index_signature, decorator:ts_decorator* @ts_parser_class_modifiers "[" parameter:emit(ts_parameter, name:type_identifier optional:"?"? ":" type:goal(types, type_expression)) ("," parameter:emit(ts_parameter, name:type_identifier optional:"?"? ":" type:goal(types, type_expression)))* "]" (":" type:goal(types, type_expression))?) discard(@ts_parser_semi)
449
455
  production class_method = emit(ts_method, decorator:ts_decorator* @ts_parser_class_modifiers (
450
456
  modifier:"*" @ts_parser_method_header (body:clear(no_in, set(generator, ts_block)) | @ts_parser_semi) |
451
457
  @ts_parser_method_header (body:clear(no_in, clear(generator, ts_block)) | @ts_parser_semi)
@@ -454,7 +460,11 @@ parser ts_syntax in js {
454
460
  modifier:"*" @ts_parser_method_header (body:clear(no_in, set(generator, ts_block)) | @ts_parser_semi) |
455
461
  @ts_parser_method_header (body:clear(no_in, clear(generator, ts_block)) | @ts_parser_semi)
456
462
  ))
457
- production ts_class_property = decorator:ts_decorator* @ts_parser_class_modifiers modifier:"*"? (kind:/get|set/ peek(accessor_next))? key:property_key optional:"?"? "!"? (":" type:goal(types, type_expression))? ("=" init:clear(no_in, assignment))? @ts_parser_semi
463
+ # Extension points: `extend parser ts { production class_modifier |= ... }`
464
+ # adds member modifiers, `class_initializer` tokens standing for `=`.
465
+ production class_modifier = text("static") peek(accessor_next) | /public|private|protected|readonly|abstract|override|declare|accessor|async/ same_line peek(accessor_next)
466
+ production class_initializer = "="
467
+ production ts_class_property = decorator:ts_decorator* @ts_parser_class_modifiers modifier:"*"? (kind:/get|set/ peek(accessor_next))? key:property_key optional:"?"? "!"? (":" type:goal(types, type_expression))? (discard(class_initializer) init:clear(no_in, assignment))? @ts_parser_semi
458
468
  production ts_interface = modifier:"declare"* "interface" same_line name:type_identifier (type_parameter:goal(types, type_parameters))? ("extends" extends:goal(types, type_expression) ("," extends:goal(types, type_expression))*)? member:goal(types, type_members)
459
469
  production ts_type_alias = modifier:"declare"* "type" same_line name:type_identifier (type_parameter:goal(types, type_parameters))? "=" type:goal(types, type_expression) @ts_parser_semi
460
470
  production ts_enum = modifier:"declare"* modifier:"const"? "enum" name:type_identifier "{" (member:ts_enum_member ("," member:ts_enum_member)* ","?)? "}"
@@ -44,7 +44,9 @@ in ts_program let import_deprecated_defaults {
44
44
  map (item) item.default_import.node.text
45
45
  }
46
46
 
47
+ in ts_program let import_deprecated_any count(import_deprecated_names) > 0 || count(import_deprecated_defaults) > 0
47
48
  in ts_identifier rule es-import-no-deprecated:use {
49
+ when ts_program.import_deprecated_any
48
50
  when contains(ts_program.import_deprecated_names, text) || contains(ts_program.import_deprecated_defaults, text)
49
51
  when resolved == "import"
50
52
  when containing(ts_jsx_opening) == null && containing(ts_jsx_closing) == null