Skip to content

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

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

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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' }),
},
],
});