From 94b97b028fb4d1b740039466c2a73879d7265ccb Mon Sep 17 00:00:00 2001 From: Shobhit Chittora Date: Wed, 1 Nov 2017 01:31:02 +0530 Subject: [PATCH 1/3] tools: auto fix custom eslint rule for prefer-assert-iferror.js 1. Adds the fixer method 2. Extends Tests Refs: #16636 --- .../test-eslint-prefer-assert-iferror.js | 6 +++-- tools/eslint-rules/prefer-assert-iferror.js | 23 +++++++++++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-eslint-prefer-assert-iferror.js b/test/parallel/test-eslint-prefer-assert-iferror.js index 00df47975b8007..ff3340e92f5e58 100644 --- a/test/parallel/test-eslint-prefer-assert-iferror.js +++ b/test/parallel/test-eslint-prefer-assert-iferror.js @@ -15,11 +15,13 @@ new RuleTester().run('prefer-assert-iferror', rule, { invalid: [ { code: 'if (err) throw err;', - errors: [{ message: 'Use assert.ifError(err) instead.' }] + errors: [{ message: 'Use assert.ifError(err) instead.' }], + output: 'assert.ifError(err);' }, { code: 'if (error) { throw error; }', - errors: [{ message: 'Use assert.ifError(error) instead.' }] + errors: [{ message: 'Use assert.ifError(error) instead.' }], + output: 'assert.ifError(error);' } ] }); diff --git a/tools/eslint-rules/prefer-assert-iferror.js b/tools/eslint-rules/prefer-assert-iferror.js index e15287417693e0..399ee7403a6c88 100644 --- a/tools/eslint-rules/prefer-assert-iferror.js +++ b/tools/eslint-rules/prefer-assert-iferror.js @@ -5,9 +5,12 @@ 'use strict'; +const utils = require('./rules-utils.js'); + module.exports = { create(context) { const sourceCode = context.getSourceCode(); + var assertImported = false; function hasSameTokens(nodeA, nodeB) { const aTokens = sourceCode.getTokens(nodeA); @@ -20,8 +23,15 @@ module.exports = { }); } + function checkAssertNode(node) { + if (utils.isRequired(node, ['assert'])) { + assertImported = true; + } + } + return { - IfStatement(node) { + 'CallExpression': (node) => checkAssertNode(node), + 'IfStatement': (node) => { const firstStatement = node.consequent.type === 'BlockStatement' ? node.consequent.body[0] : node.consequent; @@ -30,10 +40,19 @@ module.exports = { firstStatement.type === 'ThrowStatement' && hasSameTokens(node.test, firstStatement.argument) ) { + const argument = sourceCode.getText(node.test); context.report({ node: firstStatement, message: 'Use assert.ifError({{argument}}) instead.', - data: { argument: sourceCode.getText(node.test) } + data: { argument }, + fix: (fixer) => { + if (assertImported) { + return fixer.replaceText( + node, + `assert.ifError(${argument});` + ); + } + } }); } } From e061cba6d8b353113532d73856e35751738b4e81 Mon Sep 17 00:00:00 2001 From: Shobhit Chittora Date: Wed, 1 Nov 2017 01:31:02 +0530 Subject: [PATCH 2/3] tools: auto fix custom eslint rule for prefer-assert-iferror.js 1. Adds the fixer method 2. Extends Tests Refs: #16636 --- .../test-eslint-prefer-assert-iferror.js | 6 +++-- tools/eslint-rules/prefer-assert-iferror.js | 23 +++++++++++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-eslint-prefer-assert-iferror.js b/test/parallel/test-eslint-prefer-assert-iferror.js index 00df47975b8007..ff3340e92f5e58 100644 --- a/test/parallel/test-eslint-prefer-assert-iferror.js +++ b/test/parallel/test-eslint-prefer-assert-iferror.js @@ -15,11 +15,13 @@ new RuleTester().run('prefer-assert-iferror', rule, { invalid: [ { code: 'if (err) throw err;', - errors: [{ message: 'Use assert.ifError(err) instead.' }] + errors: [{ message: 'Use assert.ifError(err) instead.' }], + output: 'assert.ifError(err);' }, { code: 'if (error) { throw error; }', - errors: [{ message: 'Use assert.ifError(error) instead.' }] + errors: [{ message: 'Use assert.ifError(error) instead.' }], + output: 'assert.ifError(error);' } ] }); diff --git a/tools/eslint-rules/prefer-assert-iferror.js b/tools/eslint-rules/prefer-assert-iferror.js index e15287417693e0..399ee7403a6c88 100644 --- a/tools/eslint-rules/prefer-assert-iferror.js +++ b/tools/eslint-rules/prefer-assert-iferror.js @@ -5,9 +5,12 @@ 'use strict'; +const utils = require('./rules-utils.js'); + module.exports = { create(context) { const sourceCode = context.getSourceCode(); + var assertImported = false; function hasSameTokens(nodeA, nodeB) { const aTokens = sourceCode.getTokens(nodeA); @@ -20,8 +23,15 @@ module.exports = { }); } + function checkAssertNode(node) { + if (utils.isRequired(node, ['assert'])) { + assertImported = true; + } + } + return { - IfStatement(node) { + 'CallExpression': (node) => checkAssertNode(node), + 'IfStatement': (node) => { const firstStatement = node.consequent.type === 'BlockStatement' ? node.consequent.body[0] : node.consequent; @@ -30,10 +40,19 @@ module.exports = { firstStatement.type === 'ThrowStatement' && hasSameTokens(node.test, firstStatement.argument) ) { + const argument = sourceCode.getText(node.test); context.report({ node: firstStatement, message: 'Use assert.ifError({{argument}}) instead.', - data: { argument: sourceCode.getText(node.test) } + data: { argument }, + fix: (fixer) => { + if (assertImported) { + return fixer.replaceText( + node, + `assert.ifError(${argument});` + ); + } + } }); } } From 756399c6d85e8c2fde2a197e417a3d5d198e2e87 Mon Sep 17 00:00:00 2001 From: Shobhit Chittora Date: Sun, 4 Feb 2018 09:39:52 +0530 Subject: [PATCH 3/3] tools: auto fix custom eslint rule for prefer-assert-iferror.js 1. Fixes the failing test for the rule. Adds require("assert") to code and output. Refs: #16636 --- test/parallel/test-eslint-prefer-assert-iferror.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-eslint-prefer-assert-iferror.js b/test/parallel/test-eslint-prefer-assert-iferror.js index ff3340e92f5e58..5c61f1f4383b45 100644 --- a/test/parallel/test-eslint-prefer-assert-iferror.js +++ b/test/parallel/test-eslint-prefer-assert-iferror.js @@ -14,14 +14,18 @@ new RuleTester().run('prefer-assert-iferror', rule, { ], invalid: [ { - code: 'if (err) throw err;', + code: 'require("assert");\n' + + 'if (err) throw err;', errors: [{ message: 'Use assert.ifError(err) instead.' }], - output: 'assert.ifError(err);' + output: 'require("assert");\n' + + 'assert.ifError(err);' }, { - code: 'if (error) { throw error; }', + code: 'require("assert");\n' + + 'if (error) { throw error; }', errors: [{ message: 'Use assert.ifError(error) instead.' }], - output: 'assert.ifError(error);' + output: 'require("assert");\n' + + 'assert.ifError(error);' } ] });