From 8ffcb2d2cab59fa8174c52a8914266eee2915362 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Mon, 30 Jul 2018 16:33:41 -0700 Subject: [PATCH] tools: prevent string literals in some assertions String literals provided as the third argument to assert.strictEqual() or assert.deepStrictEqual() will mask the values that are causing issues. Use a lint rule to prevent such usage. Backport-PR-URL: https://github.com/nodejs/node/pull/22912 PR-URL: https://github.com/nodejs/node/pull/22849 Reviewed-By: Teddy Katz Reviewed-By: Matteo Collina Reviewed-By: Anna Henningsen Reviewed-By: Trivikram Kamat Reviewed-By: Luigi Pinca Reviewed-By: Colin Ihrig --- .eslintrc.js | 5 +++++ lib/.eslintrc.yaml | 4 ++++ test/.eslintrc.yaml | 4 ++++ .../test-timers-same-timeout-wrong-list-deleted.js | 8 +++----- test/parallel/test-timers-unref-reuse-no-exposed-list.js | 6 ++---- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 9c3a78d81cd..e4192c35399 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -156,6 +156,7 @@ module.exports = { ], /* eslint-disable max-len */ // If this list is modified, please copy the change to lib/.eslintrc.yaml + // and test/.eslintrc.yaml. 'no-restricted-syntax': [ 'error', { @@ -166,6 +167,10 @@ module.exports = { selector: "CallExpression[callee.object.name='assert'][callee.property.name='rejects'][arguments.length<2]", message: 'assert.rejects() must be invoked with at least two arguments.', }, + { + selector: "CallExpression[callee.object.name='assert'][callee.property.name='strictEqual'][arguments.2.type='Literal']", + message: 'Do not use a literal for the third argument of assert.strictEqual()' + }, { selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.1.type='Literal']:not([arguments.1.regex])", message: 'Use an object as second argument of assert.throws()', diff --git a/lib/.eslintrc.yaml b/lib/.eslintrc.yaml index e7cab0ad931..a1a0b9490a9 100644 --- a/lib/.eslintrc.yaml +++ b/lib/.eslintrc.yaml @@ -2,10 +2,14 @@ rules: no-restricted-syntax: # Config copied from .eslintrc.js - error + - selector: "CallExpression[callee.object.name='assert'][callee.property.name='deepStrictEqual'][arguments.2.type='Literal']" + message: "Do not use a literal for the third argument of assert.deepStrictEqual()" - selector: "CallExpression[callee.object.name='assert'][callee.property.name='doesNotThrow']" message: "Please replace `assert.doesNotThrow()` and add a comment next to the code instead." - selector: "CallExpression[callee.object.name='assert'][callee.property.name='rejects'][arguments.length<2]" message: "assert.rejects() must be invoked with at least two arguments." + - selector: "CallExpression[callee.object.name='assert'][callee.property.name='strictEqual'][arguments.2.type='Literal']" + message: "Do not use a literal for the third argument of assert.strictEqual()" - selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.1.type='Literal']:not([arguments.1.regex])" message: "Use an object as second argument of assert.throws()" - selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.length<2]" diff --git a/test/.eslintrc.yaml b/test/.eslintrc.yaml index 25026fec5a1..63d2127d738 100644 --- a/test/.eslintrc.yaml +++ b/test/.eslintrc.yaml @@ -23,10 +23,14 @@ rules: no-restricted-syntax: # Config copied from .eslintrc.js - error + - selector: "CallExpression[callee.object.name='assert'][callee.property.name='deepStrictEqual'][arguments.2.type='Literal']" + message: "Do not use a literal for the third argument of assert.deepStrictEqual()" - selector: "CallExpression[callee.object.name='assert'][callee.property.name='doesNotThrow']" message: "Please replace `assert.doesNotThrow()` and add a comment next to the code instead." - selector: "CallExpression[callee.object.name='assert'][callee.property.name='rejects'][arguments.length<2]" message: "assert.rejects() must be invoked with at least two arguments." + - selector: "CallExpression[callee.object.name='assert'][callee.property.name='strictEqual'][arguments.2.type='Literal']" + message: "Do not use a literal for the third argument of assert.strictEqual()" - selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.1.type='Literal']:not([arguments.1.regex])" message: "Use an object as second argument of assert.throws()" - selector: "CallExpression[callee.object.name='assert'][callee.property.name='throws'][arguments.length<2]" diff --git a/test/parallel/test-timers-same-timeout-wrong-list-deleted.js b/test/parallel/test-timers-same-timeout-wrong-list-deleted.js index c66ba0a57ef..8ad267cd16f 100644 --- a/test/parallel/test-timers-same-timeout-wrong-list-deleted.js +++ b/test/parallel/test-timers-same-timeout-wrong-list-deleted.js @@ -37,7 +37,7 @@ const handle1 = setTimeout(common.mustCall(function() { // Make sure our clearTimeout succeeded. One timer finished and // the other was canceled, so none should be active. - assert.strictEqual(activeTimers.length, 0, 'Timers remain.'); + assert.strictEqual(activeTimers.length, 0); })); })); }), 1); @@ -53,11 +53,9 @@ const handle1 = setTimeout(common.mustCall(function() { // Make sure our clearTimeout succeeded. One timer finished and // the other was canceled, so none should be active. - assert.strictEqual(activeTimers.length, 3, - 'There should be 3 timers in the list.'); + assert.strictEqual(activeTimers.length, 3); assert(shortTimer instanceof Timer, 'The shorter timer is not in the list.'); - assert.strictEqual(longTimers.length, 2, - 'Both longer timers should be in the list.'); + assert.strictEqual(longTimers.length, 2); // When this callback completes, `listOnTimeout` should now look at the // correct list and refrain from removing the new TIMEOUT list which diff --git a/test/parallel/test-timers-unref-reuse-no-exposed-list.js b/test/parallel/test-timers-unref-reuse-no-exposed-list.js index 33b2da2f9e2..269a2b5e85f 100644 --- a/test/parallel/test-timers-unref-reuse-no-exposed-list.js +++ b/test/parallel/test-timers-unref-reuse-no-exposed-list.js @@ -4,11 +4,9 @@ require('../common'); const assert = require('assert'); const timer1 = setTimeout(() => {}, 1).unref(); -assert.strictEqual(timer1._handle._list, undefined, - 'timer1._handle._list should be undefined'); +assert.strictEqual(timer1._handle._list, undefined); // Check that everything works even if the handle was not re-used. setTimeout(() => {}, 1); const timer2 = setTimeout(() => {}, 1).unref(); -assert.strictEqual(timer2._handle._list, undefined, - 'timer2._handle._list should be undefined'); +assert.strictEqual(timer2._handle._list, undefined);