@yearofthedan/warp 0.4.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -7
- package/dist/{analysis → adapters/analysis}/complexity.d.ts +1 -1
- package/dist/adapters/analysis/folders.d.ts +2 -0
- package/dist/{app/facts.d.ts → adapters/analysis/freshFacts.d.ts} +1 -2
- package/dist/{app/facts.js → adapters/analysis/freshFacts.js} +0 -10
- package/dist/{analysis → adapters/analysis}/imports.d.ts +2 -1
- package/dist/{analysis → adapters/analysis}/imports.js +52 -18
- package/dist/{analysis → adapters/analysis}/members.d.ts +1 -1
- package/dist/adapters/analysis/specifiers.d.ts +10 -0
- package/dist/adapters/analysis/specifiers.js +39 -0
- package/dist/adapters/analysis/typescript.d.ts +2 -0
- package/dist/{analysis → adapters/analysis}/typescript.js +10 -4
- package/dist/{config → adapters/config}/loadConfig.d.ts +2 -7
- package/dist/{config → adapters/config}/loadConfig.js +5 -4
- package/dist/{git → adapters/git}/cache.d.ts +1 -1
- package/dist/adapters/git/factsStore.d.ts +2 -0
- package/dist/adapters/git/factsStore.js +10 -0
- package/dist/{git → adapters/git}/repository.d.ts +1 -1
- package/dist/{config → adapters/git}/toolDigest.js +1 -1
- package/dist/{reporters → adapters/reporters}/model.d.ts +2 -2
- package/dist/{reporters → adapters/reporters}/reporter.d.ts +2 -2
- package/dist/{reporters → adapters/reporters}/text.d.ts +2 -2
- package/dist/app/check.d.ts +5 -6
- package/dist/app/check.js +3 -3
- package/dist/app/checkModel.d.ts +5 -5
- package/dist/app/checkModel.js +4 -5
- package/dist/app/configLines.d.ts +1 -1
- package/dist/app/diff.d.ts +1 -1
- package/dist/app/diff.js +1 -1
- package/dist/app/failures.d.ts +6 -0
- package/dist/app/failures.js +15 -0
- package/dist/app/load.d.ts +11 -5
- package/dist/app/load.js +22 -59
- package/dist/app/ports.d.ts +13 -3
- package/dist/builtins/advice.d.ts +4 -4
- package/dist/builtins/advice.js +1 -1
- package/dist/builtins/cognitive-complexity/{cognitiveComplexity.d.ts → rule.d.ts} +1 -1
- package/dist/builtins/cognitive-complexity/{cognitiveComplexity.js → rule.js} +1 -1
- package/dist/builtins/components.d.ts +1 -1
- package/dist/builtins/dependency-direction/rule.d.ts +2 -0
- package/dist/builtins/dependency-direction/{dependencyDirection.js → rule.js} +3 -3
- package/dist/builtins/file-cohesion/{fileCohesion.d.ts → rule.d.ts} +1 -1
- package/dist/builtins/file-cohesion/{fileCohesion.js → rule.js} +1 -1
- package/dist/builtins/function-cohesion/{functionCohesion.d.ts → rule.d.ts} +1 -1
- package/dist/builtins/function-cohesion/{functionCohesion.js → rule.js} +1 -1
- package/dist/builtins/index.d.ts +2 -2
- package/dist/builtins/index.js +18 -13
- package/dist/builtins/loops.d.ts +1 -1
- package/dist/builtins/loops.js +1 -1
- package/dist/builtins/module-cycle/{moduleCycle.d.ts → rule.d.ts} +1 -1
- package/dist/builtins/module-externals/rule.d.ts +2 -0
- package/dist/builtins/module-externals/{moduleExternals.js → rule.js} +3 -3
- package/dist/builtins/no-cross-module-reexport/rule.d.ts +2 -0
- package/dist/builtins/no-cross-module-reexport/{noCrossModuleReexport.js → rule.js} +1 -1
- package/dist/builtins/no-cycle/{noCycle.d.ts → rule.d.ts} +1 -1
- package/dist/builtins/no-double-export/rule.d.ts +2 -0
- package/dist/builtins/no-double-export/{noDoubleExport.js → rule.js} +1 -1
- package/dist/builtins/packages.d.ts +1 -1
- package/dist/builtins/place.d.ts +1 -1
- package/dist/builtins/shared.d.ts +1 -1
- package/dist/builtins/unclassified/rule.d.ts +2 -0
- package/dist/builtins/unclassified/rule.js +15 -0
- package/dist/builtins/unresolved-import/rule.d.ts +2 -0
- package/dist/builtins/unresolved-import/{unresolvedImport.js → rule.js} +1 -1
- package/dist/builtins/unused-declaration/declarations.d.ts +3 -3
- package/dist/builtins/unused-declaration/empty.d.ts +1 -1
- package/dist/builtins/unused-declaration/empty.js +1 -1
- package/dist/builtins/unused-declaration/grants.d.ts +1 -1
- package/dist/builtins/unused-declaration/grants.js +42 -10
- package/dist/builtins/unused-declaration/rule.d.ts +2 -0
- package/dist/builtins/unused-declaration/{unusedDeclaration.js → rule.js} +1 -1
- package/dist/builtins/unused-declaration/surfaces.d.ts +1 -1
- package/dist/builtins/unused-declaration/surfaces.js +1 -1
- package/dist/cli.js +34 -54
- package/dist/{model/archetype → domain/fixes}/fixes.d.ts +2 -2
- package/dist/{model/archetype → domain/fixes}/fixes.js +2 -6
- package/dist/domain/language/compile.d.ts +11 -0
- package/dist/domain/language/compile.js +58 -0
- package/dist/{model/archetype → domain/language}/references.d.ts +3 -6
- package/dist/{model/archetype → domain/language}/references.js +16 -21
- package/dist/{model/archetype → domain/language}/requirements.d.ts +3 -3
- package/dist/{model/archetype → domain/language}/requirements.js +2 -1
- package/dist/domain/language/rulesets.d.ts +10 -0
- package/dist/domain/language/rulesets.js +60 -0
- package/dist/{model/archetype/language.d.ts → domain/language/structure.d.ts} +4 -3
- package/dist/{model/archetype/language.js → domain/language/structure.js} +32 -12
- package/dist/{model/archetype → domain/model}/data.d.ts +9 -6
- package/dist/{model/archetype → domain/model}/reach.d.ts +1 -0
- package/dist/{model/archetype → domain/model}/reach.js +3 -7
- package/dist/{model/archetype → domain/model}/resolve.d.ts +2 -0
- package/dist/{model/archetype → domain/model}/resolve.js +6 -1
- package/dist/{model/archetype → domain/model}/settings.d.ts +0 -1
- package/dist/domain/model/settings.js +82 -0
- package/dist/domain/model/text.d.ts +4 -0
- package/dist/domain/model/text.js +13 -0
- package/dist/{rules → domain/rules}/registry.d.ts +1 -0
- package/dist/{rules → domain/rules}/registry.js +10 -0
- package/dist/{rules → domain/rules}/rule.d.ts +10 -4
- package/dist/index.d.ts +6 -6
- package/dist/index.js +2 -2
- package/package.json +1 -1
- package/dist/analysis/folders.d.ts +0 -2
- package/dist/analysis/specifiers.d.ts +0 -2
- package/dist/analysis/specifiers.js +0 -6
- package/dist/analysis/typescript.d.ts +0 -2
- package/dist/app/unclassified.d.ts +0 -5
- package/dist/app/unclassified.js +0 -13
- package/dist/builtins/dependency-direction/dependencyDirection.d.ts +0 -2
- package/dist/builtins/module-externals/moduleExternals.d.ts +0 -2
- package/dist/builtins/no-cross-module-reexport/noCrossModuleReexport.d.ts +0 -2
- package/dist/builtins/no-double-export/noDoubleExport.d.ts +0 -2
- package/dist/builtins/unresolved-import/unresolvedImport.d.ts +0 -2
- package/dist/builtins/unused-declaration/unusedDeclaration.d.ts +0 -2
- package/dist/model/archetype/rulesets.d.ts +0 -5
- package/dist/model/archetype/rulesets.js +0 -139
- package/dist/model/archetype/settings.js +0 -54
- /package/dist/{analysis → adapters/analysis}/complexity.js +0 -0
- /package/dist/{analysis → adapters/analysis}/folders.js +0 -0
- /package/dist/{analysis → adapters/analysis}/members.js +0 -0
- /package/dist/{analysis → adapters/analysis}/source.d.ts +0 -0
- /package/dist/{analysis → adapters/analysis}/source.js +0 -0
- /package/dist/{git → adapters/git}/cache.js +0 -0
- /package/dist/{git → adapters/git}/repository.js +0 -0
- /package/dist/{config → adapters/git}/toolDigest.d.ts +0 -0
- /package/dist/{reporters → adapters/reporters}/agent.d.ts +0 -0
- /package/dist/{reporters → adapters/reporters}/agent.js +0 -0
- /package/dist/{reporters → adapters/reporters}/claudeHook.d.ts +0 -0
- /package/dist/{reporters → adapters/reporters}/claudeHook.js +0 -0
- /package/dist/{reporters → adapters/reporters}/index.d.ts +0 -0
- /package/dist/{reporters → adapters/reporters}/index.js +0 -0
- /package/dist/{reporters → adapters/reporters}/json.d.ts +0 -0
- /package/dist/{reporters → adapters/reporters}/json.js +0 -0
- /package/dist/{reporters → adapters/reporters}/model.js +0 -0
- /package/dist/{reporters → adapters/reporters}/omp.d.ts +0 -0
- /package/dist/{reporters → adapters/reporters}/omp.js +0 -0
- /package/dist/{reporters → adapters/reporters}/reporter.js +0 -0
- /package/dist/{reporters → adapters/reporters}/sarif.d.ts +0 -0
- /package/dist/{reporters → adapters/reporters}/sarif.js +0 -0
- /package/dist/{reporters → adapters/reporters}/text.js +0 -0
- /package/dist/builtins/module-cycle/{moduleCycle.js → rule.js} +0 -0
- /package/dist/builtins/no-cycle/{noCycle.js → rule.js} +0 -0
- /package/dist/{model → domain/facts}/facts.d.ts +0 -0
- /package/dist/{model → domain/facts}/facts.js +0 -0
- /package/dist/{model → domain/model}/configError.d.ts +0 -0
- /package/dist/{model → domain/model}/configError.js +0 -0
- /package/dist/{model/archetype → domain/model}/data.js +0 -0
- /package/dist/{model/archetype → domain/model}/shared.d.ts +0 -0
- /package/dist/{model/archetype → domain/model}/shared.js +0 -0
- /package/dist/{rules → domain/rules}/diff.d.ts +0 -0
- /package/dist/{rules → domain/rules}/diff.js +0 -0
- /package/dist/{rules → domain/rules}/rule.js +0 -0
package/README.md
CHANGED
|
@@ -34,11 +34,8 @@ root:
|
|
|
34
34
|
provides: [npm:vitest]
|
|
35
35
|
modules:
|
|
36
36
|
tests: { files: 'src/**/*.test.ts' }
|
|
37
|
-
|
|
38
|
-
|
|
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,8 @@ 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
|
|
70
|
-
|
|
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.
|
|
71
69
|
- A namespace import (`import * as x`) points at the module the specifier resolves to, a barrel included.
|
|
72
70
|
- Each class method is checked as a function of its own, and a class is one member of its file.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { Node } from 'typescript/ast';
|
|
2
|
-
import type { Complexity } from '
|
|
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;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type {
|
|
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 '
|
|
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
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
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(...
|
|
17
|
-
else if (isBare(
|
|
18
|
-
edges.externals.push(externalImport(
|
|
19
|
-
else if (!
|
|
20
|
-
edges.unresolved.push({ from: source.file, specifier:
|
|
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 = (
|
|
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(
|
|
46
|
+
line: source.line(declaration.node),
|
|
44
47
|
};
|
|
45
48
|
};
|
|
46
|
-
const
|
|
49
|
+
const referenceEdges = (declaration, via, source, resolver) => {
|
|
47
50
|
const edge = (to, names, typeOnly, reExport) => ({
|
|
48
51
|
from: source.file,
|
|
49
52
|
to,
|
|
@@ -52,7 +55,7 @@ const statementEdges = (statement, declaration, via, source, resolver) => {
|
|
|
52
55
|
names,
|
|
53
56
|
typeOnly,
|
|
54
57
|
...(reExport ? { reExport } : {}),
|
|
55
|
-
line: source.line(
|
|
58
|
+
line: source.line(declaration.node),
|
|
56
59
|
});
|
|
57
60
|
if (declaration.bindings.length === 0)
|
|
58
61
|
return [edge(via, [], declaration.typeOnly, declaration.reExport)];
|
|
@@ -67,11 +70,29 @@ const statementEdges = (statement, declaration, via, source, resolver) => {
|
|
|
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
|
-
:
|
|
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;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { FunctionFacts, MemberGraph } from '
|
|
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[];
|
|
@@ -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
|
+
];
|
|
@@ -1,8 +1,8 @@
|
|
|
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 '
|
|
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';
|
|
@@ -24,7 +24,7 @@ export const typescriptAnalyzer = {
|
|
|
24
24
|
const fresh = freshFacts(store?.read(), { digest, hashes });
|
|
25
25
|
const cached = new Map();
|
|
26
26
|
for (const { project, files } of projects)
|
|
27
|
-
for (const [file, entry] of analyzeProgram(project, files, fresh, cached))
|
|
27
|
+
for (const [file, entry] of analyzeProgram(api, project, files, fresh, cached))
|
|
28
28
|
cached.set(file, entry);
|
|
29
29
|
const facts = [...cached.values()].flatMap(({ facts }) => (facts ? [facts] : []));
|
|
30
30
|
if (facts.length === 0)
|
|
@@ -65,16 +65,22 @@ const digestOf = (root, projects) => {
|
|
|
65
65
|
}
|
|
66
66
|
return digest.digest('hex').slice(0, 16);
|
|
67
67
|
};
|
|
68
|
-
const analyzeProgram = ({ program, checker }, files, fresh, done) => {
|
|
68
|
+
const analyzeProgram = (api, { program, checker }, files, fresh, done) => {
|
|
69
69
|
const byPath = new Map([...files].flatMap(([fileName, file]) => [
|
|
70
70
|
[fileName, file],
|
|
71
71
|
[fileName.toLowerCase(), file],
|
|
72
72
|
]));
|
|
73
|
+
let moduleResolver;
|
|
73
74
|
const resolver = {
|
|
74
75
|
program,
|
|
75
76
|
checker,
|
|
76
77
|
relative: (path) => byPath.get(path) ?? byPath.get(path.toLowerCase()),
|
|
77
78
|
exists: existsSync,
|
|
79
|
+
requireTarget: (specifier, directory) => {
|
|
80
|
+
moduleResolver ??= api.createModuleResolver(program.getCompilerOptions());
|
|
81
|
+
return moduleResolver.resolveModuleName(specifier, directory, ModuleKind.CommonJS).resolvedModule
|
|
82
|
+
?.resolvedFileName;
|
|
83
|
+
},
|
|
78
84
|
};
|
|
79
85
|
const analyzed = new Map();
|
|
80
86
|
for (const [fileName, file] of files) {
|
|
@@ -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
|
|
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 '
|
|
24
|
+
import { ConfigError } from '../../domain/model/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') ? '
|
|
101
|
-
const ownFiles = new URL('
|
|
102
|
-
export const
|
|
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);
|
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
};
|
|
@@ -3,7 +3,7 @@ import { readdirSync, readFileSync } from 'node:fs';
|
|
|
3
3
|
import { createRequire } from 'node:module';
|
|
4
4
|
import { join } from 'node:path';
|
|
5
5
|
import { fileURLToPath } from 'node:url';
|
|
6
|
-
const sourceDir = fileURLToPath(new URL('
|
|
6
|
+
const sourceDir = fileURLToPath(new URL('../../', import.meta.url));
|
|
7
7
|
const code = /(?<!\.test)\.(ts|js)$/;
|
|
8
8
|
export const toolDigest = () => {
|
|
9
9
|
const digest = createHash('sha256').update(createRequire(import.meta.url)('typescript/package.json').version);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Model } from '
|
|
2
|
-
import type { RulesOf } from '
|
|
1
|
+
import type { Model } from '../../domain/model/resolve.ts';
|
|
2
|
+
import type { RulesOf } from '../../domain/model/settings.ts';
|
|
3
3
|
export declare const modelText: (model: Model, rules?: RulesOf) => string;
|
|
4
4
|
export declare const modelJson: (model: Model, rules?: RulesOf) => string;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Delta } from '
|
|
2
|
-
import type { Finding } from '
|
|
1
|
+
import type { Delta } from '../../domain/rules/diff.ts';
|
|
2
|
+
import type { Finding } from '../../domain/rules/rule.ts';
|
|
3
3
|
export type Report = {
|
|
4
4
|
mode: 'check';
|
|
5
5
|
findings: Finding[];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Delta } from '
|
|
2
|
-
import type { Finding } from '
|
|
1
|
+
import type { Delta } from '../../domain/rules/diff.ts';
|
|
2
|
+
import type { Finding } from '../../domain/rules/rule.ts';
|
|
3
3
|
import type { Reporter } from './reporter.ts';
|
|
4
4
|
export declare const textReporter: Reporter;
|
|
5
5
|
export declare const diffText: (delta: Delta) => string;
|
package/dist/app/check.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { ConfigData } from '../model/
|
|
1
|
+
import type { ConfigData } from '../domain/model/data.ts';
|
|
2
2
|
import { type ConfigLines } from './configLines.ts';
|
|
3
|
-
import { type Model } from '../model/
|
|
4
|
-
import type { Project } from '../
|
|
5
|
-
import type { Finding, Rule } from '../rules/rule.ts';
|
|
3
|
+
import { type Model } from '../domain/model/resolve.ts';
|
|
4
|
+
import type { Project } from '../domain/facts/facts.ts';
|
|
5
|
+
import type { Finding, Rule } from '../domain/rules/rule.ts';
|
|
6
6
|
import type { Analyzer, FactsStore, FoldersAt } from './ports.ts';
|
|
7
7
|
export interface ProjectCheck {
|
|
8
8
|
config: unknown;
|
|
@@ -18,8 +18,7 @@ export declare const modelProject: ({ analyzer, foldersAt, facts }: Adapters, ro
|
|
|
18
18
|
model: Model;
|
|
19
19
|
project: Project;
|
|
20
20
|
};
|
|
21
|
-
export declare const projectCheck: (adapters: Adapters, config: ConfigData, { rules,
|
|
21
|
+
export declare const projectCheck: (adapters: Adapters, config: ConfigData, { rules, lines }?: {
|
|
22
22
|
rules?: readonly Rule[];
|
|
23
|
-
unclassifiedDocs?: string;
|
|
24
23
|
lines?: ConfigLines;
|
|
25
24
|
}) => ProjectCheck;
|
package/dist/app/check.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import { checkModel } from './checkModel.js';
|
|
2
2
|
import { placeFindings } from './configLines.js';
|
|
3
|
-
import { resolveModel } from '../model/
|
|
3
|
+
import { resolveModel } from '../domain/model/resolve.js';
|
|
4
4
|
export const modelProject = ({ analyzer, foldersAt, facts }, root, config) => {
|
|
5
5
|
const analyzed = analyzer.analyze(root, config.tsconfig, facts);
|
|
6
6
|
const project = { files: analyzed.files.filter((file) => file.path !== config.file) };
|
|
7
7
|
const files = project.files.map((file) => file.path);
|
|
8
8
|
return { model: resolveModel(config, foldersAt(root), files), project };
|
|
9
9
|
};
|
|
10
|
-
export const projectCheck = (adapters, config, { rules = [],
|
|
10
|
+
export const projectCheck = (adapters, config, { rules = [], lines = new Map() } = {}) => ({
|
|
11
11
|
config,
|
|
12
12
|
requiredFile: config.tsconfig[0],
|
|
13
13
|
check: (root) => {
|
|
14
14
|
const { model, project } = modelProject(adapters, root, config);
|
|
15
|
-
return placeFindings(lines, config.file, checkModel(config, model, project, rules
|
|
15
|
+
return placeFindings(lines, config.file, checkModel(config, model, project, rules));
|
|
16
16
|
},
|
|
17
17
|
});
|
package/dist/app/checkModel.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Project } from '../
|
|
2
|
-
import type { Rule, Finding } from '../rules/rule.ts';
|
|
3
|
-
import type { ConfigData } from '../model/
|
|
4
|
-
import type { Model } from '../model/
|
|
5
|
-
export declare const checkModel: (config: ConfigData, model: Model, project: Project, rules?: readonly Rule[]
|
|
1
|
+
import type { Project } from '../domain/facts/facts.ts';
|
|
2
|
+
import type { Rule, Finding } from '../domain/rules/rule.ts';
|
|
3
|
+
import type { ConfigData } from '../domain/model/data.ts';
|
|
4
|
+
import type { Model } from '../domain/model/resolve.ts';
|
|
5
|
+
export declare const checkModel: (config: ConfigData, model: Model, project: Project, rules?: readonly Rule[]) => Finding[];
|
package/dist/app/checkModel.js
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
import { verdicts } from '../model/
|
|
2
|
-
import { settings } from '../model/
|
|
3
|
-
|
|
4
|
-
export const checkModel = (config, model, project, rules = [], unclassifiedDocs) => {
|
|
1
|
+
import { verdicts } from '../domain/model/reach.js';
|
|
2
|
+
import { settings } from '../domain/model/settings.js';
|
|
3
|
+
export const checkModel = (config, model, project, rules = []) => {
|
|
5
4
|
const settled = settings(config, model);
|
|
6
5
|
const judged = verdicts(config, model);
|
|
7
6
|
const found = rules.flatMap((rule) => {
|
|
8
7
|
const findings = run(rule, { project, config, model, verdicts: judged }, settled);
|
|
9
8
|
return rule.docs ? findings.map((finding) => ({ ...finding, docs: rule.docs })) : findings;
|
|
10
9
|
});
|
|
11
|
-
return
|
|
10
|
+
return found.map((finding) => placed(model, finding));
|
|
12
11
|
};
|
|
13
12
|
const placed = (model, finding) => {
|
|
14
13
|
const module = model.moduleOf(finding.file);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Finding } from '../rules/rule.ts';
|
|
1
|
+
import type { Finding } from '../domain/rules/rule.ts';
|
|
2
2
|
export type ConfigLines = ReadonlyMap<string, number>;
|
|
3
3
|
export declare const lineAt: (lines: ConfigLines, text: string) => number | undefined;
|
|
4
4
|
export declare const placeProblems: (lines: ConfigLines, file: string, message: string) => string;
|
package/dist/app/diff.d.ts
CHANGED
package/dist/app/diff.js
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const failures = (cache, toolDigest) => {
|
|
2
|
+
const key = `${toolDigest}-failure`;
|
|
3
|
+
return {
|
|
4
|
+
isNew(problem) {
|
|
5
|
+
if (cache?.get(key) === problem)
|
|
6
|
+
return false;
|
|
7
|
+
cache?.set(key, problem);
|
|
8
|
+
return true;
|
|
9
|
+
},
|
|
10
|
+
forget() {
|
|
11
|
+
if (cache?.get(key) !== undefined)
|
|
12
|
+
cache.prune((other) => other !== key);
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
};
|
package/dist/app/load.d.ts
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type
|
|
1
|
+
import type { ConfigData } from '../domain/model/data.ts';
|
|
2
|
+
import { type Plugin, type Rule } from '../domain/rules/rule.ts';
|
|
3
|
+
import { type ConfigLines } from './configLines.ts';
|
|
4
|
+
import type { ConfigSource } from './ports.ts';
|
|
3
5
|
export interface LoadedConfigData {
|
|
4
6
|
config: ConfigData;
|
|
5
7
|
rules: Rule[];
|
|
6
8
|
}
|
|
7
9
|
export interface LoadOptions {
|
|
8
|
-
builtins?:
|
|
9
|
-
ruleset?: string;
|
|
10
|
+
builtins?: Plugin;
|
|
10
11
|
file?: string;
|
|
11
12
|
plugins?: Record<string, unknown>;
|
|
12
13
|
}
|
|
13
|
-
export declare const loadConfigData: (raw: unknown, { builtins,
|
|
14
|
+
export declare const loadConfigData: (raw: unknown, { builtins, file, plugins }?: LoadOptions) => LoadedConfigData;
|
|
15
|
+
export interface LoadedConfig extends LoadedConfigData {
|
|
16
|
+
lines: ConfigLines;
|
|
17
|
+
digest: string;
|
|
18
|
+
}
|
|
19
|
+
export declare const loadConfig: (source: ConfigSource, options?: Omit<LoadOptions, 'file' | 'plugins'>) => Promise<LoadedConfig>;
|