Skip to content
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
22 changes: 14 additions & 8 deletions src/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ export class Configuration {
- format: (required) `text` | `json`
- rules: [string array] whitelist rules
- customRulePaths: [string array] path to additional custom rules to be loaded
- commentDescriptions: [boolean] use old way of defining descriptions in GraphQL SDL
- oldImplementsSyntax: [boolean] use old way of defining implemented interfaces in GraphQL SDL
*/
constructor(schema, options = {}) {
const defaultOptions = {
format: 'text',
customRulePaths: [],
commentDescriptions: false,
oldImplementsSyntax: false,
};

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

getCommentDescriptions() {
return this.options.commentDescriptions;
}

getOldImplementsSyntax() {
return this.options.oldImplementsSyntax;
}
Expand Down Expand Up @@ -55,23 +61,23 @@ export class Configuration {
let specifiedRules;
if (this.options.rules && this.options.rules.length > 0) {
specifiedRules = this.options.rules.map(toUpperCamelCase);
rules = this.getAllRules().filter((rule) => {
rules = this.getAllRules().filter(rule => {
return specifiedRules.indexOf(rule.name) >= 0;
});
}

// DEPRECATED - This code should be removed in v1.0.0.
if (this.options.only && this.options.only.length > 0) {
specifiedRules = this.options.only.map(toUpperCamelCase);
rules = this.getAllRules().filter((rule) => {
rules = this.getAllRules().filter(rule => {
return specifiedRules.indexOf(rule.name) >= 0;
});
}

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

expandedPaths.map((rulePath) => {
expandedPaths.map(rulePath => {
let ruleMap = require(rulePath);
Object.keys(ruleMap).forEach((k) => rules.add(ruleMap[k]));
Object.keys(ruleMap).forEach(k => rules.add(ruleMap[k]));
});

return Array.from(rules);
Expand Down Expand Up @@ -128,7 +134,7 @@ export class Configuration {
}
}

const ruleNames = rules.map((rule) => rule.name);
const ruleNames = rules.map(rule => rule.name);

let misConfiguredRuleNames = []
.concat(
Expand All @@ -137,7 +143,7 @@ export class Configuration {
this.options.rules || []
)
.map(toUpperCamelCase)
.filter((name) => ruleNames.indexOf(name) == -1);
.filter(name => ruleNames.indexOf(name) == -1);

if (this.getFormatter() == null) {
issues.push({
Expand All @@ -164,6 +170,6 @@ export class Configuration {
function toUpperCamelCase(string) {
return string
.split('-')
.map((part) => part[0].toUpperCase() + part.slice(1))
.map(part => part[0].toUpperCase() + part.slice(1))
.join('');
}
26 changes: 14 additions & 12 deletions src/rules/arguments_have_descriptions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getDescription } from 'graphql/utilities/buildASTSchema';
import { getDescription } from 'graphql/utilities/extendSchema';
import { ValidationError } from '../validation_error';

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

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

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

context.reportError(
new ValidationError(
'arguments-have-descriptions',
`The \`${argName}\` argument of \`${fieldName}\` is missing a description.`,
[arg]
)
);
context.reportError(
new ValidationError(
'arguments-have-descriptions',
`The \`${argName}\` argument of \`${fieldName}\` is missing a description.`,
[arg]
)
);
}
}
},
};
Expand Down
12 changes: 6 additions & 6 deletions src/rules/descriptions_are_capitalized.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { getDescription } from 'graphql/utilities/buildASTSchema';
import { getDescription } from 'graphql/utilities/extendSchema';
import { ValidationError } from '../validation_error';

export function DescriptionsAreCapitalized(configuration, context) {
return {
FieldDefinition(node, key, parent, path, ancestors) {
const description = getDescription(node, {
commentDescriptions: configuration.getCommentDescriptions(),
});

// Rule should pass if there's an empty/missing string description. If empty
// strings aren't wanted, the `*_have_descriptions` rules can be used.
if (!node.description || node.description.value == '') {
return;
}

const description = node.description.value;
if (typeof description !== 'string' || description.length === 0) return;

// It's possible there could be some markdown characters that do not
// pass this test. If we discover some examples of this, we can improve.
Expand Down
8 changes: 6 additions & 2 deletions src/rules/enum_values_have_descriptions.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { getDescription } from 'graphql/utilities/buildASTSchema';
import { getDescription } from 'graphql/utilities/extendSchema';
import { ValidationError } from '../validation_error';

export function EnumValuesHaveDescriptions(configuration, context) {
return {
EnumValueDefinition(node, key, parent, path, ancestors) {
if (node.description && node.description.value != '') {
if (
getDescription(node, {
commentDescriptions: configuration.getCommentDescriptions(),
})
) {
return;
}

Expand Down
8 changes: 6 additions & 2 deletions src/rules/fields_have_descriptions.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { getDescription } from 'graphql/utilities/buildASTSchema';
import { getDescription } from 'graphql/utilities/extendSchema';
import { ValidationError } from '../validation_error';

export function FieldsHaveDescriptions(configuration, context) {
return {
FieldDefinition(node, key, parent, path, ancestors) {
if (node.description && node.description.value != '') {
if (
getDescription(node, {
commentDescriptions: configuration.getCommentDescriptions(),
})
) {
return;
}

Expand Down
8 changes: 6 additions & 2 deletions src/rules/input_object_values_have_descriptions.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { getDescription } from 'graphql/utilities/buildASTSchema';
import { getDescription } from 'graphql/utilities/extendSchema';
import { ValidationError } from '../validation_error';

export function InputObjectValuesHaveDescriptions(configuration, context) {
return {
InputValueDefinition(node, key, parent, path, ancestors) {
if (node.description && node.description.value != '') {
if (
getDescription(node, {
commentDescriptions: configuration.getCommentDescriptions(),
})
) {
return;
}

Expand Down
8 changes: 6 additions & 2 deletions src/rules/types_have_descriptions.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { getDescription } from 'graphql/utilities/buildASTSchema';
import { getDescription } from 'graphql/utilities/extendSchema';
import { ValidationError } from '../validation_error';

function validateTypeHasDescription(configuration, context, node, typeKind) {
if (node.description && node.description.value != '') {
if (
getDescription(node, {
commentDescriptions: configuration.getCommentDescriptions(),
})
) {
return;
}

Expand Down
12 changes: 10 additions & 2 deletions src/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export async function run(stdout, stdin, stderr, argv) {
'-p, --custom-rule-paths <paths>',
'path to additional custom rules to be loaded. Example: rules/*.js'
)
.option(
'--comment-descriptions',
'use old way of defining descriptions in GraphQL SDL'
)
.option(
'--old-implements-syntax',
'use old way of defining implemented interfaces in GraphQL SDL'
Expand Down Expand Up @@ -77,7 +81,7 @@ export async function run(stdout, stdin, stderr, argv) {

const issues = configuration.validate();

issues.map((issue) => {
issues.map(issue => {
var prefix;
if (issue.type == 'error') {
prefix = `${chalk.red(figures.cross)} Error`;
Expand All @@ -89,7 +93,7 @@ export async function run(stdout, stdin, stderr, argv) {
);
});

if (issues.some((issue) => issue.type == 'error')) {
if (issues.some(issue => issue.type == 'error')) {
return 2;
}

Expand Down Expand Up @@ -152,6 +156,10 @@ function getOptionsFromCommander(commander) {
options.customRulePaths = commander.customRulePaths.split(',');
}

if (commander.commentDescriptions) {
options.commentDescriptions = commander.commentDescriptions;
}

if (commander.oldImplementsSyntax) {
options.oldImplementsSyntax = commander.oldImplementsSyntax;
}
Expand Down
9 changes: 5 additions & 4 deletions src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function validateSchemaDefinition(
let schemaErrors = validateSDL(ast);
if (schemaErrors.length > 0) {
return sortErrors(
schemaErrors.map((error) => {
schemaErrors.map(error => {
return new ValidationError(
'invalid-graphql-schema',
error.message,
Expand All @@ -44,6 +44,7 @@ export function validateSchemaDefinition(
}

const schema = buildASTSchema(ast, {
commentDescriptions: configuration.getCommentDescriptions(),
assumeValidSDL: true,
assumeValid: true,
});
Expand All @@ -52,7 +53,7 @@ export function validateSchemaDefinition(
schemaErrors = validateSchema(schema);
if (schemaErrors.length > 0) {
return sortErrors(
schemaErrors.map((error) => {
schemaErrors.map(error => {
return new ValidationError(
'invalid-graphql-schema',
error.message,
Expand All @@ -62,7 +63,7 @@ export function validateSchemaDefinition(
);
}

const rulesWithConfiguration = rules.map((rule) => {
const rulesWithConfiguration = rules.map(rule => {
return ruleWithConfiguration(rule, configuration);
});

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

function ruleWithConfiguration(rule, configuration) {
if (rule.length == 2) {
return function (context) {
return function(context) {
return rule(configuration, context);
};
} else {
Expand Down
Loading