Skip to content

Commit

Permalink
inline-invariant: stop inserting // istanbul ignore next comments (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Jun 5, 2020
1 parent daf628d commit ba2216c
Show file tree
Hide file tree
Showing 19 changed files with 50 additions and 50 deletions.
13 changes: 7 additions & 6 deletions .babelrc.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand All @@ -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"
]
}
}
},
Expand Down
22 changes: 1 addition & 21 deletions resources/inline-invariant.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ module.exports = function inlineInvariant(context) {
(%%cond%%) || devAssert(0, %%args%%)
`);

const t = context.types;
return {
visitor: {
CallExpression(path) {
Expand All @@ -40,31 +39,12 @@ 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 }));
}
},
},
};

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 ');
}
}
};
3 changes: 2 additions & 1 deletion src/__tests__/starWarsSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
});
Expand Down
9 changes: 6 additions & 3 deletions src/execution/__tests__/abstract-promise-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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: {
Expand Down
9 changes: 6 additions & 3 deletions src/execution/__tests__/abstract-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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: {
Expand Down
3 changes: 2 additions & 1 deletion src/execution/__tests__/union-interface-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
});
Expand Down
3 changes: 2 additions & 1 deletion src/execution/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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: ' +
Expand Down
3 changes: 2 additions & 1 deletion src/type/introspection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))}".`);
},
},
Expand Down
3 changes: 2 additions & 1 deletion src/utilities/astFromValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)));
}

Expand Down
3 changes: 2 additions & 1 deletion src/utilities/coerceInputValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)));
}
5 changes: 3 additions & 2 deletions src/utilities/extendSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}

Expand Down Expand Up @@ -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)),
Expand Down
3 changes: 2 additions & 1 deletion src/utilities/findBreakingChanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}

Expand Down
3 changes: 2 additions & 1 deletion src/utilities/lexicographicSortSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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)));
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/utilities/printSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}

Expand Down
3 changes: 2 additions & 1 deletion src/utilities/typeFromAST.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
3 changes: 2 additions & 1 deletion src/utilities/valueFromAST.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)));
}

Expand Down
2 changes: 1 addition & 1 deletion src/utilities/valueFromASTUntyped.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
2 changes: 1 addition & 1 deletion src/validation/rules/KnownDirectivesRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
5 changes: 3 additions & 2 deletions src/validation/rules/PossibleTypeExtensionsRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}

Expand All @@ -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));
}

0 comments on commit ba2216c

Please sign in to comment.