@cassiomc1/forgeloop 1.12.0 → 1.13.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/.github/copilot-instructions.md +1 -1
- package/AGENTS.md +1 -1
- package/CLAUDE.md +1 -1
- package/CONTRIBUTING.md +90 -0
- package/DOCS_INDEX.md +13 -11
- package/ENG/c-development-eng.md +112 -0
- package/ENG/cpp-development-eng.md +109 -0
- package/ENG/dotnet-aspnetcore-development-eng.md +401 -0
- package/ENG/go-development-eng.md +103 -0
- package/ENG/java-development-eng.md +125 -0
- package/ENG/nodejs-backend-development-eng.md +605 -0
- package/ENG/php-development-eng.md +104 -0
- package/ENG/rust-development-eng.md +422 -0
- package/ENG/sql-development-eng.md +108 -0
- package/ENG/swift-development-eng.md +111 -0
- package/ENG/typescript-development-eng.md +108 -0
- package/GUIDE_ROUTER.md +418 -9
- package/QUALITY_SCORECARD.md +1 -0
- package/README.md +44 -33
- package/THIRD_PARTY_NOTICES.md +19 -7
- package/completions/_forgeloop +3 -3
- package/completions/forgeloop.bash +3 -3
- package/completions/forgeloop.fish +7 -0
- package/docs/AGENT_PROTOCOL_SUMMARY.md +55 -2
- package/docs/CLI_REFERENCE.md +28 -6
- package/docs/DOCUMENTATION_GUIDE.md +2 -1
- package/docs/GETTING_STARTED.md +59 -0
- package/docs/PACKAGE_CONTENTS.md +28 -14
- package/docs/RECIPES.md +23 -0
- package/docs/RELEASE_CHECKLIST.md +30 -2
- package/docs/TROUBLESHOOTING.md +100 -2
- package/docs/documentation-manifest.json +652 -0
- package/docs/protocol-requirements.json +77 -0
- package/package.json +19 -4
- package/schemas/routing-input.schema.json +1 -1
- package/scripts/CI_VALIDATORS.md +84 -11
- package/scripts/generate-agent-protocol-summary.mjs +36 -0
- package/src/commands/next.js +19 -7
- package/src/commands/task-create.js +84 -25
- package/src/commands/task-list.js +22 -2
- package/src/config/guides.json +44 -0
- package/src/core/build-script.js +151 -0
- package/src/core/c-cpp-project.js +143 -0
- package/src/core/cli-command-definitions.js +8 -1
- package/src/core/command-executors.js +5 -3
- package/src/core/command-input.js +140 -102
- package/src/core/contract-presets.js +82 -0
- package/src/core/error-codes.js +3 -3
- package/src/core/filesystem.js +1 -10
- package/src/core/go-project.js +206 -0
- package/src/core/java-project.js +403 -0
- package/src/core/multi-language-project.js +117 -0
- package/src/core/next-explanation.js +63 -0
- package/src/core/php-project.js +85 -0
- package/src/core/project-detection.js +1760 -52
- package/src/core/reconcile-closure.js +4 -1
- package/src/core/router.js +156 -3
- package/src/core/rust-project.js +400 -0
- package/src/core/sql-project.js +141 -0
- package/src/core/swift-project.js +200 -0
- package/src/core/typescript-project.js +349 -0
- package/src/core/xml-structure.js +123 -0
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { parseXmlStructure } from "./xml-structure.js";
|
|
2
|
+
|
|
3
|
+
const SQL_STATEMENT_PATTERNS = Object.freeze([
|
|
4
|
+
/(?:^|[;\n])\s*select\b[\s\S]{0,240}\bfrom\s+[A-Za-z_][A-Za-z0-9_$]*(?:\s*\.\s*[A-Za-z_][A-Za-z0-9_$]*)*(?:\s*(?:;|$)|\s+(?:where|group|order|having|limit|offset|union|join|left|right|inner|outer|on)\b)/imu,
|
|
5
|
+
/(?:^|[;\n])\s*select\s+(?:distinct\s+)?[-+]?\d+(?:\s*,\s*[-+]?\d+)*\s*;?\s*(?:$|[\n])/imu,
|
|
6
|
+
/(?:^|[;\n])\s*insert\s+into\b/imu,
|
|
7
|
+
/(?:^|[;\n])\s*update\b[\s\S]{0,160}\bset\b/imu,
|
|
8
|
+
/(?:^|[;\n])\s*delete\s+from\b/imu,
|
|
9
|
+
/(?:^|[;\n])\s*merge\s+into\b/imu,
|
|
10
|
+
/(?:^|[;\n])\s*with\b[\s\S]{0,1024}\bas\s*\([\s\S]*\)\s*(?:select|insert|update|delete|merge)\b/imu,
|
|
11
|
+
/(?:^|[;\n])\s*create\s+(?:table|view|index|schema|database|function|procedure|trigger|type|sequence)\s+(?:if\s+not\s+exists\s+)?[A-Za-z_][A-Za-z0-9_$]*(?:\s*\.\s*[A-Za-z_][A-Za-z0-9_$]*)*(?:\s*(?:\(|;|$)|\s+(?:as|like)\b)/imu,
|
|
12
|
+
/(?:^|[;\n])\s*alter\s+(?:table|view|index|schema|database|function|procedure|trigger|type|sequence)\s+(?:if\s+exists\s+)?[A-Za-z_][A-Za-z0-9_$]*(?:\s*\.\s*[A-Za-z_][A-Za-z0-9_$]*)*(?:\s*(?:;|$)|\s+(?:add|drop|alter|rename|set|owner|enable|disable)\b)/imu,
|
|
13
|
+
/(?:^|[;\n])\s*drop\s+(?:table|view|index|schema|database|function|procedure|trigger|type|sequence)\s+(?:if\s+exists\s+)?[A-Za-z_][A-Za-z0-9_$]*(?:\s*\.\s*[A-Za-z_][A-Za-z0-9_$]*)*(?:\s*(?:;|$)|\s+(?:cascade|restrict)\b)/imu,
|
|
14
|
+
/(?:^|[;\n])\s*(?:grant|revoke)\b[\s\S]{0,240}\bon\b/imu,
|
|
15
|
+
/(?:^|[;\n])\s*begin(?:\s+(?:transaction|work))?\s*;?\s*(?:$|[\n])/imu,
|
|
16
|
+
/(?:^|[;\n])\s*(?:commit|rollback)(?:\s+(?:transaction|work))?\s*;?\s*(?:$|[\n])/imu,
|
|
17
|
+
/(?:^|[;\n])\s*savepoint\s+[A-Za-z_][A-Za-z0-9_$]*\s*;?/imu,
|
|
18
|
+
/(?:^|[;\n])\s*release\s+savepoint\s+[A-Za-z_][A-Za-z0-9_$]*\s*;?/imu,
|
|
19
|
+
]);
|
|
20
|
+
|
|
21
|
+
function maskSqlComment(text, start, block) {
|
|
22
|
+
const lineEnd = text.slice(start).search(/[\n\r]/u);
|
|
23
|
+
const end = block ? text.indexOf("*/", start + 2) : lineEnd < 0 ? -1 : start + lineEnd;
|
|
24
|
+
if (block && end < 0) return null;
|
|
25
|
+
const finish = block ? end + 2 : (end < 0 ? text.length : end);
|
|
26
|
+
const value = text.slice(start, finish);
|
|
27
|
+
return { text: value.replace(/[^\n\r]/gu, " "), next: finish };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function maskSqlQuoted(text, start, quote) {
|
|
31
|
+
let result = " ";
|
|
32
|
+
for (let index = start + 1; index < text.length; index += 1) {
|
|
33
|
+
const character = text[index];
|
|
34
|
+
const next = text[index + 1];
|
|
35
|
+
if (character === quote && next === quote) {
|
|
36
|
+
result += " ";
|
|
37
|
+
index += 1;
|
|
38
|
+
} else if (character === quote) {
|
|
39
|
+
return { text: result + " ", next: index + 1 };
|
|
40
|
+
} else {
|
|
41
|
+
result += character === "\n" || character === "\r" ? character : " ";
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function maskSqlQuotedIdentifier(text, start, quote) {
|
|
48
|
+
for (let index = start + 1; index < text.length; index += 1) {
|
|
49
|
+
const character = text[index];
|
|
50
|
+
const next = text[index + 1];
|
|
51
|
+
if (character === quote && next === quote) {
|
|
52
|
+
index += 1;
|
|
53
|
+
} else if (character === quote) {
|
|
54
|
+
return { text: "__identifier__", next: index + 1 };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function maskSqlBracketIdentifier(text, start) {
|
|
61
|
+
for (let index = start + 1; index < text.length; index += 1) {
|
|
62
|
+
if (text[index] === "]") {
|
|
63
|
+
if (text[index + 1] === "]") {
|
|
64
|
+
index += 1;
|
|
65
|
+
} else {
|
|
66
|
+
return { text: "__identifier__", next: index + 1 };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function maskSqlDollarQuote(text, start) {
|
|
74
|
+
const marker = text.slice(start).match(/^\$[A-Za-z_][A-Za-z0-9_]*\$|^\$\$/u)?.[0];
|
|
75
|
+
if (!marker) return null;
|
|
76
|
+
const end = text.indexOf(marker, start + marker.length);
|
|
77
|
+
if (end < 0) return { invalid: true };
|
|
78
|
+
const finish = end + marker.length;
|
|
79
|
+
return { text: text.slice(start, finish).replace(/[^\n\r]/gu, " "), next: finish };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function maskSqlToken(text, index) {
|
|
83
|
+
const character = text[index];
|
|
84
|
+
const next = text[index + 1];
|
|
85
|
+
if (character === "-" && next === "-") {
|
|
86
|
+
return maskSqlComment(text, index, false);
|
|
87
|
+
}
|
|
88
|
+
if (character === "/" && next === "*") return maskSqlComment(text, index, true);
|
|
89
|
+
if (character === "'") return maskSqlQuoted(text, index, character);
|
|
90
|
+
if (character === '"' || character === "`") return maskSqlQuotedIdentifier(text, index, character);
|
|
91
|
+
if (character === "[") return maskSqlBracketIdentifier(text, index);
|
|
92
|
+
if (character === "$") {
|
|
93
|
+
const dollarQuote = maskSqlDollarQuote(text, index);
|
|
94
|
+
if (dollarQuote?.invalid) return null;
|
|
95
|
+
return dollarQuote ?? { text: character, next: index + 1 };
|
|
96
|
+
}
|
|
97
|
+
return { text: character, next: index + 1 };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function maskSql(text) {
|
|
101
|
+
let result = "";
|
|
102
|
+
for (let index = 0; index < text.length;) {
|
|
103
|
+
const segment = maskSqlToken(text, index);
|
|
104
|
+
if (!segment) return null;
|
|
105
|
+
result += segment.text;
|
|
106
|
+
index = segment.next;
|
|
107
|
+
}
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function looksLikeSql(text) {
|
|
112
|
+
if (typeof text !== "string" || text.length > 512 * 1024 || /\r(?!\n)/u.test(text)) return false;
|
|
113
|
+
const masked = maskSql(text);
|
|
114
|
+
return masked !== null && SQL_STATEMENT_PATTERNS.some((pattern) => pattern.test(masked));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function sqlSignal(text) {
|
|
118
|
+
if (!looksLikeSql(text)) return null;
|
|
119
|
+
const masked = maskSql(text);
|
|
120
|
+
return masked === null ? null : SQL_STATEMENT_PATTERNS.find((pattern) => pattern.test(masked))?.source ?? "statement";
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function childValues(node, name) {
|
|
124
|
+
return node.children
|
|
125
|
+
.filter((child) => child.name.toLowerCase() === name)
|
|
126
|
+
.map((child) => child.text.trim())
|
|
127
|
+
.filter(Boolean);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function parseSqlProject(text) {
|
|
131
|
+
const document = parseXmlStructure(text);
|
|
132
|
+
if (!document || document.root.name.toLowerCase() !== "project") return { valid: false, database: false };
|
|
133
|
+
const sdk = document.root.attributes.Sdk ?? document.root.attributes.sdk ?? "";
|
|
134
|
+
const databaseMarkers = [
|
|
135
|
+
sdk.toLowerCase().startsWith("microsoft.build.sql/"),
|
|
136
|
+
childValues(document.root, "dsp").length > 0,
|
|
137
|
+
document.nodes.some((node) => ["databaseproject", "sqlscript"].includes(node.name.toLowerCase())),
|
|
138
|
+
];
|
|
139
|
+
const database = databaseMarkers.some(Boolean);
|
|
140
|
+
return { valid: database, database };
|
|
141
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { createLanguageProject, C_SOURCE_EXTENSIONS, CPP_SOURCE_EXTENSIONS, SWIFT_SOURCE_EXTENSIONS, pathBelongsToRoot, portablePath } from "./multi-language-project.js";
|
|
2
|
+
import { extractBuildCalls, maskBuildScript, quotedBuildArguments } from "./build-script.js";
|
|
3
|
+
|
|
4
|
+
function invalidSwift() {
|
|
5
|
+
return { valid: false, swift: false, c: false, cpp: false };
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function stripSwiftComments(text) {
|
|
9
|
+
let result = "";
|
|
10
|
+
let state = "code";
|
|
11
|
+
let escaped = false;
|
|
12
|
+
let blockDepth = 0;
|
|
13
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
14
|
+
const character = text[index];
|
|
15
|
+
const next = text[index + 1];
|
|
16
|
+
if (state === "string") {
|
|
17
|
+
result += character;
|
|
18
|
+
if (escaped) escaped = false;
|
|
19
|
+
else if (character === "\\") escaped = true;
|
|
20
|
+
else if (character === '"') state = "code";
|
|
21
|
+
} else if (state === "line-comment") {
|
|
22
|
+
if (character === "\n") {
|
|
23
|
+
result += character;
|
|
24
|
+
state = "code";
|
|
25
|
+
} else result += " ";
|
|
26
|
+
} else if (state === "block-comment") {
|
|
27
|
+
if (character === "/" && next === "*") {
|
|
28
|
+
blockDepth += 1;
|
|
29
|
+
result += " ";
|
|
30
|
+
index += 1;
|
|
31
|
+
} else if (character === "*" && next === "/") {
|
|
32
|
+
blockDepth -= 1;
|
|
33
|
+
result += " ";
|
|
34
|
+
index += 1;
|
|
35
|
+
if (blockDepth === 0) state = "code";
|
|
36
|
+
} else result += character === "\n" ? "\n" : " ";
|
|
37
|
+
} else if (character === '"') {
|
|
38
|
+
result += character;
|
|
39
|
+
state = "string";
|
|
40
|
+
} else if (character === "/" && next === "/") {
|
|
41
|
+
result += " ";
|
|
42
|
+
index += 1;
|
|
43
|
+
state = "line-comment";
|
|
44
|
+
} else if (character === "/" && next === "*") {
|
|
45
|
+
result += " ";
|
|
46
|
+
index += 1;
|
|
47
|
+
blockDepth = 1;
|
|
48
|
+
state = "block-comment";
|
|
49
|
+
} else result += character;
|
|
50
|
+
}
|
|
51
|
+
return state === "block-comment" ? null : result;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function maskSwiftStrings(text) {
|
|
55
|
+
let result = "";
|
|
56
|
+
let state = "code";
|
|
57
|
+
let escaped = false;
|
|
58
|
+
let delimiter = null;
|
|
59
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
60
|
+
const character = text[index];
|
|
61
|
+
const rawMatch = character === "#" ? text.slice(index).match(/^(#+)("{1,3})/u) : null;
|
|
62
|
+
if (state === "string") {
|
|
63
|
+
if (escaped) escaped = false;
|
|
64
|
+
else if (character === "\\") escaped = true;
|
|
65
|
+
else if (text.startsWith(delimiter, index)) {
|
|
66
|
+
result += " ".repeat(delimiter.length);
|
|
67
|
+
index += delimiter.length - 1;
|
|
68
|
+
state = "code";
|
|
69
|
+
delimiter = null;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
result += character === "\n" ? "\n" : " ";
|
|
73
|
+
} else if (character === '"') {
|
|
74
|
+
delimiter = text.startsWith('"""', index) ? '"""' : '"';
|
|
75
|
+
state = "string";
|
|
76
|
+
result += " ".repeat(delimiter.length);
|
|
77
|
+
index += delimiter.length - 1;
|
|
78
|
+
} else if (rawMatch) {
|
|
79
|
+
delimiter = `${rawMatch[2]}${rawMatch[1]}`;
|
|
80
|
+
state = "string";
|
|
81
|
+
result += " ".repeat(rawMatch[0].length);
|
|
82
|
+
index += rawMatch[0].length - 1;
|
|
83
|
+
} else {
|
|
84
|
+
result += character;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return state === "string" ? null : result;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function maskSwiftLexicalNoise(text) {
|
|
91
|
+
const withoutComments = stripSwiftComments(text);
|
|
92
|
+
return withoutComments === null ? null : maskSwiftStrings(withoutComments);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function hasBalancedSwiftDelimiters(source) {
|
|
96
|
+
const expectedClosers = new Map([
|
|
97
|
+
["(", ")"],
|
|
98
|
+
["{", "}"],
|
|
99
|
+
["[", "]"],
|
|
100
|
+
]);
|
|
101
|
+
const stack = [];
|
|
102
|
+
for (const character of source) {
|
|
103
|
+
if (expectedClosers.has(character)) {
|
|
104
|
+
stack.push(expectedClosers.get(character));
|
|
105
|
+
} else if (")]}".includes(character) && stack.pop() !== character) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return stack.length === 0;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function parseSwiftPackage(text) {
|
|
113
|
+
if (typeof text !== "string" || text.length > 1024 * 1024 || /\r(?!\n)/u.test(text)) return invalidSwift();
|
|
114
|
+
const toolsVersion = /^\s*\/\/\s*swift-tools-version:\s*\d+(?:\.\d+){1,2}\s*$/mu.test(text);
|
|
115
|
+
const source = maskSwiftLexicalNoise(text);
|
|
116
|
+
const packageDescription = source !== null && /\bimport\s+PackageDescription\b/u.test(source);
|
|
117
|
+
const balanced = source !== null && hasBalancedSwiftDelimiters(source);
|
|
118
|
+
const packageDeclaration = source !== null && /\bPackage\s*\(/u.test(source);
|
|
119
|
+
return {
|
|
120
|
+
valid: toolsVersion && packageDescription && packageDeclaration && balanced,
|
|
121
|
+
swift: toolsVersion && packageDescription && packageDeclaration && balanced,
|
|
122
|
+
c: false,
|
|
123
|
+
cpp: false,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function parseXcodeProject(text) {
|
|
128
|
+
if (typeof text !== "string" || text.length > 1024 * 1024 || /\r(?!\n)/u.test(text)) return invalidSwift();
|
|
129
|
+
const source = stripSwiftComments(text);
|
|
130
|
+
const swift = source !== null && /\bSWIFT_VERSION\s*=/u.test(source) && /\.swift\b/u.test(source);
|
|
131
|
+
return { valid: swift, swift, c: false, cpp: false };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function parseSwiftCMake(text) {
|
|
135
|
+
if (typeof text !== "string" || text.length > 1024 * 1024 || /\r(?!\n)/u.test(text)) return invalidSwift();
|
|
136
|
+
const source = maskBuildScript(text);
|
|
137
|
+
const swift = source !== null && /\b(?:LANGUAGES[^\n)]*\bSwift\b|enable_language\s*\(\s*Swift\b)/iu.test(source);
|
|
138
|
+
return { valid: swift, swift, c: false, cpp: false };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function parseSwiftMeson(text) {
|
|
142
|
+
if (typeof text !== "string" || text.length > 1024 * 1024 || /\r(?!\n)/u.test(text)) return invalidSwift();
|
|
143
|
+
const calls = extractBuildCalls(text, ["project", "add_languages"]);
|
|
144
|
+
const swift = Boolean(calls?.some((call) => {
|
|
145
|
+
const values = quotedBuildArguments(call.body) ?? [];
|
|
146
|
+
return (call.name === "project" ? values.slice(1) : values)
|
|
147
|
+
.some((value) => value.toLowerCase() === "swift");
|
|
148
|
+
}));
|
|
149
|
+
return { valid: swift, swift, c: false, cpp: false };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function sourceEvidence(sourceFiles, targetRoot, projectRoot, projectRoots) {
|
|
153
|
+
const owns = (file) => pathBelongsToRoot(portablePath(targetRoot, file.path), projectRoot, projectRoots);
|
|
154
|
+
return {
|
|
155
|
+
swift: sourceFiles.some((file) => SWIFT_SOURCE_EXTENSIONS.has(file.extension) && owns(file)),
|
|
156
|
+
c: sourceFiles.some((file) => C_SOURCE_EXTENSIONS.has(file.extension) && owns(file)),
|
|
157
|
+
cpp: sourceFiles.some((file) => CPP_SOURCE_EXTENSIONS.has(file.extension) && owns(file)),
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function swiftSourceLooksExecutable(text) {
|
|
162
|
+
const source = maskSwiftLexicalNoise(text);
|
|
163
|
+
return source !== null && /\b(?:import|class|struct|enum|protocol|func|let|var)\b/u.test(source);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function inspectSwiftProject({ projectRoot, manifestRelative, manifestText, manifestName, targetRoot, sourceFiles, projectRoots, directClaim = false }) {
|
|
167
|
+
const normalizedManifestName = manifestName.toLowerCase();
|
|
168
|
+
const parsed = normalizedManifestName === "package.swift"
|
|
169
|
+
? parseSwiftPackage(manifestText)
|
|
170
|
+
: normalizedManifestName === "project.pbxproj"
|
|
171
|
+
? parseXcodeProject(manifestText)
|
|
172
|
+
: normalizedManifestName === "meson.build"
|
|
173
|
+
? parseSwiftMeson(manifestText)
|
|
174
|
+
: parseSwiftCMake(manifestText);
|
|
175
|
+
const source = sourceEvidence(sourceFiles, targetRoot, projectRoot, projectRoots);
|
|
176
|
+
const swift = parsed.swift
|
|
177
|
+
&& (normalizedManifestName !== "package.swift" || source.swift || directClaim);
|
|
178
|
+
const frameworks = [];
|
|
179
|
+
if (swift) frameworks.push("swift");
|
|
180
|
+
if (normalizedManifestName === "package.swift" && parsed.valid && source.c) frameworks.push("c");
|
|
181
|
+
if (normalizedManifestName === "package.swift" && parsed.valid && source.cpp) frameworks.push("cpp");
|
|
182
|
+
const primary = frameworks.length > 0;
|
|
183
|
+
const primarySignals = [];
|
|
184
|
+
const manifestSignal = normalizedManifestName === "package.swift"
|
|
185
|
+
? "package"
|
|
186
|
+
: normalizedManifestName === "project.pbxproj" ? "xcode" : "build";
|
|
187
|
+
if (swift) primarySignals.push(`${manifestRelative}:${manifestSignal}`);
|
|
188
|
+
if (normalizedManifestName === "package.swift" && parsed.valid && source.c) primarySignals.push(`${projectRoot}/source:c`);
|
|
189
|
+
if (normalizedManifestName === "package.swift" && parsed.valid && source.cpp) primarySignals.push(`${projectRoot}/source:cpp`);
|
|
190
|
+
return createLanguageProject({
|
|
191
|
+
kind: "swift",
|
|
192
|
+
root: projectRoot,
|
|
193
|
+
manifest: manifestRelative,
|
|
194
|
+
frameworks,
|
|
195
|
+
primary,
|
|
196
|
+
primarySignals,
|
|
197
|
+
supportingSignals: [],
|
|
198
|
+
internal: { ...parsed, valid: parsed.valid, source },
|
|
199
|
+
});
|
|
200
|
+
}
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
|
|
3
|
+
import { createLanguageProject } from "./multi-language-project.js";
|
|
4
|
+
|
|
5
|
+
const TS_CONFIG_NAME = /^tsconfig(?:\.[^/]+)?\.json$/u;
|
|
6
|
+
|
|
7
|
+
function isPlainObject(value) {
|
|
8
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function invalidConfig() {
|
|
12
|
+
return {
|
|
13
|
+
valid: false,
|
|
14
|
+
references: [],
|
|
15
|
+
extends: [],
|
|
16
|
+
compilerOptions: {},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function maskJsonComments(text) {
|
|
21
|
+
let result = "";
|
|
22
|
+
let state = "code";
|
|
23
|
+
let escaped = false;
|
|
24
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
25
|
+
const character = text[index];
|
|
26
|
+
const next = text[index + 1];
|
|
27
|
+
if (state === "string") {
|
|
28
|
+
result += character;
|
|
29
|
+
if (escaped) escaped = false;
|
|
30
|
+
else if (character === "\\") escaped = true;
|
|
31
|
+
else if (character === '"') state = "code";
|
|
32
|
+
} else if (state === "line-comment") {
|
|
33
|
+
if (character === "\n") {
|
|
34
|
+
result += character;
|
|
35
|
+
state = "code";
|
|
36
|
+
} else result += " ";
|
|
37
|
+
} else if (state === "block-comment") {
|
|
38
|
+
if (character === "*" && next === "/") {
|
|
39
|
+
result += " ";
|
|
40
|
+
index += 1;
|
|
41
|
+
state = "code";
|
|
42
|
+
} else result += character === "\n" ? "\n" : " ";
|
|
43
|
+
} else if (character === '"') {
|
|
44
|
+
result += character;
|
|
45
|
+
state = "string";
|
|
46
|
+
} else if (character === "/" && next === "/") {
|
|
47
|
+
result += " ";
|
|
48
|
+
index += 1;
|
|
49
|
+
state = "line-comment";
|
|
50
|
+
} else if (character === "/" && next === "*") {
|
|
51
|
+
result += " ";
|
|
52
|
+
index += 1;
|
|
53
|
+
state = "block-comment";
|
|
54
|
+
} else result += character;
|
|
55
|
+
}
|
|
56
|
+
return state === "block-comment" ? null : result;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function removeTrailingCommas(text) {
|
|
60
|
+
let result = "";
|
|
61
|
+
let state = "code";
|
|
62
|
+
let escaped = false;
|
|
63
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
64
|
+
const character = text[index];
|
|
65
|
+
if (state === "string") {
|
|
66
|
+
result += character;
|
|
67
|
+
if (escaped) escaped = false;
|
|
68
|
+
else if (character === "\\") escaped = true;
|
|
69
|
+
else if (character === '"') state = "code";
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (character === '"') {
|
|
73
|
+
result += character;
|
|
74
|
+
state = "string";
|
|
75
|
+
} else if (character === "," && /^[\s]*[}\]]/u.test(text.slice(index + 1))) {
|
|
76
|
+
continue;
|
|
77
|
+
} else result += character;
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function isTypeScriptConfigName(fileName) {
|
|
83
|
+
return typeof fileName === "string" && TS_CONFIG_NAME.test(fileName.toLowerCase());
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function normalizedReferencePath(projectRoot, reference) {
|
|
87
|
+
return path.posix.normalize(path.posix.join(projectRoot, reference.replaceAll("\\", "/")));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function referenceMatchesConfig(projectRoot, reference, configRelative, allowBareReference = false) {
|
|
91
|
+
if (typeof reference !== "string" || reference.trim() === "") return false;
|
|
92
|
+
const normalizedReference = reference.replaceAll("\\", "/");
|
|
93
|
+
if ((!allowBareReference && !normalizedReference.startsWith("."))
|
|
94
|
+
|| normalizedReference.startsWith("/")
|
|
95
|
+
|| /^[A-Za-z]:\//u.test(normalizedReference)) return false;
|
|
96
|
+
const candidate = normalizedReferencePath(projectRoot, normalizedReference);
|
|
97
|
+
if (candidate === ".." || candidate.startsWith("../")) return false;
|
|
98
|
+
const normalizedConfig = configRelative.toLowerCase();
|
|
99
|
+
const normalizedCandidate = candidate.toLowerCase();
|
|
100
|
+
return normalizedConfig === normalizedCandidate
|
|
101
|
+
|| normalizedConfig === normalizedCandidate + ".json"
|
|
102
|
+
|| normalizedConfig === normalizedCandidate + "/tsconfig.json";
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function isConfigCandidate(file) {
|
|
106
|
+
return file?.kind === "typescript" || file?.kind === "typescript-config";
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function localConfigReferences(configRelative, projectFiles) {
|
|
110
|
+
return projectFiles.filter((file) => {
|
|
111
|
+
if (!isConfigCandidate(file)) return false;
|
|
112
|
+
const parsed = parseTypeScriptConfig(file.text, file.name);
|
|
113
|
+
return parsed.valid && (parsed.references
|
|
114
|
+
.some((reference) => referenceMatchesConfig(path.posix.dirname(file.relative), reference, configRelative, true))
|
|
115
|
+
|| parsed.extends.some((reference) => referenceMatchesConfig(
|
|
116
|
+
path.posix.dirname(file.relative),
|
|
117
|
+
reference,
|
|
118
|
+
configRelative,
|
|
119
|
+
)));
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function configRelationships(projectFiles) {
|
|
124
|
+
const entries = projectFiles
|
|
125
|
+
.filter(isConfigCandidate)
|
|
126
|
+
.map((file) => ({ file, parsed: parseTypeScriptConfig(file.text, file.name) }))
|
|
127
|
+
.filter(({ parsed }) => parsed.valid);
|
|
128
|
+
const forward = new Map();
|
|
129
|
+
const reverse = new Map();
|
|
130
|
+
for (const { file, parsed } of entries) {
|
|
131
|
+
const relations = [
|
|
132
|
+
...parsed.references.map((value) => ({ value, kind: "reference", allowBare: true })),
|
|
133
|
+
...parsed.extends.map((value) => ({ value, kind: "extends", allowBare: false })),
|
|
134
|
+
];
|
|
135
|
+
for (const relation of relations) {
|
|
136
|
+
const target = entries.find(({ file: candidate }) => referenceMatchesConfig(
|
|
137
|
+
path.posix.dirname(file.relative), relation.value, candidate.relative, relation.allowBare,
|
|
138
|
+
));
|
|
139
|
+
if (!target) continue;
|
|
140
|
+
const sourceRelative = file.relative.toLowerCase();
|
|
141
|
+
const targetRelative = target.file.relative.toLowerCase();
|
|
142
|
+
if (!forward.has(sourceRelative)) forward.set(sourceRelative, []);
|
|
143
|
+
if (!reverse.has(targetRelative)) reverse.set(targetRelative, []);
|
|
144
|
+
forward.get(sourceRelative).push({ relative: targetRelative, kind: relation.kind });
|
|
145
|
+
reverse.get(targetRelative).push({ relative: sourceRelative, kind: relation.kind });
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return { entries, forward, reverse };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function resolveTypeScriptConfigGraph(projectFiles = [], directClaims = []) {
|
|
152
|
+
const { entries, forward, reverse } = configRelationships(projectFiles);
|
|
153
|
+
const byRelative = new Map(entries.map((entry) => [entry.file.relative.toLowerCase(), entry]));
|
|
154
|
+
const active = new Set(entries
|
|
155
|
+
.filter(({ file }) => file.name.toLowerCase() === "tsconfig.json")
|
|
156
|
+
.map(({ file }) => file.relative.toLowerCase()));
|
|
157
|
+
for (const claim of directClaims) {
|
|
158
|
+
if (byRelative.has(claim.toLowerCase())) active.add(claim.toLowerCase());
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const queue = [...active];
|
|
162
|
+
for (let index = 0; index < queue.length; index += 1) {
|
|
163
|
+
const current = byRelative.get(queue[index]);
|
|
164
|
+
const parsed = current?.parsed;
|
|
165
|
+
if (!parsed) continue;
|
|
166
|
+
for (const relation of forward.get(queue[index]) ?? []) {
|
|
167
|
+
if (!active.has(relation.relative)) {
|
|
168
|
+
active.add(relation.relative);
|
|
169
|
+
queue.push(relation.relative);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (directClaims.length > 0) {
|
|
173
|
+
for (const relation of reverse.get(queue[index]) ?? []) {
|
|
174
|
+
if (!active.has(relation.relative)) {
|
|
175
|
+
active.add(relation.relative);
|
|
176
|
+
queue.push(relation.relative);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return active;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function resolveTypeScriptConfigOwnershipRoots(projectFiles = [], activeConfigRelatives = new Set(), directClaims = []) {
|
|
185
|
+
const { entries, reverse } = configRelationships(projectFiles);
|
|
186
|
+
const candidates = entries.filter(({ file }) => activeConfigRelatives.has(file.relative.toLowerCase()));
|
|
187
|
+
const hasIndependentConfigSemantics = (parsed) => (parsed.compilerOptions
|
|
188
|
+
&& Object.keys(parsed.compilerOptions).length > 0)
|
|
189
|
+
|| parsed.references.length > 0
|
|
190
|
+
|| parsed.extends.length > 0;
|
|
191
|
+
const roots = new Set(candidates
|
|
192
|
+
.filter(({ file, parsed }) => file.name.toLowerCase() === "tsconfig.json"
|
|
193
|
+
&& (directClaims.length === 0 || hasIndependentConfigSemantics(parsed)))
|
|
194
|
+
.map(({ file }) => file.relative.toLowerCase()));
|
|
195
|
+
for (const claim of directClaims) {
|
|
196
|
+
const relative = claim.toLowerCase();
|
|
197
|
+
const consumers = (reverse.get(relative) ?? [])
|
|
198
|
+
.filter((relation) => relation.kind === "extends")
|
|
199
|
+
.map((relation) => relation.relative);
|
|
200
|
+
if (consumers.length === 0 && candidates.some(({ file }) => file.relative.toLowerCase() === relative)) {
|
|
201
|
+
roots.add(relative);
|
|
202
|
+
}
|
|
203
|
+
const queue = [...consumers];
|
|
204
|
+
const seen = new Set(queue);
|
|
205
|
+
for (let index = 0; index < queue.length; index += 1) {
|
|
206
|
+
const consumer = candidates.find(({ file }) => file.relative.toLowerCase() === queue[index]);
|
|
207
|
+
if (consumer?.file.name.toLowerCase() === "tsconfig.json") roots.add(queue[index]);
|
|
208
|
+
for (const relation of reverse.get(queue[index]) ?? []) {
|
|
209
|
+
if (relation.kind === "extends" && !seen.has(relation.relative)) {
|
|
210
|
+
seen.add(relation.relative);
|
|
211
|
+
queue.push(relation.relative);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
for (const { file, parsed } of candidates) {
|
|
217
|
+
for (const reference of parsed.references) {
|
|
218
|
+
const target = candidates.find(({ file: candidate }) => referenceMatchesConfig(
|
|
219
|
+
path.posix.dirname(file.relative),
|
|
220
|
+
reference,
|
|
221
|
+
candidate.relative,
|
|
222
|
+
true,
|
|
223
|
+
));
|
|
224
|
+
if (target) roots.add(target.file.relative.toLowerCase());
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return roots;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export function shouldActivateTypeScriptConfig({
|
|
231
|
+
manifestName,
|
|
232
|
+
manifestRelative,
|
|
233
|
+
projectFiles = [],
|
|
234
|
+
directClaim = false,
|
|
235
|
+
configKind = "typescript",
|
|
236
|
+
activeConfigRelatives = null,
|
|
237
|
+
ownershipConfigRelatives = null,
|
|
238
|
+
}) {
|
|
239
|
+
if (!isConfigCandidate({ kind: configKind }) || manifestName.toLowerCase() === "jsconfig.json") return false;
|
|
240
|
+
if (activeConfigRelatives) {
|
|
241
|
+
return activeConfigRelatives.has(manifestRelative.toLowerCase())
|
|
242
|
+
&& (!ownershipConfigRelatives || ownershipConfigRelatives.has(manifestRelative.toLowerCase()));
|
|
243
|
+
}
|
|
244
|
+
if (manifestName.toLowerCase() === "tsconfig.json" || directClaim) return true;
|
|
245
|
+
return localConfigReferences(manifestRelative, projectFiles).length > 0;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export function parseJsonc(text) {
|
|
249
|
+
if (typeof text !== "string" || text.length > 1024 * 1024 || /\r(?!\n)/u.test(text)) return null;
|
|
250
|
+
try {
|
|
251
|
+
return JSON.parse(removeTrailingCommas(maskJsonComments(text)));
|
|
252
|
+
} catch {
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export function parseTypeScriptConfig(text, fileName = "tsconfig.json") {
|
|
258
|
+
const value = parseJsonc(text);
|
|
259
|
+
if (!isPlainObject(value)) return invalidConfig();
|
|
260
|
+
const references = value.references === undefined
|
|
261
|
+
? []
|
|
262
|
+
: Array.isArray(value.references) && value.references.every((reference) => isPlainObject(reference) && typeof reference.path === "string" && reference.path.trim() !== "")
|
|
263
|
+
? value.references.map((reference) => reference.path)
|
|
264
|
+
: null;
|
|
265
|
+
const extendsValue = value.extends === undefined
|
|
266
|
+
? []
|
|
267
|
+
: typeof value.extends === "string" && value.extends.trim() !== ""
|
|
268
|
+
? [value.extends]
|
|
269
|
+
: Array.isArray(value.extends) && value.extends.every((reference) => typeof reference === "string" && reference.trim() !== "")
|
|
270
|
+
? [...value.extends]
|
|
271
|
+
: undefined;
|
|
272
|
+
const compilerOptions = value.compilerOptions === undefined
|
|
273
|
+
? {}
|
|
274
|
+
: isPlainObject(value.compilerOptions) ? value.compilerOptions : null;
|
|
275
|
+
if (references === null || extendsValue === undefined || compilerOptions === null) return invalidConfig();
|
|
276
|
+
for (const key of ["files", "include", "exclude"]) {
|
|
277
|
+
if (value[key] !== undefined && (!Array.isArray(value[key])
|
|
278
|
+
|| value[key].some((entry) => typeof entry !== "string" || entry.trim() === ""))) return invalidConfig();
|
|
279
|
+
}
|
|
280
|
+
return {
|
|
281
|
+
valid: true,
|
|
282
|
+
references,
|
|
283
|
+
extends: extendsValue,
|
|
284
|
+
compilerOptions,
|
|
285
|
+
fileName,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export function inspectTypeScriptProject({
|
|
290
|
+
projectRoot,
|
|
291
|
+
manifestRelative,
|
|
292
|
+
manifestText,
|
|
293
|
+
manifestName,
|
|
294
|
+
targetRoot,
|
|
295
|
+
projectFiles,
|
|
296
|
+
directClaim = false,
|
|
297
|
+
configKind = "typescript",
|
|
298
|
+
activeConfigRelatives = null,
|
|
299
|
+
ownershipConfigRelatives = null,
|
|
300
|
+
}) {
|
|
301
|
+
const parsed = parseTypeScriptConfig(manifestText, manifestName);
|
|
302
|
+
const isConfig = shouldActivateTypeScriptConfig({
|
|
303
|
+
manifestName,
|
|
304
|
+
manifestRelative,
|
|
305
|
+
projectFiles,
|
|
306
|
+
directClaim,
|
|
307
|
+
configKind,
|
|
308
|
+
activeConfigRelatives,
|
|
309
|
+
ownershipConfigRelatives,
|
|
310
|
+
});
|
|
311
|
+
const primary = parsed.valid && isConfig;
|
|
312
|
+
const primarySignals = primary ? [`${manifestRelative}:tsconfig`] : [];
|
|
313
|
+
const supportingSignals = [];
|
|
314
|
+
for (const reference of parsed.references) supportingSignals.push(`${manifestRelative}:reference=${reference}`);
|
|
315
|
+
for (const reference of parsed.extends) supportingSignals.push(`${manifestRelative}:extends=${reference}`);
|
|
316
|
+
const localReferences = parsed.references.filter((reference) => {
|
|
317
|
+
return projectFiles.some((file) => referenceMatchesConfig(
|
|
318
|
+
projectRoot,
|
|
319
|
+
reference,
|
|
320
|
+
path.posix.normalize(file.relative ?? ""),
|
|
321
|
+
true,
|
|
322
|
+
));
|
|
323
|
+
});
|
|
324
|
+
const localExtends = parsed.extends.flatMap((reference) => projectFiles
|
|
325
|
+
.filter((file) => referenceMatchesConfig(path.posix.dirname(manifestRelative), reference, file.relative))
|
|
326
|
+
.map((file) => file.relative.toLowerCase()));
|
|
327
|
+
for (const reference of localReferences) supportingSignals.push(`${manifestRelative}:local-reference=${reference}`);
|
|
328
|
+
for (const reference of parsed.extends) {
|
|
329
|
+
if (projectFiles.some((file) => referenceMatchesConfig(
|
|
330
|
+
path.posix.dirname(manifestRelative),
|
|
331
|
+
reference,
|
|
332
|
+
file.relative,
|
|
333
|
+
))) supportingSignals.push(`${manifestRelative}:local-extends=${reference}`);
|
|
334
|
+
}
|
|
335
|
+
return createLanguageProject({
|
|
336
|
+
kind: "typescript",
|
|
337
|
+
root: projectRoot,
|
|
338
|
+
manifest: manifestRelative,
|
|
339
|
+
frameworks: primary ? ["typescript"] : [],
|
|
340
|
+
primary,
|
|
341
|
+
primarySignals,
|
|
342
|
+
supportingSignals,
|
|
343
|
+
internal: { ...parsed, valid: parsed.valid, localReferences, localExtends },
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
export function isDeclarationFile(fileName) {
|
|
348
|
+
return typeof fileName === "string" && /\.d\.(?:ts|mts|cts)$/iu.test(fileName);
|
|
349
|
+
}
|