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

Add prevent-abbreviations rule #237

Merged
merged 40 commits into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
ce06954
Add `prevent-abbreviations` rule
futpib Jan 31, 2019
c864997
Fix compatibility with Node.js version 6
futpib Feb 3, 2019
03c3623
Apply suggestions from code review
Feb 3, 2019
95bd7d6
Update prevent-abbreviations.js
sindresorhus Feb 13, 2019
14798e5
Document the new utility functions
futpib Feb 13, 2019
0c02d75
Fix the overlapping fixes error
futpib Feb 13, 2019
b0b617e
Fix shorthand property fixer renaming
futpib Feb 15, 2019
7d1d3c6
Add `prop` => `property` replacement
futpib Feb 15, 2019
39458e8
Fix import/export and class name fixing
futpib Feb 16, 2019
e570377
Fix arguments in strict mode renamings
futpib Feb 16, 2019
edcb725
Update prevent-abbreviations.md
sindresorhus Feb 18, 2019
8a937ea
Switch to suffixing with underscores instead of a number to avoid sha…
futpib Feb 23, 2019
0629752
Add original variable name to the messages
futpib Feb 23, 2019
41151bb
Rename an option
futpib Feb 23, 2019
fe18839
Add a test
futpib Feb 23, 2019
94bc269
Report property names
futpib Feb 23, 2019
d42ae8c
Ignore leading and trailing underscores when checking if name is banned
futpib Feb 23, 2019
bb298fc
Add automatic PascalCase matching option (on by default)
futpib Feb 24, 2019
6816219
Add an option to disable variable names checking
futpib Feb 24, 2019
2556cb8
Update docs
futpib Feb 24, 2019
9a5b9d1
Add a bunch of replacements
futpib Mar 3, 2019
3d7e2fe
Fix name collisions with reserved words
futpib Mar 3, 2019
d87e1c1
Add wordwise identifier matching, remove `matchPascalCase` option (no…
futpib Mar 7, 2019
2f0f9dd
Add `whitelist` option and fix word-by-word replacements
futpib Mar 7, 2019
4905a8d
Fix an import renaming bug
futpib Mar 7, 2019
fd909dc
Add a failing test
futpib Mar 7, 2019
50040d6
Fix overlapping fixes error on destructuring assignment
futpib Mar 7, 2019
5020c71
Bring back exact matching (for multiword replacements)
futpib Mar 7, 2019
ab2df1c
Whitelist `stdDev`
futpib Mar 8, 2019
ec879b7
Fix multiple replacements with same target name leading to name colli…
futpib Mar 8, 2019
7503555
Don't report uppercase constant names
futpib Mar 8, 2019
7cd9aac
Remove `dir` -> `direction` replacement
futpib Mar 8, 2019
1d35ec9
Preserve leading and trailing underscores when renaming
futpib Mar 8, 2019
c68d20c
Update docs
futpib Mar 10, 2019
108b8b3
Use same renaming strategy for `arguments` as when avoiding capture a…
futpib Mar 10, 2019
9f91294
Rename check{Property,Variable}Names options to check{Properties,Vari…
futpib Mar 11, 2019
d9f9b10
Allow names from default/named imports and object destructuring by de…
futpib Mar 11, 2019
80ea61f
Update prevent-abbreviations.md
sindresorhus Mar 13, 2019
7b86154
Update avoid-capture.js
sindresorhus Mar 13, 2019
26f43c2
Update resolve-variable-name.js
sindresorhus Mar 13, 2019
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
145 changes: 145 additions & 0 deletions docs/rules/prevent-abbreviations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# Prevent abbreviations

Using complete words results in more readable code. Not everyone knows all your abbreviations. You only write code once, but it's read many times.

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

Default replacements are available [here](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/master/rules/prevent-abbreviations.js#L13).

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


futpib marked this conversation as resolved.
Show resolved Hide resolved
## 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

### replacements

You can extend default replacements by passing this option.

The example below disables the default `e` → `event` replacement and adds a custom `cmd` → `command` one.

```js
"unicorn/prevent-abbreviations": [
"error",
{
"replacements": {
"cmd": {
"command": true
},
"e": {
"event": false
}
}
}
]
```
Copy link
Owner

Choose a reason for hiding this comment

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

@futpib What do you think about starting to use JS config syntax for our examples? The JSON looks so ugly, and most people use JS config anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, but can we defer this to a separate issue too? This would require editing other rules docs.

Copy link
Contributor Author

@futpib futpib Mar 7, 2019

Choose a reason for hiding this comment

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

I use xo key in package.json for configuration though, so it is in json.


### extendDefaultReplacements

Pass `false` here 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
}
}
}
]
```

### matchPascalCase

By default replacements defined in camelCase match PascalCase identifiers too. For example, default replacement `evt` → `event` will match both `evt` and `Evt` (which could be automatically fixed to `Event`).

Pass `false` here to disable this case-agnostic matching.

The example below defines two replacements `res` → `response` and `Res` → `Result` different in case.

```js
"unicorn/prevent-abbreviations": [
"error",
{
"matchPascalCase": false,
"extendDefaultReplacements": false,
"replacements": {
"res": {
"response": true
},
"Res": {
"Result": true
}
}
}
]
```

### checkPropertyNames

Pass `false` here to disable checking property names.

### checkVariableNames

Pass `false` here to disable checking variable names.


## Edge cases

### arguments

This rule does not report or fix `args` → `arguments` when it would result in a collision with the built-in [`arguments` object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments);
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
"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",
"safe-regex": "^2.0.1"
},
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