Skip to content

Commit 6b8e6e5

Browse files
authored
Expose program diagnostics in cli main callback (#1625)
1 parent 5b51a13 commit 6b8e6e5

File tree

4 files changed

+82
-5
lines changed

4 files changed

+82
-5
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ out/
66
raw/
77
.history
88
*.backup
9-
.vscode
9+
.vscode
10+
.idea

NOTICE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ under the licensing terms detailed in LICENSE:
3030
* Gabor Greif <[email protected]>
3131
* Martin Fredriksson <[email protected]>
3232
* forcepusher <[email protected]>
33+
* Piotr Oleś <[email protected]>
3334

3435
Portions of this software are derived from third-party works licensed under
3536
the following terms:

cli/asc.d.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,38 @@ export interface MemoryStream extends OutputStream {
5555
toString(): string;
5656
}
5757

58+
/** A subset of Source interface from assemblyscript package */
59+
export interface Source {
60+
/** Normalized path with file extension. */
61+
normalizedPath: string;
62+
}
63+
64+
/** A subset of Range interface from assemblyscript package */
65+
export interface Range {
66+
/** Range start char index in a source */
67+
start: number;
68+
/** Range end char index in a source */
69+
end: number;
70+
source: Source;
71+
}
72+
73+
/** A subset of DiagnosticMessage interface from assemblyscript package */
74+
export interface DiagnosticMessage {
75+
/** Message code. */
76+
code: number;
77+
/** Message category. */
78+
category: number;
79+
/** Message text. */
80+
message: string;
81+
/** Respective source range, if any. */
82+
range: Range | null;
83+
/** Related range, if any. */
84+
relatedRange: Range | null;
85+
}
86+
87+
/** A function that handles diagnostic */
88+
type DiagnosticReporter = (diagnostic: DiagnosticMessage) => void;
89+
5890
/** Compiler options. */
5991
export interface CompilerOptions {
6092
/** Prints just the compiler's version and exits. */
@@ -157,6 +189,8 @@ export interface APIOptions {
157189
writeFile?: (filename: string, contents: Uint8Array, baseDir: string) => void;
158190
/** Lists all files within a directory. */
159191
listFiles?: (dirname: string, baseDir: string) => string[] | null;
192+
/** Handles diagnostic messages. */
193+
reportDiagnostic?: DiagnosticReporter;
160194
}
161195

162196
/** Convenience function that parses and compiles source strings directly. */
@@ -176,7 +210,7 @@ export function main(argv: string[], options: APIOptions, callback?: (err: Error
176210
export function main(argv: string[], callback?: (err: Error | null) => number): number;
177211

178212
/** Checks diagnostics emitted so far for errors. */
179-
export function checkDiagnostics(emitter: Record<string,unknown>, stderr?: OutputStream): boolean;
213+
export function checkDiagnostics(emitter: Record<string,unknown>, stderr?: OutputStream, reportDiagnostic?: DiagnosticReporter): boolean;
180214

181215
/** An object of stats for the current task. */
182216
export interface Stats {

cli/asc.js

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,20 @@ function loadAssemblyScriptWasm(binaryPath) {
129129
return exports;
130130
}
131131

132+
/**
133+
* Wraps object in WASM environment, returns untouched otherwise.
134+
* @template T
135+
* @param {number | T} ptrOrObj Pointer in WASM environment, object otherwise
136+
* @param {typeof T} type Object type that provides wrap method in WASM environment
137+
* @returns {T | null}
138+
*/
139+
function __wrap(ptrOrObj, type) {
140+
if (typeof ptrOrObj === "number") {
141+
return ptrOrObj === 0 ? null : type.wrap(ptrOrObj);
142+
}
143+
return ptrOrObj;
144+
}
145+
132146
var assemblyscript, __newString, __getString, __pin, __unpin, __collect;
133147

134148
function loadAssemblyScript() {
@@ -702,7 +716,7 @@ exports.main = function main(argv, options, callback) {
702716
});
703717
}
704718
}
705-
var numErrors = checkDiagnostics(program, stderr);
719+
var numErrors = checkDiagnostics(program, stderr, options.reportDiagnostic);
706720
if (numErrors) {
707721
const err = Error(numErrors + " parse error(s)");
708722
err.stack = err.message; // omit stack
@@ -816,7 +830,7 @@ exports.main = function main(argv, options, callback) {
816830
};
817831
}
818832
});
819-
var numErrors = checkDiagnostics(program, stderr);
833+
var numErrors = checkDiagnostics(program, stderr, options.reportDiagnostic);
820834
if (numErrors) {
821835
if (module) module.dispose();
822836
const err = Error(numErrors + " compile error(s)");
@@ -1155,7 +1169,7 @@ function getAsconfig(file, baseDir, readFile) {
11551169
exports.getAsconfig = getAsconfig;
11561170

11571171
/** Checks diagnostics emitted so far for errors. */
1158-
function checkDiagnostics(program, stderr) {
1172+
function checkDiagnostics(program, stderr, reportDiagnostic) {
11591173
var numErrors = 0;
11601174
do {
11611175
let diagnosticPtr = assemblyscript.nextDiagnostic(program);
@@ -1167,6 +1181,33 @@ function checkDiagnostics(program, stderr) {
11671181
EOL + EOL
11681182
);
11691183
}
1184+
if (reportDiagnostic) {
1185+
const diagnostic = __wrap(diagnosticPtr, assemblyscript.DiagnosticMessage);
1186+
const range = __wrap(diagnostic.range, assemblyscript.Range);
1187+
const relatedRange = __wrap(diagnostic.relatedRange, assemblyscript.Range);
1188+
const rangeSource = range ? __wrap(range.source, assemblyscript.Source) : null;
1189+
const relatedRangeSource = relatedRange ? __wrap(relatedRange.source, assemblyscript.Source) : null;
1190+
1191+
reportDiagnostic({
1192+
message: __getString(diagnostic.message),
1193+
code: diagnostic.code,
1194+
category: diagnostic.category,
1195+
range: range ? {
1196+
start: range.start,
1197+
end: range.end,
1198+
source: rangeSource ? {
1199+
normalizedPath: __getString(rangeSource.normalizedPath)
1200+
} : null,
1201+
} : null,
1202+
relatedRange: relatedRange ? {
1203+
start: relatedRange.start,
1204+
end: relatedRange.end,
1205+
source: relatedRangeSource ? {
1206+
normalizedPath: __getString(relatedRangeSource.normalizedPath)
1207+
} : null
1208+
} : null
1209+
});
1210+
}
11701211
if (assemblyscript.isError(diagnosticPtr)) ++numErrors;
11711212
__unpin(diagnosticPtr);
11721213
} while (true);

0 commit comments

Comments
 (0)