Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [naming-convention] cover case that requires quotes #4582

Merged
merged 7 commits into from Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
lonyele marked this conversation as resolved.
Show resolved Hide resolved
'': 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' }),
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
},
],
});