Skip to content

Commit 0b8acdc

Browse files
authored
fix race condition of loading loadGraphQLConfig() when processor.preprocess() can be called before parseForESLint() (#1170)
* fix race condition of loading `loadGraphQLConfig()` when `processor.preprocess()` can be called before `parseForESLint()` * one last time * one yet last time * one yet yet last time
1 parent 02125cb commit 0b8acdc

File tree

10 files changed

+45
-45
lines changed

10 files changed

+45
-45
lines changed

.changeset/tiny-geese-type.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-eslint/eslint-plugin': patch
3+
---
4+
5+
fix race condition of loading `loadGraphQLConfig()` when `processor.preprocess()` can be called before `parseForESLint()`

examples/basic/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
{
22
"name": "@graphql-eslint/example-basic",
33
"private": true,
4-
"version": "0.0.1",
4+
"version": "0.0.0",
55
"repository": "https://github.com/B2o5T/graphql-eslint",
66
"author": "Dotan Simha <[email protected]>",
7-
"license": "MIT",
87
"scripts": {
98
"lint": "eslint ."
109
},

examples/code-file/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
{
22
"name": "@graphql-eslint/example-code-file",
33
"private": true,
4-
"version": "0.0.1",
4+
"version": "0.0.0",
55
"repository": "https://github.com/B2o5T/graphql-eslint",
66
"author": "Dotan Simha <[email protected]>",
7-
"license": "MIT",
87
"scripts": {
98
"lint": "eslint ."
109
},

examples/graphql-config-code-file/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
{
22
"name": "@graphql-eslint/example-graphql-config-code-file",
33
"private": true,
4-
"version": "0.0.1",
4+
"version": "0.0.0",
55
"repository": "https://github.com/B2o5T/graphql-eslint",
66
"author": "Dotan Simha <[email protected]>",
7-
"license": "MIT",
87
"scripts": {
98
"lint": "eslint --ext graphql,js ."
109
},

examples/graphql-config/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
{
22
"name": "@graphql-eslint/example-graphql-config",
33
"private": true,
4-
"version": "0.0.1",
4+
"version": "0.0.0",
55
"repository": "https://github.com/B2o5T/graphql-eslint",
66
"author": "Dotan Simha <[email protected]>",
7-
"license": "MIT",
87
"scripts": {
98
"lint": "eslint ."
109
},

examples/monorepo/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"name": "monorepo",
3-
"version": "0.0.1",
3+
"version": "0.0.0",
44
"private": true,
5-
"license": "MIT",
65
"author": "Dimitri POSTOLOV",
76
"scripts": {
87
"lint": "eslint ."

examples/prettier/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
{
22
"name": "@graphql-eslint/example-prettier",
33
"private": true,
4-
"version": "0.0.1",
4+
"version": "0.0.0",
55
"repository": "https://github.com/B2o5T/graphql-eslint",
66
"author": "JounQin <[email protected]>",
7-
"license": "MIT",
87
"scripts": {
98
"lint": "eslint ."
109
},

packages/plugin/src/graphql-config.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,24 @@ import { ParserOptions } from './types';
1212
const debug = debugFactory('graphql-eslint:graphql-config');
1313
let graphQLConfig: GraphQLConfig;
1414

15+
export function loadOnDiskGraphQLConfig(filePath: string): GraphQLConfig {
16+
return loadConfigSync({
17+
// load config relative to the file being linted
18+
rootDir: filePath ? dirname(filePath) : undefined,
19+
throwOnEmpty: false,
20+
throwOnMissing: false,
21+
extensions: [addCodeFileLoaderExtension],
22+
});
23+
}
24+
1525
export function loadGraphQLConfig(options: ParserOptions = {}): GraphQLConfig {
1626
// We don't want cache config on test environment
1727
// Otherwise schema and documents will be same for all tests
1828
if (process.env.NODE_ENV !== 'test' && graphQLConfig) {
1929
return graphQLConfig;
2030
}
2131

22-
const onDiskConfig = options.skipGraphQLConfig
23-
? null
24-
: loadConfigSync({
25-
// load config relative to the file being linted
26-
rootDir: options.filePath ? dirname(options.filePath) : undefined,
27-
throwOnEmpty: false,
28-
throwOnMissing: false,
29-
extensions: [addCodeFileLoaderExtension],
30-
});
32+
const onDiskConfig = !options.skipGraphQLConfig && loadOnDiskGraphQLConfig(options.filePath);
3133

3234
debug('options.skipGraphQLConfig: %o', options.skipGraphQLConfig);
3335
if (onDiskConfig) {

packages/plugin/src/processor.ts

+21-22
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
11
import { Linter } from 'eslint';
22
import { parseCode, GraphQLTagPluckOptions } from '@graphql-tools/graphql-tag-pluck';
33
import { asArray } from '@graphql-tools/utils';
4-
import { loadGraphQLConfig } from './graphql-config';
4+
import { GraphQLConfig } from 'graphql-config';
5+
import { loadOnDiskGraphQLConfig } from './graphql-config';
56

67
export type Block = Linter.ProcessorFile & {
78
lineOffset: number;
89
offset: number;
910
};
1011

11-
let RELEVANT_KEYWORDS: string[];
12-
let graphQLTagPluckOptions: GraphQLTagPluckOptions;
13-
1412
const blocksMap = new Map<string, Block[]>();
1513

14+
let onDiskConfig: GraphQLConfig;
15+
1616
export const processor: Linter.Processor<Block | string> = {
1717
supportsAutofix: true,
1818
preprocess(code, filePath) {
19-
if (!RELEVANT_KEYWORDS) {
20-
graphQLTagPluckOptions = loadGraphQLConfig().getDefault()?.extensions?.graphqlTagPluck;
19+
onDiskConfig ||= loadOnDiskGraphQLConfig(filePath);
20+
const graphQLTagPluckOptions: GraphQLTagPluckOptions =
21+
onDiskConfig?.getProjectForFile?.(filePath)?.extensions?.graphqlTagPluck;
22+
const {
23+
modules = [],
24+
globalGqlIdentifierName = ['gql', 'graphql'],
25+
gqlMagicComment = 'GraphQL',
26+
} = graphQLTagPluckOptions || {};
2127

22-
const {
23-
modules = [],
24-
globalGqlIdentifierName = ['gql', 'graphql'],
25-
gqlMagicComment = 'GraphQL',
26-
} = graphQLTagPluckOptions || {};
27-
28-
RELEVANT_KEYWORDS = [
29-
...new Set(
30-
[
31-
...modules.map(({ identifier }) => identifier),
32-
...asArray(globalGqlIdentifierName),
33-
gqlMagicComment,
34-
].filter(Boolean)
35-
),
36-
];
37-
}
28+
const RELEVANT_KEYWORDS: string[] = [
29+
...new Set(
30+
[
31+
...modules.map(({ identifier }) => identifier),
32+
...asArray(globalGqlIdentifierName),
33+
gqlMagicComment,
34+
].filter(Boolean)
35+
),
36+
];
3837

3938
if (RELEVANT_KEYWORDS.every(keyword => !code.includes(keyword))) {
4039
return [code];

packages/plugin/tests/processor-with-graphql-config.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Block, processor } from '../src/processor';
22

33
jest.mock('../src/graphql-config', () => ({
4-
loadGraphQLConfig: jest.fn(() => ({
5-
getDefault: () => ({
4+
loadOnDiskGraphQLConfig: jest.fn(() => ({
5+
getProjectForFile: () => ({
66
extensions: {
77
graphqlTagPluck: {
88
modules: [{ name: 'custom-gql-tag', identifier: 'custom' }],

0 commit comments

Comments
 (0)