diff --git a/docs/rules/prevent-abbreviations.md b/docs/rules/prevent-abbreviations.md
index f70e272b26..1d82c12c1a 100644
--- a/docs/rules/prevent-abbreviations.md
+++ b/docs/rules/prevent-abbreviations.md
@@ -249,3 +249,27 @@ Type: `boolean`
Default: `true`
Pass `"checkFilenames": false` to disable checking file names.
+
+### ignore
+
+Type: `Array`\
+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, .
diff --git a/rules/prevent-abbreviations.js b/rules/prevent-abbreviations.js
index 4e98c589f8..31508e2b8b 100644
--- a/rules/prevent-abbreviations.js
+++ b/rules/prevent-abbreviations.js
@@ -257,7 +257,9 @@ const prepareOptions = ({
replacements = {},
extendDefaultWhitelist = true,
- whitelist = {}
+ whitelist = {},
+
+ ignore = []
} = {}) => {
const mergedReplacements = extendDefaultReplacements ?
defaultsDeep({}, replacements, defaultReplacements) :
@@ -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,
@@ -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
};
};
@@ -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};
}
@@ -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) {
@@ -778,6 +790,10 @@ const schema = [
},
whitelist: {
$ref: '#/items/0/definitions/booleanObject'
+ },
+ ignore: {
+ type: 'array',
+ uniqueItems: true
}
},
additionalProperties: false,
diff --git a/test/prevent-abbreviations.js b/test/prevent-abbreviations.js
index 851329ece4..08c78052df 100644
--- a/test/prevent-abbreviations.js
+++ b/test/prevent-abbreviations.js
@@ -124,6 +124,16 @@ const noExtendDefaultWhitelistOptions = [
}
];
+const ignoreOptions = [
+ {
+ ignore: [
+ /^e_/,
+ new RegExp('_e$', 'i'),
+ '\\.e2e\\.'
+ ]
+ }
+];
+
ruleTester.run('prevent-abbreviations', rule, {
valid: [
'let x',
@@ -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,
}
],
@@ -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.')
+ ]
+ }
]
});