Skip to content

Commit

Permalink
fix: allow leading underscores in lint rules (wundergraph#1281)
Browse files Browse the repository at this point in the history
  • Loading branch information
JivusAyrus authored Oct 17, 2024
1 parent 1819b8e commit bda997a
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 18 deletions.
49 changes: 38 additions & 11 deletions controlplane/src/core/services/SchemaLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,59 +55,86 @@ export default class SchemaLinter {
const ruleName = rule.ruleName;
switch (ruleName) {
case 'FIELD_NAMES_SHOULD_BE_CAMEL_CASE': {
rulesConfig[ruleName] = [rule.severity, { FieldDefinition: { style: 'camelCase' } }];
rulesConfig[ruleName] = [
rule.severity,
{ FieldDefinition: { style: 'camelCase' }, allowLeadingUnderscore: true },
];
break;
}
case 'TYPE_NAMES_SHOULD_BE_PASCAL_CASE': {
rulesConfig[ruleName] = [rule.severity, { ObjectTypeDefinition: { style: 'PascalCase' } }];
rulesConfig[ruleName] = [
rule.severity,
{ ObjectTypeDefinition: { style: 'PascalCase' }, allowLeadingUnderscore: true },
];
break;
}
case 'SHOULD_NOT_HAVE_TYPE_PREFIX': {
rulesConfig[ruleName] = [rule.severity, { ObjectTypeDefinition: { forbiddenPrefixes: ['Type', 'type'] } }];
rulesConfig[ruleName] = [
rule.severity,
{ ObjectTypeDefinition: { forbiddenPrefixes: ['Type', 'type'] }, allowLeadingUnderscore: true },
];
break;
}
case 'SHOULD_NOT_HAVE_TYPE_SUFFIX': {
rulesConfig[ruleName] = [rule.severity, { ObjectTypeDefinition: { forbiddenSuffixes: ['Type', 'type'] } }];
rulesConfig[ruleName] = [
rule.severity,
{ ObjectTypeDefinition: { forbiddenSuffixes: ['Type', 'type'] }, allowLeadingUnderscore: true },
];
break;
}
case 'SHOULD_NOT_HAVE_INPUT_PREFIX': {
rulesConfig[ruleName] = [
rule.severity,
{ InputObjectTypeDefinition: { forbiddenPrefixes: ['Input', 'input'] } },
{ InputObjectTypeDefinition: { forbiddenPrefixes: ['Input', 'input'] }, allowLeadingUnderscore: true },
];
break;
}
case 'SHOULD_HAVE_INPUT_SUFFIX': {
rulesConfig[ruleName] = [rule.severity, { InputObjectTypeDefinition: { requiredSuffixes: ['Input'] } }];
rulesConfig[ruleName] = [
rule.severity,
{ InputObjectTypeDefinition: { requiredSuffixes: ['Input'] }, allowLeadingUnderscore: true },
];
break;
}
case 'SHOULD_NOT_HAVE_ENUM_PREFIX': {
rulesConfig[ruleName] = [
rule.severity,
{ EnumTypeDefinition: { forbiddenPrefixes: ['Enum', 'enum', 'ENUM'] } },
{ EnumTypeDefinition: { forbiddenPrefixes: ['Enum', 'enum', 'ENUM'] }, allowLeadingUnderscore: true },
];
break;
}
case 'SHOULD_NOT_HAVE_ENUM_SUFFIX': {
rulesConfig[ruleName] = [rule.severity, { EnumTypeDefinition: { forbiddenSuffixes: ['Enum', 'enum'] } }];
rulesConfig[ruleName] = [
rule.severity,
{ EnumTypeDefinition: { forbiddenSuffixes: ['Enum', 'enum'] }, allowLeadingUnderscore: true },
];
break;
}
case 'SHOULD_NOT_HAVE_INTERFACE_PREFIX': {
rulesConfig[ruleName] = [
rule.severity,
{ InterfaceTypeDefinition: { forbiddenPrefixes: ['Interface', 'interface'] } },
{
InterfaceTypeDefinition: { forbiddenPrefixes: ['Interface', 'interface'] },
allowLeadingUnderscore: true,
},
];
break;
}
case 'SHOULD_NOT_HAVE_INTERFACE_SUFFIX': {
rulesConfig[ruleName] = [
rule.severity,
{ InterfaceTypeDefinition: { forbiddenSuffixes: ['Interface', 'interface'] } },
{
InterfaceTypeDefinition: { forbiddenSuffixes: ['Interface', 'interface'] },
allowLeadingUnderscore: true,
},
];
break;
}
case 'ENUM_VALUES_SHOULD_BE_UPPER_CASE': {
rulesConfig[ruleName] = [rule.severity, { EnumValueDefinition: { style: 'UPPER_CASE' } }];
rulesConfig[ruleName] = [
rule.severity,
{ EnumValueDefinition: { style: 'UPPER_CASE' }, allowLeadingUnderscore: true },
];
break;
}
case 'DISALLOW_CASE_INSENSITIVE_ENUM_VALUES': {
Expand Down
74 changes: 67 additions & 7 deletions controlplane/test/lint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,45 @@ input InputUser{
]);
});

test('Should test type names which start with "_"', (testContext) => {
const schema = `type _Service{
sdl: String
}
input UserInput{
id: String
firstName: _Service
}
type User{
service: _Service
}
interface A{
service: _Service
}
`;

const rules: { severity: LintSeverityLevel; ruleName: LintRuleEnum }[] = [
{ severity: 'warn', ruleName: LintRules.FIELD_NAMES_SHOULD_BE_CAMEL_CASE },
{ severity: 'warn', ruleName: LintRules.TYPE_NAMES_SHOULD_BE_PASCAL_CASE },
{ severity: 'warn', ruleName: LintRules.SHOULD_NOT_HAVE_TYPE_PREFIX },
{ severity: 'warn', ruleName: LintRules.SHOULD_NOT_HAVE_TYPE_SUFFIX },
{ severity: 'warn', ruleName: LintRules.SHOULD_NOT_HAVE_INPUT_PREFIX },
{ severity: 'warn', ruleName: LintRules.SHOULD_HAVE_INPUT_SUFFIX },
{ severity: 'warn', ruleName: LintRules.SHOULD_NOT_HAVE_ENUM_PREFIX },
{ severity: 'warn', ruleName: LintRules.SHOULD_NOT_HAVE_ENUM_SUFFIX },
{ severity: 'warn', ruleName: LintRules.SHOULD_NOT_HAVE_INTERFACE_PREFIX },
{ severity: 'warn', ruleName: LintRules.SHOULD_NOT_HAVE_INTERFACE_SUFFIX },
{ severity: 'warn', ruleName: LintRules.ENUM_VALUES_SHOULD_BE_UPPER_CASE },
];

const schemaLinter = new SchemaLinter();
const lintIssues = schemaLinter.schemaLintCheck({ schema, rulesInput: rules });
expect(lintIssues.warnings.length).toBe(0);
expect(lintIssues.warnings).toStrictEqual([]);
});

test('Should test creating rules config', (testContext) => {
const rules: { severity: LintSeverityLevel; ruleName: LintRuleEnum }[] = [
{ severity: 'warn', ruleName: LintRules.TYPE_NAMES_SHOULD_BE_PASCAL_CASE },
Expand All @@ -324,14 +363,35 @@ input InputUser{
const rulesConfig = schemaLinter.createRulesConfig(rules);

expect(rulesConfig).toStrictEqual({
TYPE_NAMES_SHOULD_BE_PASCAL_CASE: ['warn', { ObjectTypeDefinition: { style: 'PascalCase' } }],
SHOULD_NOT_HAVE_INPUT_PREFIX: ['warn', { InputObjectTypeDefinition: { forbiddenPrefixes: ['Input', 'input'] } }],
TYPE_NAMES_SHOULD_BE_PASCAL_CASE: [
'warn',
{ ObjectTypeDefinition: { style: 'PascalCase' }, allowLeadingUnderscore: true },
],
SHOULD_NOT_HAVE_INPUT_PREFIX: [
'warn',
{ InputObjectTypeDefinition: { forbiddenPrefixes: ['Input', 'input'] }, allowLeadingUnderscore: true },
],
DISALLOW_CASE_INSENSITIVE_ENUM_VALUES: ['warn'],
ENUM_VALUES_SHOULD_BE_UPPER_CASE: ['warn', { EnumValueDefinition: { style: 'UPPER_CASE' } }],
FIELD_NAMES_SHOULD_BE_CAMEL_CASE: ['error', { FieldDefinition: { style: 'camelCase' } }],
SHOULD_NOT_HAVE_TYPE_PREFIX: ['error', { ObjectTypeDefinition: { forbiddenPrefixes: ['Type', 'type'] } }],
SHOULD_NOT_HAVE_TYPE_SUFFIX: ['error', { ObjectTypeDefinition: { forbiddenSuffixes: ['Type', 'type'] } }],
SHOULD_HAVE_INPUT_SUFFIX: ['error', { InputObjectTypeDefinition: { requiredSuffixes: ['Input'] } }],
ENUM_VALUES_SHOULD_BE_UPPER_CASE: [
'warn',
{ EnumValueDefinition: { style: 'UPPER_CASE' }, allowLeadingUnderscore: true },
],
FIELD_NAMES_SHOULD_BE_CAMEL_CASE: [
'error',
{ FieldDefinition: { style: 'camelCase' }, allowLeadingUnderscore: true },
],
SHOULD_NOT_HAVE_TYPE_PREFIX: [
'error',
{ ObjectTypeDefinition: { forbiddenPrefixes: ['Type', 'type'] }, allowLeadingUnderscore: true },
],
SHOULD_NOT_HAVE_TYPE_SUFFIX: [
'error',
{ ObjectTypeDefinition: { forbiddenSuffixes: ['Type', 'type'] }, allowLeadingUnderscore: true },
],
SHOULD_HAVE_INPUT_SUFFIX: [
'error',
{ InputObjectTypeDefinition: { requiredSuffixes: ['Input'] }, allowLeadingUnderscore: true },
],
});
});
});

0 comments on commit bda997a

Please sign in to comment.