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

Added a rule for using lodash with destructuring assignments #8

Open
wants to merge 1 commit 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Finally, enable all of the rules that you would like to use.
# List of supported rules

* [import](docs/rules/import.md): Prevent importing the entire lodash (or lodash-compat) library.
* [import-destructed](docs/rules/import-destructed.md): Prevent importing the entire lodash library without using destructuring assignments.


## To Do

Expand Down
21 changes: 21 additions & 0 deletions docs/rules/import-destructed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Enforce using destructuring when using lodash functions

This rule enforces a cleaner codebase when using lodash's utility functions.

## Rule Details

The following patterns are considered warnings:

```js
import _ from 'lodash';
```

```js
import 'lodash';
```

The following patterns are considered valid:

```js
import {find} from 'lodash';
```
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import importRule from './rules/import';
import importDestructedRule from './rules/import-destructed';

export default {
rules: {
'import': importRule
'import': importRule,
'import-destructed': importDestructedRule
},
rulesConfig: {
'import': 1
'import': 1,
'import-destructed': 1
}
};
28 changes: 28 additions & 0 deletions src/rules/import-destructed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @fileoverview Rule to enforce only allowing destructuring assignment when using lodash
* @author Richard van der Dys
* @copyright 2015 Matt Smith. All rights reserved.
*/

const message = 'Please use the destructuring assignment when using lodash functions';

export default function(context) {
return {
ImportDeclaration(node) {
if (node.source.value === 'lodash') {
const vars = context.getDeclaredVariables(node);
if (vars.length === 0) {
context.report(node.source, message);
} else {
const names = vars[0].identifiers.map(id => id.name);
const usingEntireLibrary = names.some(name => {
return name === 'lodash' || name === '_';
});
if (usingEntireLibrary) {
context.report(node.source, message);
}
}
}
}
};
}
47 changes: 47 additions & 0 deletions test/rules/import-destructed.Spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import eslint from 'eslint';
import 'babel-eslint';
import rule from '../../src/rules/import-destructed';

const ruleTester = new eslint.RuleTester();

const message = 'Please use the destructuring assignment when using lodash functions';

ruleTester.run('lodash/import-destructed', rule, {
valid: [
{
code: 'import {pluck, find} from "lodash"',
parser: 'babel-eslint'
},
{
code: 'import { pluck as plink } from "lodash"',
parser: 'babel-eslint'
},
{
code: 'import { pluck } from "lodash"',
parser: 'babel-eslint'
},
],
invalid: [
{
code: 'import "lodash"',
parser: 'babel-eslint',
errors: [{
message
}]
},
{
code: 'import _ from "lodash"',
parser: 'babel-eslint',
errors: [{
message
}]
},
{
code: 'import lodash from "lodash"',
parser: 'babel-eslint',
errors: [{
message
}]
}
]
});