From 50e0645b8a43239d0b623d7afbaa13e5c71f6b90 Mon Sep 17 00:00:00 2001 From: tafelnl <35837839+tafelnl@users.noreply.github.com> Date: Wed, 10 Aug 2022 12:19:13 +0200 Subject: [PATCH] refactor: enable `noUncheckedIndexedAccess` --- index.ts | 43 ++++++++++++++++++++++++++++--------------- test/mocks/schema.ts | 2 +- tsconfig.json | 3 ++- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/index.ts b/index.ts index 1efd84f..675c81e 100644 --- a/index.ts +++ b/index.ts @@ -289,12 +289,14 @@ function skipTree(skip: string[]): SkipTree { const prop = props[i]; const all = props[i + 1] === '*'; - if (!propTree[prop]) { - propTree[prop] = i === s - 1 || all ? true : {}; - all && i++; - } + if (prop) { + if (!propTree[prop]) { + propTree[prop] = i === s - 1 || all ? true : {}; + all && i++; + } - propTree = propTree[prop] as SkipTree; + propTree = propTree[prop] as SkipTree; + } } } @@ -311,9 +313,14 @@ function verifySkip(node: string, skip: SkipValue): SkipValue { return false; } - // true['string'] is a valid operation is JS resulting in `undefined` - if ((skip as SkipTree)[node]) { - return (skip as SkipTree)[node]; + if (skip === true) { + return false; + } + + const skipValue = skip[node]; + + if (skipValue) { + return skipValue; } // lookup through wildcard patterns @@ -324,7 +331,7 @@ function verifySkip(node: string, skip: SkipValue): SkipValue { const rx: RegExp = new RegExp(pattern.replace(RX_AST, '.*')); if (rx.test(node)) { - nodeTree = (skip as SkipTree)[pattern]; + nodeTree = (skip as SkipTree)[pattern] ?? false; // istanbul ignore else if (nodeTree === true) { @@ -367,8 +374,10 @@ function traverse( const name = (node as FieldNode).name.value; - if (opts.fragments[name]) { - traverse(getNodes(opts.fragments[name]), root, opts, skip); + const selection = opts.fragments[name] + + if (selection) { + traverse(getNodes(selection), root, opts, skip); continue; } @@ -377,13 +386,15 @@ function traverse( const nodeSkip = verifySkip(name, skip); if (nodeSkip !== true) { - (root as MapResult)[name] = (root as MapResult)[name] || ( + const mapResult = (root as MapResult)[name] || ( nodes.length ? {} : false ); + (root as MapResult)[name] = mapResult; + nodes.length && traverse( nodes, - (root as MapResult)[name], + mapResult, opts, nodeSkip, ); @@ -583,8 +594,10 @@ export function fieldsProjection( let dotName = toDotNotation(stack[0].node, j); - if (transform[dotName]) { - dotName = transform[dotName]; + const transformValue = transform[dotName]; + + if (transformValue) { + dotName = transformValue; } map[dotName] = 1; diff --git a/test/mocks/schema.ts b/test/mocks/schema.ts index 6bfe886..7aae2e1 100644 --- a/test/mocks/schema.ts +++ b/test/mocks/schema.ts @@ -129,7 +129,7 @@ export const schema = new GraphQLSchema({ export async function exec(query: string, vars: any) { const queryId = uuid.v4(); await graphql(schema, query, null, { queryId }, vars); - const info: GraphQLResolveInfo = resolveInfo[queryId]; + const info: GraphQLResolveInfo = resolveInfo[queryId]!; delete resolveInfo[queryId]; return info; diff --git a/tsconfig.json b/tsconfig.json index 6e94da0..069c528 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,6 +17,7 @@ "dom", "es2017", "esnext.asynciterable" - ] + ], + "noUncheckedIndexedAccess": true } }