Skip to content

Commit eace50e

Browse files
TS2Swift: inject diagnostics for export assignment test
1 parent 017f172 commit eace50e

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

Plugins/BridgeJS/Sources/TS2Swift/JavaScript/src/cli.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ Examples:
9797
/**
9898
* Run ts2swift for a single input file (programmatic API, no process I/O).
9999
* @param {string[]} filePaths - Paths to the .d.ts files
100-
* @param {{ tsconfigPath: string, logLevel?: keyof typeof DiagnosticEngine.LEVELS, globalFiles?: string[] }} options
100+
* @param {{ tsconfigPath: string, logLevel?: keyof typeof DiagnosticEngine.LEVELS, globalFiles?: string[], diagnosticEngine?: DiagnosticEngine }} options
101101
* @returns {string} Generated Swift source
102102
* @throws {Error} on parse/type-check errors (diagnostics are included in the message)
103103
*/
104104
export function run(filePaths, options) {
105-
const { tsconfigPath, logLevel = 'info', globalFiles = [] } = options;
106-
const diagnosticEngine = new DiagnosticEngine(logLevel);
105+
const { tsconfigPath, logLevel = 'info', globalFiles = [], diagnosticEngine } = options;
106+
const engine = diagnosticEngine ?? new DiagnosticEngine(logLevel);
107107

108108
const configFile = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
109109
const configParseResult = ts.parseJsonConfigFileContent(
@@ -164,7 +164,7 @@ export function run(filePaths, options) {
164164
const bodies = [];
165165
const globalFileSet = new Set(globalFiles);
166166
for (const inputPath of [...filePaths, ...globalFiles]) {
167-
const processor = new TypeProcessor(program.getTypeChecker(), diagnosticEngine, {
167+
const processor = new TypeProcessor(program.getTypeChecker(), engine, {
168168
defaultImportFromGlobal: globalFileSet.has(inputPath),
169169
});
170170
const result = processor.processTypeDeclarations(program, inputPath);
@@ -247,7 +247,7 @@ export function main(args) {
247247

248248
let swiftOutput;
249249
try {
250-
swiftOutput = run(filePaths, { tsconfigPath, logLevel, globalFiles });
250+
swiftOutput = run(filePaths, { tsconfigPath, logLevel, globalFiles, diagnosticEngine });
251251
} catch (/** @type {unknown} */ err) {
252252
if (err instanceof Error) {
253253
diagnosticEngine.print("error", err.message);

Plugins/BridgeJS/Sources/TS2Swift/JavaScript/test/ts2swift.test.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @ts-check
2-
import { describe, it, expect, vi } from 'vitest';
2+
import { describe, it, expect } from 'vitest';
33
import { readdirSync, mkdtempSync, writeFileSync, rmSync } from 'fs';
44
import { fileURLToPath } from 'url';
55
import path from 'path';
@@ -53,16 +53,15 @@ describe('ts2swift', () => {
5353

5454
it('emits a warning when export assignments cannot be generated', () => {
5555
const dtsPath = path.join(inputsDir, 'ExportAssignment.d.ts');
56-
const stderrSpy = vi.spyOn(process.stderr, 'write').mockImplementation(() => true);
57-
try {
58-
run([dtsPath], { tsconfigPath, logLevel: 'warning' });
59-
const combined = stderrSpy.mock.calls.map(args => String(args[0])).join('');
60-
expect(combined).toMatch(/Skipping export assignment/);
61-
// Only warn once for the export assignment node
62-
const occurrences = (combined.match(/Skipping export assignment/g) || []).length;
63-
expect(occurrences).toBe(1);
64-
} finally {
65-
stderrSpy.mockRestore();
66-
}
56+
/** @type {{ level: string, message: string }[]} */
57+
const diagnostics = [];
58+
const diagnosticEngine = {
59+
print: (level, message) => diagnostics.push({ level, message }),
60+
};
61+
run([dtsPath], { tsconfigPath, logLevel: 'warning', diagnosticEngine });
62+
const messages = diagnostics.map((d) => d.message).join('\n');
63+
expect(messages).toMatch(/Skipping export assignment/);
64+
const occurrences = (messages.match(/Skipping export assignment/g) || []).length;
65+
expect(occurrences).toBe(1);
6766
});
6867
});

0 commit comments

Comments
 (0)