diff --git a/.babelrc.json b/.babelrc.json index fafcc33869..a6bde80c55 100644 --- a/.babelrc.json +++ b/.babelrc.json @@ -1,9 +1,6 @@ { "presets": [["@babel/preset-env", { "targets": { "node": "current" } }]], - "plugins": [ - "./resources/inline-invariant", - "@babel/plugin-transform-flow-strip-types" - ], + "plugins": ["@babel/plugin-transform-flow-strip-types"], "overrides": [ { "exclude": [ @@ -20,11 +17,15 @@ ], "env": { "cjs": { - "presets": [["@babel/preset-env", { "modules": "commonjs" }]] + "presets": [["@babel/preset-env", { "modules": "commonjs" }]], + "plugins": ["./resources/inline-invariant"] }, "mjs": { "presets": [["@babel/preset-env", { "modules": false }]], - "plugins": ["./resources/add-extension-to-import-paths"] + "plugins": [ + "./resources/add-extension-to-import-paths", + "./resources/inline-invariant" + ] } } }, diff --git a/resources/inline-invariant.js b/resources/inline-invariant.js index 682d24e276..9daed59862 100644 --- a/resources/inline-invariant.js +++ b/resources/inline-invariant.js @@ -21,7 +21,6 @@ module.exports = function inlineInvariant(context) { (%%cond%%) || devAssert(0, %%args%%) `); - const t = context.types; return { visitor: { CallExpression(path) { @@ -40,13 +39,7 @@ module.exports = function inlineInvariant(context) { if (calleeName === 'invariant') { const [cond, args] = node.arguments; - // Check if it is unreachable invariant: "invariant(false, ...)" - if (cond.type === 'BooleanLiteral' && cond.value === false) { - addIstanbulIgnoreElse(path); - } else { - path.replaceWith(invariantTemplate({ cond, args })); - } - path.addComment('leading', ' istanbul ignore next '); + path.replaceWith(invariantTemplate({ cond, args })); } else if (calleeName === 'devAssert') { const [cond, args] = node.arguments; path.replaceWith(assertTemplate({ cond, args })); @@ -54,17 +47,4 @@ module.exports = function inlineInvariant(context) { }, }, }; - - function addIstanbulIgnoreElse(path) { - const parentStatement = path.getStatementParent(); - const previousStatement = - parentStatement.container[parentStatement.key - 1]; - if ( - previousStatement != null && - previousStatement.type === 'IfStatement' && - previousStatement.alternate == null - ) { - t.addComment(previousStatement, 'leading', ' istanbul ignore else '); - } - } }; diff --git a/src/__tests__/starWarsSchema.js b/src/__tests__/starWarsSchema.js index 78a95647d3..6d3c778ba7 100644 --- a/src/__tests__/starWarsSchema.js +++ b/src/__tests__/starWarsSchema.js @@ -130,11 +130,12 @@ const characterInterface = new GraphQLInterfaceType({ if (character.type === 'Human') { return humanType; } + // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618') if (character.type === 'Droid') { return droidType; } - // Not reachable. All possible types have been considered. + // istanbul ignore next (Not reachable. All possible types have been considered) invariant(false); }, }); diff --git a/src/execution/__tests__/abstract-promise-test.js b/src/execution/__tests__/abstract-promise-test.js index 6f58497225..0c79b54ff2 100644 --- a/src/execution/__tests__/abstract-promise-test.js +++ b/src/execution/__tests__/abstract-promise-test.js @@ -276,11 +276,12 @@ describe('Execute: Handles execution of abstract types with promises', () => { if (obj instanceof Cat) { return Promise.resolve(CatType); } + // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618') if (obj instanceof Human) { return Promise.resolve(HumanType); } - // Not reachable. All possible types have been considered. + // istanbul ignore next (Not reachable. All possible types have been considered) invariant(false); }, fields: { @@ -405,11 +406,12 @@ describe('Execute: Handles execution of abstract types with promises', () => { if (obj instanceof Cat) { return Promise.resolve(CatType); } + // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618') if (obj instanceof Human) { return Promise.resolve(HumanType); } - // Not reachable. All possible types have been considered. + // istanbul ignore next (Not reachable. All possible types have been considered) invariant(false); }, types: [DogType, CatType], @@ -481,11 +483,12 @@ describe('Execute: Handles execution of abstract types with promises', () => { if (obj instanceof Dog) { return Promise.resolve('Dog'); } + // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618') if (obj instanceof Cat) { return Promise.resolve('Cat'); } - // Not reachable. All possible types have been considered. + // istanbul ignore next (Not reachable. All possible types have been considered) invariant(false); }, fields: { diff --git a/src/execution/__tests__/abstract-test.js b/src/execution/__tests__/abstract-test.js index 10b07c6d6b..3e42ead8b5 100644 --- a/src/execution/__tests__/abstract-test.js +++ b/src/execution/__tests__/abstract-test.js @@ -199,11 +199,12 @@ describe('Execute: Handles execution of abstract types', () => { if (obj instanceof Cat) { return CatType; } + // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618') if (obj instanceof Human) { return HumanType; } - // Not reachable. All possible types have been considered. + // istanbul ignore next (Not reachable. All possible types have been considered) invariant(false); }, fields: { @@ -329,11 +330,12 @@ describe('Execute: Handles execution of abstract types', () => { if (obj instanceof Cat) { return CatType; } + // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618') if (obj instanceof Human) { return HumanType; } - // Not reachable. All possible types have been considered. + // istanbul ignore next (Not reachable. All possible types have been considered) invariant(false); }, types: [DogType, CatType], @@ -490,11 +492,12 @@ describe('Execute: Handles execution of abstract types', () => { if (obj instanceof Dog) { return 'Dog'; } + // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618') if (obj instanceof Cat) { return 'Cat'; } - // Not reachable. All possible types have been considered. + // istanbul ignore next (Not reachable. All possible types have been considered) invariant(false); }, fields: { diff --git a/src/execution/__tests__/union-interface-test.js b/src/execution/__tests__/union-interface-test.js index 39e2518a4f..602385edaf 100644 --- a/src/execution/__tests__/union-interface-test.js +++ b/src/execution/__tests__/union-interface-test.js @@ -119,11 +119,12 @@ const PetType = new GraphQLUnionType({ if (value instanceof Dog) { return DogType; } + // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618') if (value instanceof Cat) { return CatType; } - // Not reachable. All possible types have been considered. + // istanbul ignore next (Not reachable. All possible types have been considered) invariant(false); }, }); diff --git a/src/execution/execute.js b/src/execution/execute.js index 084ea46ba5..821b6e403f 100644 --- a/src/execution/execute.js +++ b/src/execution/execute.js @@ -868,6 +868,7 @@ function completeValue( } // If field type is Object, execute and complete all sub-selections. + // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618') if (isObjectType(returnType)) { return completeObjectValue( exeContext, @@ -879,7 +880,7 @@ function completeValue( ); } - // Not reachable. All possible output types have been considered. + // istanbul ignore next (Not reachable. All possible output types have been considered) invariant( false, 'Cannot complete value of unexpected output type: ' + diff --git a/src/type/introspection.js b/src/type/introspection.js index 6ef4b0137c..12d833cf11 100644 --- a/src/type/introspection.js +++ b/src/type/introspection.js @@ -222,11 +222,12 @@ export const __Type = new GraphQLObjectType({ if (isListType(type)) { return TypeKind.LIST; } + // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618') if (isNonNullType(type)) { return TypeKind.NON_NULL; } - // Not reachable. All possible types have been considered. + // istanbul ignore next (Not reachable. All possible types have been considered) invariant(false, `Unexpected type: "${inspect((type: empty))}".`); }, }, diff --git a/src/utilities/astFromValue.js b/src/utilities/astFromValue.js index d4ea450624..4756225085 100644 --- a/src/utilities/astFromValue.js +++ b/src/utilities/astFromValue.js @@ -101,6 +101,7 @@ export function astFromValue(value: mixed, type: GraphQLInputType): ?ValueNode { return { kind: Kind.OBJECT, fields: fieldNodes }; } + // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618') if (isLeafType(type)) { // Since value is an internally represented value, it must be serialized // to an externally represented value before converting into an AST. @@ -142,7 +143,7 @@ export function astFromValue(value: mixed, type: GraphQLInputType): ?ValueNode { throw new TypeError(`Cannot convert value to AST: ${inspect(serialized)}.`); } - // Not reachable. All possible input types have been considered. + // istanbul ignore next (Not reachable. All possible input types have been considered) invariant(false, 'Unexpected input type: ' + inspect((type: empty))); } diff --git a/src/utilities/coerceInputValue.js b/src/utilities/coerceInputValue.js index b116d514c2..865c16769c 100644 --- a/src/utilities/coerceInputValue.js +++ b/src/utilities/coerceInputValue.js @@ -148,6 +148,7 @@ function coerceInputValueImpl( return coercedValue; } + // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618') if (isLeafType(type)) { let parseResult; @@ -185,6 +186,6 @@ function coerceInputValueImpl( return parseResult; } - // Not reachable. All possible input types have been considered. + // istanbul ignore next (Not reachable. All possible input types have been considered) invariant(false, 'Unexpected input type: ' + inspect((type: empty))); } diff --git a/src/utilities/extendSchema.js b/src/utilities/extendSchema.js index 9de5c74ea3..6142bf360e 100644 --- a/src/utilities/extendSchema.js +++ b/src/utilities/extendSchema.js @@ -282,11 +282,12 @@ export function extendSchemaImpl( if (isEnumType(type)) { return extendEnumType(type); } + // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618') if (isInputObjectType(type)) { return extendInputObjectType(type); } - // Not reachable. All possible types have been considered. + // istanbul ignore next (Not reachable. All possible types have been considered) invariant(false, 'Unexpected type: ' + inspect((type: empty))); } @@ -686,7 +687,7 @@ export function extendSchemaImpl( } } - // Not reachable. All possible type definition nodes have been considered. + // istanbul ignore next (Not reachable. All possible type definition nodes have been considered) invariant( false, 'Unexpected type definition node: ' + inspect((astNode: empty)), diff --git a/src/utilities/findBreakingChanges.js b/src/utilities/findBreakingChanges.js index b3dc7f5eb0..3eeca5089b 100644 --- a/src/utilities/findBreakingChanges.js +++ b/src/utilities/findBreakingChanges.js @@ -526,11 +526,12 @@ function typeKindName(type: GraphQLNamedType): string { if (isEnumType(type)) { return 'an Enum type'; } + // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618') if (isInputObjectType(type)) { return 'an Input type'; } - // Not reachable. All possible named types have been considered. + // istanbul ignore next (Not reachable. All possible named types have been considered) invariant(false, 'Unexpected type: ' + inspect((type: empty))); } diff --git a/src/utilities/lexicographicSortSchema.js b/src/utilities/lexicographicSortSchema.js index 494fc5adfc..0ef144a694 100644 --- a/src/utilities/lexicographicSortSchema.js +++ b/src/utilities/lexicographicSortSchema.js @@ -137,6 +137,7 @@ export function lexicographicSortSchema(schema: GraphQLSchema): GraphQLSchema { values: sortObjMap(config.values), }); } + // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618') if (isInputObjectType(type)) { const config = type.toConfig(); return new GraphQLInputObjectType({ @@ -145,7 +146,7 @@ export function lexicographicSortSchema(schema: GraphQLSchema): GraphQLSchema { }); } - // Not reachable. All possible types have been considered. + // istanbul ignore next (Not reachable. All possible types have been considered) invariant(false, 'Unexpected type: ' + inspect((type: empty))); } } diff --git a/src/utilities/printSchema.js b/src/utilities/printSchema.js index e856abfea5..1f86f22332 100644 --- a/src/utilities/printSchema.js +++ b/src/utilities/printSchema.js @@ -172,11 +172,12 @@ export function printType(type: GraphQLNamedType, options?: Options): string { if (isEnumType(type)) { return printEnum(type, options); } + // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618') if (isInputObjectType(type)) { return printInputObject(type, options); } - // Not reachable. All possible types have been considered. + // istanbul ignore next (Not reachable. All possible types have been considered) invariant(false, 'Unexpected type: ' + inspect((type: empty))); } diff --git a/src/utilities/typeFromAST.js b/src/utilities/typeFromAST.js index c418511dae..9018682b9e 100644 --- a/src/utilities/typeFromAST.js +++ b/src/utilities/typeFromAST.js @@ -48,10 +48,11 @@ export function typeFromAST(schema, typeNode) { innerType = typeFromAST(schema, typeNode.type); return innerType && GraphQLNonNull(innerType); } + // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618') if (typeNode.kind === Kind.NAMED_TYPE) { return schema.getType(typeNode.name.value); } - // Not reachable. All possible type nodes have been considered. + // istanbul ignore next (Not reachable. All possible type nodes have been considered) invariant(false, 'Unexpected type node: ' + inspect((typeNode: empty))); } diff --git a/src/utilities/valueFromAST.js b/src/utilities/valueFromAST.js index 4820b57c46..9b5efce077 100644 --- a/src/utilities/valueFromAST.js +++ b/src/utilities/valueFromAST.js @@ -131,6 +131,7 @@ export function valueFromAST( return coercedObj; } + // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618') if (isLeafType(type)) { // Scalars and Enums fulfill parsing a literal value via parseLiteral(). // Invalid values represent a failure to parse correctly, in which case @@ -147,7 +148,7 @@ export function valueFromAST( return result; } - // Not reachable. All possible input types have been considered. + // istanbul ignore next (Not reachable. All possible input types have been considered) invariant(false, 'Unexpected input type: ' + inspect((type: empty))); } diff --git a/src/utilities/valueFromASTUntyped.js b/src/utilities/valueFromASTUntyped.js index 5ccfef9e7d..a879d460ac 100644 --- a/src/utilities/valueFromASTUntyped.js +++ b/src/utilities/valueFromASTUntyped.js @@ -53,6 +53,6 @@ export function valueFromASTUntyped( return variables?.[valueNode.name.value]; } - // Not reachable. All possible value nodes have been considered. + // istanbul ignore next (Not reachable. All possible value nodes have been considered) invariant(false, 'Unexpected value node: ' + inspect((valueNode: empty))); } diff --git a/src/validation/rules/KnownDirectivesRule.js b/src/validation/rules/KnownDirectivesRule.js index 9bece5bfce..61fd54f234 100644 --- a/src/validation/rules/KnownDirectivesRule.js +++ b/src/validation/rules/KnownDirectivesRule.js @@ -134,6 +134,6 @@ function getDirectiveLocationForOperation( return DirectiveLocation.SUBSCRIPTION; } - // Not reachable. All possible types have been considered. + // istanbul ignore next (Not reachable. All possible types have been considered) invariant(false, 'Unexpected operation: ' + inspect((operation: empty))); } diff --git a/src/validation/rules/PossibleTypeExtensionsRule.js b/src/validation/rules/PossibleTypeExtensionsRule.js index 1d0db909e9..3e7044dd5e 100644 --- a/src/validation/rules/PossibleTypeExtensionsRule.js +++ b/src/validation/rules/PossibleTypeExtensionsRule.js @@ -113,11 +113,12 @@ function typeToExtKind(type) { if (isEnumType(type)) { return Kind.ENUM_TYPE_EXTENSION; } + // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618') if (isInputObjectType(type)) { return Kind.INPUT_OBJECT_TYPE_EXTENSION; } - // Not reachable. All possible types have been considered. + // istanbul ignore next (Not reachable. All possible types have been considered) invariant(false, 'Unexpected type: ' + inspect((type: empty))); } @@ -137,6 +138,6 @@ function extensionKindToTypeName(kind) { return 'input object'; } - // Not reachable. All possible types have been considered. + // istanbul ignore next (Not reachable. All possible types have been considered) invariant(false, 'Unexpected kind: ' + inspect(kind)); }