Skip to content

Commit

Permalink
Add fixer for no-proxy
Browse files Browse the repository at this point in the history
Part of #115
  • Loading branch information
edg2s committed Oct 8, 2020
1 parent 4cbdad4 commit 3a5cc55
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
20 changes: 19 additions & 1 deletion src/rules/no-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,23 @@ const utils = require( '../utils.js' );

module.exports = utils.createUtilMethodRule(
'proxy',
'Prefer `Function#bind` to `$.proxy`'
'Prefer `Function#bind` to `$.proxy`',
{
fixable: 'code',
fix: function ( node, context, fixer ) {
if (
node.arguments.length >= 2 &&
node.arguments[ 1 ].type !== 'Literal'
) {
const fnText = context.getSourceCode().getText( node.arguments[ 0 ] );
return [
fixer.replaceText( node.callee, fnText + '.bind' ),
fixer.removeRange( [
node.arguments[ 0 ].range[ 0 ],
node.arguments[ 1 ].range[ 0 ]
] )
];
}
}
}
);
23 changes: 21 additions & 2 deletions tests/rules/no-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,30 @@ const error = 'Prefer Function#bind to $.proxy';

const ruleTester = new RuleTester();
ruleTester.run( 'no-proxy', rule, {
valid: [ 'proxy()', '"test".proxy()', '"test".proxy' ],
valid: [ 'proxy(fn, context)', '"test".proxy(fn, context)', '"test".proxy' ],
invalid: [
{
code: '$.proxy()',
code: '$.proxy(this.fn, context)',
errors: [ error ],
output: 'this.fn.bind(context)'
},
{
code: '$.proxy(fn, context, arg1, arg2)',
errors: [ error ],
output: 'fn.bind(context, arg1, arg2)'
},
{
code: '$.proxy(context, "fnName")',
errors: [ error ]
},
{
code: '$.proxy(context, "fnName", arg1, arg2)',
errors: [ error ]
},
{
code: '$.proxy()',
errors: [ error ],
docgen: false
}
]
} );

0 comments on commit 3a5cc55

Please sign in to comment.