Skip to content

Latest commit

 

History

History
222 lines (154 loc) · 4.34 KB

prevent-abbreviations.md

File metadata and controls

222 lines (154 loc) · 4.34 KB

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 and extendDefaultReplacements options.

You can find the default replacements here.

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

Fail

const e = new Error();
const e = document.createEvent('Event');
const levels = {
	err: 0
};
this.evt = 'click';
class Btn {}

Pass

const error = new Error();
const event = document.createEvent('Event');
const levels = {
	error: 0
};
this.event = 'click';
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 eevent replacement (leaving eerror enabled),
  • disables res replacement completely (both resresponse and resresult from defaults are disabled),
  • adds a custom cmdcommand replacement,
  • adds a custom errCbhandleError replacement.
"unicorn/prevent-abbreviations": [
	"error",
	{
		"replacements": {
			"e": {
				"event": false
			},
			"res": false,
			"cmd": {
				"command": true
			},
			"errCb": {
				"handleError": true
			}
		}
	}
]

extendDefaultReplacements

Type: boolean
Default: true

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

The example below disables all the default replacements and enables a custom cmdcommand one.

"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 propsproperties (enabled by default), but allow getInitialProps, you could use the following configuration.

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

extendDefaultWhitelist

Type: boolean
Default: true

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

checkDefaultAndNamespaceImports

Type: boolean
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.

import tempWrite from 'temp-write';
import * as err from 'err';
const err = require('err');

checkShorthandImports

Type: boolean
Default: false

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

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

import {prop} from 'ramda';

checkShorthandProperties

Type: boolean
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.

const {prop} = require('ramda');
const {err} = foo;
function f({err}) {}

checkProperties

Type: boolean
Default: true

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

checkVariables

Type: boolean
Default: true

Pass "checkVariables": false to disable checking variable names.