Skip to content

Commit

Permalink
Add prevent-abbreviations rule (#237)
Browse files Browse the repository at this point in the history
Fixes #169


Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
futpib and sindresorhus committed Mar 13, 2019
1 parent 8fe6630 commit 76ff30e
Show file tree
Hide file tree
Showing 9 changed files with 2,218 additions and 2 deletions.
222 changes: 222 additions & 0 deletions docs/rules/prevent-abbreviations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
# Prevent abbreviations

Using complete words results in more readable code. Not everyone knows all your abbreviations. Code is written only once, but read many times.

This rule can also be used to replace terms, disallow words, etc. See the [`replacements`](#replacements) and [`extendDefaultReplacements`](#extenddefaultreplacements) options.

You can find the default replacements [here](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/master/rules/prevent-abbreviations.js#L13).

This rule is fixable only for variable names with exactly one replacement defined.


## Fail

```js
const e = new Error();
```

```js
const e = document.createEvent('Event');
```

```js
const levels = {
err: 0
};
```

```js
this.evt = 'click';
```

```js
class Btn {}
```


## Pass

```js
const error = new Error();
```

```js
const event = document.createEvent('Event');
```

```js
const levels = {
error: 0
};
```

```js
this.event = 'click';
```

```js
class Button {}
```


## Options

Type: `Object`

### replacements

Type: `Object`

You can extend default replacements by passing the `replacements` option.

Lowercase replacements will match both camelcase and pascalcase identifiers. For example, `err` will match both `err` and `Err`. `errCb` will match both `errCb` and `ErrCb`.

Lowercase replacements will match both complete identifiers and separate words inside identifiers. For example, `cmd` will match all of `cmd`, `createCmd` and `CmdFactory`.

Camelcase replacements will only match complete identifiers. For example `errCb` will only match `errCb` and `ErrCb`. It will not match `fooErrCb` or `errCbFoo`.

The example below:
- disables the default `e``event` replacement (leaving `e``error` enabled),
- disables `res` replacement completely (both `res``response` and `res``result` from defaults are disabled),
- adds a custom `cmd``command` replacement,
- adds a custom `errCb``handleError` replacement.

```js
"unicorn/prevent-abbreviations": [
"error",
{
"replacements": {
"e": {
"event": false
},
"res": false,
"cmd": {
"command": true
},
"errCb": {
"handleError": true
}
}
}
]
```

### extendDefaultReplacements

Type: `boolean`<br>
Default: `true`

Pass `"extendDefaultReplacements": false` to override the default `replacements` completely.

The example below disables all the default replacements and enables a custom `cmd``command` one.

```js
"unicorn/prevent-abbreviations": [
"error",
{
"extendDefaultReplacements": false,
"replacements": {
"cmd": {
"command": true
}
}
}
]
```

### whitelist

Type: `Object`

You can extend the default whitelist by passing the `whitelist` option.

Unlike the `replacements` option, `whitelist` matches full identifier names case-sensitively.

For example, if you want to report `props``properties` (enabled by default), but allow `getInitialProps`, you could use the following configuration.

```js
"unicorn/prevent-abbreviations": [
"error",
{
"whitelist": {
"getInitialProps": true
}
}
]
```

### extendDefaultWhitelist

Type: `boolean`<br>
Default: `true`

Pass `"extendDefaultWhitelist": false` to override the default `whitelist` completely.

### checkDefaultAndNamespaceImports

Type: `boolean`<br>
Default: `false`

Pass `"checkDefaultAndNamespaceImports": true` to check variables declared in default or namespace import.

With this set to `true` the following code will be reported.

```js
import tempWrite from 'temp-write';
```

```js
import * as err from 'err';
```

```js
const err = require('err');
```

### checkShorthandImports

Type: `boolean`<br>
Default: `false`

Pass `"checkShorthandImports": true` to check variables declared in shorthand import.

With this set to `true` the following code will be reported.

```js
import {prop} from 'ramda';
```

### checkShorthandProperties

Type: `boolean`<br>
Default: `false`

Pass `"checkShorthandProperties": true` to check variables declared as shorhand properties in object destructuring.

With this set to `true` the following code will be reported.

```js
const {prop} = require('ramda');
```

```js
const {err} = foo;
```

```js
function f({err}) {}
```

### checkProperties

Type: `boolean`<br>
Default: `true`

Pass `"checkProperties": false` to disable checking property names.

### checkVariables

Type: `boolean`<br>
Default: `true`

Pass `"checkVariables": false` to disable checking variable names.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ module.exports = {
'unicorn/prefer-query-selector': 'error',
'unicorn/prefer-node-remove': 'error',
'unicorn/prefer-text-content': 'error',
'unicorn/no-for-loop': 'error'
'unicorn/no-for-loop': 'error',
'unicorn/prevent-abbreviations': 'error'
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@
"eslint-ast-utils": "^1.0.0",
"import-modules": "^1.1.0",
"lodash.camelcase": "^4.1.1",
"lodash.defaultsdeep": "^4.6.0",
"lodash.kebabcase": "^4.0.1",
"lodash.snakecase": "^4.0.1",
"lodash.topairs": "^4.3.0",
"lodash.upperfirst": "^4.2.0",
"reserved-words": "^0.1.2",
"safe-regex": "^2.0.1"
},
"devDependencies": {
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ Configure it in `package.json`.
"unicorn/prefer-query-selector": "error",
"unicorn/prefer-node-remove": "error",
"unicorn/prefer-text-content": "error",
"unicorn/no-for-loop": "error"
"unicorn/no-for-loop": "error",
"unicorn/prevent-abbreviations": "error"
}
}
}
Expand Down Expand Up @@ -104,6 +105,7 @@ Configure it in `package.json`.
- [prefer-node-remove](docs/rules/prefer-node-remove.md) - Prefer `remove` over `parentNode.removeChild` and `parentElement.removeChild`. *(fixable)*
- [prefer-text-content](docs/rules/prefer-text-content.md) - Prefer `textContent` over `innerText`. *(fixable)*
- [no-for-loop](docs/rules/no-for-loop.md) - Do not use a `for` loop that can be replaced with a `for-of` loop. *(fixable)*
- [prevent-abbreviations](docs/rules/prevent-abbreviations.md) - Prevent abbreviations *(partly fixable)*


## Recommended config
Expand Down
1 change: 1 addition & 0 deletions rules/catch-error-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function isLintablePromiseCatch(node) {
return arg0.type === 'FunctionExpression' || arg0.type === 'ArrowFunctionExpression';
}

// TODO: Use `./utils/avoid-capture.js` instead
function indexifyName(name, scope) {
const variables = scope.variableScope.set;

Expand Down
Loading

0 comments on commit 76ff30e

Please sign in to comment.