Skip to content

Commit

Permalink
prevent-abbreviations: Add ignore option
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Oct 21, 2020
1 parent 769ac35 commit 59c66e9
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 6 deletions.
24 changes: 24 additions & 0 deletions docs/rules/prevent-abbreviations.md
Expand Up @@ -249,3 +249,27 @@ Type: `boolean`<br>
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 basename is tested, file named `foo.e2e.js`,with `ignore: [/\.e2e$/]` would pass, with `ignore: [/\.e2e\.js/]` would fail, .
26 changes: 21 additions & 5 deletions rules/prevent-abbreviations.js
Expand Up @@ -257,7 +257,9 @@ const prepareOptions = ({
replacements = {},

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

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

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

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

ignore
};
};

Expand All @@ -309,10 +317,15 @@ const getWordReplacements = (word, {replacements, whitelist}) => {
};

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

if (name.includes('e2e')) {
console.log({name, ignore, t: ignore.some(regexp => regexp.test(name))})

}

// 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 @@ -712,7 +725,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 @@ -778,6 +790,10 @@ const schema = [
},
whitelist: {
$ref: '#/items/0/definitions/booleanObject'
},
ignore: {
type: 'array',
uniqueItems: true
}
},
additionalProperties: false,
Expand Down
36 changes: 35 additions & 1 deletion test/prevent-abbreviations.js
Expand Up @@ -124,6 +124,16 @@ const noExtendDefaultWhitelistOptions = [
}
];

const ignoreOptions = [
{
ignore: [
/^e_/,
new RegExp('_e$', 'i'),
'\\.e2e\\.'
]
}
];

ruleTester.run('prevent-abbreviations', rule, {
valid: [
'let x',
Expand Down Expand Up @@ -267,6 +277,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 @@ -1631,7 +1651,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 59c66e9

Please sign in to comment.