diff --git a/src/execution/execute.js b/src/execution/execute.js index cc62e0b556..c9c2c6a809 100644 --- a/src/execution/execute.js +++ b/src/execution/execute.js @@ -1,5 +1,3 @@ -import arrayFrom from '../polyfills/arrayFrom'; - import type { Path } from '../jsutils/Path'; import type { ObjMap } from '../jsutils/ObjMap'; import type { PromiseOrValue } from '../jsutils/PromiseOrValue'; @@ -833,7 +831,7 @@ function completeListValue( // where the list contains no Promises by avoiding creating another Promise. const itemType = returnType.ofType; let containsPromise = false; - const completedResults = arrayFrom(result, (item, index) => { + const completedResults = Array.from(result, (item, index) => { // No need to modify the info object containing the path, // since from here on it is not ever accessed by resolver functions. const itemPath = addPath(path, index, undefined); diff --git a/src/jsutils/isCollection.js b/src/jsutils/isCollection.js index 731470256d..a1d2b0ace0 100644 --- a/src/jsutils/isCollection.js +++ b/src/jsutils/isCollection.js @@ -21,7 +21,11 @@ import { SYMBOL_ITERATOR } from '../polyfills/symbols'; * An Object value which might implement the Iterable or Array-like protocols. * @return {boolean} true if Iterable or Array-like Object. */ -export default function isCollection(obj: mixed): boolean { +declare function isCollection(value: mixed): boolean %checks(value instanceof + Iterable); + +// eslint-disable-next-line no-redeclare +export default function isCollection(obj) { if (obj == null || typeof obj !== 'object') { return false; } diff --git a/src/polyfills/arrayFrom.js b/src/polyfills/arrayFrom.js deleted file mode 100644 index 2d153c56da..0000000000 --- a/src/polyfills/arrayFrom.js +++ /dev/null @@ -1,57 +0,0 @@ -import { SYMBOL_ITERATOR } from './symbols'; - -declare function arrayFrom(arrayLike: Iterable): Array; -// eslint-disable-next-line no-redeclare -declare function arrayFrom( - arrayLike: mixed, - mapFn?: (elem: mixed, index: number) => T, - thisArg?: mixed, -): Array; - -/* eslint-disable no-redeclare */ -// $FlowFixMe[name-already-bound] -const arrayFrom = - Array.from || - function (obj, mapFn, thisArg) { - if (obj == null) { - throw new TypeError( - 'Array.from requires an array-like object - not null or undefined', - ); - } - - // Is Iterable? - const iteratorMethod = obj[SYMBOL_ITERATOR]; - if (typeof iteratorMethod === 'function') { - const iterator = iteratorMethod.call(obj); - const result = []; - let step; - - for (let i = 0; !(step = iterator.next()).done; ++i) { - result.push(mapFn.call(thisArg, step.value, i)); - // Infinite Iterators could cause forEach to run forever. - // After a very large number of iterations, produce an error. - // istanbul ignore if (Too big to actually test) - if (i > 9999999) { - throw new TypeError('Near-infinite iteration.'); - } - } - return result; - } - - // Is Array like? - const length = obj.length; - if (typeof length === 'number' && length >= 0 && length % 1 === 0) { - const result = []; - - for (let i = 0; i < length; ++i) { - if (Object.prototype.hasOwnProperty.call(obj, i)) { - result.push(mapFn.call(thisArg, obj[i], i)); - } - } - return result; - } - - return []; - }; - -export default arrayFrom; diff --git a/src/type/schema.js b/src/type/schema.js index 5e07980649..e20e769077 100644 --- a/src/type/schema.js +++ b/src/type/schema.js @@ -1,4 +1,3 @@ -import arrayFrom from '../polyfills/arrayFrom'; import objectValues from '../polyfills/objectValues'; import { SYMBOL_TO_STRING_TAG } from '../polyfills/symbols'; @@ -207,7 +206,7 @@ export class GraphQLSchema { // Keep track of all implementations by interface name. this._implementationsMap = Object.create(null); - for (const namedType of arrayFrom(allReferencedTypes)) { + for (const namedType of Array.from(allReferencedTypes)) { if (namedType == null) { continue; } diff --git a/src/utilities/astFromValue.js b/src/utilities/astFromValue.js index eb63cadeea..da5ad95051 100644 --- a/src/utilities/astFromValue.js +++ b/src/utilities/astFromValue.js @@ -1,4 +1,3 @@ -import arrayFrom from '../polyfills/arrayFrom'; import objectValues from '../polyfills/objectValues'; import inspect from '../jsutils/inspect'; @@ -67,7 +66,7 @@ export function astFromValue(value: mixed, type: GraphQLInputType): ?ValueNode { const valuesNodes = []; // Since we transpile for-of in loose mode it doesn't support iterators // and it's required to first convert iteratable into array - for (const item of arrayFrom(value)) { + for (const item of Array.from(value)) { const itemNode = astFromValue(item, itemType); if (itemNode != null) { valuesNodes.push(itemNode); diff --git a/src/utilities/coerceInputValue.js b/src/utilities/coerceInputValue.js index be8526108a..9bb1195b09 100644 --- a/src/utilities/coerceInputValue.js +++ b/src/utilities/coerceInputValue.js @@ -1,4 +1,3 @@ -import arrayFrom from '../polyfills/arrayFrom'; import objectValues from '../polyfills/objectValues'; import type { Path } from '../jsutils/Path'; @@ -79,7 +78,7 @@ function coerceInputValueImpl( if (isListType(type)) { const itemType = type.ofType; if (isCollection(inputValue)) { - return arrayFrom(inputValue, (itemValue, index) => { + return Array.from(inputValue, (itemValue, index) => { const itemPath = addPath(path, index, undefined); return coerceInputValueImpl(itemValue, itemType, onError, itemPath); }); diff --git a/src/validation/rules/FieldsOnCorrectTypeRule.js b/src/validation/rules/FieldsOnCorrectTypeRule.js index 58149d90d5..6e56c3bb22 100644 --- a/src/validation/rules/FieldsOnCorrectTypeRule.js +++ b/src/validation/rules/FieldsOnCorrectTypeRule.js @@ -1,5 +1,3 @@ -import arrayFrom from '../../polyfills/arrayFrom'; - import didYouMean from '../../jsutils/didYouMean'; import suggestionList from '../../jsutils/suggestionList'; import naturalCompare from '../../jsutils/naturalCompare'; @@ -107,7 +105,7 @@ function getSuggestedTypeNames( } } - return arrayFrom(suggestedTypes) + return Array.from(suggestedTypes) .sort((typeA, typeB) => { // Suggest both interface and object types based on how common they are. const usageCountDiff = usageCount[typeB.name] - usageCount[typeA.name];