Skip to content

Commit

Permalink
Add checkFilenames option to the prevent-abbreviations rule (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored and sindresorhus committed Jul 6, 2019
1 parent e83f8ed commit 4c3d61c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
7 changes: 7 additions & 0 deletions docs/rules/prevent-abbreviations.md
Expand Up @@ -220,3 +220,10 @@ Type: `boolean`<br>
Default: `true`

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

### checkFilenames

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

Pass `"checkFilenames": false` to disable checking file names.
49 changes: 49 additions & 0 deletions rules/prevent-abbreviations.js
@@ -1,10 +1,12 @@
'use strict';
const path = require('path');
const astUtils = require('eslint-ast-utils');
const defaultsDeep = require('lodash.defaultsdeep');
const toPairs = require('lodash.topairs');
const camelCase = require('lodash.camelcase');
const kebabCase = require('lodash.kebabcase');
const upperfirst = require('lodash.upperfirst');
const snakeCase = require('lodash.snakecase');

const getDocsUrl = require('./utils/get-docs-url');
const avoidCapture = require('./utils/avoid-capture');
Expand Down Expand Up @@ -178,6 +180,16 @@ const defaultWhitelist = {
stdDev: true
};

const getCase = string => {
for (const fn of [camelCase, kebabCase, snakeCase, upperfirst]) {
if (string === fn(string)) {
return fn;
}
}

return camelCase;
};

const prepareOptions = ({
checkProperties = true,
checkVariables = true,
Expand All @@ -186,6 +198,8 @@ const prepareOptions = ({
checkShorthandImports = false,
checkShorthandProperties = false,

checkFilenames = true,

extendDefaultReplacements = true,
replacements = {},

Expand All @@ -208,6 +222,8 @@ const prepareOptions = ({
checkShorthandImports,
checkShorthandProperties,

checkFilenames,

replacements: new Map(toPairs(mergedReplacements).map(([discouragedName, replacements]) => {
return [discouragedName, new Map(toPairs(replacements))];
})),
Expand Down Expand Up @@ -533,6 +549,7 @@ const create = context => {
ecmaVersion
} = context.parserOptions;
const options = prepareOptions(context.options[0]);
const filenameWithExtension = context.getFilename();

// A `class` declaration produces two variables in two scopes:
// the inner class scope, and the outer one (whereever the class is declared).
Expand Down Expand Up @@ -678,6 +695,36 @@ const create = context => {
context.report(problem);
},

Program(node) {
if (!options.checkFilenames) {
return;
}

if (filenameWithExtension === '<input>') {
return {};
}

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

const filenameReplacements = getNameReplacements(
options.replacements,
options.whitelist,
filename
)
.map(replacement => `${originalCase(replacement)}${extension}`);

if (filenameReplacements.length === 0) {
return;
}

context.report({
node,
message: formatMessage(filenameWithExtension, filenameReplacements, 'file')
});
},

'Program:exit'() {
if (!options.checkVariables) {
return;
Expand All @@ -698,6 +745,8 @@ const schema = [{
checkShorthandImports: {type: 'boolean'},
checkShorthandProperties: {type: 'boolean'},

checkFilenames: {type: 'boolean'},

extendDefaultReplacements: {type: 'boolean'},
replacements: {$ref: '#/items/0/definitions/abbreviations'},

Expand Down
28 changes: 27 additions & 1 deletion test/prevent-abbreviations.js
Expand Up @@ -63,6 +63,8 @@ const customOptions = [{
checkShorthandImports: true,
checkShorthandProperties: true,

checkFilenames: false,

extendDefaultReplacements: false,
replacements: {
args: {
Expand Down Expand Up @@ -178,6 +180,20 @@ ruleTester.run('prevent-abbreviations', rule, {
{
code: '({__proto__: null})',
options: customOptions
},
// `checkFilenames` option
{
code: 'foo();',
filename: 'http-error.js'
},
{
code: 'foo();',
filename: 'http-err.js',
options: customOptions
},
{
code: 'foo();',
filename: 'err/http-error.js'
}
],

Expand Down Expand Up @@ -896,7 +912,6 @@ ruleTester.run('prevent-abbreviations', rule, {
`,
errors: createErrors()
},

{
code: outdent`
function unicorn(unicorn) {
Expand All @@ -911,6 +926,17 @@ ruleTester.run('prevent-abbreviations', rule, {
}
`,
errors: createErrors()
},
// `checkFilenames` option
{
code: 'foo();',
filename: 'err/http-err.js',
errors: createErrors()
},
{
code: 'foo();',
filename: 'http-err.js',
errors: createErrors()
}
]
});
Expand Down

0 comments on commit 4c3d61c

Please sign in to comment.