Skip to content

Commit

Permalink
prevent-abbreviations: Add ignore option (#891)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Dec 9, 2020
1 parent 877bef9 commit 898fcb4
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 17 deletions.
42 changes: 31 additions & 11 deletions docs/rules/prevent-abbreviations.md
Expand Up @@ -8,7 +8,6 @@ You can find the default replacements [here](https://github.com/sindresorhus/esl

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


## Fail

```js
Expand All @@ -23,7 +22,6 @@ const e = document.createEvent('Event');
class Btn {}
```


## Pass

```js
Expand Down Expand Up @@ -60,7 +58,6 @@ const levels = {
this.evt = 'click';
```


## Options

Type: `object`
Expand Down Expand Up @@ -105,7 +102,7 @@ The example below:

### extendDefaultReplacements

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

Pass `"extendDefaultReplacements": false` to override the default `replacements` completely.
Expand Down Expand Up @@ -149,14 +146,14 @@ For example, if you want to report `props` → `properties` (enabled by default)

### extendDefaultWhitelist

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

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

### checkDefaultAndNamespaceImports

Type: `'internal' | boolean`<br>
Type: `'internal' | boolean`\
Default: `'internal'`

- `'internal'` - Check variables declared in default or namespace import, **but only for internal modules**.
Expand Down Expand Up @@ -189,7 +186,7 @@ const err = require('err');

### checkShorthandImports

Type: `'internal'` | `boolean`<br>
Type: `'internal'` | `boolean`\
Default: `'internal'`

- `'internal'` - Check variables declared in shorthand import, **but only for internal modules**.
Expand All @@ -210,7 +207,7 @@ import {prop} from 'ramda';

### checkShorthandProperties

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

Pass `"checkShorthandProperties": true` to check variables declared as shorhand properties in object destructuring.
Expand All @@ -231,21 +228,44 @@ function f({err}) {}

### checkProperties

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

Pass `"checkProperties": true` to enable checking property names.

### checkVariables

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

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

### checkFilenames

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

Pass `"checkFilenames": false` to disable checking file names.

### ignore

Type: `Array<string | RegExp>`\
Default: `[]`

This option lets you specify a regex pattern for matches to ignore.

When a string is given, it's interpreted as a regular expressions inside a string. Needed for ESLint config in JSON.

```js
"unicorn/prevent-abbreviations": [
"error",
{
"ignore": [
"\\.e2e$",
/^ignore/i
]
}
]
```

When checking filenames, only the basename is tested. For example, with a file named `foo.e2e.js`, `ignore: [/\.e2e$/]` would pass and `ignore: [/\.e2e\.js/]` would fail.
21 changes: 16 additions & 5 deletions rules/prevent-abbreviations.js
Expand Up @@ -263,7 +263,9 @@ const prepareOptions = ({
replacements = {},

extendDefaultWhitelist = true,
whitelist = {}
whitelist = {},

ignore = []
} = {}) => {
const mergedReplacements = extendDefaultReplacements ?
defaultsDeep({}, replacements, defaultReplacements) :
Expand All @@ -273,6 +275,10 @@ const prepareOptions = ({
defaultsDeep({}, whitelist, defaultWhitelist) :
whitelist;

ignore = ignore.map(
pattern => pattern instanceof RegExp ? pattern : new RegExp(pattern, 'u')
);

return {
checkProperties,
checkVariables,
Expand All @@ -289,7 +295,9 @@ const prepareOptions = ({
[discouragedName, new Map(Object.entries(replacements))]
)
),
whitelist: new Map(Object.entries(mergedWhitelist))
whitelist: new Map(Object.entries(mergedWhitelist)),

ignore
};
};

Expand All @@ -315,10 +323,10 @@ const getWordReplacements = (word, {replacements, whitelist}) => {
};

const getNameReplacements = (name, options, limit = 3) => {
const {whitelist} = options;
const {whitelist, ignore} = options;

// Skip constants and whitelist
if (isUpperCase(name) || whitelist.get(name)) {
if (isUpperCase(name) || whitelist.get(name) || ignore.some(regexp => regexp.test(name))) {
return {total: 0};
}

Expand Down Expand Up @@ -718,7 +726,6 @@ const create = context => {

const extension = path.extname(filenameWithExtension);
const filename = path.basename(filenameWithExtension, extension);

const filenameReplacements = getNameReplacements(filename, options);

if (filenameReplacements.total === 0) {
Expand Down Expand Up @@ -784,6 +791,10 @@ const schema = [
},
whitelist: {
$ref: '#/items/0/definitions/booleanObject'
},
ignore: {
type: 'array',
uniqueItems: true
}
},
additionalProperties: false,
Expand Down
37 changes: 36 additions & 1 deletion test/prevent-abbreviations.js
Expand Up @@ -124,6 +124,17 @@ const noExtendDefaultWhitelistOptions = [
}
];

const ignoreOptions = [
{
ignore: [
/^e_/,
// eslint-disable-next-line prefer-regex-literals
new RegExp('_e$', 'i'),
'\\.e2e\\.'
]
}
];

ruleTester.run('prevent-abbreviations', rule, {
valid: [
'let x',
Expand Down Expand Up @@ -267,6 +278,16 @@ ruleTester.run('prevent-abbreviations', rule, {
{
code: 'const propTypes = 2;const err = 2;',
options: extendDefaultWhitelistOptions
},

// `ignore` option
{
code: outdent`
const e_at_start = 1;
const end_with_e = 2;
`,
filename: 'some.spec.e2e.test.js',
options: ignoreOptions
}
],

Expand Down Expand Up @@ -1632,7 +1653,21 @@ runTest({
`,
options: checkPropertiesOptions,
errors: createErrors()
})
}),

// `ignore` option
{
code: outdent`
const e_at_start = 1;
const end_with_e = 2;
`,
filename: 'some.spec.e2e.test.js',
errors: [
...createErrors('Please rename the filename `some.spec.e2e.test.js`. Suggested names are: `some.spec.error2error.test.js`, `some.spec.error2event.test.js`, `some.spec.event2error.test.js`, ... (1 more omitted). A more descriptive name will do too.'),
...createErrors('Please rename the variable `e_at_start`. Suggested names are: `error_at_start`, `event_at_start`. A more descriptive name will do too.'),
...createErrors('Please rename the variable `end_with_e`. Suggested names are: `end_with_error`, `end_with_event`. A more descriptive name will do too.')
]
}
]
});

Expand Down

0 comments on commit 898fcb4

Please sign in to comment.