Skip to content

Commit dec3cc5

Browse files
Enable "no-case-declarations" ESLint rule (#1887)
1 parent 827880d commit dec3cc5

File tree

8 files changed

+69
-49
lines changed

8 files changed

+69
-49
lines changed

.eslintrc.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ rules:
112112
max-classes-per-file: off
113113
no-alert: error
114114
no-caller: error
115-
# TODO: recommended rule but disable due to errors in existing code
116-
# no-case-declarations: error
115+
no-case-declarations: error
117116
no-div-regex: error
118117
no-else-return: error
119118
no-empty-function: off # TODO

src/execution/execute.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ export function collectFields(
528528
for (let i = 0; i < selectionSet.selections.length; i++) {
529529
const selection = selectionSet.selections[i];
530530
switch (selection.kind) {
531-
case Kind.FIELD:
531+
case Kind.FIELD: {
532532
if (!shouldIncludeNode(exeContext, selection)) {
533533
continue;
534534
}
@@ -538,7 +538,8 @@ export function collectFields(
538538
}
539539
fields[name].push(selection);
540540
break;
541-
case Kind.INLINE_FRAGMENT:
541+
}
542+
case Kind.INLINE_FRAGMENT: {
542543
if (
543544
!shouldIncludeNode(exeContext, selection) ||
544545
!doesFragmentConditionMatch(exeContext, selection, runtimeType)
@@ -553,7 +554,8 @@ export function collectFields(
553554
visitedFragmentNames,
554555
);
555556
break;
556-
case Kind.FRAGMENT_SPREAD:
557+
}
558+
case Kind.FRAGMENT_SPREAD: {
557559
const fragName = selection.name.value;
558560
if (
559561
visitedFragmentNames[fragName] ||
@@ -577,6 +579,7 @@ export function collectFields(
577579
visitedFragmentNames,
578580
);
579581
break;
582+
}
580583
}
581584
}
582585
return fields;

src/language/lexer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,8 @@ function readString(source, start, line, col, prev): Token {
597597
case 116:
598598
value += '\t';
599599
break;
600-
case 117: // u
600+
case 117: {
601+
// uXXXX
601602
const charCode = uniCharCode(
602603
body.charCodeAt(position + 1),
603604
body.charCodeAt(position + 2),
@@ -615,6 +616,7 @@ function readString(source, start, line, col, prev): Token {
615616
value += String.fromCharCode(charCode);
616617
position += 4;
617618
break;
619+
}
618620
default:
619621
throw syntaxError(
620622
source,

src/utilities/TypeInfo.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,14 @@ export class TypeInfo {
144144
// checked before continuing since TypeInfo is used as part of validation
145145
// which occurs before guarantees of schema and document validity.
146146
switch (node.kind) {
147-
case Kind.SELECTION_SET:
147+
case Kind.SELECTION_SET: {
148148
const namedType: mixed = getNamedType(this.getType());
149149
this._parentTypeStack.push(
150150
isCompositeType(namedType) ? namedType : undefined,
151151
);
152152
break;
153-
case Kind.FIELD:
153+
}
154+
case Kind.FIELD: {
154155
const parentType = this.getParentType();
155156
let fieldDef;
156157
let fieldType: mixed;
@@ -163,10 +164,11 @@ export class TypeInfo {
163164
this._fieldDefStack.push(fieldDef);
164165
this._typeStack.push(isOutputType(fieldType) ? fieldType : undefined);
165166
break;
167+
}
166168
case Kind.DIRECTIVE:
167169
this._directive = schema.getDirective(node.name.value);
168170
break;
169-
case Kind.OPERATION_DEFINITION:
171+
case Kind.OPERATION_DEFINITION: {
170172
let type: mixed;
171173
if (node.operation === 'query') {
172174
type = schema.getQueryType();
@@ -177,21 +179,24 @@ export class TypeInfo {
177179
}
178180
this._typeStack.push(isObjectType(type) ? type : undefined);
179181
break;
182+
}
180183
case Kind.INLINE_FRAGMENT:
181-
case Kind.FRAGMENT_DEFINITION:
184+
case Kind.FRAGMENT_DEFINITION: {
182185
const typeConditionAST = node.typeCondition;
183186
const outputType: mixed = typeConditionAST
184187
? typeFromAST(schema, typeConditionAST)
185188
: getNamedType(this.getType());
186189
this._typeStack.push(isOutputType(outputType) ? outputType : undefined);
187190
break;
188-
case Kind.VARIABLE_DEFINITION:
191+
}
192+
case Kind.VARIABLE_DEFINITION: {
189193
const inputType: mixed = typeFromAST(schema, node.type);
190194
this._inputTypeStack.push(
191195
isInputType(inputType) ? inputType : undefined,
192196
);
193197
break;
194-
case Kind.ARGUMENT:
198+
}
199+
case Kind.ARGUMENT: {
195200
let argDef;
196201
let argType: mixed;
197202
const fieldOrDirective = this.getDirective() || this.getFieldDef();
@@ -208,7 +213,8 @@ export class TypeInfo {
208213
this._defaultValueStack.push(argDef ? argDef.defaultValue : undefined);
209214
this._inputTypeStack.push(isInputType(argType) ? argType : undefined);
210215
break;
211-
case Kind.LIST:
216+
}
217+
case Kind.LIST: {
212218
const listType: mixed = getNullableType(this.getInputType());
213219
const itemType: mixed = isListType(listType)
214220
? listType.ofType
@@ -217,7 +223,8 @@ export class TypeInfo {
217223
this._defaultValueStack.push(undefined);
218224
this._inputTypeStack.push(isInputType(itemType) ? itemType : undefined);
219225
break;
220-
case Kind.OBJECT_FIELD:
226+
}
227+
case Kind.OBJECT_FIELD: {
221228
const objectType: mixed = getNamedType(this.getInputType());
222229
let inputFieldType: GraphQLInputType | void;
223230
let inputField: GraphQLInputField | void;
@@ -234,14 +241,16 @@ export class TypeInfo {
234241
isInputType(inputFieldType) ? inputFieldType : undefined,
235242
);
236243
break;
237-
case Kind.ENUM:
244+
}
245+
case Kind.ENUM: {
238246
const enumType: mixed = getNamedType(this.getInputType());
239247
let enumValue;
240248
if (isEnumType(enumType)) {
241249
enumValue = enumType.getValue(node.value);
242250
}
243251
this._enumValue = enumValue;
244252
break;
253+
}
245254
}
246255
}
247256

src/utilities/getOperationRootType.js

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,41 @@ export function getOperationRootType(
2222
schema: GraphQLSchema,
2323
operation: OperationDefinitionNode | OperationTypeDefinitionNode,
2424
): GraphQLObjectType {
25-
switch (operation.operation) {
26-
case 'query':
27-
const queryType = schema.getQueryType();
28-
if (!queryType) {
29-
throw new GraphQLError(
30-
'Schema does not define the required query root type.',
31-
operation,
32-
);
33-
}
34-
return queryType;
35-
case 'mutation':
36-
const mutationType = schema.getMutationType();
37-
if (!mutationType) {
38-
throw new GraphQLError(
39-
'Schema is not configured for mutations.',
40-
operation,
41-
);
42-
}
43-
return mutationType;
44-
case 'subscription':
45-
const subscriptionType = schema.getSubscriptionType();
46-
if (!subscriptionType) {
47-
throw new GraphQLError(
48-
'Schema is not configured for subscriptions.',
49-
operation,
50-
);
51-
}
52-
return subscriptionType;
53-
default:
25+
if (operation.operation === 'query') {
26+
const queryType = schema.getQueryType();
27+
if (!queryType) {
5428
throw new GraphQLError(
55-
'Can only have query, mutation and subscription operations.',
29+
'Schema does not define the required query root type.',
5630
operation,
5731
);
32+
}
33+
return queryType;
5834
}
35+
36+
if (operation.operation === 'mutation') {
37+
const mutationType = schema.getMutationType();
38+
if (!mutationType) {
39+
throw new GraphQLError(
40+
'Schema is not configured for mutations.',
41+
operation,
42+
);
43+
}
44+
return mutationType;
45+
}
46+
47+
if (operation.operation === 'subscription') {
48+
const subscriptionType = schema.getSubscriptionType();
49+
if (!subscriptionType) {
50+
throw new GraphQLError(
51+
'Schema is not configured for subscriptions.',
52+
operation,
53+
);
54+
}
55+
return subscriptionType;
56+
}
57+
58+
throw new GraphQLError(
59+
'Can only have query, mutation and subscription operations.',
60+
operation,
61+
);
5962
}

src/utilities/valueFromASTUntyped.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,12 @@ export function valueFromASTUntyped(
5353
field => field.name.value,
5454
field => valueFromASTUntyped(field.value, variables),
5555
);
56-
case Kind.VARIABLE:
56+
case Kind.VARIABLE: {
5757
const variableName = valueNode.name.value;
5858
return variables && !isInvalid(variables[variableName])
5959
? variables[variableName]
6060
: undefined;
61+
}
6162
}
6263

6364
// Not reachable. All possible value nodes have been considered.

src/validation/rules/KnownDirectives.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,12 @@ function getDirectiveLocationForASTPath(ancestors) {
127127
case Kind.INPUT_OBJECT_TYPE_DEFINITION:
128128
case Kind.INPUT_OBJECT_TYPE_EXTENSION:
129129
return DirectiveLocation.INPUT_OBJECT;
130-
case Kind.INPUT_VALUE_DEFINITION:
130+
case Kind.INPUT_VALUE_DEFINITION: {
131131
const parentNode = ancestors[ancestors.length - 3];
132132
return parentNode.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION
133133
? DirectiveLocation.INPUT_FIELD_DEFINITION
134134
: DirectiveLocation.ARGUMENT_DEFINITION;
135+
}
135136
}
136137
}
137138
}

src/validation/rules/OverlappingFieldsCanBeMerged.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ function _collectFieldsAndFragmentNames(
747747
for (let i = 0; i < selectionSet.selections.length; i++) {
748748
const selection = selectionSet.selections[i];
749749
switch (selection.kind) {
750-
case Kind.FIELD:
750+
case Kind.FIELD: {
751751
const fieldName = selection.name.value;
752752
let fieldDef;
753753
if (isObjectType(parentType) || isInterfaceType(parentType)) {
@@ -761,10 +761,11 @@ function _collectFieldsAndFragmentNames(
761761
}
762762
nodeAndDefs[responseName].push([parentType, selection, fieldDef]);
763763
break;
764+
}
764765
case Kind.FRAGMENT_SPREAD:
765766
fragmentNames[selection.name.value] = true;
766767
break;
767-
case Kind.INLINE_FRAGMENT:
768+
case Kind.INLINE_FRAGMENT: {
768769
const typeCondition = selection.typeCondition;
769770
const inlineFragmentType = typeCondition
770771
? typeFromAST(context.getSchema(), typeCondition)
@@ -777,6 +778,7 @@ function _collectFieldsAndFragmentNames(
777778
fragmentNames,
778779
);
779780
break;
781+
}
780782
}
781783
}
782784
}

0 commit comments

Comments
 (0)