Skip to content

Commit

Permalink
fix(eslint-plugin): [naming-convention] cover case that requires quot…
Browse files Browse the repository at this point in the history
…es (#4582)

* fix: cover case that requires quotes

* test: cover more cases

* chore: fix lint style

* test: use noFormat for better testing case

* nit add comment to test

Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
  • Loading branch information
lonyele and bradzacher committed Mar 1, 2022
1 parent fabfc2b commit 3ea0947
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
Expand Up @@ -127,7 +127,9 @@ function createValidator(
return;
}

if (!validatePredefinedFormat(config, name, node, originalName)) {
if (
!validatePredefinedFormat(config, name, node, originalName, modifiers)
) {
// fail
return;
}
Expand Down Expand Up @@ -376,16 +378,19 @@ function createValidator(
name: string,
node: TSESTree.Identifier | TSESTree.PrivateIdentifier | TSESTree.Literal,
originalName: string,
modifiers: Set<Modifiers>,
): boolean {
const formats = config.format;
if (formats === null || formats.length === 0) {
return true;
}

for (const format of formats) {
const checker = PredefinedFormatToCheckFunction[format];
if (checker(name)) {
return true;
if (!modifiers.has(Modifiers.requiresQuotes)) {
for (const format of formats) {
const checker = PredefinedFormatToCheckFunction[format];
if (checker(name)) {
return true;
}
}
}

Expand Down
20 changes: 19 additions & 1 deletion packages/eslint-plugin/tests/rules/naming-convention.test.ts
Expand Up @@ -6,7 +6,7 @@ import {
Selector,
selectorTypeToMessageString,
} from '../../src/rules/naming-convention-utils';
import { getFixturesRootDir, RuleTester } from '../RuleTester';
import { getFixturesRootDir, noFormat, RuleTester } from '../RuleTester';

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
Expand Down Expand Up @@ -2224,5 +2224,23 @@ ruleTester.run('naming-convention', rule, {
],
errors: Array(13).fill({ messageId: 'doesNotMatchFormat' }),
},
{
code: noFormat`
type Foo = {
'foo Bar': string;
'': string;
'0': string;
'foo': string;
'foo-bar': string;
'#foo-bar': string;
};
interface Bar {
'boo-----foo': string;
}
`,
// 6, not 7 because 'foo' is valid
errors: Array(6).fill({ messageId: 'doesNotMatchFormat' }),
},
],
});

0 comments on commit 3ea0947

Please sign in to comment.