From 552cb203978e1b36d47b81b8b4d5744181d7c2e8 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Sat, 27 Jul 2019 21:17:54 +0300 Subject: [PATCH] flow: Enable 'uninitialized-instance-property' lint --- .flowconfig | 2 +- src/language/parser.js | 10 +++++----- src/type/definition.js | 16 ++++++++-------- src/type/schema.js | 15 +++++---------- 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/.flowconfig b/.flowconfig index e49ebedcf55..89693b4fc5b 100644 --- a/.flowconfig +++ b/.flowconfig @@ -28,7 +28,7 @@ implicit-inexact-object=error unnecessary-optional-chain=error unnecessary-invariant=off signature-verification-failure=error -uninitialized-instance-property=off +uninitialized-instance-property=error non-array-spread=error [options] diff --git a/src/language/parser.js b/src/language/parser.js index 8889ba91b84..6e3831b2e45 100644 --- a/src/language/parser.js +++ b/src/language/parser.js @@ -1,6 +1,7 @@ // @flow strict import inspect from '../jsutils/inspect'; +import invariant from '../jsutils/invariant'; import defineToJSON from '../jsutils/defineToJSON'; import { Source } from './source'; import { type GraphQLError } from '../error/GraphQLError'; @@ -171,11 +172,10 @@ class Parser { constructor(source: string | Source, options?: ParseOptions) { const sourceObj = typeof source === 'string' ? new Source(source) : source; - if (!(sourceObj instanceof Source)) { - throw new TypeError( - `Must provide Source. Received: ${inspect(sourceObj)}`, - ); - } + invariant( + sourceObj instanceof Source, + `Must provide Source. Received: ${inspect(sourceObj)}`, + ); this._lexer = createLexer(sourceObj); this._options = options || {}; diff --git a/src/type/definition.js b/src/type/definition.js index b7c967b2435..709adcb019a 100644 --- a/src/type/definition.js +++ b/src/type/definition.js @@ -555,13 +555,13 @@ export class GraphQLScalarType { extensionASTNodes: ?$ReadOnlyArray; constructor(config: GraphQLScalarTypeConfig<*, *>): void { + const parseValue = config.parseValue || identityFunc; this.name = config.name; this.description = config.description; this.serialize = config.serialize || identityFunc; - this.parseValue = config.parseValue || identityFunc; + this.parseValue = parseValue; this.parseLiteral = - config.parseLiteral || - (node => this.parseValue(valueFromASTUntyped(node))); + config.parseLiteral || (node => parseValue(valueFromASTUntyped(node))) this.astNode = config.astNode; this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes); @@ -1155,7 +1155,7 @@ export class GraphQLEnumType /* */ { this.description = config.description; this.astNode = config.astNode; this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes); - this._values = defineEnumValues(this, config.values); + this._values = defineEnumValues(this.name, config.values); this._valueLookup = new Map( this._values.map(enumValue => [enumValue.value, enumValue]), ); @@ -1232,22 +1232,22 @@ defineToStringTag(GraphQLEnumType); defineToJSON(GraphQLEnumType); function defineEnumValues( - type: GraphQLEnumType, + typeName: string, valueMap: GraphQLEnumValueConfigMap /* */, ): Array */> { invariant( isPlainObj(valueMap), - `${type.name} values must be an object with value names as keys.`, + `${typeName} values must be an object with value names as keys.`, ); return objectEntries(valueMap).map(([valueName, value]) => { invariant( isPlainObj(value), - `${type.name}.${valueName} must refer to an object with a "value" key ` + + `${typeName}.${valueName} must refer to an object with a "value" key ` + `representing an internal value but got: ${inspect(value)}.`, ); invariant( !('isDeprecated' in value), - `${type.name}.${valueName} should provide "deprecationReason" instead of "isDeprecated".`, + `${typeName}.${valueName} should provide "deprecationReason" instead of "isDeprecated".`, ); return { name: valueName, diff --git a/src/type/schema.js b/src/type/schema.js index 7214b343ade..4608b5f6599 100644 --- a/src/type/schema.js +++ b/src/type/schema.js @@ -165,17 +165,12 @@ export class GraphQLSchema { this.extensionASTNodes = config.extensionASTNodes; // Build type map now to detect any errors within this schema. - let initialTypes: Array = [ - this.getQueryType(), - this.getMutationType(), - this.getSubscriptionType(), + const initialTypes: Array = [ + this._queryType, + this._mutationType, + this._subscriptionType, __Schema, - ]; - - const types = config.types; - if (types) { - initialTypes = initialTypes.concat(types); - } + ].concat(config.types); // Keep track of all types referenced within the schema. let typeMap: TypeMap = Object.create(null);