| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 727c5e3 commit 3adda4b
10 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,4 +3,5 @@ | |||
| 3 | 3 | rules: | |
| 4 | 4 | ## common module is mandatory in tests | |
| 5 | 5 | required-modules: [2, common] | |
| 6 | + prefer-assert-iferror: 2 | ||
| 6 | 7 | prefer-assert-methods: 2 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -52,7 +52,7 @@ assert.strictEqual(key.toString('hex'), expected); | |||
| 52 | 52 | ||
| 53 | 53 | crypto.pbkdf2('password', 'salt', 32, 32, 'sha256', common.mustCall(ondone)); | |
| 54 | 54 | function ondone(err, key) { | |
| 55 | - if (err) throw err; | ||
| 55 | + assert.ifError(err); | ||
| 56 | 56 | assert.strictEqual(key.toString('hex'), expected); | |
| 57 | 57 | } | |
| 58 | 58 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,7 @@ | |||
| 2 | 2 | const common = require('../common'); | |
| 3 | 3 | const fs = require('fs'); | |
| 4 | 4 | const path = require('path'); | |
| 5 | + const assert = require('assert'); | ||
| 5 | 6 | ||
| 6 | 7 | if (!common.isWindows) { | |
| 7 | 8 | common.skip('this test is Windows-specific.'); | |
@@ -21,10 +22,10 @@ console.log({ | |||
| 21 | 22 | }); | |
| 22 | 23 | ||
| 23 | 24 | fs.writeFile(fullPath, 'ok', common.mustCall(function(err) { | |
| 24 | - if (err) throw err; | ||
| 25 | + assert.ifError(err); | ||
| 25 | 26 | ||
| 26 | 27 | fs.stat(fullPath, common.mustCall(function(err, stats) { | |
| 27 | - if (err) throw err; | ||
| 28 | + assert.ifError(err); | ||
| 28 | 29 | })); | |
| 29 | 30 | })); | |
| 30 | 31 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,7 +15,7 @@ const dataExpected = fs.readFileSync(__filename, 'utf8'); | |||
| 15 | 15 | ||
| 16 | 16 | if (process.argv[2] === 'child') { | |
| 17 | 17 | fs.readFile('/dev/stdin', function(er, data) { | |
| 18 | - if (er) throw er; | ||
| 18 | + assert.ifError(er); | ||
| 19 | 19 | process.stdout.write(data); | |
| 20 | 20 | }); | |
| 21 | 21 | return; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,7 +15,7 @@ common.refreshTmpDir(); | |||
| 15 | 15 | ||
| 16 | 16 | // Test fs.symlink() | |
| 17 | 17 | fs.symlink(linkData, linkPath1, 'junction', common.mustCall(function(err) { | |
| 18 | - if (err) throw err; | ||
| 18 | + assert.ifError(err); | ||
| 19 | 19 | verifyLink(linkPath1); | |
| 20 | 20 | })); | |
| 21 | 21 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,18 +14,18 @@ console.log('linkData: ' + linkData); | |||
| 14 | 14 | console.log('linkPath: ' + linkPath); | |
| 15 | 15 | ||
| 16 | 16 | fs.symlink(linkData, linkPath, 'junction', common.mustCall(function(err) { | |
| 17 | - if (err) throw err; | ||
| 17 | + assert.ifError(err); | ||
| 18 | 18 | ||
| 19 | 19 | fs.lstat(linkPath, common.mustCall(function(err, stats) { | |
| 20 | - if (err) throw err; | ||
| 20 | + assert.ifError(err); | ||
| 21 | 21 | assert.ok(stats.isSymbolicLink()); | |
| 22 | 22 | ||
| 23 | 23 | fs.readlink(linkPath, common.mustCall(function(err, destination) { | |
| 24 | - if (err) throw err; | ||
| 24 | + assert.ifError(err); | ||
| 25 | 25 | assert.strictEqual(destination, linkData); | |
| 26 | 26 | ||
| 27 | 27 | fs.unlink(linkPath, common.mustCall(function(err) { | |
| 28 | - if (err) throw err; | ||
| 28 | + assert.ifError(err); | ||
| 29 | 29 | assert(!common.fileExists(linkPath)); | |
| 30 | 30 | assert(common.fileExists(linkData)); | |
| 31 | 31 | })); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -34,15 +34,15 @@ const fixtureThrows = fixture('throws_error4.js'); | |||
| 34 | 34 | // test preloading a single module works | |
| 35 | 35 | childProcess.exec(nodeBinary + ' ' + preloadOption([fixtureA]) + ' ' + fixtureB, | |
| 36 | 36 | function(err, stdout, stderr) { | |
| 37 | - if (err) throw err; | ||
| 37 | + assert.ifError(err); | ||
| 38 | 38 | assert.strictEqual(stdout, 'A\nB\n'); | |
| 39 | 39 | }); | |
| 40 | 40 | ||
| 41 | 41 | // test preloading multiple modules works | |
| 42 | 42 | childProcess.exec( | |
| 43 | 43 | nodeBinary + ' ' + preloadOption([fixtureA, fixtureB]) + ' ' + fixtureC, | |
| 44 | 44 | function(err, stdout, stderr) { | |
| 45 | - if (err) throw err; | ||
| 45 | + assert.ifError(err); | ||
| 46 | 46 | assert.strictEqual(stdout, 'A\nB\nC\n'); | |
| 47 | 47 | } | |
| 48 | 48 | ); | |
@@ -63,7 +63,7 @@ childProcess.exec( | |||
| 63 | 63 | childProcess.exec( | |
| 64 | 64 | nodeBinary + ' ' + preloadOption([fixtureA]) + '-e "console.log(\'hello\');"', | |
| 65 | 65 | function(err, stdout, stderr) { | |
| 66 | - if (err) throw err; | ||
| 66 | + assert.ifError(err); | ||
| 67 | 67 | assert.strictEqual(stdout, 'A\nhello\n'); | |
| 68 | 68 | } | |
| 69 | 69 | ); | |
@@ -110,7 +110,7 @@ childProcess.exec( | |||
| 110 | 110 | nodeBinary + ' ' + preloadOption([fixtureA]) + | |
| 111 | 111 | '-e "console.log(\'hello\');" ' + preloadOption([fixtureA, fixtureB]), | |
| 112 | 112 | function(err, stdout, stderr) { | |
| 113 | - if (err) throw err; | ||
| 113 | + assert.ifError(err); | ||
| 114 | 114 | assert.strictEqual(stdout, 'A\nB\nhello\n'); | |
| 115 | 115 | } | |
| 116 | 116 | ); | |
@@ -131,7 +131,7 @@ childProcess.exec( | |||
| 131 | 131 | nodeBinary + ' ' + '--require ' + fixture('cluster-preload.js') + ' ' + | |
| 132 | 132 | fixture('cluster-preload-test.js'), | |
| 133 | 133 | function(err, stdout, stderr) { | |
| 134 | - if (err) throw err; | ||
| 134 | + assert.ifError(err); | ||
| 135 | 135 | assert.ok(/worker terminated with code 43/.test(stdout)); | |
| 136 | 136 | } | |
| 137 | 137 | ); | |
@@ -142,7 +142,7 @@ childProcess.exec( | |||
| 142 | 142 | nodeBinary + ' ' + '--expose_debug_as=v8debug ' + '--require ' + | |
| 143 | 143 | fixture('cluster-preload.js') + ' ' + 'cluster-preload-test.js', | |
| 144 | 144 | function(err, stdout, stderr) { | |
| 145 | - if (err) throw err; | ||
| 145 | + assert.ifError(err); | ||
| 146 | 146 | assert.ok(/worker terminated with code 43/.test(stdout)); | |
| 147 | 147 | } | |
| 148 | 148 | ); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,7 @@ | |||
| 2 | 2 | // Flags: --expose_gc | |
| 3 | 3 | ||
| 4 | 4 | const common = require('../common'); | |
| 5 | + const assert = require('assert'); | ||
| 5 | 6 | ||
| 6 | 7 | function newBuffer(size, value) { | |
| 7 | 8 | var buffer = Buffer.allocUnsafe(size); | |
@@ -59,7 +60,5 @@ var timeToQuit = Date.now() + 8e3; //Test during no more than this seconds. | |||
| 59 | 60 | ||
| 60 | 61 | ||
| 61 | 62 | function cb(err, written) { | |
| 62 | - if (err) { | ||
| 63 | - throw err; | ||
| 64 | - } | ||
| 63 | + assert.ifError(err); | ||
| 65 | 64 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,7 @@ | |||
| 2 | 2 | // Flags: --expose_gc | |
| 3 | 3 | ||
| 4 | 4 | const common = require('../common'); | |
| 5 | + const assert = require('assert'); | ||
| 5 | 6 | ||
| 6 | 7 | const fs = require('fs'); | |
| 7 | 8 | const testFileName = require('path').join(common.tmpDir, 'GH-814_test.txt'); | |
@@ -64,9 +65,7 @@ function writer() { | |||
| 64 | 65 | ||
| 65 | 66 | function writerCB(err, written) { | |
| 66 | 67 | //console.error('cb.'); | |
| 67 | - if (err) { | ||
| 68 | - throw err; | ||
| 69 | - } | ||
| 68 | + assert.ifError(err); | ||
| 70 | 69 | } | |
| 71 | 70 | ||
| 72 | 71 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,42 @@ | |||
| 1 | + /** | ||
| 2 | + * @fileoverview Prohibit the `if (err) throw err;` pattern | ||
| 3 | + * @author Teddy Katz | ||
| 4 | + */ | ||
| 5 | + | ||
| 6 | + 'use strict'; | ||
| 7 | + | ||
| 8 | + module.exports = { | ||
| 9 | + create(context) { | ||
| 10 | + const sourceCode = context.getSourceCode(); | ||
| 11 | + | ||
| 12 | + function hasSameTokens(nodeA, nodeB) { | ||
| 13 | + const aTokens = sourceCode.getTokens(nodeA); | ||
| 14 | + const bTokens = sourceCode.getTokens(nodeB); | ||
| 15 | + | ||
| 16 | + return aTokens.length === bTokens.length && | ||
| 17 | + aTokens.every((token, index) => { | ||
| 18 | + return token.type === bTokens[index].type && | ||
| 19 | + token.value === bTokens[index].value; | ||
| 20 | + }); | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + return { | ||
| 24 | + IfStatement(node) { | ||
| 25 | + const firstStatement = node.consequent.type === 'BlockStatement' ? | ||
| 26 | + node.consequent.body[0] : | ||
| 27 | + node.consequent; | ||
| 28 | + if ( | ||
| 29 | + firstStatement && | ||
| 30 | + firstStatement.type === 'ThrowStatement' && | ||
| 31 | + hasSameTokens(node.test, firstStatement.argument) | ||
| 32 | + ) { | ||
| 33 | + context.report({ | ||
| 34 | + node: firstStatement, | ||
| 35 | + message: 'Use assert.ifError({{argument}}) instead.', | ||
| 36 | + data: {argument: sourceCode.getText(node.test)} | ||
| 37 | + }); | ||
| 38 | + } | ||
| 39 | + } | ||
| 40 | + }; | ||
| 41 | + } | ||
| 42 | + }; | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments