@yearofthedan/warp 0.4.1 → 0.5.1

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 (169) hide show
  1. package/README.md +6 -10
  2. package/dist/adapters/analysis/chains.d.ts +4 -0
  3. package/dist/adapters/analysis/chains.js +97 -0
  4. package/dist/adapters/analysis/classes.d.ts +3 -0
  5. package/dist/adapters/analysis/classes.js +109 -0
  6. package/dist/{analysis → adapters/analysis}/complexity.d.ts +1 -1
  7. package/dist/adapters/analysis/folders.d.ts +2 -0
  8. package/dist/{app/facts.d.ts → adapters/analysis/freshFacts.d.ts} +1 -2
  9. package/dist/{app/facts.js → adapters/analysis/freshFacts.js} +0 -10
  10. package/dist/{analysis → adapters/analysis}/imports.d.ts +2 -1
  11. package/dist/{analysis → adapters/analysis}/imports.js +60 -23
  12. package/dist/{analysis → adapters/analysis}/members.d.ts +1 -1
  13. package/dist/{analysis → adapters/analysis}/members.js +1 -1
  14. package/dist/{analysis → adapters/analysis}/source.d.ts +2 -2
  15. package/dist/{analysis → adapters/analysis}/source.js +2 -2
  16. package/dist/adapters/analysis/specifiers.d.ts +10 -0
  17. package/dist/adapters/analysis/specifiers.js +39 -0
  18. package/dist/adapters/analysis/typescript.d.ts +2 -0
  19. package/dist/{analysis → adapters/analysis}/typescript.js +14 -4
  20. package/dist/{config → adapters/config}/loadConfig.d.ts +2 -7
  21. package/dist/{config → adapters/config}/loadConfig.js +5 -4
  22. package/dist/{git → adapters/git}/cache.d.ts +1 -1
  23. package/dist/adapters/git/factsStore.d.ts +2 -0
  24. package/dist/adapters/git/factsStore.js +10 -0
  25. package/dist/{git → adapters/git}/repository.d.ts +1 -1
  26. package/dist/{config → adapters/git}/toolDigest.js +1 -1
  27. package/dist/{reporters → adapters/reporters}/model.d.ts +2 -2
  28. package/dist/{reporters → adapters/reporters}/reporter.d.ts +2 -2
  29. package/dist/{reporters → adapters/reporters}/text.d.ts +2 -2
  30. package/dist/app/check.d.ts +5 -6
  31. package/dist/app/check.js +3 -3
  32. package/dist/app/checkModel.d.ts +5 -5
  33. package/dist/app/checkModel.js +7 -8
  34. package/dist/app/configLines.d.ts +1 -1
  35. package/dist/app/diff.d.ts +1 -1
  36. package/dist/app/diff.js +1 -1
  37. package/dist/app/failures.d.ts +6 -0
  38. package/dist/app/failures.js +15 -0
  39. package/dist/app/load.d.ts +11 -5
  40. package/dist/app/load.js +23 -59
  41. package/dist/app/ports.d.ts +13 -3
  42. package/dist/builtins/advice.d.ts +4 -4
  43. package/dist/builtins/advice.js +1 -1
  44. package/dist/builtins/boundaries.d.ts +17 -0
  45. package/dist/builtins/boundaries.js +35 -0
  46. package/dist/builtins/class-cohesion/rule.d.ts +6 -0
  47. package/dist/builtins/class-cohesion/rule.js +34 -0
  48. package/dist/builtins/cognitive-complexity/{cognitiveComplexity.d.ts → rule.d.ts} +1 -1
  49. package/dist/builtins/cognitive-complexity/{cognitiveComplexity.js → rule.js} +1 -1
  50. package/dist/builtins/components.d.ts +1 -1
  51. package/dist/builtins/dependency-direction/rule.d.ts +2 -0
  52. package/dist/builtins/dependency-direction/{dependencyDirection.js → rule.js} +3 -3
  53. package/dist/builtins/export-consumers/rule.d.ts +6 -0
  54. package/dist/builtins/export-consumers/rule.js +65 -0
  55. package/dist/builtins/export-spread/rule.d.ts +3 -0
  56. package/dist/builtins/export-spread/rule.js +65 -0
  57. package/dist/builtins/export-surface/rule.d.ts +6 -0
  58. package/dist/builtins/export-surface/rule.js +60 -0
  59. package/dist/builtins/file-cohesion/{fileCohesion.d.ts → rule.d.ts} +1 -1
  60. package/dist/builtins/file-cohesion/{fileCohesion.js → rule.js} +1 -1
  61. package/dist/builtins/function-cohesion/{functionCohesion.d.ts → rule.d.ts} +1 -1
  62. package/dist/builtins/function-cohesion/{functionCohesion.js → rule.js} +1 -1
  63. package/dist/builtins/index.d.ts +2 -2
  64. package/dist/builtins/index.js +33 -13
  65. package/dist/builtins/loops.d.ts +1 -1
  66. package/dist/builtins/loops.js +1 -1
  67. package/dist/builtins/module-cycle/{moduleCycle.d.ts → rule.d.ts} +1 -1
  68. package/dist/builtins/module-dependencies/rule.d.ts +3 -0
  69. package/dist/builtins/module-dependencies/rule.js +67 -0
  70. package/dist/builtins/module-externals/rule.d.ts +2 -0
  71. package/dist/builtins/module-externals/{moduleExternals.js → rule.js} +3 -3
  72. package/dist/builtins/module-size/rule.d.ts +6 -0
  73. package/dist/builtins/module-size/rule.js +36 -0
  74. package/dist/builtins/no-cross-module-reexport/rule.d.ts +2 -0
  75. package/dist/builtins/no-cross-module-reexport/{noCrossModuleReexport.js → rule.js} +1 -1
  76. package/dist/builtins/no-cycle/{noCycle.d.ts → rule.d.ts} +1 -1
  77. package/dist/builtins/no-double-export/rule.d.ts +2 -0
  78. package/dist/builtins/no-double-export/{noDoubleExport.js → rule.js} +1 -1
  79. package/dist/builtins/packages.d.ts +1 -1
  80. package/dist/builtins/place.d.ts +1 -1
  81. package/dist/builtins/shared.d.ts +1 -1
  82. package/dist/builtins/unclassified/rule.d.ts +2 -0
  83. package/dist/builtins/unclassified/rule.js +15 -0
  84. package/dist/builtins/unresolved-import/rule.d.ts +2 -0
  85. package/dist/builtins/unresolved-import/{unresolvedImport.js → rule.js} +1 -1
  86. package/dist/builtins/unused-declaration/declarations.d.ts +3 -3
  87. package/dist/builtins/unused-declaration/empty.d.ts +1 -1
  88. package/dist/builtins/unused-declaration/empty.js +1 -1
  89. package/dist/builtins/unused-declaration/grants.d.ts +1 -1
  90. package/dist/builtins/unused-declaration/grants.js +42 -10
  91. package/dist/builtins/unused-declaration/rule.d.ts +2 -0
  92. package/dist/builtins/unused-declaration/{unusedDeclaration.js → rule.js} +1 -1
  93. package/dist/builtins/unused-declaration/surfaces.d.ts +1 -1
  94. package/dist/builtins/unused-declaration/surfaces.js +1 -1
  95. package/dist/cli.js +34 -54
  96. package/dist/{model → domain/facts}/facts.d.ts +20 -0
  97. package/dist/{model/archetype → domain/fixes}/fixes.d.ts +2 -2
  98. package/dist/{model/archetype → domain/fixes}/fixes.js +2 -6
  99. package/dist/domain/language/compile.d.ts +12 -0
  100. package/dist/domain/language/compile.js +58 -0
  101. package/dist/{model/archetype → domain/language}/references.d.ts +6 -7
  102. package/dist/{model/archetype → domain/language}/references.js +41 -26
  103. package/dist/{model/archetype → domain/language}/requirements.d.ts +3 -3
  104. package/dist/{model/archetype → domain/language}/requirements.js +2 -1
  105. package/dist/domain/language/rulesets.d.ts +11 -0
  106. package/dist/domain/language/rulesets.js +60 -0
  107. package/dist/{model/archetype/language.d.ts → domain/language/structure.d.ts} +4 -3
  108. package/dist/{model/archetype/language.js → domain/language/structure.js} +33 -12
  109. package/dist/{model/archetype → domain/model}/data.d.ts +9 -6
  110. package/dist/{model/archetype → domain/model}/reach.d.ts +2 -0
  111. package/dist/{model/archetype → domain/model}/reach.js +10 -7
  112. package/dist/{model/archetype → domain/model}/resolve.d.ts +2 -0
  113. package/dist/{model/archetype → domain/model}/resolve.js +6 -1
  114. package/dist/{model/archetype → domain/model}/settings.d.ts +3 -3
  115. package/dist/domain/model/settings.js +83 -0
  116. package/dist/domain/model/text.d.ts +4 -0
  117. package/dist/domain/model/text.js +13 -0
  118. package/dist/{rules → domain/rules}/registry.d.ts +1 -0
  119. package/dist/{rules → domain/rules}/registry.js +11 -1
  120. package/dist/{rules → domain/rules}/rule.d.ts +11 -4
  121. package/dist/{rules → domain/rules}/rule.js +1 -1
  122. package/dist/index.d.ts +6 -6
  123. package/dist/index.js +2 -2
  124. package/package.json +1 -1
  125. package/dist/analysis/folders.d.ts +0 -2
  126. package/dist/analysis/specifiers.d.ts +0 -2
  127. package/dist/analysis/specifiers.js +0 -6
  128. package/dist/analysis/typescript.d.ts +0 -2
  129. package/dist/app/unclassified.d.ts +0 -5
  130. package/dist/app/unclassified.js +0 -13
  131. package/dist/builtins/dependency-direction/dependencyDirection.d.ts +0 -2
  132. package/dist/builtins/module-externals/moduleExternals.d.ts +0 -2
  133. package/dist/builtins/no-cross-module-reexport/noCrossModuleReexport.d.ts +0 -2
  134. package/dist/builtins/no-double-export/noDoubleExport.d.ts +0 -2
  135. package/dist/builtins/unresolved-import/unresolvedImport.d.ts +0 -2
  136. package/dist/builtins/unused-declaration/unusedDeclaration.d.ts +0 -2
  137. package/dist/model/archetype/rulesets.d.ts +0 -5
  138. package/dist/model/archetype/rulesets.js +0 -139
  139. package/dist/model/archetype/settings.js +0 -54
  140. /package/dist/{analysis → adapters/analysis}/complexity.js +0 -0
  141. /package/dist/{analysis → adapters/analysis}/folders.js +0 -0
  142. /package/dist/{git → adapters/git}/cache.js +0 -0
  143. /package/dist/{git → adapters/git}/repository.js +0 -0
  144. /package/dist/{config → adapters/git}/toolDigest.d.ts +0 -0
  145. /package/dist/{reporters → adapters/reporters}/agent.d.ts +0 -0
  146. /package/dist/{reporters → adapters/reporters}/agent.js +0 -0
  147. /package/dist/{reporters → adapters/reporters}/claudeHook.d.ts +0 -0
  148. /package/dist/{reporters → adapters/reporters}/claudeHook.js +0 -0
  149. /package/dist/{reporters → adapters/reporters}/index.d.ts +0 -0
  150. /package/dist/{reporters → adapters/reporters}/index.js +0 -0
  151. /package/dist/{reporters → adapters/reporters}/json.d.ts +0 -0
  152. /package/dist/{reporters → adapters/reporters}/json.js +0 -0
  153. /package/dist/{reporters → adapters/reporters}/model.js +0 -0
  154. /package/dist/{reporters → adapters/reporters}/omp.d.ts +0 -0
  155. /package/dist/{reporters → adapters/reporters}/omp.js +0 -0
  156. /package/dist/{reporters → adapters/reporters}/reporter.js +0 -0
  157. /package/dist/{reporters → adapters/reporters}/sarif.d.ts +0 -0
  158. /package/dist/{reporters → adapters/reporters}/sarif.js +0 -0
  159. /package/dist/{reporters → adapters/reporters}/text.js +0 -0
  160. /package/dist/builtins/module-cycle/{moduleCycle.js → rule.js} +0 -0
  161. /package/dist/builtins/no-cycle/{noCycle.js → rule.js} +0 -0
  162. /package/dist/{model → domain/errors}/configError.d.ts +0 -0
  163. /package/dist/{model → domain/errors}/configError.js +0 -0
  164. /package/dist/{model → domain/facts}/facts.js +0 -0
  165. /package/dist/{model/archetype → domain/model}/data.js +0 -0
  166. /package/dist/{model/archetype → domain/model}/shared.d.ts +0 -0
  167. /package/dist/{model/archetype → domain/model}/shared.js +0 -0
  168. /package/dist/{rules → domain/rules}/diff.d.ts +0 -0
  169. /package/dist/{rules → domain/rules}/diff.js +0 -0
package/README.md CHANGED
@@ -6,7 +6,7 @@ each may use, and warp reports the code that steps outside it:
6
6
  - imports outside a module's reach, including packages it doesn't list.
7
7
  - imports and re-exports that go around a module's surface.
8
8
  - import loops between files and between modules.
9
- - files and functions that are too large or too tangled to follow.
9
+ - files, classes and functions that are too large or too tangled to follow.
10
10
  - parts of the config that the code no longer uses.
11
11
 
12
12
  `warp check` judges the whole codebase, for CI. `warp diff` reports only what a change made new or worse, so it can
@@ -34,11 +34,8 @@ root:
34
34
  provides: [npm:vitest]
35
35
  modules:
36
36
  tests: { files: 'src/**/*.test.ts' }
37
-
38
- rulesets:
39
- default:
40
- extends: [warp:recommended]
41
- test: { function-cohesion: off }
37
+ ruleset: { rules: { function-cohesion: off } }
38
+ ruleset: [warp:recommended]
42
39
  ```
43
40
 
44
41
  Then check it, and after an edit, see what the edit broke:
@@ -66,7 +63,6 @@ new: src/utils/slug.ts:1 [dependency-direction] src/utils/slug.ts (root.utils sr
66
63
 
67
64
  ## Limits
68
65
 
69
- - warp follows static `import` and `export … from` declarations. Dependencies through `import()`, `require()` and
70
- `import x = require()` go unchecked.
71
- - A namespace import (`import * as x`) points at the module the specifier resolves to, a barrel included.
72
- - Each class method is checked as a function of its own, and a class is one member of its file.
66
+ - warp follows `import`, `export … from`, `import x = require()`, `import()`, `require()` and `import('…')` types
67
+ when the specifier is a string literal, a template literal without `${}` included. A specifier computed at runtime,
68
+ such as `import(name)` or `` import(`./${name}.ts`) ``, goes unchecked.
@@ -0,0 +1,4 @@
1
+ import type { Chain } from '../../domain/facts/facts.ts';
2
+ import type { Resolver } from './imports.ts';
3
+ import { type Source } from './source.ts';
4
+ export declare const fileChains: (source: Source, resolver: Resolver) => Chain[];
@@ -0,0 +1,97 @@
1
+ import { SyntaxKind } from 'typescript/ast';
2
+ import { isCallExpression, isHeritageClause, isIdentifier, isNonNullExpression, isParenthesizedExpression, isPropertyAccessExpression, isShorthandPropertyAssignment, } from 'typescript/ast/is';
3
+ import { SymbolFlags } from 'typescript/sync';
4
+ import { walk } from './source.js';
5
+ export const fileChains = (source, resolver) => {
6
+ const declared = declaredIn(source, resolver);
7
+ const chains = [];
8
+ walk(source.sourceFile, (node) => {
9
+ if (skipped(node))
10
+ return false;
11
+ if (isPropertyAccessExpression(node) && !isPropertyAccessExpression(node.parent)) {
12
+ const chain = chainOf(node, source, declared);
13
+ if (chain)
14
+ chains.push(chain);
15
+ }
16
+ if (isIdentifier(node) && !isPropertyAccessExpression(node.parent)) {
17
+ const root = declared.root(node);
18
+ if (root)
19
+ chains.push({ line: source.line(node), root, hops: [], call: isCalled(node) });
20
+ }
21
+ return undefined;
22
+ });
23
+ return chains;
24
+ };
25
+ const typeNodes = (node) => node.kind >= SyntaxKind.FirstTypeNode && node.kind <= SyntaxKind.LastTypeNode;
26
+ const skipped = (node) => typeNodes(node) ||
27
+ node.kind === SyntaxKind.ImportDeclaration ||
28
+ node.kind === SyntaxKind.ImportEqualsDeclaration ||
29
+ node.kind === SyntaxKind.ExportDeclaration ||
30
+ node.kind === SyntaxKind.ExportAssignment ||
31
+ node.kind === SyntaxKind.InterfaceDeclaration ||
32
+ node.kind === SyntaxKind.TypeAliasDeclaration ||
33
+ (isHeritageClause(node) && node.token === SyntaxKind.ImplementsKeyword);
34
+ const unwrap = (expression) => isParenthesizedExpression(expression) || isNonNullExpression(expression) ? unwrap(expression.expression) : expression;
35
+ const isCalled = (node) => isCallExpression(node.parent) && node.parent.expression === node;
36
+ const chainOf = (outer, source, declared) => {
37
+ const accesses = [];
38
+ let at = outer;
39
+ while (isPropertyAccessExpression(at)) {
40
+ accesses.unshift(at);
41
+ at = unwrap(at.expression);
42
+ }
43
+ const root = isIdentifier(at) ? declared.root(at) : undefined;
44
+ const hops = accesses.flatMap((access) => {
45
+ if (!isIdentifier(access.name))
46
+ return [];
47
+ const hop = declared.hop(access.name, root);
48
+ return hop ? [hop] : [];
49
+ });
50
+ if (!root && hops.length === 0)
51
+ return undefined;
52
+ return { line: source.line(outer), ...(root ? { root } : {}), hops, call: isCalled(outer) };
53
+ };
54
+ const declaredIn = (source, resolver) => {
55
+ const lookup = { source, resolver, tops: new Map() };
56
+ return { root: (identifier) => rootOf(lookup, identifier), hop: (name, root) => hopOf(lookup, name, root) };
57
+ };
58
+ const otherFile = ({ source, resolver }, symbol) => {
59
+ const file = symbol.declarations.map((declaration) => resolver.relative(declaration.path)).find(Boolean);
60
+ return file === source.file ? undefined : file;
61
+ };
62
+ const valueSymbol = ({ source, resolver }, identifier) => isShorthandPropertyAssignment(identifier.parent)
63
+ ? resolver.checker.getShorthandAssignmentValueSymbol(identifier.parent)
64
+ : source.symbolOf(identifier);
65
+ const rootOf = (lookup, identifier) => {
66
+ const { checker } = lookup.resolver;
67
+ const symbol = valueSymbol(lookup, identifier);
68
+ if (!symbol || !(symbol.flags & SymbolFlags.Alias))
69
+ return undefined;
70
+ const target = checker.getAliasedSymbol(symbol);
71
+ if (checker.isUnknownSymbol(target) || !(target.flags & SymbolFlags.Value))
72
+ return undefined;
73
+ const file = otherFile(lookup, target);
74
+ return file ? { file, name: isFileSymbol(target) ? '*' : target.name } : undefined;
75
+ };
76
+ const hopOf = (lookup, name, root) => {
77
+ const symbol = lookup.source.symbolOf(name);
78
+ const file = symbol && otherFile(lookup, symbol);
79
+ if (!symbol || !file)
80
+ return undefined;
81
+ const top = topOf(lookup, symbol);
82
+ if (top === symbol)
83
+ return { file, name: symbol.name };
84
+ if (top)
85
+ return { file, name: top.name, member: symbol.name };
86
+ return root && root.name !== '*' ? { file: root.file, name: root.name, member: symbol.name } : undefined;
87
+ };
88
+ const topOf = ({ tops }, symbol) => {
89
+ if (!tops.has(symbol)) {
90
+ let top = symbol;
91
+ for (let parent = top.getParent(); parent && !isFileSymbol(parent); parent = parent.getParent())
92
+ top = parent;
93
+ tops.set(symbol, top.name.startsWith('__') ? undefined : top);
94
+ }
95
+ return tops.get(symbol);
96
+ };
97
+ const isFileSymbol = (symbol) => symbol.declarations.some((declaration) => declaration.kind === SyntaxKind.SourceFile);
@@ -0,0 +1,3 @@
1
+ import type { ClassFacts } from '../../domain/facts/facts.ts';
2
+ import { type Source } from './source.ts';
3
+ export declare const classFacts: (source: Source) => ClassFacts[];
@@ -0,0 +1,109 @@
1
+ import { isArrowFunction, isClassDeclaration, isClassLikeDeclaration, isConstructorDeclaration, isFunctionDeclaration, isFunctionExpression, isGetAccessorDeclaration, isIdentifier, isMethodDeclaration, isPrivateIdentifier, isPropertyAccessExpression, isPropertyDeclaration, isSetAccessorDeclaration, isThisExpression, isVariableDeclaration, } from 'typescript/ast/is';
2
+ import { getCombinedModifierFlags } from 'typescript/ast/utils';
3
+ import { ModifierFlags, SymbolFlags } from 'typescript/sync';
4
+ import { symbolKey, walk } from './source.js';
5
+ export const classFacts = (source) => {
6
+ const facts = [];
7
+ walk(source.sourceFile, (node) => {
8
+ if (isClassLikeDeclaration(node))
9
+ facts.push(classOf(node, source));
10
+ });
11
+ return facts;
12
+ };
13
+ const classOf = (node, source) => {
14
+ const line = source.line(node);
15
+ const endLine = source.endLine(node);
16
+ return { name: className(node, source), line, endLine, lines: endLine - line + 1, members: classGraph(node, source) };
17
+ };
18
+ const className = (node, source) => {
19
+ if (node.name)
20
+ return node.name.text;
21
+ if (isVariableDeclaration(node.parent))
22
+ return node.parent.name.getText(source.sourceFile);
23
+ return isClassDeclaration(node) ? 'default' : '<anonymous class>';
24
+ };
25
+ const classGraph = (node, source) => {
26
+ const self = selfKey(node, source);
27
+ const parts = byKey(node.members.flatMap((element) => partsOf(element, source)));
28
+ const members = parts.map((part) => part.member);
29
+ const owner = new Map(parts.flatMap((part, index) => (part.key ? [[part.key, index]] : [])));
30
+ const links = [];
31
+ parts.forEach((part, index) => {
32
+ for (const touch of part.bodies.flatMap((body) => touches(body, self, source))) {
33
+ let other = owner.get(touch.key);
34
+ if (other === undefined && isField(touch.symbol)) {
35
+ other = members.push({ name: touch.symbol.name, kind: 'state', line: touch.line, endLine: touch.line }) - 1;
36
+ owner.set(touch.key, other);
37
+ }
38
+ if (other !== undefined && other !== index)
39
+ links.push([index, other]);
40
+ }
41
+ });
42
+ return { members, links };
43
+ };
44
+ const partsOf = (element, source) => {
45
+ if (isConstructorDeclaration(element)) {
46
+ return element.parameters.filter(isParameterProperty).map((parameter) => part(parameter, 'state', [], source));
47
+ }
48
+ if (isMethodDeclaration(element))
49
+ return element.body ? [part(element, 'function', [element.body], source)] : [];
50
+ if (isPropertyDeclaration(element)) {
51
+ const value = element.initializer ? [element.initializer] : [];
52
+ return [part(element, isFunctionValue(element.initializer) ? 'function' : 'state', value, source)];
53
+ }
54
+ if (isGetAccessorDeclaration(element) || isSetAccessorDeclaration(element)) {
55
+ return [part(element, 'state', element.body ? [element.body] : [], source)];
56
+ }
57
+ return [];
58
+ };
59
+ const part = (node, kind, bodies, source) => {
60
+ const name = node.name;
61
+ const key = isIdentifier(name) || isPrivateIdentifier(name) ? symbolKey(source.symbolOf(name)) : undefined;
62
+ return {
63
+ member: {
64
+ name: name.getText(source.sourceFile),
65
+ kind,
66
+ line: source.line(node),
67
+ endLine: source.endLine(node),
68
+ },
69
+ key,
70
+ bodies,
71
+ };
72
+ };
73
+ const byKey = (parts) => {
74
+ const kept = new Map();
75
+ return parts.filter((candidate) => {
76
+ const first = candidate.key ? kept.get(candidate.key) : undefined;
77
+ if (!first) {
78
+ if (candidate.key)
79
+ kept.set(candidate.key, candidate);
80
+ return true;
81
+ }
82
+ first.bodies.push(...candidate.bodies);
83
+ return false;
84
+ });
85
+ };
86
+ const touches = (body, self, source) => {
87
+ const found = [];
88
+ walk(body, (node) => {
89
+ if (node !== body && rebindsThis(node))
90
+ return false;
91
+ if (!isPropertyAccessExpression(node) || !isSelf(node.expression, self, source))
92
+ return;
93
+ const symbol = source.symbolOf(node.name);
94
+ const key = symbolKey(symbol);
95
+ if (symbol && key)
96
+ found.push({ key, symbol, line: source.line(node) });
97
+ });
98
+ return found;
99
+ };
100
+ const rebindsThis = (node) => isFunctionDeclaration(node) || isFunctionExpression(node) || isClassLikeDeclaration(node);
101
+ const isSelf = (expression, self, source) => isThisExpression(expression) ||
102
+ (self !== undefined && isIdentifier(expression) && symbolKey(source.symbolOf(expression)) === self);
103
+ const selfKey = (node, source) => {
104
+ const name = node.name ?? (isVariableDeclaration(node.parent) ? node.parent.name : undefined);
105
+ return name && isIdentifier(name) ? symbolKey(source.symbolOf(name)) : undefined;
106
+ };
107
+ const isField = (symbol) => (symbol.flags & SymbolFlags.PropertyOrAccessor) !== 0 && (symbol.flags & SymbolFlags.Method) === 0;
108
+ const isParameterProperty = (parameter) => (getCombinedModifierFlags(parameter) & ModifierFlags.ParameterPropertyModifier) !== 0;
109
+ const isFunctionValue = (value) => value !== undefined && (isArrowFunction(value) || isFunctionExpression(value));
@@ -1,4 +1,4 @@
1
1
  import type { Node } from 'typescript/ast';
2
- import type { Complexity } from '../model/facts.ts';
2
+ import type { Complexity } from '../../domain/facts/facts.ts';
3
3
  import { type Source } from './source.ts';
4
4
  export declare const cognitiveComplexity: (fn: Node, self: string | undefined, source: Source) => Complexity;
@@ -0,0 +1,2 @@
1
+ import type { Folders } from '../../domain/model/resolve.ts';
2
+ export declare const diskFolders: (root: string) => Folders;
@@ -1,7 +1,6 @@
1
- import type { Cache, CachedFacts, CachedFile, FactsStore } from './ports.ts';
1
+ import type { CachedFacts, CachedFile } from '../../app/ports.ts';
2
2
  export interface CurrentFiles {
3
3
  digest: string;
4
4
  hashes: ReadonlyMap<string, string>;
5
5
  }
6
- export declare const factsStore: (cache: Cache<CachedFacts>, toolDigest: string, projectPath: string) => FactsStore;
7
6
  export declare const freshFacts: (cached: CachedFacts | undefined, current: CurrentFiles) => Map<string, CachedFile>;
@@ -1,13 +1,3 @@
1
- export const factsStore = (cache, toolDigest, projectPath) => {
2
- const key = `${toolDigest}-facts-${projectPath.replaceAll('/', '_')}`;
3
- return {
4
- read: () => cache.get(key),
5
- write(facts) {
6
- cache.set(key, facts);
7
- cache.prune((other) => other.startsWith(`${toolDigest}-`));
8
- },
9
- };
10
- };
11
1
  export const freshFacts = (cached, current) => {
12
2
  if (!cached || cached.digest !== current.digest)
13
3
  return new Map();
@@ -1,11 +1,12 @@
1
1
  import { type Checker, type Program } from 'typescript/sync';
2
- import type { ExternalImport, ImportEdge, UnresolvedImport } from '../model/facts.ts';
2
+ import type { ExternalImport, ImportEdge, UnresolvedImport } from '../../domain/facts/facts.ts';
3
3
  import type { Source } from './source.ts';
4
4
  export interface Resolver {
5
5
  checker: Checker;
6
6
  program: Program;
7
7
  relative: (pathOrFileName: string) => string | undefined;
8
8
  exists: (fileName: string) => boolean;
9
+ requireTarget: (specifier: string, directory: string) => string | undefined;
9
10
  }
10
11
  export interface FileEdges {
11
12
  imports: ImportEdge[];
@@ -1,26 +1,29 @@
1
1
  import { SyntaxKind } from 'typescript/ast';
2
- import { isExportAssignment, isExportDeclaration, isIdentifier, isImportDeclaration, isNamedExports, isNamedImports, isNamespaceImport, isStringLiteralLikeNode, } from 'typescript/ast/is';
2
+ import { isExportAssignment, isExportDeclaration, isIdentifier, isImportDeclaration, isImportEqualsDeclaration, isNamedExports, isNamedImports, isNamespaceImport, isStringLiteralLikeNode, } from 'typescript/ast/is';
3
3
  import { SymbolFlags } from 'typescript/sync';
4
4
  import { isBuiltin } from 'node:module';
5
5
  import { dirname, resolve } from 'node:path';
6
+ import { inlineReferences, statementSpecifier } from './specifiers.js';
6
7
  export const fileEdges = (source, resolver) => {
7
- const exported = locallyExportedNames(source.sourceFile);
8
8
  const edges = { imports: [], externals: [], unresolved: [] };
9
- for (const statement of source.sourceFile.statements) {
10
- const declaration = moduleDeclaration(statement, exported);
11
- if (!declaration)
12
- continue;
13
- const resolved = resolver.program.getResolvedModuleFromModuleSpecifier(declaration.specifier, source.sourceFile.fileName);
14
- const via = resolved?.resolvedFileName ? resolver.relative(resolved.resolvedFileName) : undefined;
9
+ for (const reference of moduleReferences(source.sourceFile)) {
10
+ const { specifier } = reference;
11
+ const resolvedFileName = resolvedTarget(reference, source, resolver);
12
+ const via = resolvedFileName ? resolver.relative(resolvedFileName) : undefined;
15
13
  if (via)
16
- edges.imports.push(...statementEdges(statement, declaration, via, source, resolver));
17
- else if (isBare(declaration.specifier.text))
18
- edges.externals.push(externalImport(statement, declaration, source));
19
- else if (!resolved?.resolvedFileName && !namesFileOnDisk(declaration.specifier.text, source, resolver))
20
- edges.unresolved.push({ from: source.file, specifier: declaration.specifier.text, line: source.line(statement) });
14
+ edges.imports.push(...referenceEdges(reference, via, source, resolver));
15
+ else if (isBare(specifier.text))
16
+ edges.externals.push(externalImport(reference, source));
17
+ else if (!resolvedFileName && !namesFileOnDisk(specifier.text, source, resolver))
18
+ edges.unresolved.push({ from: source.file, specifier: specifier.text, line: source.line(reference.node) });
21
19
  }
22
20
  return edges;
23
21
  };
22
+ const resolvedTarget = (reference, source, resolver) => {
23
+ const { fileName } = source.sourceFile;
24
+ const resolved = resolver.program.getResolvedModuleFromModuleSpecifier(reference.specifier, fileName)?.resolvedFileName;
25
+ return (resolved ?? (reference.require ? resolver.requireTarget(reference.specifier.text, dirname(fileName)) : undefined));
26
+ };
24
27
  const namesFileOnDisk = (specifier, source, resolver) => resolver.exists(resolve(dirname(source.sourceFile.fileName), specifier));
25
28
  const isBare = (specifier) => !specifier.startsWith('.') && !specifier.startsWith('/');
26
29
  const packageOf = (specifier) => {
@@ -30,7 +33,7 @@ const packageOf = (specifier) => {
30
33
  const [name] = segments;
31
34
  return !name.includes(':') && isBuiltin(name) ? `node:${name}` : name;
32
35
  };
33
- const externalImport = (statement, declaration, source) => {
36
+ const externalImport = (declaration, source) => {
34
37
  const reExport = declaration.reExport || declaration.bindings.some((binding) => binding.reExport);
35
38
  return {
36
39
  from: source.file,
@@ -40,10 +43,10 @@ const externalImport = (statement, declaration, source) => {
40
43
  typeOnly: declaration.typeOnly ||
41
44
  (declaration.bindings.length > 0 && declaration.bindings.every((binding) => binding.typeOnly)),
42
45
  ...(reExport ? { reExport } : {}),
43
- line: source.line(statement),
46
+ line: source.line(declaration.node),
44
47
  };
45
48
  };
46
- const statementEdges = (statement, declaration, via, source, resolver) => {
49
+ const referenceEdges = (declaration, via, source, resolver) => {
47
50
  const edge = (to, names, typeOnly, reExport) => ({
48
51
  from: source.file,
49
52
  to,
@@ -52,26 +55,44 @@ const statementEdges = (statement, declaration, via, source, resolver) => {
52
55
  names,
53
56
  typeOnly,
54
57
  ...(reExport ? { reExport } : {}),
55
- line: source.line(statement),
58
+ line: source.line(declaration.node),
56
59
  });
57
60
  if (declaration.bindings.length === 0)
58
61
  return [edge(via, [], declaration.typeOnly, declaration.reExport)];
59
62
  const byTarget = new Map();
60
63
  for (const binding of declaration.bindings) {
61
64
  const target = resolveBinding(binding, source, resolver) ?? { file: via, typeOnly: false };
62
- const key = `${target.file}\0${binding.reExport}`;
63
- const entry = byTarget.get(key) ?? { to: target.file, names: [], typeOnly: true, reExport: binding.reExport };
64
- entry.names.push(binding.identifier.text);
65
- entry.typeOnly &&= declaration.typeOnly || binding.typeOnly || target.typeOnly;
65
+ const typeOnly = declaration.typeOnly || binding.typeOnly || target.typeOnly;
66
+ const key = `${target.file}\0${binding.reExport}\0${typeOnly}`;
67
+ const entry = byTarget.get(key) ?? { to: target.file, names: [], typeOnly, reExport: binding.reExport };
68
+ entry.names.push(target.name ?? binding.identifier.text);
66
69
  byTarget.set(key, entry);
67
70
  }
68
71
  return [...byTarget.values()].map(({ to, names, typeOnly, reExport }) => edge(to, names, typeOnly, reExport));
69
72
  };
73
+ const moduleReferences = (sourceFile) => {
74
+ const exported = locallyExportedNames(sourceFile);
75
+ const statements = sourceFile.statements.flatMap((statement) => {
76
+ const declaration = moduleDeclaration(statement, exported);
77
+ return declaration ? [{ ...declaration, node: statement, require: false }] : [];
78
+ });
79
+ const inline = inlineReferences(sourceFile).map(({ node, specifier, typeOnly, require }) => ({
80
+ node,
81
+ specifier,
82
+ typeOnly,
83
+ require,
84
+ reExport: false,
85
+ bindings: [],
86
+ }));
87
+ return [...statements, ...inline];
88
+ };
70
89
  const moduleDeclaration = (statement, exported) => isImportDeclaration(statement)
71
90
  ? importStatement(statement, exported)
72
91
  : isExportDeclaration(statement)
73
92
  ? reExportStatement(statement)
74
- : undefined;
93
+ : isImportEqualsDeclaration(statement)
94
+ ? importEqualsStatement(statement, exported)
95
+ : undefined;
75
96
  const locallyExportedNames = (sourceFile) => {
76
97
  const names = new Set();
77
98
  for (const statement of sourceFile.statements) {
@@ -115,6 +136,19 @@ const importStatement = (statement, exported) => {
115
136
  bindings,
116
137
  };
117
138
  };
139
+ const importEqualsStatement = (statement, exported) => {
140
+ const specifier = statementSpecifier(statement);
141
+ if (!specifier || !isIdentifier(statement.name))
142
+ return undefined;
143
+ const reExport = exported.has(statement.name.text) ||
144
+ (statement.modifiers?.some((modifier) => modifier.kind === SyntaxKind.ExportKeyword) ?? false);
145
+ return {
146
+ specifier,
147
+ typeOnly: statement.isTypeOnly,
148
+ reExport: false,
149
+ bindings: [{ identifier: statement.name, typeOnly: false, reExport }],
150
+ };
151
+ };
118
152
  const reExportStatement = (statement) => {
119
153
  if (!statement.moduleSpecifier || !isStringLiteralLikeNode(statement.moduleSpecifier))
120
154
  return undefined;
@@ -135,5 +169,8 @@ const resolveBinding = (binding, source, resolver) => {
135
169
  .find((path) => path !== undefined);
136
170
  if (!file)
137
171
  return undefined;
138
- return { file, typeOnly: (target.flags & SymbolFlags.Value) === 0 };
172
+ const name = target.declarations.some((declaration) => declaration.kind === SyntaxKind.SourceFile)
173
+ ? '*'
174
+ : target.name;
175
+ return { file, typeOnly: (target.flags & SymbolFlags.Value) === 0, name };
139
176
  };
@@ -1,4 +1,4 @@
1
- import type { FunctionFacts, MemberGraph } from '../model/facts.ts';
1
+ import type { FunctionFacts, MemberGraph } from '../../domain/facts/facts.ts';
2
2
  import { type Source } from './source.ts';
3
3
  export declare const fileMembers: (source: Source) => MemberGraph;
4
4
  export declare const functionFacts: (source: Source) => FunctionFacts[];
@@ -87,7 +87,7 @@ const references = (node, source) => {
87
87
  if (key)
88
88
  keys.add(key);
89
89
  }
90
- if (isPropertyAccessExpression(child) && isThisExpression(child.expression) && isIdentifier(child.name)) {
90
+ if (isPropertyAccessExpression(child) && isThisExpression(child.expression)) {
91
91
  keys.add(`this.${child.name.text}`);
92
92
  }
93
93
  });
@@ -1,9 +1,9 @@
1
- import type { Identifier, Node, SourceFile } from 'typescript/ast';
1
+ import type { Identifier, Node, PrivateIdentifier, SourceFile } from 'typescript/ast';
2
2
  import type { Checker, Symbol } from 'typescript/sync';
3
3
  export interface Source {
4
4
  file: string;
5
5
  sourceFile: SourceFile;
6
- symbolOf: (identifier: Identifier) => Symbol | undefined;
6
+ symbolOf: (identifier: Identifier | PrivateIdentifier) => Symbol | undefined;
7
7
  line: (node: Node) => number;
8
8
  endLine: (node: Node) => number;
9
9
  }
@@ -1,8 +1,8 @@
1
- import { isIdentifier } from 'typescript/ast/is';
1
+ import { isIdentifier, isPrivateIdentifier } from 'typescript/ast/is';
2
2
  export const readSource = (checker, sourceFile, file) => {
3
3
  const identifiers = [];
4
4
  walk(sourceFile, (node) => {
5
- if (isIdentifier(node))
5
+ if (isIdentifier(node) || isPrivateIdentifier(node))
6
6
  identifiers.push(node);
7
7
  });
8
8
  const symbols = identifiers.length > 0 ? checker.getSymbolAtLocation(identifiers) : [];
@@ -0,0 +1,10 @@
1
+ import type { Node, SourceFile, StringLiteralLikeNode } from 'typescript/ast';
2
+ export interface InlineReference {
3
+ node: Node;
4
+ specifier: StringLiteralLikeNode;
5
+ typeOnly: boolean;
6
+ require: boolean;
7
+ }
8
+ export declare const inlineReferences: (sourceFile: SourceFile) => InlineReference[];
9
+ export declare const statementSpecifier: (node: Node) => StringLiteralLikeNode | undefined;
10
+ export declare const moduleSpecifiers: (sourceFile: SourceFile) => string[];
@@ -0,0 +1,39 @@
1
+ import { SyntaxKind } from 'typescript/ast';
2
+ import { isCallExpression, isExportDeclaration, isExternalModuleReference, isIdentifier, isImportDeclaration, isImportEqualsDeclaration, isImportTypeNode, isLiteralTypeNode, isStringLiteralLikeNode, } from 'typescript/ast/is';
3
+ import { walk } from './source.js';
4
+ export const inlineReferences = (sourceFile) => {
5
+ const found = [];
6
+ walk(sourceFile, (node) => {
7
+ const reference = inlineReference(node);
8
+ if (reference)
9
+ found.push(reference);
10
+ });
11
+ return found;
12
+ };
13
+ const inlineReference = (node) => {
14
+ if (isImportTypeNode(node))
15
+ return isLiteralTypeNode(node.argument) && isStringLiteralLikeNode(node.argument.literal)
16
+ ? { node, specifier: node.argument.literal, typeOnly: true, require: false }
17
+ : undefined;
18
+ if (!isCallExpression(node))
19
+ return undefined;
20
+ const [specifier] = node.arguments;
21
+ if (!specifier || !isStringLiteralLikeNode(specifier))
22
+ return undefined;
23
+ if (node.expression.kind === SyntaxKind.ImportKeyword)
24
+ return { node, specifier, typeOnly: false, require: false };
25
+ const isRequire = isIdentifier(node.expression) && node.expression.text === 'require' && node.arguments.length === 1;
26
+ return isRequire ? { node, specifier, typeOnly: false, require: true } : undefined;
27
+ };
28
+ export const statementSpecifier = (node) => {
29
+ const specifier = isImportDeclaration(node) || isExportDeclaration(node)
30
+ ? node.moduleSpecifier
31
+ : isImportEqualsDeclaration(node) && isExternalModuleReference(node.moduleReference)
32
+ ? node.moduleReference.expression
33
+ : undefined;
34
+ return specifier && isStringLiteralLikeNode(specifier) ? specifier : undefined;
35
+ };
36
+ export const moduleSpecifiers = (sourceFile) => [
37
+ ...sourceFile.statements.flatMap((statement) => statementSpecifier(statement)?.text ?? []),
38
+ ...inlineReferences(sourceFile).map((reference) => reference.specifier.text),
39
+ ];
@@ -0,0 +1,2 @@
1
+ import type { Analyzer } from '../../app/ports.ts';
2
+ export declare const typescriptAnalyzer: Analyzer;
@@ -1,11 +1,13 @@
1
1
  import { createHash } from 'node:crypto';
2
2
  import { existsSync, readFileSync, statSync } from 'node:fs';
3
3
  import { join, relative, sep } from 'node:path';
4
- import { API } from 'typescript/sync';
5
- import { freshFacts } from '../app/facts.js';
4
+ import { API, ModuleKind } from 'typescript/sync';
5
+ import { freshFacts } from './freshFacts.js';
6
6
  import { fileEdges } from './imports.js';
7
7
  import { moduleSpecifiers } from './specifiers.js';
8
8
  import { countLines, readSource } from './source.js';
9
+ import { fileChains } from './chains.js';
10
+ import { classFacts } from './classes.js';
9
11
  import { functionFacts, fileMembers } from './members.js';
10
12
  export const typescriptAnalyzer = {
11
13
  analyze(root, tsconfig, store) {
@@ -24,7 +26,7 @@ export const typescriptAnalyzer = {
24
26
  const fresh = freshFacts(store?.read(), { digest, hashes });
25
27
  const cached = new Map();
26
28
  for (const { project, files } of projects)
27
- for (const [file, entry] of analyzeProgram(project, files, fresh, cached))
29
+ for (const [file, entry] of analyzeProgram(api, project, files, fresh, cached))
28
30
  cached.set(file, entry);
29
31
  const facts = [...cached.values()].flatMap(({ facts }) => (facts ? [facts] : []));
30
32
  if (facts.length === 0)
@@ -65,16 +67,22 @@ const digestOf = (root, projects) => {
65
67
  }
66
68
  return digest.digest('hex').slice(0, 16);
67
69
  };
68
- const analyzeProgram = ({ program, checker }, files, fresh, done) => {
70
+ const analyzeProgram = (api, { program, checker }, files, fresh, done) => {
69
71
  const byPath = new Map([...files].flatMap(([fileName, file]) => [
70
72
  [fileName, file],
71
73
  [fileName.toLowerCase(), file],
72
74
  ]));
75
+ let moduleResolver;
73
76
  const resolver = {
74
77
  program,
75
78
  checker,
76
79
  relative: (path) => byPath.get(path) ?? byPath.get(path.toLowerCase()),
77
80
  exists: existsSync,
81
+ requireTarget: (specifier, directory) => {
82
+ moduleResolver ??= api.createModuleResolver(program.getCompilerOptions());
83
+ return moduleResolver.resolveModuleName(specifier, directory, ModuleKind.CommonJS).resolvedModule
84
+ ?.resolvedFileName;
85
+ },
78
86
  };
79
87
  const analyzed = new Map();
80
88
  for (const [fileName, file] of files) {
@@ -95,6 +103,8 @@ const analyzeProgram = ({ program, checker }, files, fresh, done) => {
95
103
  ...fileEdges(source, resolver),
96
104
  members: fileMembers(source),
97
105
  functions: functionFacts(source),
106
+ classes: classFacts(source),
107
+ chains: fileChains(source, resolver),
98
108
  };
99
109
  analyzed.set(file, { specifiers: moduleSpecifiers(sourceFile), facts });
100
110
  }
@@ -1,13 +1,8 @@
1
+ import type { ConfigSource } from '../../app/ports.ts';
1
2
  export declare const configName = "warp.yaml";
2
3
  export declare const findConfig: (start: string, exists?: (path: string) => boolean) => string;
3
- export interface LoadedConfig {
4
- raw: unknown;
5
- plugins: Record<string, unknown>;
6
- lines: ReadonlyMap<string, number>;
7
- digest: string;
8
- }
9
4
  export declare const parseConfig: (text: string, file: string) => {
10
5
  raw: unknown;
11
6
  lines: Map<string, number>;
12
7
  };
13
- export declare const loadConfig: (file: string) => Promise<LoadedConfig>;
8
+ export declare const yamlConfig: (file: string) => ConfigSource;
@@ -21,7 +21,7 @@ import { createRequire, registerHooks } from 'node:module';
21
21
  import { basename, dirname, join, resolve } from 'node:path';
22
22
  import { pathToFileURL } from 'node:url';
23
23
  import { isAlias, isMap, isNode, isPair, isScalar, parseDocument, visit } from 'yaml';
24
- import { ConfigError } from '../model/configError.js';
24
+ import { ConfigError } from '../../domain/errors/configError.js';
25
25
  export const configName = 'warp.yaml';
26
26
  export const findConfig = (start, exists = existsSync) => {
27
27
  for (let dir = start;; dir = dirname(dir)) {
@@ -97,9 +97,10 @@ const top = (key) => {
97
97
  return { inside: moduleAt };
98
98
  return { path: key, inside: plain(key) };
99
99
  };
100
- const entry = new URL(import.meta.url.endsWith('.ts') ? '../index.ts' : '../index.js', import.meta.url).href;
101
- const ownFiles = new URL('../../', import.meta.url).href;
102
- export const loadConfig = async (file) => {
100
+ const entry = new URL(import.meta.url.endsWith('.ts') ? '../../index.ts' : '../../index.js', import.meta.url).href;
101
+ const ownFiles = new URL('../../../', import.meta.url).href;
102
+ export const yamlConfig = (file) => ({ file: basename(file), read: () => readConfig(file) });
103
+ const readConfig = async (file) => {
103
104
  const text = readFileSync(file, 'utf8');
104
105
  const digest = createHash('sha256').update(text);
105
106
  const name = basename(file);