Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New rule: prefer-native-method (Fixes #221) #335

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
39 changes: 39 additions & 0 deletions src/rules/prefer-native-method.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @fileoverview Rule to make sure certain native functions are used instead of lodash functions.
*/
'use strict'

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

const getDocsUrl = require('../util/getDocsUrl')

module.exports = {
meta: {
docs: {
url: getDocsUrl('prefer-native-method')
},
fixable: 'code'
},

create(context) {
const {getLodashMethodVisitors} = require('../util/lodashUtil')
const {isAliasOfMethod} = require('../util/methodDataUtil')

const sourceCode = context.getSourceCode()

return getLodashMethodVisitors(context, (node, iteratee, {method, version}) => {
if (isAliasOfMethod(version, 'map', method)) {
context.report({
node,
message: 'Prefer \'Array.prototype.map\' over the lodash function.',
fix(fixer) {
const [firstArg, ...restArgs] = node.arguments
return fixer.replaceText(node, `(${sourceCode.getText(firstArg)}).map(${restArgs.map(arg => sourceCode.getText(arg)).join(', ')})`)
}
})
}
})
}
}
63 changes: 63 additions & 0 deletions tests/lib/rules/prefer-native-method.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const rule = require('../../../src/rules/prefer-native-method')
const ruleTesterUtil = require('../testUtil/ruleTesterUtil')

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------

const ruleTester = ruleTesterUtil.getRuleTester()
const {withDefaultPragma, fromVersion3WithDefaultPragma} = require('../testUtil/optionsUtil')
ruleTester.run('prefer-native-method', rule, {
valid: [
...[
'[1,2,3].map((i) => i)',
'arr.map((i) => i)'
].map(withDefaultPragma),
...[
'var x = arr.map(something, withSomething)'
].map(fromVersion3WithDefaultPragma)
],
invalid: [{
code: 'var x = _.map(arr, f)',
errors: [{message: 'Prefer \'Array.prototype.map\' over the lodash function.'}],
output: 'var x = (arr).map(f)'
}, {
code: 'import {map} from "lodash"; var x = map(arr, f)',
errors: [{message: 'Prefer \'Array.prototype.map\' over the lodash function.'}],
output: 'import {map} from "lodash"; var x = (arr).map(f)',
parserOptions: {
sourceType: 'module'
}
}, {
code: 'var x = _.map(arr, (i) => i)',
errors: [{message: 'Prefer \'Array.prototype.map\' over the lodash function.'}],
output: 'var x = (arr).map((i) => i)'
}, {
code: 'import {map as renamedMap} from "lodash"; var x = renamedMap(arr, (i) => i)',
errors: [{message: 'Prefer \'Array.prototype.map\' over the lodash function.'}],
output: 'import {map as renamedMap} from "lodash"; var x = (arr).map((i) => i)',
parserOptions: {
sourceType: 'module'
}
}, {
code: 'import {map as renamedMap} from "lodash"; var x = renamedMap(compact([1,2,3,null]), (i) => i)',
errors: [{message: 'Prefer \'Array.prototype.map\' over the lodash function.'}],
output: 'import {map as renamedMap} from "lodash"; var x = (compact([1,2,3,null])).map((i) => i)',
parserOptions: {
sourceType: 'module'
}
}, {
code: 'import {map} from "lodash"; var x = map(a ? b : c, (i) => i)',
errors: [{message: 'Prefer \'Array.prototype.map\' over the lodash function.'}],
output: 'import {map} from "lodash"; var x = (a ? b : c).map((i) => i)',
parserOptions: {
sourceType: 'module'
}
}].map(withDefaultPragma)
})