Skip to content

Commit 67a3104

Browse files
BridgeJS: Improve ts2swift diagnostics on invalid definitions
``` $ cat /tmp/tmp.UUlUCtSa64/foo.d.ts export function x(): ; $ node Plugins/BridgeJS/Sources/TS2Swift/JavaScript/bin/ts2swift.js /tmp/tmp.UUlUCtSa64/foo.d.ts error: TypeScript syntax errors: /tmp/tmp.UUlUCtSa64/foo.d.ts:1:22 - error TS1110: Type expected. 1 export function x(): ; ```
1 parent 2dc9a66 commit 67a3104

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,30 @@ export function run(filePaths, options) {
122122
}
123123

124124
const program = TypeProcessor.createProgram([...filePaths, ...globalFiles], configParseResult.options);
125-
const diagnostics = program.getSemanticDiagnostics();
126-
if (diagnostics.length > 0) {
125+
126+
const formatDiagnostics = (diagnostics, kind) => {
127+
if (diagnostics.length === 0) return null;
127128
const message = ts.formatDiagnosticsWithColorAndContext(diagnostics, {
128129
getCanonicalFileName: (fileName) => fileName,
129130
getNewLine: () => ts.sys.newLine,
130131
getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
131132
});
132-
throw new Error(`TypeScript semantic errors:\n${message}`);
133+
return `${kind} errors:\n${message}`;
134+
};
135+
136+
const syntaxErrors = formatDiagnostics(program.getSyntacticDiagnostics(), "TypeScript syntax");
137+
if (syntaxErrors) {
138+
throw new Error(syntaxErrors);
139+
}
140+
141+
const optionErrors = formatDiagnostics(program.getOptionsDiagnostics(), "TypeScript option");
142+
if (optionErrors) {
143+
throw new Error(optionErrors);
144+
}
145+
146+
const semanticErrors = formatDiagnostics(program.getSemanticDiagnostics(), "TypeScript semantic");
147+
if (semanticErrors) {
148+
throw new Error(semanticErrors);
133149
}
134150

135151
const prelude = [
@@ -244,6 +260,13 @@ export function main(args) {
244260
cleanup();
245261
}
246262
}
263+
264+
if (swiftOutput.length === 0) {
265+
diagnosticEngine.print(
266+
"warning",
267+
"No Swift declarations were generated. This usually means the .d.ts contained constructs that BridgeJS cannot import."
268+
);
269+
}
247270
// Write to file or stdout
248271
if (options.values.output && options.values.output !== "-") {
249272
if (swiftOutput.length > 0) {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// @ts-check
22
import { describe, it, expect } from 'vitest';
3-
import { readdirSync } from 'fs';
3+
import { readdirSync, mkdtempSync, writeFileSync, rmSync } from 'fs';
44
import { fileURLToPath } from 'url';
55
import path from 'path';
6+
import os from 'os';
67
import { run } from '../src/cli.js';
78

89
const __dirname = path.dirname(fileURLToPath(import.meta.url));
@@ -38,4 +39,15 @@ describe('ts2swift', () => {
3839
expect(swiftOutput).toMatchSnapshot(name);
3940
});
4041
}
42+
43+
it('reports TypeScript syntax errors via thrown message', () => {
44+
const tmpDir = mkdtempSync(path.join(os.tmpdir(), 'ts2swift-invalid-'));
45+
const invalidPath = path.join(tmpDir, 'invalid.d.ts');
46+
writeFileSync(invalidPath, 'function foo(x');
47+
try {
48+
expect(() => runTs2Swift(invalidPath)).toThrowError(/TypeScript syntax errors/);
49+
} finally {
50+
rmSync(tmpDir, { recursive: true, force: true });
51+
}
52+
});
4153
});

0 commit comments

Comments
 (0)