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

new rule: prefer cap type names #81

Merged
merged 2 commits into from
Jul 31, 2017
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
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,50 @@ module.exports = {
]
}
```

### Capitalization of a first letter of a Type name

This rule enforces that first letter of types is capitalized

**Pass**
```
query {
someUnion {
... on SomeType {
someField
}
}
}
```

**Fail**
```
query {
someUnion {
... on someType {
someField
}
}
}
```

The rule is defined as `graphql/capitalized-type-name` and requires a `schema`;

```js
// In a file called .eslintrc.js
module.exports = {
parser: "babel-eslint",
rules: {
"graphql/template-strings": ['error', {
env: 'apollo',
schemaJson: require('./schema.json'),
}],
"graphql/capitalized-type-name": ['warn', {
schemaJson: require('./schema.json'),
}],
},
plugins: [
'graphql'
]
}
```
30 changes: 29 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,34 @@ export const rules = {
);
},
},
'capitalized-type-name': {
meta: {
schema: {
type: 'array',
minLength: 1,
items: {
additionalProperties: false,
properties: { ...defaultRuleProperties },
oneOf: [{
required: ['schemaJson'],
not: { required: ['schemaString', 'schemaJsonFilepath'], },
}, {
required: ['schemaJsonFilepath'],
not: { required: ['schemaString', 'schemaJson'], },
}, {
required: ['schemaString'],
not: { required: ['schemaJson', 'schemaJsonFilepath'], },
}],
},
},
},
create: (context) => {
return createRule(context, (optionGroup) => parseOptions({
validators: ['typeNamesShouldBeCapitalized'],
...optionGroup,
}));;
},
},
};

function parseOptions(optionGroup) {
Expand Down Expand Up @@ -458,6 +486,6 @@ export const processors = reduce(gqlFiles, (result, value) => {
}, {})

export default {
rules,
rules,
processors
}
13 changes: 13 additions & 0 deletions src/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,16 @@ export function RequiredFields(context, options) {
},
};
}

export function typeNamesShouldBeCapitalized(context) {
return {
NamedType(node) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does NamedType get called for inline fragments? i.e., can we add a test case for

query {
  someUnion {
    ... on someUnionMember {
      someField
    }
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test.

const typeName = node.name.value;
if (typeName[0] == typeName[0].toLowerCase()) {
context.reportError(
new GraphQLError("All type names should start with a capital letter", [ node ])
);
}
}
}
}
30 changes: 30 additions & 0 deletions test/makeRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,28 @@ const requiredFieldsTestCases = {
],
};

const typeNameCapValidatorCases = {
pass: [
'const x = gql`fragment FilmFragment on Film { title } { allFilms { films { ...FilmFragment } } }`',
'const x = gql`query { someUnion {... on SomeUnionMember { someField }}}`',
],
fail: [
{
code: 'const x = gql`fragment FilmFragment on film { title } { allFilms { films { ...FilmFragment } } }`',
errors: [{
message: 'All type names should start with a capital letter',
type: 'TaggedTemplateExpression',
}],
},
{
code: 'const x = gql`query { someUnion {... on someUnionMember { someField }}}`',
errors: [{
message: 'All type names should start with a capital letter',
type: 'TaggedTemplateExpression',
}],
},
]
};
{
let options = [{
schemaJson, tagName: 'gql',
Expand Down Expand Up @@ -970,3 +992,11 @@ const requiredFieldsTestCases = {
invalid: requiredFieldsTestCases.fail.map(({code, errors}) => ({options, parserOptions, code, errors})),
});
}

let options = [{
schemaJson, tagName: 'gql',
}];
ruleTester.run('testing capitalized-type-name rule', rules['capitalized-type-name'], {
valid: typeNameCapValidatorCases.pass.map((code) => ({options, parserOptions, code})),
invalid: typeNameCapValidatorCases.fail.map(({code, errors}) => ({options, parserOptions, code, errors})),
});