@webpieces/dev-config 0.0.0-dev → 0.2.21
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/architecture/executors/generate/executor.d.ts +17 -0
- package/architecture/executors/generate/executor.js +67 -0
- package/architecture/executors/generate/executor.js.map +1 -0
- package/architecture/executors/generate/executor.ts +83 -0
- package/architecture/executors/generate/schema.json +14 -0
- package/architecture/executors/validate-architecture-unchanged/executor.d.ts +17 -0
- package/architecture/executors/validate-architecture-unchanged/executor.js +65 -0
- package/architecture/executors/validate-architecture-unchanged/executor.js.map +1 -0
- package/architecture/executors/validate-architecture-unchanged/executor.ts +81 -0
- package/architecture/executors/validate-architecture-unchanged/schema.json +14 -0
- package/architecture/executors/validate-no-cycles/executor.d.ts +16 -0
- package/architecture/executors/validate-no-cycles/executor.js +48 -0
- package/architecture/executors/validate-no-cycles/executor.js.map +1 -0
- package/architecture/executors/validate-no-cycles/executor.ts +60 -0
- package/architecture/executors/validate-no-cycles/schema.json +8 -0
- package/architecture/executors/validate-no-skiplevel-deps/executor.d.ts +19 -0
- package/architecture/executors/validate-no-skiplevel-deps/executor.js +227 -0
- package/architecture/executors/validate-no-skiplevel-deps/executor.js.map +1 -0
- package/architecture/executors/validate-no-skiplevel-deps/executor.ts +267 -0
- package/architecture/executors/validate-no-skiplevel-deps/schema.json +8 -0
- package/architecture/executors/visualize/executor.d.ts +17 -0
- package/architecture/executors/visualize/executor.js +49 -0
- package/architecture/executors/visualize/executor.js.map +1 -0
- package/architecture/executors/visualize/executor.ts +63 -0
- package/architecture/executors/visualize/schema.json +14 -0
- package/architecture/index.d.ts +19 -0
- package/architecture/index.js +23 -0
- package/architecture/index.js.map +1 -0
- package/architecture/index.ts +20 -0
- package/architecture/lib/graph-comparator.d.ts +39 -0
- package/architecture/lib/graph-comparator.js +100 -0
- package/architecture/lib/graph-comparator.js.map +1 -0
- package/architecture/lib/graph-comparator.ts +141 -0
- package/architecture/lib/graph-generator.d.ts +19 -0
- package/architecture/lib/graph-generator.js +88 -0
- package/architecture/lib/graph-generator.js.map +1 -0
- package/architecture/lib/graph-generator.ts +102 -0
- package/architecture/lib/graph-loader.d.ts +31 -0
- package/architecture/lib/graph-loader.js +70 -0
- package/architecture/lib/graph-loader.js.map +1 -0
- package/architecture/lib/graph-loader.ts +82 -0
- package/architecture/lib/graph-sorter.d.ts +37 -0
- package/architecture/lib/graph-sorter.js +110 -0
- package/architecture/lib/graph-sorter.js.map +1 -0
- package/architecture/lib/graph-sorter.ts +137 -0
- package/architecture/lib/graph-visualizer.d.ts +29 -0
- package/architecture/lib/graph-visualizer.js +209 -0
- package/architecture/lib/graph-visualizer.js.map +1 -0
- package/architecture/lib/graph-visualizer.ts +222 -0
- package/architecture/lib/package-validator.d.ts +38 -0
- package/architecture/lib/package-validator.js +105 -0
- package/architecture/lib/package-validator.js.map +1 -0
- package/architecture/lib/package-validator.ts +144 -0
- package/config/eslint/base.mjs +6 -0
- package/eslint-plugin/__tests__/catch-error-pattern.test.ts +0 -1
- package/eslint-plugin/__tests__/max-file-lines.test.ts +29 -17
- package/eslint-plugin/__tests__/max-method-lines.test.ts +27 -15
- package/eslint-plugin/__tests__/no-unmanaged-exceptions.test.ts +359 -0
- package/eslint-plugin/index.d.ts +9 -0
- package/eslint-plugin/index.js +11 -0
- package/eslint-plugin/index.js.map +1 -1
- package/eslint-plugin/index.ts +11 -0
- package/eslint-plugin/rules/enforce-architecture.d.ts +15 -0
- package/eslint-plugin/rules/enforce-architecture.js +406 -0
- package/eslint-plugin/rules/enforce-architecture.js.map +1 -0
- package/eslint-plugin/rules/enforce-architecture.ts +469 -0
- package/eslint-plugin/rules/max-file-lines.js +11 -11
- package/eslint-plugin/rules/max-file-lines.js.map +1 -1
- package/eslint-plugin/rules/max-file-lines.ts +11 -11
- package/eslint-plugin/rules/max-method-lines.js +71 -88
- package/eslint-plugin/rules/max-method-lines.js.map +1 -1
- package/eslint-plugin/rules/max-method-lines.ts +85 -102
- package/eslint-plugin/rules/no-unmanaged-exceptions.d.ts +22 -0
- package/eslint-plugin/rules/no-unmanaged-exceptions.js +605 -0
- package/eslint-plugin/rules/no-unmanaged-exceptions.js.map +1 -0
- package/eslint-plugin/rules/no-unmanaged-exceptions.ts +621 -0
- package/executors.json +29 -0
- package/package.json +13 -7
- package/plugins/circular-deps/index.d.ts +8 -0
- package/plugins/circular-deps/index.js +14 -0
- package/plugins/circular-deps/index.js.map +1 -0
- package/plugins/circular-deps/index.ts +9 -0
- package/plugins/circular-deps/plugin.d.ts +32 -0
- package/plugins/circular-deps/plugin.js +73 -0
- package/plugins/circular-deps/plugin.js.map +1 -0
- package/plugins/circular-deps/plugin.ts +83 -0
package/eslint-plugin/index.js
CHANGED
|
@@ -4,16 +4,27 @@
|
|
|
4
4
|
* Provides rules for enforcing WebPieces code patterns
|
|
5
5
|
*
|
|
6
6
|
* This plugin is automatically included in @webpieces/dev-config
|
|
7
|
+
*
|
|
8
|
+
* Available rules:
|
|
9
|
+
* - catch-error-pattern: Enforce toError() usage in catch blocks (HOW to handle)
|
|
10
|
+
* - no-unmanaged-exceptions: Discourage try-catch outside tests (WHERE to handle)
|
|
11
|
+
* - max-method-lines: Enforce maximum method length (default: 70 lines)
|
|
12
|
+
* - max-file-lines: Enforce maximum file length (default: 700 lines)
|
|
13
|
+
* - enforce-architecture: Enforce architecture dependency boundaries
|
|
7
14
|
*/
|
|
8
15
|
const tslib_1 = require("tslib");
|
|
9
16
|
const catch_error_pattern_1 = tslib_1.__importDefault(require("./rules/catch-error-pattern"));
|
|
17
|
+
const no_unmanaged_exceptions_1 = tslib_1.__importDefault(require("./rules/no-unmanaged-exceptions"));
|
|
10
18
|
const max_method_lines_1 = tslib_1.__importDefault(require("./rules/max-method-lines"));
|
|
11
19
|
const max_file_lines_1 = tslib_1.__importDefault(require("./rules/max-file-lines"));
|
|
20
|
+
const enforce_architecture_1 = tslib_1.__importDefault(require("./rules/enforce-architecture"));
|
|
12
21
|
module.exports = {
|
|
13
22
|
rules: {
|
|
14
23
|
'catch-error-pattern': catch_error_pattern_1.default,
|
|
24
|
+
'no-unmanaged-exceptions': no_unmanaged_exceptions_1.default,
|
|
15
25
|
'max-method-lines': max_method_lines_1.default,
|
|
16
26
|
'max-file-lines': max_file_lines_1.default,
|
|
27
|
+
'enforce-architecture': enforce_architecture_1.default,
|
|
17
28
|
},
|
|
18
29
|
};
|
|
19
30
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../packages/tooling/dev-config/eslint-plugin/index.ts"],"names":[],"mappings":";AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../packages/tooling/dev-config/eslint-plugin/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;GAYG;;AAEH,8FAA4D;AAC5D,sGAAoE;AACpE,wFAAsD;AACtD,oFAAkD;AAClD,gGAA+D;AAE/D,iBAAS;IACL,KAAK,EAAE;QACH,qBAAqB,EAAE,6BAAiB;QACxC,yBAAyB,EAAE,iCAAqB;QAChD,kBAAkB,EAAE,0BAAc;QAClC,gBAAgB,EAAE,wBAAY;QAC9B,sBAAsB,EAAE,8BAAmB;KAC9C;CACJ,CAAC","sourcesContent":["/**\n * ESLint plugin for WebPieces\n * Provides rules for enforcing WebPieces code patterns\n *\n * This plugin is automatically included in @webpieces/dev-config\n *\n * Available rules:\n * - catch-error-pattern: Enforce toError() usage in catch blocks (HOW to handle)\n * - no-unmanaged-exceptions: Discourage try-catch outside tests (WHERE to handle)\n * - max-method-lines: Enforce maximum method length (default: 70 lines)\n * - max-file-lines: Enforce maximum file length (default: 700 lines)\n * - enforce-architecture: Enforce architecture dependency boundaries\n */\n\nimport catchErrorPattern from './rules/catch-error-pattern';\nimport noUnmanagedExceptions from './rules/no-unmanaged-exceptions';\nimport maxMethodLines from './rules/max-method-lines';\nimport maxFileLines from './rules/max-file-lines';\nimport enforceArchitecture from './rules/enforce-architecture';\n\nexport = {\n rules: {\n 'catch-error-pattern': catchErrorPattern,\n 'no-unmanaged-exceptions': noUnmanagedExceptions,\n 'max-method-lines': maxMethodLines,\n 'max-file-lines': maxFileLines,\n 'enforce-architecture': enforceArchitecture,\n },\n};\n"]}
|
package/eslint-plugin/index.ts
CHANGED
|
@@ -3,16 +3,27 @@
|
|
|
3
3
|
* Provides rules for enforcing WebPieces code patterns
|
|
4
4
|
*
|
|
5
5
|
* This plugin is automatically included in @webpieces/dev-config
|
|
6
|
+
*
|
|
7
|
+
* Available rules:
|
|
8
|
+
* - catch-error-pattern: Enforce toError() usage in catch blocks (HOW to handle)
|
|
9
|
+
* - no-unmanaged-exceptions: Discourage try-catch outside tests (WHERE to handle)
|
|
10
|
+
* - max-method-lines: Enforce maximum method length (default: 70 lines)
|
|
11
|
+
* - max-file-lines: Enforce maximum file length (default: 700 lines)
|
|
12
|
+
* - enforce-architecture: Enforce architecture dependency boundaries
|
|
6
13
|
*/
|
|
7
14
|
|
|
8
15
|
import catchErrorPattern from './rules/catch-error-pattern';
|
|
16
|
+
import noUnmanagedExceptions from './rules/no-unmanaged-exceptions';
|
|
9
17
|
import maxMethodLines from './rules/max-method-lines';
|
|
10
18
|
import maxFileLines from './rules/max-file-lines';
|
|
19
|
+
import enforceArchitecture from './rules/enforce-architecture';
|
|
11
20
|
|
|
12
21
|
export = {
|
|
13
22
|
rules: {
|
|
14
23
|
'catch-error-pattern': catchErrorPattern,
|
|
24
|
+
'no-unmanaged-exceptions': noUnmanagedExceptions,
|
|
15
25
|
'max-method-lines': maxMethodLines,
|
|
16
26
|
'max-file-lines': maxFileLines,
|
|
27
|
+
'enforce-architecture': enforceArchitecture,
|
|
17
28
|
},
|
|
18
29
|
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ESLint rule to enforce architecture boundaries
|
|
3
|
+
*
|
|
4
|
+
* Validates that imports from @webpieces/* packages comply with the
|
|
5
|
+
* blessed dependency graph in .graphs/dependencies.json
|
|
6
|
+
*
|
|
7
|
+
* Supports transitive dependencies: if A depends on B and B depends on C,
|
|
8
|
+
* then A can import from C.
|
|
9
|
+
*
|
|
10
|
+
* Configuration:
|
|
11
|
+
* '@webpieces/enforce-architecture': 'error'
|
|
12
|
+
*/
|
|
13
|
+
import type { Rule } from 'eslint';
|
|
14
|
+
declare const rule: Rule.RuleModule;
|
|
15
|
+
export = rule;
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* ESLint rule to enforce architecture boundaries
|
|
4
|
+
*
|
|
5
|
+
* Validates that imports from @webpieces/* packages comply with the
|
|
6
|
+
* blessed dependency graph in .graphs/dependencies.json
|
|
7
|
+
*
|
|
8
|
+
* Supports transitive dependencies: if A depends on B and B depends on C,
|
|
9
|
+
* then A can import from C.
|
|
10
|
+
*
|
|
11
|
+
* Configuration:
|
|
12
|
+
* '@webpieces/enforce-architecture': 'error'
|
|
13
|
+
*/
|
|
14
|
+
const tslib_1 = require("tslib");
|
|
15
|
+
const fs = tslib_1.__importStar(require("fs"));
|
|
16
|
+
const path = tslib_1.__importStar(require("path"));
|
|
17
|
+
const DEPENDENCIES_DOC_CONTENT = `# AI Agent Instructions: Architecture Dependency Violation
|
|
18
|
+
|
|
19
|
+
**READ THIS FILE FIRST before making any changes!**
|
|
20
|
+
|
|
21
|
+
## ⚠️ CRITICAL WARNING ⚠️
|
|
22
|
+
|
|
23
|
+
**This is a VERY IMPORTANT change that has LARGE REPERCUSSIONS later!**
|
|
24
|
+
|
|
25
|
+
Adding new dependencies creates technical debt that compounds over time:
|
|
26
|
+
- Creates coupling between packages that may be hard to undo
|
|
27
|
+
- Can create circular dependency tangles
|
|
28
|
+
- Makes packages harder to test in isolation
|
|
29
|
+
- Increases build times and bundle sizes
|
|
30
|
+
- May force unnecessary upgrades across the codebase
|
|
31
|
+
|
|
32
|
+
**DO NOT add dependencies without senior developer approval!**
|
|
33
|
+
|
|
34
|
+
## Understanding the Error
|
|
35
|
+
|
|
36
|
+
You've attempted to import from a package that is not in your project's allowed dependencies.
|
|
37
|
+
The architecture enforces a layered dependency structure where:
|
|
38
|
+
- Level 0 packages are foundation packages with NO dependencies on other @webpieces packages
|
|
39
|
+
- Higher level packages can only depend on lower level packages
|
|
40
|
+
- All dependencies must be explicitly declared
|
|
41
|
+
|
|
42
|
+
## Steps to Resolve
|
|
43
|
+
|
|
44
|
+
### Step 1: Generate Current Dependency Graph
|
|
45
|
+
Run this command to see the current architecture:
|
|
46
|
+
\`\`\`bash
|
|
47
|
+
npx nx run architecture:generate
|
|
48
|
+
\`\`\`
|
|
49
|
+
This creates/updates \`architecture/dependencies.json\` showing all packages and their levels.
|
|
50
|
+
|
|
51
|
+
### Step 2: Analyze the Proposed Change
|
|
52
|
+
Ask yourself:
|
|
53
|
+
1. **Is this import truly necessary?** Can you refactor to avoid it?
|
|
54
|
+
2. **Should the code move instead?** Maybe the code belongs in a different package.
|
|
55
|
+
3. **Will this create a cycle?** Use \`npx nx graph\` to visualize dependencies.
|
|
56
|
+
4. **Can you use an interface/abstraction?** Define interface in lower-level package, implement in higher-level.
|
|
57
|
+
|
|
58
|
+
### Step 3: Get Senior Developer Approval
|
|
59
|
+
|
|
60
|
+
## 🛑 AI AGENTS: STOP HERE AND ASK FOR HUMAN APPROVAL! 🛑
|
|
61
|
+
|
|
62
|
+
**YOU MUST NOT PROCEED TO STEP 4 WITHOUT EXPLICIT HUMAN APPROVAL!**
|
|
63
|
+
|
|
64
|
+
**REQUIRED**: Discuss this architectural change with a senior developer before proceeding.
|
|
65
|
+
- Explain why the dependency is needed
|
|
66
|
+
- Show you've considered alternatives (Step 2)
|
|
67
|
+
- **WAIT for explicit approval before making ANY changes to project.json or package.json**
|
|
68
|
+
|
|
69
|
+
**AI Agent Instructions:**
|
|
70
|
+
1. Present your analysis from Step 2 to the human
|
|
71
|
+
2. Explain which package needs which dependency and why
|
|
72
|
+
3. ASK: "Do you approve adding this dependency?"
|
|
73
|
+
4. **DO NOT modify project.json or package.json until you receive explicit "yes" or approval**
|
|
74
|
+
|
|
75
|
+
### Step 4: If Approved, Add the Dependency
|
|
76
|
+
|
|
77
|
+
## ⛔ NEVER MODIFY THESE FILES WITHOUT HUMAN APPROVAL FROM STEP 3! ⛔
|
|
78
|
+
|
|
79
|
+
Only after receiving explicit human approval in Step 3, make these changes:
|
|
80
|
+
|
|
81
|
+
1. **Update project.json** - Add to \`build.dependsOn\`:
|
|
82
|
+
\`\`\`json
|
|
83
|
+
{
|
|
84
|
+
"targets": {
|
|
85
|
+
"build": {
|
|
86
|
+
"dependsOn": ["^build", "dep1:build", "NEW_PACKAGE:build"]
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
\`\`\`
|
|
91
|
+
|
|
92
|
+
2. **Update package.json** - Add to \`dependencies\`:
|
|
93
|
+
\`\`\`json
|
|
94
|
+
{
|
|
95
|
+
"dependencies": {
|
|
96
|
+
"@webpieces/NEW_PACKAGE": "*"
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
\`\`\`
|
|
100
|
+
|
|
101
|
+
### Step 5: Update Architecture Definition
|
|
102
|
+
Run this command to validate and update the architecture:
|
|
103
|
+
\`\`\`bash
|
|
104
|
+
npx nx run architecture:validate --mode=update
|
|
105
|
+
\`\`\`
|
|
106
|
+
|
|
107
|
+
This will:
|
|
108
|
+
- Detect any cycles (which MUST be fixed before proceeding)
|
|
109
|
+
- Update \`architecture/dependencies.json\` with the new dependency
|
|
110
|
+
- Recalculate package levels
|
|
111
|
+
|
|
112
|
+
### Step 6: Verify No Cycles
|
|
113
|
+
\`\`\`bash
|
|
114
|
+
npx nx run architecture:validate
|
|
115
|
+
\`\`\`
|
|
116
|
+
|
|
117
|
+
If cycles are detected, you MUST refactor to break the cycle. Common strategies:
|
|
118
|
+
- Move shared code to a lower-level package
|
|
119
|
+
- Use dependency inversion (interfaces in low-level, implementations in high-level)
|
|
120
|
+
- Restructure package boundaries
|
|
121
|
+
|
|
122
|
+
## Alternative Solutions (Preferred over adding dependencies)
|
|
123
|
+
|
|
124
|
+
### Option A: Move the Code
|
|
125
|
+
If you need functionality from another package, consider moving that code to a shared lower-level package.
|
|
126
|
+
|
|
127
|
+
### Option B: Dependency Inversion
|
|
128
|
+
Define an interface in the lower-level package, implement it in the higher-level package:
|
|
129
|
+
\`\`\`typescript
|
|
130
|
+
// In foundation package (level 0)
|
|
131
|
+
export interface Logger { log(msg: string): void; }
|
|
132
|
+
|
|
133
|
+
// In higher-level package
|
|
134
|
+
export class ConsoleLogger implements Logger { ... }
|
|
135
|
+
\`\`\`
|
|
136
|
+
|
|
137
|
+
### Option C: Pass Dependencies as Parameters
|
|
138
|
+
Instead of importing, receive the dependency as a constructor or method parameter.
|
|
139
|
+
|
|
140
|
+
## Remember
|
|
141
|
+
- Every dependency you add today is technical debt for tomorrow
|
|
142
|
+
- The best dependency is the one you don't need
|
|
143
|
+
- When in doubt, refactor rather than add dependencies
|
|
144
|
+
`;
|
|
145
|
+
// Module-level flag to prevent redundant file creation
|
|
146
|
+
let dependenciesDocCreated = false;
|
|
147
|
+
/**
|
|
148
|
+
* Ensure a documentation file exists at the given path.
|
|
149
|
+
*/
|
|
150
|
+
function ensureDocFile(docPath, content) {
|
|
151
|
+
try {
|
|
152
|
+
fs.mkdirSync(path.dirname(docPath), { recursive: true });
|
|
153
|
+
fs.writeFileSync(docPath, content, 'utf-8');
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
catch (err) {
|
|
157
|
+
void err;
|
|
158
|
+
console.warn(`[webpieces] Could not create doc file: ${docPath}`);
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Ensure the dependencies documentation file exists.
|
|
164
|
+
* Called when an architecture violation is detected.
|
|
165
|
+
*/
|
|
166
|
+
function ensureDependenciesDoc(workspaceRoot) {
|
|
167
|
+
if (dependenciesDocCreated)
|
|
168
|
+
return;
|
|
169
|
+
const docPath = path.join(workspaceRoot, 'tmp', 'webpieces', 'webpieces.dependencies.md');
|
|
170
|
+
if (ensureDocFile(docPath, DEPENDENCIES_DOC_CONTENT)) {
|
|
171
|
+
dependenciesDocCreated = true;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
// Cache for blessed graph (loaded once per lint run)
|
|
175
|
+
let cachedGraph = null;
|
|
176
|
+
let cachedGraphPath = null;
|
|
177
|
+
// Cache for project mappings
|
|
178
|
+
let cachedProjectMappings = null;
|
|
179
|
+
/**
|
|
180
|
+
* Find workspace root by walking up from file location
|
|
181
|
+
*/
|
|
182
|
+
function findWorkspaceRoot(startPath) {
|
|
183
|
+
let currentDir = path.dirname(startPath);
|
|
184
|
+
for (let i = 0; i < 20; i++) {
|
|
185
|
+
const packagePath = path.join(currentDir, 'package.json');
|
|
186
|
+
if (fs.existsSync(packagePath)) {
|
|
187
|
+
try {
|
|
188
|
+
const pkg = JSON.parse(fs.readFileSync(packagePath, 'utf-8'));
|
|
189
|
+
if (pkg.workspaces || pkg.name === 'webpieces-ts') {
|
|
190
|
+
return currentDir;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
catch (err) {
|
|
194
|
+
//const error = toError(err);
|
|
195
|
+
void err;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
const parent = path.dirname(currentDir);
|
|
199
|
+
if (parent === currentDir)
|
|
200
|
+
break;
|
|
201
|
+
currentDir = parent;
|
|
202
|
+
}
|
|
203
|
+
return process.cwd();
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Load blessed graph from architecture/dependencies.json
|
|
207
|
+
*/
|
|
208
|
+
function loadBlessedGraph(workspaceRoot) {
|
|
209
|
+
const graphPath = path.join(workspaceRoot, 'architecture', 'dependencies.json');
|
|
210
|
+
// Return cached if same path
|
|
211
|
+
if (cachedGraphPath === graphPath && cachedGraph !== null) {
|
|
212
|
+
return cachedGraph;
|
|
213
|
+
}
|
|
214
|
+
if (!fs.existsSync(graphPath)) {
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
try {
|
|
218
|
+
const content = fs.readFileSync(graphPath, 'utf-8');
|
|
219
|
+
cachedGraph = JSON.parse(content);
|
|
220
|
+
cachedGraphPath = graphPath;
|
|
221
|
+
return cachedGraph;
|
|
222
|
+
}
|
|
223
|
+
catch (err) {
|
|
224
|
+
//const error = toError(err);
|
|
225
|
+
// err is used below
|
|
226
|
+
console.error(`[ESLint @webpieces/enforce-architecture] Could not load graph: ${err}`);
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Build project mappings from project.json files in workspace
|
|
232
|
+
*/
|
|
233
|
+
function buildProjectMappings(workspaceRoot) {
|
|
234
|
+
if (cachedProjectMappings !== null) {
|
|
235
|
+
return cachedProjectMappings;
|
|
236
|
+
}
|
|
237
|
+
const mappings = [];
|
|
238
|
+
// Scan common locations for project.json files
|
|
239
|
+
const searchDirs = ['packages', 'apps', 'libs', 'libraries', 'services'];
|
|
240
|
+
for (const searchDir of searchDirs) {
|
|
241
|
+
const searchPath = path.join(workspaceRoot, searchDir);
|
|
242
|
+
if (!fs.existsSync(searchPath))
|
|
243
|
+
continue;
|
|
244
|
+
scanForProjects(searchPath, workspaceRoot, mappings);
|
|
245
|
+
}
|
|
246
|
+
// Sort by path length (longest first) for more specific matching
|
|
247
|
+
mappings.sort((a, b) => b.root.length - a.root.length);
|
|
248
|
+
cachedProjectMappings = mappings;
|
|
249
|
+
return mappings;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Recursively scan for project.json files
|
|
253
|
+
*/
|
|
254
|
+
function scanForProjects(dir, workspaceRoot, mappings) {
|
|
255
|
+
try {
|
|
256
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
257
|
+
for (const entry of entries) {
|
|
258
|
+
const fullPath = path.join(dir, entry.name);
|
|
259
|
+
if (entry.isDirectory() && !entry.name.startsWith('.') && entry.name !== 'node_modules') {
|
|
260
|
+
// Check for project.json in this directory
|
|
261
|
+
const projectJsonPath = path.join(fullPath, 'project.json');
|
|
262
|
+
if (fs.existsSync(projectJsonPath)) {
|
|
263
|
+
try {
|
|
264
|
+
const projectJson = JSON.parse(fs.readFileSync(projectJsonPath, 'utf-8'));
|
|
265
|
+
const projectRoot = path.relative(workspaceRoot, fullPath);
|
|
266
|
+
// Determine project name
|
|
267
|
+
let projectName = projectJson.name || entry.name;
|
|
268
|
+
// Add @webpieces/ prefix if not present
|
|
269
|
+
if (!projectName.startsWith('@webpieces/')) {
|
|
270
|
+
projectName = `@webpieces/${projectName}`;
|
|
271
|
+
}
|
|
272
|
+
mappings.push({
|
|
273
|
+
root: projectRoot,
|
|
274
|
+
name: projectName,
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
catch (err) {
|
|
278
|
+
//const error = toError(err);
|
|
279
|
+
void err;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
// Continue scanning subdirectories
|
|
283
|
+
scanForProjects(fullPath, workspaceRoot, mappings);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
catch (err) {
|
|
288
|
+
//const error = toError(err);
|
|
289
|
+
void err;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Get project name from file path
|
|
294
|
+
*/
|
|
295
|
+
function getProjectFromFile(filePath, workspaceRoot) {
|
|
296
|
+
const relativePath = path.relative(workspaceRoot, filePath).replace(/\\/g, '/');
|
|
297
|
+
const mappings = buildProjectMappings(workspaceRoot);
|
|
298
|
+
for (const mapping of mappings) {
|
|
299
|
+
if (relativePath.startsWith(mapping.root + '/') || relativePath.startsWith(mapping.root)) {
|
|
300
|
+
return mapping.name;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return null;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Compute all transitive dependencies for a project
|
|
307
|
+
*/
|
|
308
|
+
function computeTransitiveDependencies(project, graph) {
|
|
309
|
+
const result = new Set();
|
|
310
|
+
const visited = new Set();
|
|
311
|
+
function visit(currentProject) {
|
|
312
|
+
if (visited.has(currentProject))
|
|
313
|
+
return;
|
|
314
|
+
visited.add(currentProject);
|
|
315
|
+
const entry = graph[currentProject];
|
|
316
|
+
if (!entry || !entry.dependsOn)
|
|
317
|
+
return;
|
|
318
|
+
for (const dep of entry.dependsOn) {
|
|
319
|
+
result.add(dep);
|
|
320
|
+
visit(dep);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
visit(project);
|
|
324
|
+
return result;
|
|
325
|
+
}
|
|
326
|
+
const rule = {
|
|
327
|
+
meta: {
|
|
328
|
+
type: 'problem',
|
|
329
|
+
docs: {
|
|
330
|
+
description: 'Enforce blessed architecture dependencies',
|
|
331
|
+
category: 'Best Practices',
|
|
332
|
+
recommended: true,
|
|
333
|
+
url: 'https://github.com/deanhiller/webpieces-ts',
|
|
334
|
+
},
|
|
335
|
+
messages: {
|
|
336
|
+
illegalImport: "⚠️ AI Agent: READ tmp/webpieces/webpieces.dependencies.md for resolution steps!\n\n" +
|
|
337
|
+
"Import '{{imported}}' violates architecture boundaries.\n\n" +
|
|
338
|
+
"Project '{{project}}' (level {{level}}) can only import from:\n" +
|
|
339
|
+
'{{allowedList}}',
|
|
340
|
+
noGraph: 'No architecture graph found at architecture/dependencies.json\n' +
|
|
341
|
+
'Run: nx run architecture:validate --mode=update',
|
|
342
|
+
},
|
|
343
|
+
schema: [],
|
|
344
|
+
},
|
|
345
|
+
create(context) {
|
|
346
|
+
const filename = context.filename || context.getFilename();
|
|
347
|
+
const workspaceRoot = findWorkspaceRoot(filename);
|
|
348
|
+
return {
|
|
349
|
+
ImportDeclaration(node) {
|
|
350
|
+
const importPath = node.source.value;
|
|
351
|
+
// Only check @webpieces/* imports
|
|
352
|
+
if (!importPath.startsWith('@webpieces/')) {
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
// Determine which project this file belongs to
|
|
356
|
+
const project = getProjectFromFile(filename, workspaceRoot);
|
|
357
|
+
if (!project) {
|
|
358
|
+
// File not in any known project (e.g., tools/, scripts/)
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
// Self-import is always allowed
|
|
362
|
+
if (importPath === project) {
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
// Load blessed graph
|
|
366
|
+
const graph = loadBlessedGraph(workspaceRoot);
|
|
367
|
+
if (!graph) {
|
|
368
|
+
// No graph file - warn but don't fail (allows gradual adoption)
|
|
369
|
+
// Uncomment below to enforce graph existence:
|
|
370
|
+
// context.report({ node: node.source, messageId: 'noGraph' });
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
// Get project entry
|
|
374
|
+
const projectEntry = graph[project];
|
|
375
|
+
if (!projectEntry) {
|
|
376
|
+
// Project not in graph (new project?) - allow
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
// Compute allowed dependencies (direct + transitive)
|
|
380
|
+
const allowedDeps = computeTransitiveDependencies(project, graph);
|
|
381
|
+
// Check if import is allowed
|
|
382
|
+
if (!allowedDeps.has(importPath)) {
|
|
383
|
+
// Write documentation file for AI/developer to read
|
|
384
|
+
ensureDependenciesDoc(workspaceRoot);
|
|
385
|
+
const directDeps = projectEntry.dependsOn || [];
|
|
386
|
+
const allowedList = directDeps.length > 0
|
|
387
|
+
? directDeps.map((dep) => ` - ${dep}`).join('\n') +
|
|
388
|
+
'\n (and their transitive dependencies)'
|
|
389
|
+
: ' (none - this is a foundation project)';
|
|
390
|
+
context.report({
|
|
391
|
+
node: node.source,
|
|
392
|
+
messageId: 'illegalImport',
|
|
393
|
+
data: {
|
|
394
|
+
imported: importPath,
|
|
395
|
+
project: project,
|
|
396
|
+
level: String(projectEntry.level),
|
|
397
|
+
allowedList: allowedList,
|
|
398
|
+
},
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
},
|
|
402
|
+
};
|
|
403
|
+
},
|
|
404
|
+
};
|
|
405
|
+
module.exports = rule;
|
|
406
|
+
//# sourceMappingURL=enforce-architecture.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enforce-architecture.js","sourceRoot":"","sources":["../../../../../../packages/tooling/dev-config/eslint-plugin/rules/enforce-architecture.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;GAWG;;AAGH,+CAAyB;AACzB,mDAA6B;AAE7B,MAAM,wBAAwB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+HhC,CAAC;AAEF,uDAAuD;AACvD,IAAI,sBAAsB,GAAG,KAAK,CAAC;AAEnC;;GAEG;AACH,SAAS,aAAa,CAAC,OAAe,EAAE,OAAe;IACnD,IAAI,CAAC;QACD,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QACzD,EAAE,CAAC,aAAa,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;QAC5C,OAAO,IAAI,CAAC;IAChB,CAAC;IAAC,OAAO,GAAQ,EAAE,CAAC;QAChB,KAAK,GAAG,CAAC;QACT,OAAO,CAAC,IAAI,CAAC,0CAA0C,OAAO,EAAE,CAAC,CAAC;QAClE,OAAO,KAAK,CAAC;IACjB,CAAC;AACL,CAAC;AAED;;;GAGG;AACH,SAAS,qBAAqB,CAAC,aAAqB;IAChD,IAAI,sBAAsB;QAAE,OAAO;IACnC,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,KAAK,EAAE,WAAW,EAAE,2BAA2B,CAAC,CAAC;IAC1F,IAAI,aAAa,CAAC,OAAO,EAAE,wBAAwB,CAAC,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W;yBAC3B;qBACJ,CAAC,CAAC;gBACP,CAAC;YACL,CAAC;SACJ,CAAC;IACN,CAAC;CACJ,CAAC;AAEF,iBAAS,IAAI,CAAC","sourcesContent":["/**\n * ESLint rule to enforce architecture boundaries\n *\n * Validates that imports from @webpieces/* packages comply with the\n * blessed dependency graph in .graphs/dependencies.json\n *\n * Supports transitive dependencies: if A depends on B and B depends on C,\n * then A can import from C.\n *\n * Configuration:\n * '@webpieces/enforce-architecture': 'error'\n */\n\nimport type { Rule } from 'eslint';\nimport * as fs from 'fs';\nimport * as path from 'path';\n\nconst DEPENDENCIES_DOC_CONTENT = `# AI Agent Instructions: Architecture Dependency Violation\n\n**READ THIS FILE FIRST before making any changes!**\n\n## ⚠️ CRITICAL WARNING ⚠️\n\n**This is a VERY IMPORTANT change that has LARGE REPERCUSSIONS later!**\n\nAdding new dependencies creates technical debt that compounds over time:\n- Creates coupling between packages that may be hard to undo\n- Can create circular dependency tangles\n- Makes packages harder to test in isolation\n- Increases build times and bundle sizes\n- May force unnecessary upgrades across the codebase\n\n**DO NOT add dependencies without senior developer approval!**\n\n## Understanding the Error\n\nYou've attempted to import from a package that is not in your project's allowed dependencies.\nThe architecture enforces a layered dependency structure where:\n- Level 0 packages are foundation packages with NO dependencies on other @webpieces packages\n- Higher level packages can only depend on lower level packages\n- All dependencies must be explicitly declared\n\n## Steps to Resolve\n\n### Step 1: Generate Current Dependency Graph\nRun this command to see the current architecture:\n\\`\\`\\`bash\nnpx nx run architecture:generate\n\\`\\`\\`\nThis creates/updates \\`architecture/dependencies.json\\` showing all packages and their levels.\n\n### Step 2: Analyze the Proposed Change\nAsk yourself:\n1. **Is this import truly necessary?** Can you refactor to avoid it?\n2. **Should the code move instead?** Maybe the code belongs in a different package.\n3. **Will this create a cycle?** Use \\`npx nx graph\\` to visualize dependencies.\n4. **Can you use an interface/abstraction?** Define interface in lower-level package, implement in higher-level.\n\n### Step 3: Get Senior Developer Approval\n\n## 🛑 AI AGENTS: STOP HERE AND ASK FOR HUMAN APPROVAL! 🛑\n\n**YOU MUST NOT PROCEED TO STEP 4 WITHOUT EXPLICIT HUMAN APPROVAL!**\n\n**REQUIRED**: Discuss this architectural change with a senior developer before proceeding.\n- Explain why the dependency is needed\n- Show you've considered alternatives (Step 2)\n- **WAIT for explicit approval before making ANY changes to project.json or package.json**\n\n**AI Agent Instructions:**\n1. Present your analysis from Step 2 to the human\n2. Explain which package needs which dependency and why\n3. ASK: \"Do you approve adding this dependency?\"\n4. **DO NOT modify project.json or package.json until you receive explicit \"yes\" or approval**\n\n### Step 4: If Approved, Add the Dependency\n\n## ⛔ NEVER MODIFY THESE FILES WITHOUT HUMAN APPROVAL FROM STEP 3! ⛔\n\nOnly after receiving explicit human approval in Step 3, make these changes:\n\n1. **Update project.json** - Add to \\`build.dependsOn\\`:\n \\`\\`\\`json\n {\n \"targets\": {\n \"build\": {\n \"dependsOn\": [\"^build\", \"dep1:build\", \"NEW_PACKAGE:build\"]\n }\n }\n }\n \\`\\`\\`\n\n2. **Update package.json** - Add to \\`dependencies\\`:\n \\`\\`\\`json\n {\n \"dependencies\": {\n \"@webpieces/NEW_PACKAGE\": \"*\"\n }\n }\n \\`\\`\\`\n\n### Step 5: Update Architecture Definition\nRun this command to validate and update the architecture:\n\\`\\`\\`bash\nnpx nx run architecture:validate --mode=update\n\\`\\`\\`\n\nThis will:\n- Detect any cycles (which MUST be fixed before proceeding)\n- Update \\`architecture/dependencies.json\\` with the new dependency\n- Recalculate package levels\n\n### Step 6: Verify No Cycles\n\\`\\`\\`bash\nnpx nx run architecture:validate\n\\`\\`\\`\n\nIf cycles are detected, you MUST refactor to break the cycle. Common strategies:\n- Move shared code to a lower-level package\n- Use dependency inversion (interfaces in low-level, implementations in high-level)\n- Restructure package boundaries\n\n## Alternative Solutions (Preferred over adding dependencies)\n\n### Option A: Move the Code\nIf you need functionality from another package, consider moving that code to a shared lower-level package.\n\n### Option B: Dependency Inversion\nDefine an interface in the lower-level package, implement it in the higher-level package:\n\\`\\`\\`typescript\n// In foundation package (level 0)\nexport interface Logger { log(msg: string): void; }\n\n// In higher-level package\nexport class ConsoleLogger implements Logger { ... }\n\\`\\`\\`\n\n### Option C: Pass Dependencies as Parameters\nInstead of importing, receive the dependency as a constructor or method parameter.\n\n## Remember\n- Every dependency you add today is technical debt for tomorrow\n- The best dependency is the one you don't need\n- When in doubt, refactor rather than add dependencies\n`;\n\n// Module-level flag to prevent redundant file creation\nlet dependenciesDocCreated = false;\n\n/**\n * Ensure a documentation file exists at the given path.\n */\nfunction ensureDocFile(docPath: string, content: string): boolean {\n try {\n fs.mkdirSync(path.dirname(docPath), { recursive: true });\n fs.writeFileSync(docPath, content, 'utf-8');\n return true;\n } catch (err: any) {\n void err;\n console.warn(`[webpieces] Could not create doc file: ${docPath}`);\n return false;\n }\n}\n\n/**\n * Ensure the dependencies documentation file exists.\n * Called when an architecture violation is detected.\n */\nfunction ensureDependenciesDoc(workspaceRoot: string): void {\n if (dependenciesDocCreated) return;\n const docPath = path.join(workspaceRoot, 'tmp', 'webpieces', 'webpieces.dependencies.md');\n if (ensureDocFile(docPath, DEPENDENCIES_DOC_CONTENT)) {\n dependenciesDocCreated = true;\n }\n}\n\n/**\n * Graph entry format from .graphs/dependencies.json\n */\ninterface GraphEntry {\n level: number;\n dependsOn: string[];\n}\n\ntype EnhancedGraph = Record<string, GraphEntry>;\n\n/**\n * Project mapping entry\n */\ninterface ProjectMapping {\n root: string;\n name: string;\n}\n\n// Cache for blessed graph (loaded once per lint run)\nlet cachedGraph: EnhancedGraph | null = null;\nlet cachedGraphPath: string | null = null;\n\n// Cache for project mappings\nlet cachedProjectMappings: ProjectMapping[] | null = null;\n\n/**\n * Find workspace root by walking up from file location\n */\nfunction findWorkspaceRoot(startPath: string): string {\n let currentDir = path.dirname(startPath);\n\n for (let i = 0; i < 20; i++) {\n const packagePath = path.join(currentDir, 'package.json');\n if (fs.existsSync(packagePath)) {\n try {\n const pkg = JSON.parse(fs.readFileSync(packagePath, 'utf-8'));\n if (pkg.workspaces || pkg.name === 'webpieces-ts') {\n return currentDir;\n }\n } catch (err: any) {\n //const error = toError(err);\n void err;\n }\n }\n\n const parent = path.dirname(currentDir);\n if (parent === currentDir) break;\n currentDir = parent;\n }\n\n return process.cwd();\n}\n\n/**\n * Load blessed graph from architecture/dependencies.json\n */\nfunction loadBlessedGraph(workspaceRoot: string): EnhancedGraph | null {\n const graphPath = path.join(workspaceRoot, 'architecture', 'dependencies.json');\n\n // Return cached if same path\n if (cachedGraphPath === graphPath && cachedGraph !== null) {\n return cachedGraph;\n }\n\n if (!fs.existsSync(graphPath)) {\n return null;\n }\n\n try {\n const content = fs.readFileSync(graphPath, 'utf-8');\n cachedGraph = JSON.parse(content) as EnhancedGraph;\n cachedGraphPath = graphPath;\n return cachedGraph;\n } catch (err: any) {\n //const error = toError(err);\n // err is used below\n console.error(`[ESLint @webpieces/enforce-architecture] Could not load graph: ${err}`);\n return null;\n }\n}\n\n/**\n * Build project mappings from project.json files in workspace\n */\nfunction buildProjectMappings(workspaceRoot: string): ProjectMapping[] {\n if (cachedProjectMappings !== null) {\n return cachedProjectMappings;\n }\n\n const mappings: ProjectMapping[] = [];\n\n // Scan common locations for project.json files\n const searchDirs = ['packages', 'apps', 'libs', 'libraries', 'services'];\n\n for (const searchDir of searchDirs) {\n const searchPath = path.join(workspaceRoot, searchDir);\n if (!fs.existsSync(searchPath)) continue;\n\n scanForProjects(searchPath, workspaceRoot, mappings);\n }\n\n // Sort by path length (longest first) for more specific matching\n mappings.sort((a, b) => b.root.length - a.root.length);\n\n cachedProjectMappings = mappings;\n return mappings;\n}\n\n/**\n * Recursively scan for project.json files\n */\nfunction scanForProjects(\n dir: string,\n workspaceRoot: string,\n mappings: ProjectMapping[]\n): void {\n try {\n const entries = fs.readdirSync(dir, { withFileTypes: true });\n\n for (const entry of entries) {\n const fullPath = path.join(dir, entry.name);\n\n if (entry.isDirectory() && !entry.name.startsWith('.') && entry.name !== 'node_modules') {\n // Check for project.json in this directory\n const projectJsonPath = path.join(fullPath, 'project.json');\n if (fs.existsSync(projectJsonPath)) {\n try {\n const projectJson = JSON.parse(fs.readFileSync(projectJsonPath, 'utf-8'));\n const projectRoot = path.relative(workspaceRoot, fullPath);\n\n // Determine project name\n let projectName = projectJson.name || entry.name;\n\n // Add @webpieces/ prefix if not present\n if (!projectName.startsWith('@webpieces/')) {\n projectName = `@webpieces/${projectName}`;\n }\n\n mappings.push({\n root: projectRoot,\n name: projectName,\n });\n } catch (err: any) {\n //const error = toError(err);\n void err;\n }\n }\n\n // Continue scanning subdirectories\n scanForProjects(fullPath, workspaceRoot, mappings);\n }\n }\n } catch (err: any) {\n //const error = toError(err);\n void err;\n }\n}\n\n/**\n * Get project name from file path\n */\nfunction getProjectFromFile(filePath: string, workspaceRoot: string): string | null {\n const relativePath = path.relative(workspaceRoot, filePath).replace(/\\\\/g, '/');\n const mappings = buildProjectMappings(workspaceRoot);\n\n for (const mapping of mappings) {\n if (relativePath.startsWith(mapping.root + '/') || relativePath.startsWith(mapping.root)) {\n return mapping.name;\n }\n }\n\n return null;\n}\n\n/**\n * Compute all transitive dependencies for a project\n */\nfunction computeTransitiveDependencies(project: string, graph: EnhancedGraph): Set<string> {\n const result = new Set<string>();\n const visited = new Set<string>();\n\n function visit(currentProject: string): void {\n if (visited.has(currentProject)) return;\n visited.add(currentProject);\n\n const entry = graph[currentProject];\n if (!entry || !entry.dependsOn) return;\n\n for (const dep of entry.dependsOn) {\n result.add(dep);\n visit(dep);\n }\n }\n\n visit(project);\n return result;\n}\n\nconst rule: Rule.RuleModule = {\n meta: {\n type: 'problem',\n docs: {\n description: 'Enforce blessed architecture dependencies',\n category: 'Best Practices',\n recommended: true,\n url: 'https://github.com/deanhiller/webpieces-ts',\n },\n messages: {\n illegalImport:\n \"⚠️ AI Agent: READ tmp/webpieces/webpieces.dependencies.md for resolution steps!\\n\\n\" +\n \"Import '{{imported}}' violates architecture boundaries.\\n\\n\" +\n \"Project '{{project}}' (level {{level}}) can only import from:\\n\" +\n '{{allowedList}}',\n noGraph:\n 'No architecture graph found at architecture/dependencies.json\\n' +\n 'Run: nx run architecture:validate --mode=update',\n },\n schema: [],\n },\n\n create(context: Rule.RuleContext): Rule.RuleListener {\n const filename = context.filename || context.getFilename();\n const workspaceRoot = findWorkspaceRoot(filename);\n\n return {\n ImportDeclaration(node: any): void {\n const importPath = node.source.value as string;\n\n // Only check @webpieces/* imports\n if (!importPath.startsWith('@webpieces/')) {\n return;\n }\n\n // Determine which project this file belongs to\n const project = getProjectFromFile(filename, workspaceRoot);\n if (!project) {\n // File not in any known project (e.g., tools/, scripts/)\n return;\n }\n\n // Self-import is always allowed\n if (importPath === project) {\n return;\n }\n\n // Load blessed graph\n const graph = loadBlessedGraph(workspaceRoot);\n if (!graph) {\n // No graph file - warn but don't fail (allows gradual adoption)\n // Uncomment below to enforce graph existence:\n // context.report({ node: node.source, messageId: 'noGraph' });\n return;\n }\n\n // Get project entry\n const projectEntry = graph[project];\n if (!projectEntry) {\n // Project not in graph (new project?) - allow\n return;\n }\n\n // Compute allowed dependencies (direct + transitive)\n const allowedDeps = computeTransitiveDependencies(project, graph);\n\n // Check if import is allowed\n if (!allowedDeps.has(importPath)) {\n // Write documentation file for AI/developer to read\n ensureDependenciesDoc(workspaceRoot);\n\n const directDeps = projectEntry.dependsOn || [];\n const allowedList =\n directDeps.length > 0\n ? directDeps.map((dep) => ` - ${dep}`).join('\\n') +\n '\\n (and their transitive dependencies)'\n : ' (none - this is a foundation project)';\n\n context.report({\n node: node.source,\n messageId: 'illegalImport',\n data: {\n imported: importPath,\n project: project,\n level: String(projectEntry.level),\n allowedList: allowedList,\n },\n });\n }\n },\n };\n },\n};\n\nexport = rule;\n"]}
|