Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tools: auto fix custom eslint rule for prefer-assert-iferror.js
1. Adds the fixer method
2. Extends Tests

Refs: #16636
  • Loading branch information
shobhitchittora committed Jan 5, 2018
commit e061cba6d8b353113532d73856e35751738b4e81
6 changes: 4 additions & 2 deletions test/parallel/test-eslint-prefer-assert-iferror.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);'
Copy link
Copy Markdown
Contributor

@apapirovski apapirovski Dec 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing this won't work without having require('assert'); in code and output. Same thing below.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shobhitchittora would you be so kind and have a look at this comment?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test would actually work without the require('assert') specified. While using assert the user has to require assert to use it right. Also in every other test for rules, this is what is followed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell @apapirovski is right about his comment. I am going to run a CI anyway, so we do not have to speculate on it.

},
{
code: 'if (error) { throw error; }',
errors: [{ message: 'Use assert.ifError(error) instead.' }]
errors: [{ message: 'Use assert.ifError(error) instead.' }],
output: 'assert.ifError(error);'
}
]
});
23 changes: 21 additions & 2 deletions tools/eslint-rules/prefer-assert-iferror.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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});`
);
}
}
});
}
}
Expand Down