Skip to content

Commit 6e44097

Browse files
authored
feat(core/{pre,post}-process): add showWarning, showError utils (#4690)
1 parent 71e3aa0 commit 6e44097

File tree

5 files changed

+74
-6
lines changed

5 files changed

+74
-6
lines changed

src/core/post-process.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* want to be using a new module with your own profile.
1010
* - afterEnd: final thing that is called.
1111
*/
12-
import { showError } from "./utils.js";
12+
import { makePluginUtils, showError } from "./utils.js";
1313

1414
export const name = "core/post-process";
1515

@@ -24,9 +24,11 @@ export async function run(config) {
2424
}
2525
return isFunction;
2626
})
27-
.map(async f => {
27+
.map(async (f, i) => {
28+
const fnName = `${name}/${f.name || `[${i}]`}`;
29+
const utils = makePluginUtils(fnName);
2830
try {
29-
return await f(config, document);
31+
return await f(config, document, utils);
3032
} catch (err) {
3133
const msg = `Function ${f.name} threw an error during \`postProcess\`.`;
3234
const hint = "See developer console.";

src/core/pre-process.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* tested. Use with care, if you know what you're doing. Chances are you really
99
* want to be using a new module with your own profile
1010
*/
11-
import { showError } from "./utils.js";
11+
import { makePluginUtils, showError } from "./utils.js";
1212

1313
export const name = "core/pre-process";
1414

@@ -23,9 +23,11 @@ export async function run(config) {
2323
}
2424
return isFunction;
2525
})
26-
.map(async f => {
26+
.map(async (f, i) => {
27+
const fnName = `${name}/${f.name || `[${i}]`}`;
28+
const utils = makePluginUtils(fnName);
2729
try {
28-
return await f(config, document);
30+
return await f(config, document, utils);
2931
} catch (err) {
3032
const msg = `Function ${f.name} threw an error during \`preProcess\`.`;
3133
const hint = "See developer console.";

src/core/utils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,21 @@ export function showWarning(message, pluginName, options = {}) {
896896
pub("warn", new RespecError(message, pluginName, opts));
897897
}
898898

899+
/**
900+
* Creates showError, showWarning utilities for use in custom pre-process and
901+
* post-process plugins.
902+
* @param {string} pluginName
903+
*/
904+
export function makePluginUtils(pluginName) {
905+
/** @typedef {Parameters<typeof showError>[2]} Options */
906+
return {
907+
/** @type {(message: string, options?: Options) => void} */
908+
showError: (msg, options) => showError(msg, pluginName, options),
909+
/** @type {(message: string, options?: Options) => void} */
910+
showWarning: (msg, options) => showWarning(msg, pluginName, options),
911+
};
912+
}
913+
899914
/**
900915
* Makes a string `coded`.
901916
*

tests/spec/core/pre-process-spec.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
});
1616
}
1717

18+
function preWithWarning(_conf, _doc, utils) {
19+
document.getElementById("pre-warning").innerHTML = "pass";
20+
utils.showWarning("This is a warning");
21+
}
22+
1823
function postSyncFunc() {
1924
document.getElementById("post-sync").innerHTML = "pass";
2025
}
@@ -28,6 +33,17 @@
2833
});
2934
}
3035

36+
function postWithDetailedError(_conf, _doc, utils) {
37+
const el = document.getElementById("post-error");
38+
el.innerHTML = "pass";
39+
utils.showError("This is an error", {
40+
hint: "This is a hint",
41+
title: "This is a title",
42+
details: "This is a detailed error message",
43+
elements: [el],
44+
});
45+
}
46+
3147
function afterEnd() {
3248
return new Promise(resolve => {
3349
setTimeout(() => {
@@ -50,11 +66,13 @@
5066
preSyncFunc,
5167
preAsyncFunc,
5268
noOp, {},
69+
preWithWarning
5370
];
5471

5572
respecConfig.postProcess = [
5673
postSyncFunc,
5774
postAsyncFunc,
75+
postWithDetailedError,
5876
noOp, {},
5977
];
6078
respecConfig.afterEnd = afterEnd;
@@ -72,7 +90,9 @@
7290
<section>
7391
<p id="pre-sync">FAIL</p>
7492
<p id="pre-async">FAIL</p>
93+
<p id="pre-warning">FAIL</p>
7594
<p id="post-sync">FAIL</p>
95+
<p id="post-error">FAIL</p>
7696
<p id="post-async">FAIL</p>
7797
<p id="afterend">FAIL</p>
7898
</section>

tests/spec/core/pre-process-spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,35 @@ describe("Core - preProcess, postProcess, afterEnd", () => {
1616
expect(doc.getElementById("pre-async").innerHTML).toBe("pass");
1717
expect(doc.getElementById("post-sync").innerHTML).toBe("pass");
1818
expect(doc.getElementById("post-async").innerHTML).toBe("pass");
19+
expect(doc.getElementById("pre-warning").innerHTML).toBe("pass");
20+
expect(doc.getElementById("post-error").innerHTML).toBe("pass");
21+
});
22+
23+
it("can show warnings and errors", () => {
24+
const warnings = doc.respec.warnings.filter(warn =>
25+
warn.plugin.startsWith("core/pre-process")
26+
);
27+
expect(warnings).toHaveSize(1);
28+
expect(warnings[0].plugin).toBe("core/pre-process/preWithWarning");
29+
expect(warnings[0].message).toBe("This is a warning");
30+
31+
const errors = doc.respec.errors.filter(
32+
err =>
33+
err.plugin.startsWith("core/post-process") &&
34+
!err.message.startsWith("Every item")
35+
);
36+
expect(errors).toHaveSize(1);
37+
expect(errors[0]).toEqual(
38+
jasmine.objectContaining({
39+
plugin: "core/post-process/postWithDetailedError",
40+
message: "This is an error",
41+
details: "This is a detailed error message",
42+
hint: "This is a hint",
43+
title: "This is a title",
44+
name: "ReSpecError",
45+
elements: [doc.getElementById("post-error")],
46+
})
47+
);
1948
});
2049

2150
it("runs afterEnd method", () => {

0 commit comments

Comments
 (0)