Skip to content

Commit 82ade2e

Browse files
committed
Simplify runtime type error messages.
1 parent e3d569e commit 82ade2e

13 files changed

+53
-61
lines changed

analyseCoverage.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import sourceRange from "./sourceRange.mjs";
1515
*/
1616
export default async function analyseCoverage(coverageDirPath) {
1717
if (typeof coverageDirPath !== "string")
18-
throw new TypeError("First argument `coverageDirPath` must be a string.");
18+
throw new TypeError("Argument 1 `coverageDirPath` must be a string.");
1919

2020
const coverageDirFileNames = await fs.promises.readdir(coverageDirPath);
2121
const filteredProcessCoverages = [];

analyseCoverage.test.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ import childProcessPromise from "./childProcessPromise.mjs";
1414
*/
1515
export default (tests) => {
1616
tests.add(
17-
"`reportCliError` with first argument `coverageDirPath` not a string.",
17+
"`reportCliError` with argument 1 `coverageDirPath` not a string.",
1818
async () => {
1919
await rejects(
2020
analyseCoverage(
2121
// @ts-expect-error Testing invalid.
2222
true
2323
),
24-
new TypeError("First argument `coverageDirPath` must be a string.")
24+
new TypeError("Argument 1 `coverageDirPath` must be a string.")
2525
);
2626
}
2727
);

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- Renamed imports in the test index module.
2424
- Added `CliError` class tests.
2525
- Runtime type check `CLiError` constructor arguments.
26+
- Simplified runtime type error messages.
2627
- Readme tweaks.
2728

2829
## 5.0.1

childProcessPromise.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { ChildProcess } from "child_process";
1414
export default async function childProcessPromise(childProcess) {
1515
if (!(childProcess instanceof ChildProcess))
1616
throw new TypeError(
17-
"First argument `childProcess` must be a `ChildProcess` instance."
17+
"Argument 1 `childProcess` must be a `ChildProcess` instance."
1818
);
1919

2020
return new Promise((resolve, reject) => {

childProcessPromise.test.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import childProcessPromise from "./childProcessPromise.mjs";
99
*/
1010
export default (tests) => {
1111
tests.add(
12-
"`childProcessPromise` with first argument `childProcess` not a `ChildProcess` instance.",
12+
"`childProcessPromise` with argument 1 `childProcess` not a `ChildProcess` instance.",
1313
async () => {
1414
await rejects(
1515
childProcessPromise(
1616
// @ts-expect-error Testing invalid.
1717
true
1818
),
1919
new TypeError(
20-
"First argument `childProcess` must be a `ChildProcess` instance."
20+
"Argument 1 `childProcess` must be a `ChildProcess` instance."
2121
)
2222
);
2323
}

reportCliError.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import errorConsole from "./errorConsole.mjs";
1212
*/
1313
export default function reportCliError(cliDescription, error) {
1414
if (typeof cliDescription !== "string")
15-
throw new TypeError("First argument `cliDescription` must be a string.");
15+
throw new TypeError("Argument 1 `cliDescription` must be a string.");
1616

1717
errorConsole.group(
1818
// Whitespace blank lines shouldn’t have redundant indentation or color.

reportCliError.test.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ const REPORT_CLI_ERROR_PATH = fileURLToPath(
2020
*/
2121
export default (tests) => {
2222
tests.add(
23-
"`reportCliError` with first argument `cliDescription` not a string.",
23+
"`reportCliError` with argument 1 `cliDescription` not a string.",
2424
() => {
2525
throws(() => {
2626
reportCliError(
2727
// @ts-expect-error Testing invalid.
2828
true,
2929
new Error("Message.")
3030
);
31-
}, new TypeError("First argument `cliDescription` must be a string."));
31+
}, new TypeError("Argument 1 `cliDescription` must be a string."));
3232
}
3333
);
3434

semver.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99
export default function semver(semver) {
1010
if (typeof semver !== "string")
11-
throw new TypeError("First argument `semver` must be a string.");
11+
throw new TypeError("Argument 1 `semver` must be a string.");
1212

1313
const match = semver.match(
1414
// The is is official recommended RegEx, see:
@@ -17,7 +17,7 @@ export default function semver(semver) {
1717
);
1818

1919
if (!match)
20-
throw new TypeError("First argument `semver` must be a semver string.");
20+
throw new TypeError("Argument 1 `semver` must be a semver string.");
2121

2222
const [, major, minor, patch, prerelease, build] = match;
2323

semver.test.mjs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,20 @@ import semver from "./semver.mjs";
88
* @param {import("test-director").default} tests Test director.
99
*/
1010
export default (tests) => {
11-
tests.add("`semver` with first argument `semver` not a string.", () => {
11+
tests.add("`semver` with argument 1 `semver` not a string.", () => {
1212
throws(() => {
1313
semver(
1414
// @ts-expect-error Testing invalid.
1515
true
1616
);
17-
}, new TypeError("First argument `semver` must be a string."));
17+
}, new TypeError("Argument 1 `semver` must be a string."));
1818
});
1919

20-
tests.add(
21-
"`semver` with first argument `semver` not a semver string.",
22-
() => {
23-
throws(() => {
24-
semver("");
25-
}, new TypeError("First argument `semver` must be a semver string."));
26-
}
27-
);
20+
tests.add("`semver` with argument 1 `semver` not a semver string.", () => {
21+
throws(() => {
22+
semver("");
23+
}, new TypeError("Argument 1 `semver` must be a semver string."));
24+
});
2825

2926
tests.add("`semver` with a simple version.", () => {
3027
deepStrictEqual(semver("1.2.3"), {

sourceRange.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ export default function sourceRange(
1919
ignoreNextLineComment = " coverage ignore next line"
2020
) {
2121
if (typeof source !== "string")
22-
throw new TypeError("First argument `source` must be a string.");
22+
throw new TypeError("Argument 1 `source` must be a string.");
2323

2424
if (typeof startOffset !== "number")
25-
throw new TypeError("Second argument `startOffset` must be a number.");
25+
throw new TypeError("Argument 2 `startOffset` must be a number.");
2626

2727
if (typeof endOffset !== "number")
28-
throw new TypeError("Third argument `endOffset` must be a number.");
28+
throw new TypeError("Argument 3 `endOffset` must be a number.");
2929

3030
if (
3131
typeof ignoreNextLineComment !== "string" &&
3232
ignoreNextLineComment !== false
3333
)
3434
throw new TypeError(
35-
"Fourth argument `ignoreNextLineComment` must be a string or `false`."
35+
"Argument 4 `ignoreNextLineComment` must be a string or `false`."
3636
);
3737

3838
const ignoreNextLineCommentLowerCase = ignoreNextLineComment

sourceRange.test.mjs

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,41 @@ import sourceRange from "./sourceRange.mjs";
88
* @param {import("test-director").default} tests Test director.
99
*/
1010
export default (tests) => {
11-
tests.add("`sourceRange` with first argument `source` not a string.", () => {
11+
tests.add("`sourceRange` with argument 1 `source` not a string.", () => {
1212
throws(() => {
1313
sourceRange(
1414
// @ts-expect-error Testing invalid.
1515
true,
1616
0,
1717
0
1818
);
19-
}, new TypeError("First argument `source` must be a string."));
19+
}, new TypeError("Argument 1 `source` must be a string."));
2020
});
2121

22-
tests.add(
23-
"`sourceRange` with second argument `startOffset` not a number.",
24-
() => {
25-
throws(() => {
26-
sourceRange(
27-
"a",
28-
// @ts-expect-error Testing invalid.
29-
true,
30-
0
31-
);
32-
}, new TypeError("Second argument `startOffset` must be a number."));
33-
}
34-
);
22+
tests.add("`sourceRange` with argument 2 `startOffset` not a number.", () => {
23+
throws(() => {
24+
sourceRange(
25+
"a",
26+
// @ts-expect-error Testing invalid.
27+
true,
28+
0
29+
);
30+
}, new TypeError("Argument 2 `startOffset` must be a number."));
31+
});
3532

36-
tests.add(
37-
"`sourceRange` with third argument `endOffset` not a number.",
38-
() => {
39-
throws(() => {
40-
sourceRange(
41-
"a",
42-
0,
43-
// @ts-expect-error Testing invalid.
44-
true
45-
);
46-
}, new TypeError("Third argument `endOffset` must be a number."));
47-
}
48-
);
33+
tests.add("`sourceRange` with argument 3 `endOffset` not a number.", () => {
34+
throws(() => {
35+
sourceRange(
36+
"a",
37+
0,
38+
// @ts-expect-error Testing invalid.
39+
true
40+
);
41+
}, new TypeError("Argument 3 `endOffset` must be a number."));
42+
});
4943

5044
tests.add(
51-
"`sourceRange` with fourth argument `ignoreNextLineComment` not a string or `false`.",
45+
"`sourceRange` with argument 4 `ignoreNextLineComment` not a string or `false`.",
5246
() => {
5347
throws(() => {
5448
sourceRange(
@@ -58,7 +52,7 @@ export default (tests) => {
5852
// @ts-expect-error Testing invalid.
5953
true
6054
);
61-
}, new TypeError("Fourth argument `ignoreNextLineComment` must be a string or `false`."));
55+
}, new TypeError("Argument 4 `ignoreNextLineComment` must be a string or `false`."));
6256
}
6357
);
6458

test/replaceStackTraces.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ export default function replaceStackTraces(
1313
replacer = "$1<stack trace>"
1414
) {
1515
if (typeof string !== "string")
16-
throw new TypeError("First argument `string` must be a string.");
16+
throw new TypeError("Argument 1 `string` must be a string.");
1717

1818
if (typeof replacer !== "string")
19-
throw new TypeError("Second argument `replacer` must be a string.");
19+
throw new TypeError("Argument 2 `replacer` must be a string.");
2020

2121
return string.replace(
2222
/(^ {2,})at (?:(?! \{$).)+(?:\r?\n\1at (?:(?! \{$).)+)*/gmu,

test/replaceStackTraces.test.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,27 @@ import replaceStackTraces from "./replaceStackTraces.mjs";
99
*/
1010
export default (tests) => {
1111
tests.add(
12-
"`replaceStackTraces` with first argument `string` not a string.",
12+
"`replaceStackTraces` with argument 1 `string` not a string.",
1313
() => {
1414
throws(() => {
1515
replaceStackTraces(
1616
// @ts-expect-error Testing invalid.
1717
true
1818
);
19-
}, new TypeError("First argument `string` must be a string."));
19+
}, new TypeError("Argument 1 `string` must be a string."));
2020
}
2121
);
2222

2323
tests.add(
24-
"`replaceStackTraces` with second argument `replacer` not a string.",
24+
"`replaceStackTraces` with argument 2 `replacer` not a string.",
2525
() => {
2626
throws(() => {
2727
replaceStackTraces(
2828
"",
2929
// @ts-expect-error Testing invalid.
3030
true
3131
);
32-
}, new TypeError("Second argument `replacer` must be a string."));
32+
}, new TypeError("Argument 2 `replacer` must be a string."));
3333
}
3434
);
3535

0 commit comments

Comments
 (0)