Skip to content

Commit b6baf6e

Browse files
BridgeARMayaLekova
authored andcommitted
tools: add assert.doesNotThrow eslint rule
Prohibit the usage of `assert.doesNotThrow()`. PR-URL: nodejs#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 722e464 commit b6baf6e

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

.eslintrc.yaml

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

benchmark/assert/throws.js

Lines changed: 1 addition & 0 deletions
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ to the caller.
341341
The following, for instance, will throw the [`TypeError`][] because there is no
342342
matching error type in the assertion:
343343

344+
<!-- eslint-disable no-restricted-syntax -->
344345
```js
345346
assert.doesNotThrow(
346347
() => {
@@ -353,6 +354,7 @@ assert.doesNotThrow(
353354
However, the following will result in an `AssertionError` with the message
354355
'Got unwanted exception (TypeError)..':
355356

357+
<!-- eslint-disable no-restricted-syntax -->
356358
```js
357359
assert.doesNotThrow(
358360
() => {
@@ -366,6 +368,7 @@ If an `AssertionError` is thrown and a value is provided for the `message`
366368
parameter, the value of `message` will be appended to the `AssertionError`
367369
message:
368370

371+
<!-- eslint-disable no-restricted-syntax -->
369372
```js
370373
assert.doesNotThrow(
371374
() => {

test/parallel/test-assert.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ assert.throws(() => thrower(TypeError));
121121
}
122122

123123
common.expectsError(
124-
() => assert.doesNotThrow(() => thrower(Error), 'user message'),
124+
() => a.doesNotThrow(() => thrower(Error), 'user message'),
125125
{
126126
type: a.AssertionError,
127127
code: 'ERR_ASSERTION',
@@ -131,15 +131,15 @@ common.expectsError(
131131
);
132132

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

141141
common.expectsError(
142-
() => assert.doesNotThrow(() => thrower(Error)),
142+
() => a.doesNotThrow(() => thrower(Error)),
143143
{
144144
code: 'ERR_ASSERTION',
145145
message: /Got unwanted exception\.\n\[object Object\]/
@@ -292,7 +292,7 @@ try {
292292

293293
// Verify AssertionError is the result from doesNotThrow with custom Error.
294294
try {
295-
assert.doesNotThrow(() => {
295+
a.doesNotThrow(() => {
296296
throw new TypeError('wrong type');
297297
}, TypeError, rangeError);
298298
} catch (e) {
@@ -760,7 +760,6 @@ common.expectsError(
760760

761761
errObj.code = '404';
762762
common.expectsError(
763-
// eslint-disable-next-line no-restricted-syntax
764763
() => assert.throws(errFn, errObj),
765764
{
766765
code: 'ERR_ASSERTION',
@@ -772,7 +771,6 @@ common.expectsError(
772771
errObj.code = 404;
773772
errObj.foo = 'bar';
774773
common.expectsError(
775-
// eslint-disable-next-line no-restricted-syntax
776774
() => assert.throws(errFn, errObj),
777775
{
778776
code: 'ERR_ASSERTION',
@@ -791,7 +789,7 @@ common.expectsError(
791789
);
792790

793791
common.expectsError(
794-
() => assert.doesNotThrow(() => { throw new Error(); }, { foo: 'bar' }),
792+
() => a.doesNotThrow(() => { throw new Error(); }, { foo: 'bar' }),
795793
{
796794
type: TypeError,
797795
code: 'ERR_INVALID_ARG_TYPE',
@@ -822,7 +820,7 @@ common.expectsError(
822820
assert.throws(() => { throw undefined; }, /undefined/);
823821
common.expectsError(
824822
// eslint-disable-next-line no-throw-literal
825-
() => assert.doesNotThrow(() => { throw undefined; }),
823+
() => a.doesNotThrow(() => { throw undefined; }),
826824
{
827825
type: assert.AssertionError,
828826
code: 'ERR_ASSERTION',

0 commit comments

Comments
 (0)