Skip to content

Commit

Permalink
chore: fix for new lint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
cometkim committed May 23, 2020
1 parent 17d6ba8 commit 801ad77
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 69 deletions.
6 changes: 6 additions & 0 deletions .pnp.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -82,6 +82,7 @@
"gatsby": "^2.22.9",
"graphql": "^15.0.0",
"jest": "^26.0.1",
"redux": "^4.0.5",
"typescript": "^3.9.3",
"utility-types": "^3.10.0"
}
Expand Down
33 changes: 19 additions & 14 deletions src/gatsby-node.ts
@@ -1,8 +1,7 @@
import type { GatsbyNode } from 'gatsby';
import type { GraphQLSchema } from 'gatsby/graphql';
import type { Callable } from '@cometjs/core';
import type { Source } from '@graphql-toolkit/common';
import type { Option } from '@cometjs/core';
import type { GatsbyKnownAction } from './gatsby-utils';
import type { GatsbyKnownAction, GatsbyStore } from './gatsby-utils';

import path from 'path';
import { stripIndent } from 'common-tags';
Expand All @@ -29,12 +28,14 @@ import {
const trackedSource = new Map<string, Source>();

let pluginOptions: RequiredPluginOptions;
let unsubscribeQueryExtraction: Function;
let unsubscribeQueryExtraction: Callable;

export const onPreBootstrap: GatsbyNode['onPreBootstrap'] = ({
store,
store: _store,
reporter,
}, options) => {
const store = _store as GatsbyStore;

// Validate plugin options earlier.
pluginOptions = requirePluginOptions(options, { store, reporter });

Expand All @@ -45,13 +46,16 @@ export const onPreBootstrap: GatsbyNode['onPreBootstrap'] = ({
};

export const onPreExtractQueries: GatsbyNode['onPreExtractQueries'] = ({
store,
store: _store,
reporter,
}) => {
const store = _store as GatsbyStore;

reporter.verbose('[typegen] Listen on query extraction');

// eslint-disable-next-line @typescript-eslint/no-misused-promises
unsubscribeQueryExtraction = store.subscribe(async () => {
const lastAction = store.getState().lastAction as GatsbyKnownAction;
const lastAction = store.getState().lastAction;

if (lastAction.type !== 'QUERY_EXTRACTION_BABEL_SUCCESS') {
return;
Expand All @@ -76,13 +80,14 @@ export const onPreExtractQueries: GatsbyNode['onPreExtractQueries'] = ({
} catch (error) {
reporter.error(`[typegen] Fail to extract GraphQL documents from ${componentPath}`, error);
}
});
}) as Callable;
};

export const onPostBootstrap: GatsbyNode['onPostBootstrap'] = async ({
store,
store: _store,
reporter,
}) => {
const store = _store as GatsbyStore;
const {
language,
namespace,
Expand All @@ -98,7 +103,7 @@ export const onPostBootstrap: GatsbyNode['onPostBootstrap'] = async ({
unsubscribeQueryExtraction();

const state = store.getState();
const basePath = state.program.directory as string;
const basePath = state.program.directory;
const pluginState = {
schema: state.schema,
};
Expand Down Expand Up @@ -150,7 +155,7 @@ export const onPostBootstrap: GatsbyNode['onPostBootstrap'] = async ({
if (language === 'flow' && /\.jsx?$/.test(componentPath)) {
const content = await readFile(componentPath);
const hasFlowComment = content.includes('@flow');
reporter.verbose(`[typegen] Check if the file has flow comment: ${hasFlowComment}`);
reporter.verbose(`[typegen] Check if the file has flow comment: ${hasFlowComment.toString()}`);
hasFlowComment && insertTypeWorker.push({ file: componentPath });
}
};
Expand Down Expand Up @@ -187,7 +192,7 @@ export const onPostBootstrap: GatsbyNode['onPostBootstrap'] = async ({

// Task 4. Auto-fixing!
for (const componentPath of trackedSource.keys()) {
pushInsertTypeTask(componentPath);
void pushInsertTypeTask(componentPath);
}

// Subscribe GatsbyJS store and handle changes in development mode
Expand All @@ -196,7 +201,7 @@ export const onPostBootstrap: GatsbyNode['onPostBootstrap'] = async ({

store.subscribe(() => {
const state = store.getState();
const lastAction = state.lastAction as GatsbyKnownAction;
const lastAction = state.lastAction;

// Listen gatsby actions
// - SET_SCHEMA action for schema changing.
Expand All @@ -220,7 +225,7 @@ export const onPostBootstrap: GatsbyNode['onPostBootstrap'] = async ({
trackedSource.set(componentPath, document);

pushCodegenTask();
pushInsertTypeTask(componentPath);
void pushInsertTypeTask(componentPath);
}
});
}
Expand Down
11 changes: 11 additions & 0 deletions src/gatsby-utils.ts
@@ -1,5 +1,16 @@
import type { Store } from 'redux';
import type { GraphQLSchema } from 'gatsby/graphql';

export type GatsbyStore = Store<GatsbyStateKnownShape, GatsbyKnownAction>;

export type GatsbyStateKnownShape = {
schema: GraphQLSchema,
lastAction: GatsbyKnownAction,
program: {
directory: string,
},
};

export type GatsbyKnownAction = (
| GatsbyQueryExtractedAction
| GatsbyReplaceStaticQueryAction
Expand Down
6 changes: 3 additions & 3 deletions src/plugin-utils.ts
@@ -1,14 +1,14 @@
import type { Required } from 'utility-types';
import type { Store, Reporter } from 'gatsby';
import type { GraphQLTagPluckOptions } from '@graphql-toolkit/graphql-tag-pluck';
import type { GatsbyStore } from './gatsby-utils';
import type {
PluginOptions,
SchemaOutputOptions,
DeprecatedPluginOptions,
} from './types';

import path from 'path';

import { formatLanguage } from './common';
import { gatsbyInternalScalars } from './gatsby-utils';

Expand Down Expand Up @@ -41,7 +41,7 @@ interface RequirePluginOptionsFn {
options: unknown,
props: {
reporter: Reporter,
store: Store,
store: GatsbyStore,
},
): RequiredPluginOptions;
}
Expand All @@ -53,7 +53,7 @@ export const requirePluginOptions: RequirePluginOptionsFn = (
},
) => {
const { program } = store.getState();
const basePath = program.directory as string;
const basePath = program.directory;

// There are no required properties (yet), so must be compatible.
const pluginOptions = options as PluginOptions;
Expand Down
18 changes: 10 additions & 8 deletions src/workers/codegen.ts
@@ -1,6 +1,7 @@
import type { Reporter } from 'gatsby';
import type { GraphQLSchema } from 'gatsby/graphql';
import type { AsyncCargo } from 'async';
import type { Callable } from '@cometjs/core';
import type { Source } from '@graphql-toolkit/common';
import type { RequiredPluginOptions } from '../plugin-utils';

Expand Down Expand Up @@ -53,7 +54,7 @@ export type CodegenTask = {
};

export type CodegenWorker = Omit<AsyncCargo, 'push'> & {
push(task: CodegenTask, cb?: Function): void,
push(task: CodegenTask, cb?: Callable): void,
};

interface SetupCodegenWorkerFn {
Expand All @@ -80,7 +81,7 @@ export const setupCodegenWorker: SetupCodegenWorkerFn = ({

type CodegenOptions = Parameters<typeof codegen>[0];
const codegenOptions: CodegenOptions = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// eslint-disable-next-line
schema: undefined as any,
schemaAst,
documents,
Expand All @@ -95,14 +96,15 @@ export const setupCodegenWorker: SetupCodegenWorkerFn = ({
plugins: [],
pluginMap: {},
};
type CodegenPlugin = CodegenOptions['pluginMap'][string];
if (language === 'typescript') {
codegenOptions.pluginMap['typescript'] = require('@graphql-codegen/typescript');
codegenOptions.pluginMap['typescript'] = require('@graphql-codegen/typescript') as CodegenPlugin;
codegenOptions.plugins.push({
typescript: {
...DEFAULT_TYPESCRIPT_CONFIG,
},
});
codegenOptions.pluginMap['typescriptOperations'] = require('@graphql-codegen/typescript-operations');
codegenOptions.pluginMap['typescriptOperations'] = require('@graphql-codegen/typescript-operations') as CodegenPlugin;
codegenOptions.plugins.push({
typescriptOperations: {
...DEFAULT_TYPESCRIPT_CONFIG,
Expand All @@ -111,22 +113,22 @@ export const setupCodegenWorker: SetupCodegenWorkerFn = ({
},
});
if (includeResolvers) {
codegenOptions.pluginMap['typescriptResolvers'] = require('@graphql-codegen/typescript-resolvers');
codegenOptions.pluginMap['typescriptResolvers'] = require('@graphql-codegen/typescript-resolvers') as CodegenPlugin;
codegenOptions.plugins.push({
typescriptResolvers: {
contextType: 'gatsby-plugin-typegen/types#GatsbyResolverContext',
},
});
}
} else /* flow */ {
codegenOptions.pluginMap['flow'] = require('@graphql-codegen/flow');
codegenOptions.pluginMap['flow'] = require('@graphql-codegen/flow') as CodegenPlugin;
codegenOptions.plugins.push({
flow: {
...DEFAULT_FLOW_CONFIG,
typesPrefix: `${namespace}$`,
},
});
codegenOptions.pluginMap['flowOperations'] = require('@graphql-codegen/flow-operations');
codegenOptions.pluginMap['flowOperations'] = require('@graphql-codegen/flow-operations') as CodegenPlugin;
codegenOptions.plugins.push({
flowOperations: {
...DEFAULT_FLOW_CONFIG,
Expand All @@ -136,7 +138,7 @@ export const setupCodegenWorker: SetupCodegenWorkerFn = ({
},
});
if (includeResolvers) {
codegenOptions.pluginMap['flowResolvers'] = require('@graphql-codegen/flow-resolvers');
codegenOptions.pluginMap['flowResolvers'] = require('@graphql-codegen/flow-resolvers') as CodegenPlugin;
// Where is contextType option????? WHERE
codegenOptions.plugins.push({
flowResolvers: {
Expand Down
9 changes: 4 additions & 5 deletions src/workers/emit-schema.ts
@@ -1,21 +1,20 @@
import type { Reporter } from 'gatsby';
import type { GraphQLSchema } from 'gatsby/graphql';
import type { AsyncCargo } from 'async';
import type { Option } from '@cometjs/core';
import type { RequiredPluginOptions } from '../plugin-utils';
import type { Option, Callable } from '@cometjs/core';
import type { SchemaOutputOptions } from '../types';

import { cargo, asyncify } from 'async';
import { printSchema, introspectionFromSchema } from 'gatsby/graphql';
import { readFile, writeFile } from '../common';
import { writeFile } from '../common';

export type EmitSchemaTask = {
schema: GraphQLSchema,
entries: [string, SchemaOutputOptions],
entries: Array<[string, SchemaOutputOptions]>,
};

export type EmitSchemaWorker = Omit<AsyncCargo, 'push'> & {
push(task: EmitSchemaTask, cb?: Function): void,
push(task: EmitSchemaTask, cb?: Callable): void,
};

interface SetupEmitSchemaWorkerFn {
Expand Down
2 changes: 2 additions & 0 deletions src/workers/insert-types.ts
Expand Up @@ -72,11 +72,13 @@ export const setupInsertTypeWorker: SetupInsertTypeWorkerFn = ({
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.replace(STATIC_QUERY_HOOK_REGEXP, (substring: string, ...args: any[]): string => {
const { length: l, [l - 1]: groups } = args;
// eslint-disable-next-line
return substring.replace(groups['CallExpressionName'], `useStaticQuery<${namespace}${accessor}${groups['QueryName']}Query>`);
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.replace(STATIC_QUERY_COMPONENT_REGEXP, (substring: string, ...args: any[]): string => {
const { length: l, [l - 1]: groups } = args;
// eslint-disable-next-line
return substring.replace(groups['JsxTagOpening'], `<StaticQuery<${namespace}${accessor}${groups['QueryName']}Query>`);
});

Expand Down
39 changes: 0 additions & 39 deletions typings/gatsby.d.ts
@@ -1,42 +1,3 @@
declare module 'gatsby/graphql' {
export * from 'graphql';
}

declare module 'gatsby/dist/utils/gatsby-dependents' {
type ModuleInfo = {
name: string,
version: string,
path: string,
};
const getDependents: () => ModuleInfo[];
export default getDependents;
}

declare module 'gatsby/dist/query/file-parser' {
import { DocumentNode } from 'gatsby/graphql';

/**
* @type {Opentracing.Span}
*/
type Span = any;

type QueryExtractionResult = {
filePath: string,
doc: DocumentNode,
text: string,
hash: number,
isStaticQuery: boolean,
isHook: boolean,
templateLoc: unknown,
};

export default class FileParser {
constructor(args: { parentSpan: Span });
parseFile(file: string, addError: Function): Promise<(
| QueryExtractionResult
| QueryExtractionResult[]
| null
)>;
parseFiles(files: string[], addError: Function): Promise<QueryExtractionResult[]>;
}
}
1 change: 1 addition & 0 deletions yarn.lock
Expand Up @@ -9543,6 +9543,7 @@ fsevents@^1.2.7:
gatsby: ^2.22.9
graphql: ^15.0.0
jest: ^26.0.1
redux: ^4.0.5
typescript: ^3.9.3
utility-types: ^3.10.0
peerDependencies:
Expand Down

0 comments on commit 801ad77

Please sign in to comment.