Skip to content

Commit

Permalink
fix race condition of loading graphql-config (#1165)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaMachina committed Sep 26, 2022
1 parent b12da39 commit eab5492
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-drinks-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': patch
---

fix race condition of `loadGraphQLConfig()`
40 changes: 22 additions & 18 deletions packages/plugin/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,34 @@ export type Block = Linter.ProcessorFile & {
offset: number;
};

const graphQLTagPluckOptions: GraphQLTagPluckOptions =
loadGraphQLConfig().getDefault()?.extensions?.graphqlTagPluck;

const {
modules = [],
globalGqlIdentifierName = ['gql', 'graphql'],
gqlMagicComment = 'GraphQL',
} = graphQLTagPluckOptions || {};

const RELEVANT_KEYWORDS = [
...new Set(
[
...modules.map(({ identifier }) => identifier),
...asArray(globalGqlIdentifierName),
gqlMagicComment,
].filter(Boolean)
),
];
let RELEVANT_KEYWORDS: string[];
let graphQLTagPluckOptions: GraphQLTagPluckOptions;

const blocksMap = new Map<string, Block[]>();

export const processor: Linter.Processor<Block | string> = {
supportsAutofix: true,
preprocess(code, filePath) {
if (!RELEVANT_KEYWORDS) {
graphQLTagPluckOptions = loadGraphQLConfig().getDefault()?.extensions?.graphqlTagPluck;

const {
modules = [],
globalGqlIdentifierName = ['gql', 'graphql'],
gqlMagicComment = 'GraphQL',
} = graphQLTagPluckOptions || {};

RELEVANT_KEYWORDS = [
...new Set(
[
...modules.map(({ identifier }) => identifier),
...asArray(globalGqlIdentifierName),
gqlMagicComment,
].filter(Boolean)
),
];
}

if (RELEVANT_KEYWORDS.every(keyword => !code.includes(keyword))) {
return [code];
}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export type RuleDocsInfo<T> = {
export type GraphQLESLintRule<Options = any[], WithTypeInfo extends boolean = false> = {
create(context: GraphQLESLintRuleContext<Options>): GraphQLESLintRuleListener<WithTypeInfo>;
meta: Omit<Rule.RuleMetaData, 'docs'> & {
docs: RuleDocsInfo<Options>
docs: RuleDocsInfo<Options>;
};
};

Expand Down

0 comments on commit eab5492

Please sign in to comment.