Skip to content

Commit eb2a16a

Browse files
authored
Merge pull request #230 from cjoudrey/revert-remove-comment-descriptions
Re-add support for comment descriptions
2 parents 25d13df + 2934e5e commit eb2a16a

17 files changed

+235
-51
lines changed

src/configuration.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ export class Configuration {
1111
- format: (required) `text` | `json`
1212
- rules: [string array] whitelist rules
1313
- customRulePaths: [string array] path to additional custom rules to be loaded
14+
- commentDescriptions: [boolean] use old way of defining descriptions in GraphQL SDL
1415
- oldImplementsSyntax: [boolean] use old way of defining implemented interfaces in GraphQL SDL
1516
*/
1617
constructor(schema, options = {}) {
1718
const defaultOptions = {
1819
format: 'text',
1920
customRulePaths: [],
21+
commentDescriptions: false,
2022
oldImplementsSyntax: false,
2123
};
2224

@@ -27,6 +29,10 @@ export class Configuration {
2729
this.rulePaths = this.options.customRulePaths.concat(this.builtInRulePaths);
2830
}
2931

32+
getCommentDescriptions() {
33+
return this.options.commentDescriptions;
34+
}
35+
3036
getOldImplementsSyntax() {
3137
return this.options.oldImplementsSyntax;
3238
}
@@ -55,23 +61,23 @@ export class Configuration {
5561
let specifiedRules;
5662
if (this.options.rules && this.options.rules.length > 0) {
5763
specifiedRules = this.options.rules.map(toUpperCamelCase);
58-
rules = this.getAllRules().filter((rule) => {
64+
rules = this.getAllRules().filter(rule => {
5965
return specifiedRules.indexOf(rule.name) >= 0;
6066
});
6167
}
6268

6369
// DEPRECATED - This code should be removed in v1.0.0.
6470
if (this.options.only && this.options.only.length > 0) {
6571
specifiedRules = this.options.only.map(toUpperCamelCase);
66-
rules = this.getAllRules().filter((rule) => {
72+
rules = this.getAllRules().filter(rule => {
6773
return specifiedRules.indexOf(rule.name) >= 0;
6874
});
6975
}
7076

7177
// DEPRECATED - This code should be removed in v1.0.0.
7278
if (this.options.except && this.options.except.length > 0) {
7379
specifiedRules = this.options.except.map(toUpperCamelCase);
74-
rules = this.getAllRules().filter((rule) => {
80+
rules = this.getAllRules().filter(rule => {
7581
return specifiedRules.indexOf(rule.name) == -1;
7682
});
7783
}
@@ -93,9 +99,9 @@ export class Configuration {
9399
const expandedPaths = expandPaths(rulePaths);
94100
const rules = new Set([]);
95101

96-
expandedPaths.map((rulePath) => {
102+
expandedPaths.map(rulePath => {
97103
let ruleMap = require(rulePath);
98-
Object.keys(ruleMap).forEach((k) => rules.add(ruleMap[k]));
104+
Object.keys(ruleMap).forEach(k => rules.add(ruleMap[k]));
99105
});
100106

101107
return Array.from(rules);
@@ -128,7 +134,7 @@ export class Configuration {
128134
}
129135
}
130136

131-
const ruleNames = rules.map((rule) => rule.name);
137+
const ruleNames = rules.map(rule => rule.name);
132138

133139
let misConfiguredRuleNames = []
134140
.concat(
@@ -137,7 +143,7 @@ export class Configuration {
137143
this.options.rules || []
138144
)
139145
.map(toUpperCamelCase)
140-
.filter((name) => ruleNames.indexOf(name) == -1);
146+
.filter(name => ruleNames.indexOf(name) == -1);
141147

142148
if (this.getFormatter() == null) {
143149
issues.push({
@@ -164,6 +170,6 @@ export class Configuration {
164170
function toUpperCamelCase(string) {
165171
return string
166172
.split('-')
167-
.map((part) => part[0].toUpperCase() + part.slice(1))
173+
.map(part => part[0].toUpperCase() + part.slice(1))
168174
.join('');
169175
}

src/rules/arguments_have_descriptions.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getDescription } from 'graphql/utilities/buildASTSchema';
1+
import { getDescription } from 'graphql/utilities/extendSchema';
22
import { ValidationError } from '../validation_error';
33

44
export function ArgumentsHaveDescriptions(configuration, context) {
@@ -7,19 +7,21 @@ export function ArgumentsHaveDescriptions(configuration, context) {
77
const fieldName = node.name.value;
88

99
for (const arg of node.arguments || []) {
10-
if (arg.description && arg.description.value != '') {
11-
continue;
12-
}
10+
const description = getDescription(arg, {
11+
commentDescriptions: configuration.getCommentDescriptions(),
12+
});
1313

14-
const argName = arg.name.value;
14+
if (typeof description !== 'string' || description.length === 0) {
15+
const argName = arg.name.value;
1516

16-
context.reportError(
17-
new ValidationError(
18-
'arguments-have-descriptions',
19-
`The \`${argName}\` argument of \`${fieldName}\` is missing a description.`,
20-
[arg]
21-
)
22-
);
17+
context.reportError(
18+
new ValidationError(
19+
'arguments-have-descriptions',
20+
`The \`${argName}\` argument of \`${fieldName}\` is missing a description.`,
21+
[arg]
22+
)
23+
);
24+
}
2325
}
2426
},
2527
};

src/rules/descriptions_are_capitalized.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { getDescription } from 'graphql/utilities/buildASTSchema';
1+
import { getDescription } from 'graphql/utilities/extendSchema';
22
import { ValidationError } from '../validation_error';
33

44
export function DescriptionsAreCapitalized(configuration, context) {
55
return {
66
FieldDefinition(node, key, parent, path, ancestors) {
7+
const description = getDescription(node, {
8+
commentDescriptions: configuration.getCommentDescriptions(),
9+
});
10+
711
// Rule should pass if there's an empty/missing string description. If empty
812
// strings aren't wanted, the `*_have_descriptions` rules can be used.
9-
if (!node.description || node.description.value == '') {
10-
return;
11-
}
12-
13-
const description = node.description.value;
13+
if (typeof description !== 'string' || description.length === 0) return;
1414

1515
// It's possible there could be some markdown characters that do not
1616
// pass this test. If we discover some examples of this, we can improve.

src/rules/enum_values_have_descriptions.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import { getDescription } from 'graphql/utilities/buildASTSchema';
1+
import { getDescription } from 'graphql/utilities/extendSchema';
22
import { ValidationError } from '../validation_error';
33

44
export function EnumValuesHaveDescriptions(configuration, context) {
55
return {
66
EnumValueDefinition(node, key, parent, path, ancestors) {
7-
if (node.description && node.description.value != '') {
7+
if (
8+
getDescription(node, {
9+
commentDescriptions: configuration.getCommentDescriptions(),
10+
})
11+
) {
812
return;
913
}
1014

src/rules/fields_have_descriptions.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import { getDescription } from 'graphql/utilities/buildASTSchema';
1+
import { getDescription } from 'graphql/utilities/extendSchema';
22
import { ValidationError } from '../validation_error';
33

44
export function FieldsHaveDescriptions(configuration, context) {
55
return {
66
FieldDefinition(node, key, parent, path, ancestors) {
7-
if (node.description && node.description.value != '') {
7+
if (
8+
getDescription(node, {
9+
commentDescriptions: configuration.getCommentDescriptions(),
10+
})
11+
) {
812
return;
913
}
1014

src/rules/input_object_values_have_descriptions.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import { getDescription } from 'graphql/utilities/buildASTSchema';
1+
import { getDescription } from 'graphql/utilities/extendSchema';
22
import { ValidationError } from '../validation_error';
33

44
export function InputObjectValuesHaveDescriptions(configuration, context) {
55
return {
66
InputValueDefinition(node, key, parent, path, ancestors) {
7-
if (node.description && node.description.value != '') {
7+
if (
8+
getDescription(node, {
9+
commentDescriptions: configuration.getCommentDescriptions(),
10+
})
11+
) {
812
return;
913
}
1014

src/rules/types_have_descriptions.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import { getDescription } from 'graphql/utilities/buildASTSchema';
1+
import { getDescription } from 'graphql/utilities/extendSchema';
22
import { ValidationError } from '../validation_error';
33

44
function validateTypeHasDescription(configuration, context, node, typeKind) {
5-
if (node.description && node.description.value != '') {
5+
if (
6+
getDescription(node, {
7+
commentDescriptions: configuration.getCommentDescriptions(),
8+
})
9+
) {
610
return;
711
}
812

src/runner.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export async function run(stdout, stdin, stderr, argv) {
3030
'-p, --custom-rule-paths <paths>',
3131
'path to additional custom rules to be loaded. Example: rules/*.js'
3232
)
33+
.option(
34+
'--comment-descriptions',
35+
'use old way of defining descriptions in GraphQL SDL'
36+
)
3337
.option(
3438
'--old-implements-syntax',
3539
'use old way of defining implemented interfaces in GraphQL SDL'
@@ -77,7 +81,7 @@ export async function run(stdout, stdin, stderr, argv) {
7781

7882
const issues = configuration.validate();
7983

80-
issues.map((issue) => {
84+
issues.map(issue => {
8185
var prefix;
8286
if (issue.type == 'error') {
8387
prefix = `${chalk.red(figures.cross)} Error`;
@@ -89,7 +93,7 @@ export async function run(stdout, stdin, stderr, argv) {
8993
);
9094
});
9195

92-
if (issues.some((issue) => issue.type == 'error')) {
96+
if (issues.some(issue => issue.type == 'error')) {
9397
return 2;
9498
}
9599

@@ -152,6 +156,10 @@ function getOptionsFromCommander(commander) {
152156
options.customRulePaths = commander.customRulePaths.split(',');
153157
}
154158

159+
if (commander.commentDescriptions) {
160+
options.commentDescriptions = commander.commentDescriptions;
161+
}
162+
155163
if (commander.oldImplementsSyntax) {
156164
options.oldImplementsSyntax = commander.oldImplementsSyntax;
157165
}

src/validator.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function validateSchemaDefinition(
3333
let schemaErrors = validateSDL(ast);
3434
if (schemaErrors.length > 0) {
3535
return sortErrors(
36-
schemaErrors.map((error) => {
36+
schemaErrors.map(error => {
3737
return new ValidationError(
3838
'invalid-graphql-schema',
3939
error.message,
@@ -44,6 +44,7 @@ export function validateSchemaDefinition(
4444
}
4545

4646
const schema = buildASTSchema(ast, {
47+
commentDescriptions: configuration.getCommentDescriptions(),
4748
assumeValidSDL: true,
4849
assumeValid: true,
4950
});
@@ -52,7 +53,7 @@ export function validateSchemaDefinition(
5253
schemaErrors = validateSchema(schema);
5354
if (schemaErrors.length > 0) {
5455
return sortErrors(
55-
schemaErrors.map((error) => {
56+
schemaErrors.map(error => {
5657
return new ValidationError(
5758
'invalid-graphql-schema',
5859
error.message,
@@ -62,7 +63,7 @@ export function validateSchemaDefinition(
6263
);
6364
}
6465

65-
const rulesWithConfiguration = rules.map((rule) => {
66+
const rulesWithConfiguration = rules.map(rule => {
6667
return ruleWithConfiguration(rule, configuration);
6768
});
6869

@@ -80,7 +81,7 @@ function sortErrors(errors) {
8081

8182
function ruleWithConfiguration(rule, configuration) {
8283
if (rule.length == 2) {
83-
return function (context) {
84+
return function(context) {
8485
return rule(configuration, context);
8586
};
8687
} else {

0 commit comments

Comments
 (0)