Skip to content

Commit d795865

Browse files
BridgeARMylesBorins
authored andcommitted
tools: add assert.doesNotThrow eslint rule
Prohibit the usage of `assert.doesNotThrow()`. Backport-PR-URL: #19244 PR-URL: #18669 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 1eac1d7 commit d795865

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

.eslintrc.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,11 @@ rules:
137137
no-mixed-spaces-and-tabs: error
138138
no-multiple-empty-lines: [error, {max: 2, maxEOF: 0, maxBOF: 0}]
139139
no-restricted-syntax: [error, {
140+
selector: "CallExpression[callee.object.name='assert'][callee.property.name='doesNotThrow']",
141+
message: "Please replace `assert.doesNotThrow()` and add a comment next to the code instead."
142+
}, {
140143
selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.1.type='Literal']:not([arguments.1.regex])",
141-
message: "use a regular expression for second argument of assert.throws()"
144+
message: "Use a regular expression for second argument of assert.throws()"
142145
}, {
143146
selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.length<2]",
144147
message: "assert.throws() must be invoked with at least two arguments."

benchmark/assert/throws.js

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function main({ n, method }) {
2626
case 'doesNotThrow':
2727
bench.start();
2828
for (i = 0; i < n; ++i) {
29+
// eslint-disable-next-line no-restricted-syntax
2930
assert.doesNotThrow(doesNotThrow);
3031
}
3132
bench.end(n);

doc/api/assert.md

+3
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ to the caller.
346346
The following, for instance, will throw the [`TypeError`][] because there is no
347347
matching error type in the assertion:
348348

349+
<!-- eslint-disable no-restricted-syntax -->
349350
```js
350351
assert.doesNotThrow(
351352
() => {
@@ -358,6 +359,7 @@ assert.doesNotThrow(
358359
However, the following will result in an `AssertionError` with the message
359360
'Got unwanted exception (TypeError)..':
360361

362+
<!-- eslint-disable no-restricted-syntax -->
361363
```js
362364
assert.doesNotThrow(
363365
() => {
@@ -371,6 +373,7 @@ If an `AssertionError` is thrown and a value is provided for the `message`
371373
parameter, the value of `message` will be appended to the `AssertionError`
372374
message:
373375

376+
<!-- eslint-disable no-restricted-syntax -->
374377
```js
375378
assert.doesNotThrow(
376379
() => {

test/parallel/test-assert.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ assert.ifError(null);
120120
assert.ifError();
121121

122122
common.expectsError(
123-
() => assert.doesNotThrow(() => thrower(Error), 'user message'),
123+
() => a.doesNotThrow(() => thrower(Error), 'user message'),
124124
{
125125
type: a.AssertionError,
126126
code: 'ERR_ASSERTION',
@@ -130,15 +130,15 @@ common.expectsError(
130130
);
131131

132132
common.expectsError(
133-
() => assert.doesNotThrow(() => thrower(Error), 'user message'),
133+
() => a.doesNotThrow(() => thrower(Error), 'user message'),
134134
{
135135
code: 'ERR_ASSERTION',
136136
message: /Got unwanted exception: user message\n\[object Object\]/
137137
}
138138
);
139139

140140
common.expectsError(
141-
() => assert.doesNotThrow(() => thrower(Error)),
141+
() => a.doesNotThrow(() => thrower(Error)),
142142
{
143143
code: 'ERR_ASSERTION',
144144
message: /Got unwanted exception\.\n\[object Object\]/
@@ -307,7 +307,7 @@ try {
307307

308308
// Verify AssertionError is the result from doesNotThrow with custom Error.
309309
try {
310-
assert.doesNotThrow(() => {
310+
a.doesNotThrow(() => {
311311
throw new TypeError('wrong type');
312312
}, TypeError, rangeError);
313313
} catch (e) {
@@ -645,7 +645,7 @@ common.expectsError(
645645
);
646646

647647
common.expectsError(
648-
() => assert.doesNotThrow(() => { throw new Error(); }, { foo: 'bar' }),
648+
() => a.doesNotThrow(() => { throw new Error(); }, { foo: 'bar' }),
649649
{
650650
type: TypeError,
651651
code: 'ERR_INVALID_ARG_TYPE',
@@ -676,7 +676,7 @@ common.expectsError(
676676
assert.throws(() => { throw undefined; }, /undefined/);
677677
common.expectsError(
678678
// eslint-disable-next-line no-throw-literal
679-
() => assert.doesNotThrow(() => { throw undefined; }),
679+
() => a.doesNotThrow(() => { throw undefined; }),
680680
{
681681
type: assert.AssertionError,
682682
code: 'ERR_ASSERTION',

0 commit comments

Comments
 (0)