Skip to content

Commit

Permalink
Enable "no-case-declarations" ESLint rule (#1887)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed May 21, 2019
1 parent 827880d commit dec3cc5
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 49 deletions.
3 changes: 1 addition & 2 deletions .eslintrc.yml
Expand Up @@ -112,8 +112,7 @@ rules:
max-classes-per-file: off
no-alert: error
no-caller: error
# TODO: recommended rule but disable due to errors in existing code
# no-case-declarations: error
no-case-declarations: error
no-div-regex: error
no-else-return: error
no-empty-function: off # TODO
Expand Down
9 changes: 6 additions & 3 deletions src/execution/execute.js
Expand Up @@ -528,7 +528,7 @@ export function collectFields(
for (let i = 0; i < selectionSet.selections.length; i++) {
const selection = selectionSet.selections[i];
switch (selection.kind) {
case Kind.FIELD:
case Kind.FIELD: {
if (!shouldIncludeNode(exeContext, selection)) {
continue;
}
Expand All @@ -538,7 +538,8 @@ export function collectFields(
}
fields[name].push(selection);
break;
case Kind.INLINE_FRAGMENT:
}
case Kind.INLINE_FRAGMENT: {
if (
!shouldIncludeNode(exeContext, selection) ||
!doesFragmentConditionMatch(exeContext, selection, runtimeType)
Expand All @@ -553,7 +554,8 @@ export function collectFields(
visitedFragmentNames,
);
break;
case Kind.FRAGMENT_SPREAD:
}
case Kind.FRAGMENT_SPREAD: {
const fragName = selection.name.value;
if (
visitedFragmentNames[fragName] ||
Expand All @@ -577,6 +579,7 @@ export function collectFields(
visitedFragmentNames,
);
break;
}
}
}
return fields;
Expand Down
4 changes: 3 additions & 1 deletion src/language/lexer.js
Expand Up @@ -597,7 +597,8 @@ function readString(source, start, line, col, prev): Token {
case 116:
value += '\t';
break;
case 117: // u
case 117: {
// uXXXX
const charCode = uniCharCode(
body.charCodeAt(position + 1),
body.charCodeAt(position + 2),
Expand All @@ -615,6 +616,7 @@ function readString(source, start, line, col, prev): Token {
value += String.fromCharCode(charCode);
position += 4;
break;
}
default:
throw syntaxError(
source,
Expand Down
27 changes: 18 additions & 9 deletions src/utilities/TypeInfo.js
Expand Up @@ -144,13 +144,14 @@ export class TypeInfo {
// checked before continuing since TypeInfo is used as part of validation
// which occurs before guarantees of schema and document validity.
switch (node.kind) {
case Kind.SELECTION_SET:
case Kind.SELECTION_SET: {
const namedType: mixed = getNamedType(this.getType());
this._parentTypeStack.push(
isCompositeType(namedType) ? namedType : undefined,
);
break;
case Kind.FIELD:
}
case Kind.FIELD: {
const parentType = this.getParentType();
let fieldDef;
let fieldType: mixed;
Expand All @@ -163,10 +164,11 @@ export class TypeInfo {
this._fieldDefStack.push(fieldDef);
this._typeStack.push(isOutputType(fieldType) ? fieldType : undefined);
break;
}
case Kind.DIRECTIVE:
this._directive = schema.getDirective(node.name.value);
break;
case Kind.OPERATION_DEFINITION:
case Kind.OPERATION_DEFINITION: {
let type: mixed;
if (node.operation === 'query') {
type = schema.getQueryType();
Expand All @@ -177,21 +179,24 @@ export class TypeInfo {
}
this._typeStack.push(isObjectType(type) ? type : undefined);
break;
}
case Kind.INLINE_FRAGMENT:
case Kind.FRAGMENT_DEFINITION:
case Kind.FRAGMENT_DEFINITION: {
const typeConditionAST = node.typeCondition;
const outputType: mixed = typeConditionAST
? typeFromAST(schema, typeConditionAST)
: getNamedType(this.getType());
this._typeStack.push(isOutputType(outputType) ? outputType : undefined);
break;
case Kind.VARIABLE_DEFINITION:
}
case Kind.VARIABLE_DEFINITION: {
const inputType: mixed = typeFromAST(schema, node.type);
this._inputTypeStack.push(
isInputType(inputType) ? inputType : undefined,
);
break;
case Kind.ARGUMENT:
}
case Kind.ARGUMENT: {
let argDef;
let argType: mixed;
const fieldOrDirective = this.getDirective() || this.getFieldDef();
Expand All @@ -208,7 +213,8 @@ export class TypeInfo {
this._defaultValueStack.push(argDef ? argDef.defaultValue : undefined);
this._inputTypeStack.push(isInputType(argType) ? argType : undefined);
break;
case Kind.LIST:
}
case Kind.LIST: {
const listType: mixed = getNullableType(this.getInputType());
const itemType: mixed = isListType(listType)
? listType.ofType
Expand All @@ -217,7 +223,8 @@ export class TypeInfo {
this._defaultValueStack.push(undefined);
this._inputTypeStack.push(isInputType(itemType) ? itemType : undefined);
break;
case Kind.OBJECT_FIELD:
}
case Kind.OBJECT_FIELD: {
const objectType: mixed = getNamedType(this.getInputType());
let inputFieldType: GraphQLInputType | void;
let inputField: GraphQLInputField | void;
Expand All @@ -234,14 +241,16 @@ export class TypeInfo {
isInputType(inputFieldType) ? inputFieldType : undefined,
);
break;
case Kind.ENUM:
}
case Kind.ENUM: {
const enumType: mixed = getNamedType(this.getInputType());
let enumValue;
if (isEnumType(enumType)) {
enumValue = enumType.getValue(node.value);
}
this._enumValue = enumValue;
break;
}
}
}

Expand Down
63 changes: 33 additions & 30 deletions src/utilities/getOperationRootType.js
Expand Up @@ -22,38 +22,41 @@ export function getOperationRootType(
schema: GraphQLSchema,
operation: OperationDefinitionNode | OperationTypeDefinitionNode,
): GraphQLObjectType {
switch (operation.operation) {
case 'query':
const queryType = schema.getQueryType();
if (!queryType) {
throw new GraphQLError(
'Schema does not define the required query root type.',
operation,
);
}
return queryType;
case 'mutation':
const mutationType = schema.getMutationType();
if (!mutationType) {
throw new GraphQLError(
'Schema is not configured for mutations.',
operation,
);
}
return mutationType;
case 'subscription':
const subscriptionType = schema.getSubscriptionType();
if (!subscriptionType) {
throw new GraphQLError(
'Schema is not configured for subscriptions.',
operation,
);
}
return subscriptionType;
default:
if (operation.operation === 'query') {
const queryType = schema.getQueryType();
if (!queryType) {
throw new GraphQLError(
'Can only have query, mutation and subscription operations.',
'Schema does not define the required query root type.',
operation,
);
}
return queryType;
}

if (operation.operation === 'mutation') {
const mutationType = schema.getMutationType();
if (!mutationType) {
throw new GraphQLError(
'Schema is not configured for mutations.',
operation,
);
}
return mutationType;
}

if (operation.operation === 'subscription') {
const subscriptionType = schema.getSubscriptionType();
if (!subscriptionType) {
throw new GraphQLError(
'Schema is not configured for subscriptions.',
operation,
);
}
return subscriptionType;
}

throw new GraphQLError(
'Can only have query, mutation and subscription operations.',
operation,
);
}
3 changes: 2 additions & 1 deletion src/utilities/valueFromASTUntyped.js
Expand Up @@ -53,11 +53,12 @@ export function valueFromASTUntyped(
field => field.name.value,
field => valueFromASTUntyped(field.value, variables),
);
case Kind.VARIABLE:
case Kind.VARIABLE: {
const variableName = valueNode.name.value;
return variables && !isInvalid(variables[variableName])
? variables[variableName]
: undefined;
}
}

// Not reachable. All possible value nodes have been considered.
Expand Down
3 changes: 2 additions & 1 deletion src/validation/rules/KnownDirectives.js
Expand Up @@ -127,11 +127,12 @@ function getDirectiveLocationForASTPath(ancestors) {
case Kind.INPUT_OBJECT_TYPE_DEFINITION:
case Kind.INPUT_OBJECT_TYPE_EXTENSION:
return DirectiveLocation.INPUT_OBJECT;
case Kind.INPUT_VALUE_DEFINITION:
case Kind.INPUT_VALUE_DEFINITION: {
const parentNode = ancestors[ancestors.length - 3];
return parentNode.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION
? DirectiveLocation.INPUT_FIELD_DEFINITION
: DirectiveLocation.ARGUMENT_DEFINITION;
}
}
}
}
6 changes: 4 additions & 2 deletions src/validation/rules/OverlappingFieldsCanBeMerged.js
Expand Up @@ -747,7 +747,7 @@ function _collectFieldsAndFragmentNames(
for (let i = 0; i < selectionSet.selections.length; i++) {
const selection = selectionSet.selections[i];
switch (selection.kind) {
case Kind.FIELD:
case Kind.FIELD: {
const fieldName = selection.name.value;
let fieldDef;
if (isObjectType(parentType) || isInterfaceType(parentType)) {
Expand All @@ -761,10 +761,11 @@ function _collectFieldsAndFragmentNames(
}
nodeAndDefs[responseName].push([parentType, selection, fieldDef]);
break;
}
case Kind.FRAGMENT_SPREAD:
fragmentNames[selection.name.value] = true;
break;
case Kind.INLINE_FRAGMENT:
case Kind.INLINE_FRAGMENT: {
const typeCondition = selection.typeCondition;
const inlineFragmentType = typeCondition
? typeFromAST(context.getSchema(), typeCondition)
Expand All @@ -777,6 +778,7 @@ function _collectFieldsAndFragmentNames(
fragmentNames,
);
break;
}
}
}
}
Expand Down

0 comments on commit dec3cc5

Please sign in to comment.