Skip to content

Commit

Permalink
prevent-abbreviations: Rename options (#1164)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Apr 7, 2021
1 parent b9cf630 commit cf42cc5
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 37 deletions.
12 changes: 6 additions & 6 deletions docs/rules/prevent-abbreviations.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,33 +123,33 @@ The example below disables all the default replacements and enables a custom `cm
]
```

### whitelist
### allowList

Type: `object`

You can extend the default whitelist by passing the `whitelist` option.
You can extend the default allowList by passing the `allowList` option.

Unlike the `replacements` option, `whitelist` matches full identifier names case-sensitively.
Unlike the `replacements` option, `allowList` matches full identifier names case-sensitively.

For example, if you want to report `props``properties` (enabled by default), but allow `getInitialProps`, you could use the following configuration.

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

### extendDefaultWhitelist
### extendDefaultAllowList

Type: `boolean`\
Default: `true`

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

### checkDefaultAndNamespaceImports

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@
"error",
{
"replacements": {
"fn": false
"ref": {
"reference": true
}
}
}
]
Expand Down
8 changes: 4 additions & 4 deletions rules/filename-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const PLACEHOLDER_REGEX = new RegExp(PLACEHOLDER, 'i');
const isIgnoredChar = char => !/^[a-z\d-_$]$/i.test(char);
const ignoredByDefault = new Set(['index.js', 'index.mjs', 'index.cjs', 'index.ts', 'index.tsx', 'index.vue']);

function ignoreNumbers(fn) {
function ignoreNumbers(caseFunction) {
return string => {
const stack = [];
let execResult = numberRegex.exec(string);
Expand All @@ -27,7 +27,7 @@ function ignoreNumbers(fn) {
execResult = numberRegex.exec(string);
}

let withCase = fn(string);
let withCase = caseFunction(string);

while (stack.length > 0) {
withCase = withCase.replace(PLACEHOLDER_REGEX, stack.shift());
Expand Down Expand Up @@ -80,12 +80,12 @@ function getChosenCases(options) {
function validateFilename(words, caseFunctions) {
return words
.filter(({ignored}) => !ignored)
.every(({word}) => caseFunctions.some(fn => fn(word) === word));
.every(({word}) => caseFunctions.some(caseFunction => caseFunction(word) === word));
}

function fixFilename(words, caseFunctions, {leading, extension}) {
const replacements = words
.map(({word, ignored}) => ignored ? [word] : caseFunctions.map(fn => fn(word)));
.map(({word, ignored}) => ignored ? [word] : caseFunctions.map(caseFunction => caseFunction(word)));

const {
samples: combinations
Expand Down
4 changes: 2 additions & 2 deletions rules/prefer-keyboard-event-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ const isPropertyOf = (node, eventNode) => {

// The third argument is a condition function, as one passed to `Array#filter()`
// Helpful if nearest node of type also needs to have some other property
const getMatchingAncestorOfType = (node, type, fn = () => true) => {
const getMatchingAncestorOfType = (node, type, testFunction = () => true) => {
let current = node;
while (current) {
if (current.type === type && fn(current)) {
if (current.type === type && testFunction(current)) {
return current;
}

Expand Down
28 changes: 14 additions & 14 deletions rules/prevent-abbreviations.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,18 @@ const prepareOptions = ({
extendDefaultReplacements = true,
replacements = {},

extendDefaultWhitelist = true,
whitelist = {},
extendDefaultAllowList = true,
allowList = {},

ignore = []
} = {}) => {
const mergedReplacements = extendDefaultReplacements ?
defaultsDeep({}, replacements, defaultReplacements) :
replacements;

const mergedWhitelist = extendDefaultWhitelist ?
defaultsDeep({}, whitelist, defaultAllowList) :
whitelist;
const mergedAllowList = extendDefaultAllowList ?
defaultsDeep({}, allowList, defaultAllowList) :
allowList;

ignore = ignore.map(
pattern => pattern instanceof RegExp ? pattern : new RegExp(pattern, 'u')
Expand All @@ -294,15 +294,15 @@ const prepareOptions = ({
[discouragedName, new Map(Object.entries(replacements))]
)
),
whitelist: new Map(Object.entries(mergedWhitelist)),
allowList: new Map(Object.entries(mergedAllowList)),

ignore
};
};

const getWordReplacements = (word, {replacements, whitelist}) => {
// Skip constants and whitelist
if (isUpperCase(word) || whitelist.get(word)) {
const getWordReplacements = (word, {replacements, allowList}) => {
// Skip constants and allowList
if (isUpperCase(word) || allowList.get(word)) {
return [];
}

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

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

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

Expand Down Expand Up @@ -818,10 +818,10 @@ const schema = [
replacements: {
$ref: '#/items/0/definitions/abbreviations'
},
extendDefaultWhitelist: {
extendDefaultAllowList: {
type: 'boolean'
},
whitelist: {
allowList: {
$ref: '#/items/0/definitions/booleanObject'
},
ignore: {
Expand Down
20 changes: 10 additions & 10 deletions test/prevent-abbreviations.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,21 @@ const checkPropertiesOptions = [
}
];

const extendDefaultWhitelistOptions = [
const extendDefaultAllowListOptions = [
{
whitelist: {
allowList: {
err: true
},
extendDefaultWhitelist: true
extendDefaultAllowList: true
}
];

const noExtendDefaultWhitelistOptions = [
const noExtendDefaultAllowListOptions = [
{
whitelist: {
allowList: {
err: true
},
extendDefaultWhitelist: false
extendDefaultAllowList: false
}
];

Expand Down Expand Up @@ -275,10 +275,10 @@ ruleTester.run('prevent-abbreviations', rule, {
filename: 'err/http-error.js'
},

// `extendDefaultWhitelist` option
// `extendDefaultAllowList` option
{
code: 'const propTypes = 2;const err = 2;',
options: extendDefaultWhitelistOptions
options: extendDefaultAllowListOptions
},

// `ignore` option
Expand Down Expand Up @@ -1311,11 +1311,11 @@ ruleTester.run('prevent-abbreviations', rule, {
errors: createErrors('The filename `cb.js` should be named `circuitBreacker.js`. A more descriptive name will do too.')
},

// `extendDefaultWhitelist` option
// `extendDefaultAllowList` option
{
code: 'const propTypes = 2;const err = 2;',
output: 'const propertyTypes = 2;const err = 2;',
options: noExtendDefaultWhitelistOptions,
options: noExtendDefaultAllowListOptions,
errors: createErrors()
}
]
Expand Down

0 comments on commit cf42cc5

Please sign in to comment.