-
-
Notifications
You must be signed in to change notification settings - Fork 929
Expand file tree
/
Copy pathsemantic-lint.js
More file actions
190 lines (164 loc) · 4.66 KB
/
Copy pathsemantic-lint.js
File metadata and controls
190 lines (164 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const path = require('path');
const DEFAULT_TARGETS = [
'README.md',
'docs/getting-started.md',
'docs/roadmap.md',
'docs/strategy',
];
const SEMANTIC_RULESET_VERSION = '1.0.0';
const RULES = [
{
id: 'deprecated-expansion-pack',
severity: 'error',
pattern: /\bexpansion pack(s)?\b/gi,
replacement: 'squad',
reason: 'Use AIOX-first taxonomy for domain agent sets.',
},
{
id: 'deprecated-permission-mode',
severity: 'error',
pattern: /\bpermission mode(s)?\b/gi,
replacement: 'execution profile',
reason: 'Use risk-oriented autonomy terminology.',
},
{
id: 'legacy-workflow-state-term',
severity: 'warn',
pattern: /\bworkflow state\b/gi,
replacement: 'flow-state',
reason: 'Prefer flow-state in product-facing differentiation messaging.',
},
];
function parseArgs(argv = process.argv.slice(2)) {
const args = new Set(argv.filter((arg) => arg.startsWith('--')));
const files = argv.filter((arg) => !arg.startsWith('--'));
return {
staged: args.has('--staged'),
json: args.has('--json'),
files,
};
}
function collectFiles(inputPaths, projectRoot = process.cwd()) {
const selected = inputPaths && inputPaths.length > 0 ? inputPaths : DEFAULT_TARGETS;
const files = [];
for (const input of selected) {
const resolved = path.resolve(projectRoot, input);
if (!fs.existsSync(resolved)) {
continue;
}
const stat = fs.statSync(resolved);
if (stat.isFile()) {
files.push(resolved);
continue;
}
if (stat.isDirectory()) {
walkDirectory(resolved, files);
}
}
return files;
}
function walkDirectory(dir, files) {
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) {
walkDirectory(full, files);
continue;
}
if (/\.(md|js|yaml|yml)$/i.test(entry.name)) {
files.push(full);
}
}
}
function lintContent(content, relativePath) {
const findings = [];
for (const rule of RULES) {
const regex = new RegExp(rule.pattern.source, rule.pattern.flags);
let match;
while ((match = regex.exec(content)) !== null) {
const line = 1 + content.slice(0, match.index).split('\n').length - 1;
findings.push({
ruleId: rule.id,
severity: rule.severity,
term: match[0],
replacement: rule.replacement,
file: relativePath,
line,
reason: rule.reason,
});
}
}
return findings;
}
function runSemanticLint(options = {}, deps = {}) {
const projectRoot = options.projectRoot || process.cwd();
const targets = options.targets || [];
const fileCollector = deps.collectFiles || collectFiles;
const fileReader = deps.readFile || ((filePath) => fs.readFileSync(filePath, 'utf8'));
const files = fileCollector(targets, projectRoot);
const findings = [];
for (const filePath of files) {
const content = fileReader(filePath);
const relativePath = path.relative(projectRoot, filePath);
findings.push(...lintContent(content, relativePath));
}
const errors = findings.filter((f) => f.severity === 'error');
const warnings = findings.filter((f) => f.severity === 'warn');
return {
ok: errors.length === 0,
version: SEMANTIC_RULESET_VERSION,
filesScanned: files.length,
findings,
errors,
warnings,
};
}
function formatHuman(result) {
const lines = [];
lines.push(`Semantic Lint v${result.version}`);
lines.push(`Files scanned: ${result.filesScanned}`);
if (result.findings.length === 0) {
lines.push('✅ No semantic term regressions found');
return lines.join('\n');
}
for (const finding of result.findings) {
lines.push(
`${finding.severity === 'error' ? '❌' : '⚠️'} ${finding.file}:${finding.line} ${finding.ruleId} -> "${finding.term}" (use "${finding.replacement}")`,
);
}
lines.push('');
lines.push(
result.ok
? `✅ Completed with ${result.warnings.length} warning(s)`
: `❌ Failed with ${result.errors.length} error(s) and ${result.warnings.length} warning(s)`,
);
return lines.join('\n');
}
function main() {
const args = parseArgs();
const targets = args.files.length > 0
? args.files
: (args.staged ? [] : DEFAULT_TARGETS);
const result = runSemanticLint({ targets });
if (args.json) {
console.log(JSON.stringify(result, null, 2));
} else {
console.log(formatHuman(result));
}
if (!result.ok) {
process.exitCode = 1;
}
}
if (require.main === module) {
main();
}
module.exports = {
parseArgs,
collectFiles,
lintContent,
runSemanticLint,
RULES,
};