@tsonic/frontend 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +53 -0
- package/src/dependency-graph.ts +18 -0
- package/src/dotnet-metadata.ts +121 -0
- package/src/graph/builder.ts +81 -0
- package/src/graph/circular.ts +58 -0
- package/src/graph/extraction/exports.ts +55 -0
- package/src/graph/extraction/imports.ts +81 -0
- package/src/graph/extraction/index.ts +7 -0
- package/src/graph/extraction/orchestrator.ts +99 -0
- package/src/graph/extraction.ts +10 -0
- package/src/graph/helpers.ts +51 -0
- package/src/graph/index.ts +17 -0
- package/src/graph/types.ts +13 -0
- package/src/index.ts +80 -0
- package/src/ir/binding-resolution.test.ts +585 -0
- package/src/ir/builder/exports.ts +78 -0
- package/src/ir/builder/helpers.ts +27 -0
- package/src/ir/builder/imports.ts +153 -0
- package/src/ir/builder/index.ts +10 -0
- package/src/ir/builder/orchestrator.ts +178 -0
- package/src/ir/builder/statements.ts +55 -0
- package/src/ir/builder/types.ts +8 -0
- package/src/ir/builder/validation.ts +129 -0
- package/src/ir/builder.test.ts +581 -0
- package/src/ir/builder.ts +14 -0
- package/src/ir/converters/expressions/access.ts +99 -0
- package/src/ir/converters/expressions/calls.ts +137 -0
- package/src/ir/converters/expressions/collections.ts +84 -0
- package/src/ir/converters/expressions/functions.ts +62 -0
- package/src/ir/converters/expressions/helpers.ts +264 -0
- package/src/ir/converters/expressions/index.ts +43 -0
- package/src/ir/converters/expressions/literals.ts +22 -0
- package/src/ir/converters/expressions/operators.ts +147 -0
- package/src/ir/converters/expressions/other.ts +60 -0
- package/src/ir/converters/statements/control/blocks.ts +22 -0
- package/src/ir/converters/statements/control/conditionals.ts +67 -0
- package/src/ir/converters/statements/control/exceptions.ts +43 -0
- package/src/ir/converters/statements/control/index.ts +17 -0
- package/src/ir/converters/statements/control/loops.ts +99 -0
- package/src/ir/converters/statements/control.ts +17 -0
- package/src/ir/converters/statements/declarations/classes/constructors.ts +120 -0
- package/src/ir/converters/statements/declarations/classes/index.ts +12 -0
- package/src/ir/converters/statements/declarations/classes/methods.ts +61 -0
- package/src/ir/converters/statements/declarations/classes/orchestrator.ts +166 -0
- package/src/ir/converters/statements/declarations/classes/override-detection.ts +116 -0
- package/src/ir/converters/statements/declarations/classes/properties.ts +63 -0
- package/src/ir/converters/statements/declarations/classes.ts +6 -0
- package/src/ir/converters/statements/declarations/enums.ts +29 -0
- package/src/ir/converters/statements/declarations/functions.ts +39 -0
- package/src/ir/converters/statements/declarations/index.ts +14 -0
- package/src/ir/converters/statements/declarations/interfaces.ts +131 -0
- package/src/ir/converters/statements/declarations/registry.ts +45 -0
- package/src/ir/converters/statements/declarations/type-aliases.ts +25 -0
- package/src/ir/converters/statements/declarations/variables.ts +60 -0
- package/src/ir/converters/statements/declarations.ts +16 -0
- package/src/ir/converters/statements/helpers.ts +174 -0
- package/src/ir/converters/statements/index.ts +40 -0
- package/src/ir/expression-converter.ts +207 -0
- package/src/ir/generic-validator.ts +100 -0
- package/src/ir/hierarchical-bindings-e2e.test.ts +163 -0
- package/src/ir/index.ts +6 -0
- package/src/ir/statement-converter.ts +128 -0
- package/src/ir/type-converter/arrays.ts +20 -0
- package/src/ir/type-converter/converter.ts +10 -0
- package/src/ir/type-converter/functions.ts +22 -0
- package/src/ir/type-converter/index.ts +11 -0
- package/src/ir/type-converter/inference.ts +122 -0
- package/src/ir/type-converter/literals.ts +40 -0
- package/src/ir/type-converter/objects.ts +107 -0
- package/src/ir/type-converter/orchestrator.ts +85 -0
- package/src/ir/type-converter/patterns.ts +73 -0
- package/src/ir/type-converter/primitives.ts +57 -0
- package/src/ir/type-converter/references.ts +64 -0
- package/src/ir/type-converter/unions-intersections.ts +34 -0
- package/src/ir/type-converter.ts +13 -0
- package/src/ir/types/expressions.ts +215 -0
- package/src/ir/types/guards.ts +39 -0
- package/src/ir/types/helpers.ts +135 -0
- package/src/ir/types/index.ts +108 -0
- package/src/ir/types/ir-types.ts +96 -0
- package/src/ir/types/module.ts +57 -0
- package/src/ir/types/statements.ts +238 -0
- package/src/ir/types.ts +97 -0
- package/src/metadata/bindings-loader.test.ts +144 -0
- package/src/metadata/bindings-loader.ts +357 -0
- package/src/metadata/index.ts +15 -0
- package/src/metadata/library-loader.ts +153 -0
- package/src/metadata/loader.test.ts +156 -0
- package/src/metadata/loader.ts +382 -0
- package/src/program/bindings.test.ts +512 -0
- package/src/program/bindings.ts +253 -0
- package/src/program/config.ts +30 -0
- package/src/program/creation.ts +249 -0
- package/src/program/dependency-graph.ts +245 -0
- package/src/program/diagnostics.ts +103 -0
- package/src/program/index.ts +19 -0
- package/src/program/metadata.ts +68 -0
- package/src/program/queries.ts +18 -0
- package/src/program/types.ts +38 -0
- package/src/program.ts +13 -0
- package/src/resolver/dotnet-import-resolver.ts +226 -0
- package/src/resolver/import-resolution.ts +177 -0
- package/src/resolver/index.ts +18 -0
- package/src/resolver/namespace.test.ts +86 -0
- package/src/resolver/namespace.ts +42 -0
- package/src/resolver/naming.ts +38 -0
- package/src/resolver/path-resolution.ts +22 -0
- package/src/resolver/types.ts +15 -0
- package/src/resolver.test.ts +155 -0
- package/src/resolver.ts +14 -0
- package/src/symbol-table/builder.ts +114 -0
- package/src/symbol-table/creation.ts +42 -0
- package/src/symbol-table/helpers.ts +18 -0
- package/src/symbol-table/index.ts +13 -0
- package/src/symbol-table/queries.ts +42 -0
- package/src/symbol-table/types.ts +28 -0
- package/src/symbol-table.ts +14 -0
- package/src/types/bindings.ts +172 -0
- package/src/types/diagnostic.test.ts +164 -0
- package/src/types/diagnostic.ts +153 -0
- package/src/types/explicit-views.test.ts +113 -0
- package/src/types/explicit-views.ts +218 -0
- package/src/types/metadata.ts +229 -0
- package/src/types/module.ts +99 -0
- package/src/types/nested-types.test.ts +194 -0
- package/src/types/nested-types.ts +215 -0
- package/src/types/parameter-modifiers.ts +173 -0
- package/src/types/ref-parameters.test.ts +192 -0
- package/src/types/ref-parameters.ts +268 -0
- package/src/types/result.test.ts +157 -0
- package/src/types/result.ts +48 -0
- package/src/types/support-types.test.ts +81 -0
- package/src/types/support-types.ts +288 -0
- package/src/types/test-harness.ts +180 -0
- package/src/validation/exports.ts +98 -0
- package/src/validation/features.ts +89 -0
- package/src/validation/generics.ts +40 -0
- package/src/validation/helpers.ts +31 -0
- package/src/validation/imports.ts +97 -0
- package/src/validation/index.ts +11 -0
- package/src/validation/orchestrator.ts +51 -0
- package/src/validation/static-safety.ts +267 -0
- package/src/validator.test.ts +468 -0
- package/src/validator.ts +15 -0
- package/tsconfig.json +13 -0
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dependency graph builder - Multi-file compilation
|
|
3
|
+
* Traverses local imports using ts.resolveModuleName()
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import * as ts from "typescript";
|
|
7
|
+
import { relative, resolve } from "path";
|
|
8
|
+
import { Result, ok, error } from "../types/result.js";
|
|
9
|
+
import { Diagnostic, createDiagnostic } from "../types/diagnostic.js";
|
|
10
|
+
import { IrModule } from "../ir/types.js";
|
|
11
|
+
import { buildIrModule } from "../ir/builder/orchestrator.js";
|
|
12
|
+
import { createProgram, createCompilerOptions } from "./creation.js";
|
|
13
|
+
import { CompilerOptions } from "./types.js";
|
|
14
|
+
|
|
15
|
+
export type ModuleDependencyGraphResult = {
|
|
16
|
+
readonly modules: readonly IrModule[];
|
|
17
|
+
readonly entryModule: IrModule;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Extract module specifier from import or re-export declaration
|
|
22
|
+
* Returns the module specifier string literal, or null if not applicable
|
|
23
|
+
*/
|
|
24
|
+
const getModuleSpecifier = (stmt: ts.Statement): ts.StringLiteral | null => {
|
|
25
|
+
// Handle: import { x } from "./module"
|
|
26
|
+
if (
|
|
27
|
+
ts.isImportDeclaration(stmt) &&
|
|
28
|
+
ts.isStringLiteral(stmt.moduleSpecifier)
|
|
29
|
+
) {
|
|
30
|
+
return stmt.moduleSpecifier;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Handle: export { x } from "./module" (re-exports)
|
|
34
|
+
if (
|
|
35
|
+
ts.isExportDeclaration(stmt) &&
|
|
36
|
+
stmt.moduleSpecifier &&
|
|
37
|
+
ts.isStringLiteral(stmt.moduleSpecifier)
|
|
38
|
+
) {
|
|
39
|
+
return stmt.moduleSpecifier;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return null;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Build complete module dependency graph from entry point
|
|
47
|
+
* Traverses all local imports and builds IR for all discovered modules
|
|
48
|
+
* Uses TypeScript's ts.resolveModuleName() for correct module resolution
|
|
49
|
+
*/
|
|
50
|
+
export const buildModuleDependencyGraph = (
|
|
51
|
+
entryFile: string,
|
|
52
|
+
options: CompilerOptions
|
|
53
|
+
): Result<ModuleDependencyGraphResult, readonly Diagnostic[]> => {
|
|
54
|
+
const diagnostics: Diagnostic[] = [];
|
|
55
|
+
|
|
56
|
+
// Normalize entry file and source root to absolute paths
|
|
57
|
+
const entryAbs = resolve(entryFile);
|
|
58
|
+
const sourceRootAbs = resolve(options.sourceRoot);
|
|
59
|
+
|
|
60
|
+
// Get TypeScript compiler options for module resolution
|
|
61
|
+
const compilerOptions = createCompilerOptions(options);
|
|
62
|
+
|
|
63
|
+
// Track all discovered files for later type checking
|
|
64
|
+
const allDiscoveredFiles: string[] = [];
|
|
65
|
+
|
|
66
|
+
// BFS to discover all local imports
|
|
67
|
+
const visited = new Set<string>();
|
|
68
|
+
const queue: string[] = [entryAbs];
|
|
69
|
+
|
|
70
|
+
// First pass: discover all files
|
|
71
|
+
while (queue.length > 0) {
|
|
72
|
+
const currentFile = queue.shift()!;
|
|
73
|
+
|
|
74
|
+
// Dedup by realpath (handles symlinks, relative paths)
|
|
75
|
+
const realPath = ts.sys.realpath?.(currentFile) ?? currentFile;
|
|
76
|
+
if (visited.has(realPath)) {
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
visited.add(realPath);
|
|
80
|
+
allDiscoveredFiles.push(realPath);
|
|
81
|
+
|
|
82
|
+
// Read source file directly from filesystem
|
|
83
|
+
const sourceText = ts.sys.readFile(currentFile);
|
|
84
|
+
if (!sourceText) {
|
|
85
|
+
// Missing file - add diagnostic but continue traversal
|
|
86
|
+
const relativeCurrent = relative(sourceRootAbs, currentFile);
|
|
87
|
+
diagnostics.push(
|
|
88
|
+
createDiagnostic(
|
|
89
|
+
"TSN1002",
|
|
90
|
+
"error",
|
|
91
|
+
`Cannot find module '${relativeCurrent}'`,
|
|
92
|
+
{
|
|
93
|
+
file: currentFile,
|
|
94
|
+
line: 1,
|
|
95
|
+
column: 1,
|
|
96
|
+
length: 1,
|
|
97
|
+
}
|
|
98
|
+
)
|
|
99
|
+
);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Parse source file to extract imports
|
|
104
|
+
const sourceFile = ts.createSourceFile(
|
|
105
|
+
currentFile,
|
|
106
|
+
sourceText,
|
|
107
|
+
ts.ScriptTarget.Latest,
|
|
108
|
+
true
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
// Extract local imports and re-exports using TypeScript's resolution
|
|
112
|
+
for (const stmt of sourceFile.statements) {
|
|
113
|
+
// Handle import declarations: import { x } from "./module"
|
|
114
|
+
// Handle re-export declarations: export { x } from "./module"
|
|
115
|
+
const moduleSpecifier = getModuleSpecifier(stmt);
|
|
116
|
+
if (!moduleSpecifier) {
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const importSpecifier = moduleSpecifier.text;
|
|
121
|
+
|
|
122
|
+
// Only process local imports (starts with . or /)
|
|
123
|
+
if (
|
|
124
|
+
!importSpecifier.startsWith(".") &&
|
|
125
|
+
!importSpecifier.startsWith("/")
|
|
126
|
+
) {
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Resolve using TypeScript's module resolution
|
|
131
|
+
const resolved = ts.resolveModuleName(
|
|
132
|
+
importSpecifier,
|
|
133
|
+
currentFile,
|
|
134
|
+
compilerOptions,
|
|
135
|
+
ts.sys
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
if (resolved.resolvedModule?.resolvedFileName) {
|
|
139
|
+
const resolvedPath = resolved.resolvedModule.resolvedFileName;
|
|
140
|
+
|
|
141
|
+
// Only include .ts files (not .d.ts) within source root
|
|
142
|
+
if (
|
|
143
|
+
resolvedPath.startsWith(sourceRootAbs) &&
|
|
144
|
+
resolvedPath.endsWith(".ts") &&
|
|
145
|
+
!resolvedPath.endsWith(".d.ts")
|
|
146
|
+
) {
|
|
147
|
+
queue.push(resolvedPath);
|
|
148
|
+
}
|
|
149
|
+
} else {
|
|
150
|
+
// Import resolution failed - add diagnostic with context
|
|
151
|
+
const relativeCurrent = relative(sourceRootAbs, currentFile);
|
|
152
|
+
diagnostics.push(
|
|
153
|
+
createDiagnostic(
|
|
154
|
+
"TSN1002",
|
|
155
|
+
"error",
|
|
156
|
+
`Cannot resolve import '${importSpecifier}' from '${relativeCurrent}'`,
|
|
157
|
+
{
|
|
158
|
+
file: currentFile,
|
|
159
|
+
line: stmt.getStart(sourceFile),
|
|
160
|
+
column: 1,
|
|
161
|
+
length: importSpecifier.length,
|
|
162
|
+
}
|
|
163
|
+
)
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// If any diagnostics from discovery, fail the build
|
|
170
|
+
if (diagnostics.length > 0) {
|
|
171
|
+
return error(diagnostics);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Ensure we discovered at least the entry file
|
|
175
|
+
if (allDiscoveredFiles.length === 0) {
|
|
176
|
+
return error([
|
|
177
|
+
createDiagnostic(
|
|
178
|
+
"TSN1002",
|
|
179
|
+
"error",
|
|
180
|
+
`No modules found starting from entry point '${entryFile}'`,
|
|
181
|
+
{
|
|
182
|
+
file: entryFile,
|
|
183
|
+
line: 1,
|
|
184
|
+
column: 1,
|
|
185
|
+
length: 1,
|
|
186
|
+
}
|
|
187
|
+
),
|
|
188
|
+
]);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Second pass: Create TypeScript program with all discovered files for type checking
|
|
192
|
+
// Use absolute sourceRoot for consistency
|
|
193
|
+
const programResult = createProgram(allDiscoveredFiles, {
|
|
194
|
+
...options,
|
|
195
|
+
sourceRoot: sourceRootAbs,
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
if (!programResult.ok) {
|
|
199
|
+
return error(programResult.error.diagnostics);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const tsonicProgram = programResult.value;
|
|
203
|
+
|
|
204
|
+
// Third pass: Build IR for all discovered modules
|
|
205
|
+
const modules: IrModule[] = [];
|
|
206
|
+
|
|
207
|
+
for (const filePath of allDiscoveredFiles) {
|
|
208
|
+
const sourceFile = tsonicProgram.program.getSourceFile(filePath);
|
|
209
|
+
if (!sourceFile) {
|
|
210
|
+
// This shouldn't happen since we already verified files exist
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const moduleResult = buildIrModule(sourceFile, tsonicProgram, {
|
|
215
|
+
sourceRoot: sourceRootAbs,
|
|
216
|
+
rootNamespace: options.rootNamespace,
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
if (!moduleResult.ok) {
|
|
220
|
+
diagnostics.push(moduleResult.error);
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
modules.push(moduleResult.value);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// If any diagnostics from IR building, fail the build
|
|
228
|
+
if (diagnostics.length > 0) {
|
|
229
|
+
return error(diagnostics);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Sort modules by relative path for deterministic output
|
|
233
|
+
modules.sort((a, b) => a.filePath.localeCompare(b.filePath));
|
|
234
|
+
|
|
235
|
+
// Entry module is the first one (after sorting, it should be the entry file)
|
|
236
|
+
// But let's find it by matching the entry file path
|
|
237
|
+
const entryRelative = relative(sourceRootAbs, entryAbs).replace(/\\/g, "/");
|
|
238
|
+
const entryModule =
|
|
239
|
+
modules.find((m) => m.filePath === entryRelative) ?? modules[0]!;
|
|
240
|
+
|
|
241
|
+
return ok({
|
|
242
|
+
modules,
|
|
243
|
+
entryModule,
|
|
244
|
+
});
|
|
245
|
+
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypeScript diagnostics collection and conversion
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import * as ts from "typescript";
|
|
6
|
+
import {
|
|
7
|
+
Diagnostic,
|
|
8
|
+
DiagnosticsCollector,
|
|
9
|
+
createDiagnosticsCollector,
|
|
10
|
+
addDiagnostic,
|
|
11
|
+
createDiagnostic,
|
|
12
|
+
} from "../types/diagnostic.js";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Collect all TypeScript diagnostics from a program
|
|
16
|
+
*/
|
|
17
|
+
export const collectTsDiagnostics = (
|
|
18
|
+
program: ts.Program
|
|
19
|
+
): DiagnosticsCollector => {
|
|
20
|
+
const tsDiagnostics = [
|
|
21
|
+
...program.getConfigFileParsingDiagnostics(),
|
|
22
|
+
...program.getOptionsDiagnostics(),
|
|
23
|
+
...program.getSyntacticDiagnostics(),
|
|
24
|
+
...program.getGlobalDiagnostics(),
|
|
25
|
+
...program.getSemanticDiagnostics(),
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
return tsDiagnostics.reduce((collector, tsDiag) => {
|
|
29
|
+
const diagnostic = convertTsDiagnostic(tsDiag);
|
|
30
|
+
return diagnostic ? addDiagnostic(collector, diagnostic) : collector;
|
|
31
|
+
}, createDiagnosticsCollector());
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Convert TypeScript diagnostic to Tsonic diagnostic
|
|
36
|
+
*/
|
|
37
|
+
export const convertTsDiagnostic = (
|
|
38
|
+
tsDiag: ts.Diagnostic
|
|
39
|
+
): Diagnostic | null => {
|
|
40
|
+
if (tsDiag.category === ts.DiagnosticCategory.Suggestion) {
|
|
41
|
+
return null; // Ignore suggestions
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const message = ts.flattenDiagnosticMessageText(tsDiag.messageText, "\n");
|
|
45
|
+
|
|
46
|
+
// Ignore "type used as value" errors for .NET types
|
|
47
|
+
// These are handled by the Tsonic compiler
|
|
48
|
+
if (
|
|
49
|
+
message.includes("only refers to a type, but is being used as a value") &&
|
|
50
|
+
tsDiag.file
|
|
51
|
+
) {
|
|
52
|
+
// Check if this is a .NET import
|
|
53
|
+
const sourceText = tsDiag.file.getText();
|
|
54
|
+
if (
|
|
55
|
+
sourceText.includes('from "System') ||
|
|
56
|
+
sourceText.includes('from "Microsoft') ||
|
|
57
|
+
sourceText.includes('from "Windows')
|
|
58
|
+
) {
|
|
59
|
+
return null; // Ignore - will be handled by Tsonic
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const severity =
|
|
64
|
+
tsDiag.category === ts.DiagnosticCategory.Error
|
|
65
|
+
? "error"
|
|
66
|
+
: tsDiag.category === ts.DiagnosticCategory.Warning
|
|
67
|
+
? "warning"
|
|
68
|
+
: "info";
|
|
69
|
+
|
|
70
|
+
const location =
|
|
71
|
+
tsDiag.file && tsDiag.start !== undefined
|
|
72
|
+
? getSourceLocation(tsDiag.file, tsDiag.start, tsDiag.length ?? 1)
|
|
73
|
+
: undefined;
|
|
74
|
+
|
|
75
|
+
return createDiagnostic(
|
|
76
|
+
"TSN2001", // Generic TypeScript error
|
|
77
|
+
severity,
|
|
78
|
+
message,
|
|
79
|
+
location
|
|
80
|
+
);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Get source location information from TypeScript source file
|
|
85
|
+
*/
|
|
86
|
+
export const getSourceLocation = (
|
|
87
|
+
file: ts.SourceFile,
|
|
88
|
+
start: number,
|
|
89
|
+
length: number
|
|
90
|
+
): {
|
|
91
|
+
readonly file: string;
|
|
92
|
+
readonly line: number;
|
|
93
|
+
readonly column: number;
|
|
94
|
+
readonly length: number;
|
|
95
|
+
} => {
|
|
96
|
+
const { line, character } = file.getLineAndCharacterOfPosition(start);
|
|
97
|
+
return {
|
|
98
|
+
file: file.fileName,
|
|
99
|
+
line: line + 1,
|
|
100
|
+
column: character + 1,
|
|
101
|
+
length,
|
|
102
|
+
};
|
|
103
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Program - Public API
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export type { CompilerOptions, TsonicProgram, RuntimeMode } from "./types.js";
|
|
6
|
+
export { defaultTsConfig } from "./config.js";
|
|
7
|
+
export { loadDotnetMetadata } from "./metadata.js";
|
|
8
|
+
export { BindingRegistry, loadBindings } from "./bindings.js";
|
|
9
|
+
export {
|
|
10
|
+
collectTsDiagnostics,
|
|
11
|
+
convertTsDiagnostic,
|
|
12
|
+
getSourceLocation,
|
|
13
|
+
} from "./diagnostics.js";
|
|
14
|
+
export { createProgram, createCompilerOptions } from "./creation.js";
|
|
15
|
+
export { getSourceFile } from "./queries.js";
|
|
16
|
+
export {
|
|
17
|
+
buildModuleDependencyGraph,
|
|
18
|
+
type ModuleDependencyGraphResult,
|
|
19
|
+
} from "./dependency-graph.js";
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* .NET metadata file loading
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import * as fs from "node:fs";
|
|
6
|
+
import * as path from "node:path";
|
|
7
|
+
import {
|
|
8
|
+
DotnetMetadataRegistry,
|
|
9
|
+
DotnetMetadataFile,
|
|
10
|
+
} from "../dotnet-metadata.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Recursively scan a directory for .d.ts files
|
|
14
|
+
*/
|
|
15
|
+
const scanForDeclarationFiles = (dir: string): readonly string[] => {
|
|
16
|
+
if (!fs.existsSync(dir)) {
|
|
17
|
+
return [];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const results: string[] = [];
|
|
21
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
22
|
+
|
|
23
|
+
for (const entry of entries) {
|
|
24
|
+
const fullPath = path.join(dir, entry.name);
|
|
25
|
+
if (entry.isDirectory()) {
|
|
26
|
+
results.push(...scanForDeclarationFiles(fullPath));
|
|
27
|
+
} else if (entry.name.endsWith(".d.ts")) {
|
|
28
|
+
results.push(fullPath);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return results;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Load .NET metadata files from configured type roots
|
|
37
|
+
* Looks for *.metadata.json files alongside .d.ts files
|
|
38
|
+
*/
|
|
39
|
+
export const loadDotnetMetadata = (
|
|
40
|
+
typeRoots: readonly string[]
|
|
41
|
+
): DotnetMetadataRegistry => {
|
|
42
|
+
const registry = new DotnetMetadataRegistry();
|
|
43
|
+
|
|
44
|
+
// Scan all type roots for .d.ts files
|
|
45
|
+
for (const typeRoot of typeRoots) {
|
|
46
|
+
const absoluteRoot = path.resolve(typeRoot);
|
|
47
|
+
const declFiles = scanForDeclarationFiles(absoluteRoot);
|
|
48
|
+
|
|
49
|
+
for (const declPath of declFiles) {
|
|
50
|
+
// Look for corresponding .metadata.json file
|
|
51
|
+
const metadataPath = declPath.replace(/\.d\.ts$/, ".metadata.json");
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
if (fs.existsSync(metadataPath)) {
|
|
55
|
+
const content = fs.readFileSync(metadataPath, "utf-8");
|
|
56
|
+
const metadataFile = JSON.parse(content) as DotnetMetadataFile;
|
|
57
|
+
registry.loadMetadataFile(metadataPath, metadataFile);
|
|
58
|
+
}
|
|
59
|
+
} catch (err) {
|
|
60
|
+
// Silently skip files that can't be read or parsed
|
|
61
|
+
// In production, we might want to emit a warning diagnostic
|
|
62
|
+
console.warn(`Failed to load metadata from ${metadataPath}:`, err);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return registry;
|
|
68
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Program query functions
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import * as ts from "typescript";
|
|
6
|
+
import * as path from "node:path";
|
|
7
|
+
import { TsonicProgram } from "./types.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Get a source file from the program by file path
|
|
11
|
+
*/
|
|
12
|
+
export const getSourceFile = (
|
|
13
|
+
program: TsonicProgram,
|
|
14
|
+
filePath: string
|
|
15
|
+
): ts.SourceFile | null => {
|
|
16
|
+
const absolutePath = path.resolve(filePath);
|
|
17
|
+
return program.program.getSourceFile(absolutePath) ?? null;
|
|
18
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Program type definitions
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import * as ts from "typescript";
|
|
6
|
+
import { DotnetMetadataRegistry } from "../dotnet-metadata.js";
|
|
7
|
+
import { BindingRegistry } from "./bindings.js";
|
|
8
|
+
import { DotNetImportResolver } from "../resolver/dotnet-import-resolver.js";
|
|
9
|
+
|
|
10
|
+
export type RuntimeMode = "js" | "dotnet";
|
|
11
|
+
|
|
12
|
+
export type CompilerOptions = {
|
|
13
|
+
readonly sourceRoot: string;
|
|
14
|
+
readonly rootNamespace: string;
|
|
15
|
+
readonly strict?: boolean;
|
|
16
|
+
readonly typeRoots?: readonly string[];
|
|
17
|
+
readonly verbose?: boolean;
|
|
18
|
+
/** Use TypeScript standard lib (Array, Promise, etc.) instead of noLib mode */
|
|
19
|
+
readonly useStandardLib?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Runtime mode:
|
|
22
|
+
* - "js": JS built-ins available via Tsonic.JSRuntime
|
|
23
|
+
* - "dotnet": Pure .NET mode, JS built-ins forbidden
|
|
24
|
+
* Defaults to "js"
|
|
25
|
+
*/
|
|
26
|
+
readonly runtime?: RuntimeMode;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type TsonicProgram = {
|
|
30
|
+
readonly program: ts.Program;
|
|
31
|
+
readonly checker: ts.TypeChecker;
|
|
32
|
+
readonly options: CompilerOptions;
|
|
33
|
+
readonly sourceFiles: readonly ts.SourceFile[];
|
|
34
|
+
readonly metadata: DotnetMetadataRegistry;
|
|
35
|
+
readonly bindings: BindingRegistry;
|
|
36
|
+
/** Resolver for .NET namespace imports (import-driven discovery) */
|
|
37
|
+
readonly dotnetResolver: DotNetImportResolver;
|
|
38
|
+
};
|
package/src/program.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypeScript program creation and management
|
|
3
|
+
* Main dispatcher - re-exports from program/ subdirectory
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export type { CompilerOptions, TsonicProgram } from "./program/index.js";
|
|
7
|
+
export type { ModuleDependencyGraphResult } from "./program/dependency-graph.js";
|
|
8
|
+
export {
|
|
9
|
+
createProgram,
|
|
10
|
+
getSourceFile,
|
|
11
|
+
BindingRegistry,
|
|
12
|
+
buildModuleDependencyGraph,
|
|
13
|
+
} from "./program/index.js";
|