diff --git a/packages/plugin/src/parser.ts b/packages/plugin/src/parser.ts index b3186dc8af7a..a4ce56440f81 100644 --- a/packages/plugin/src/parser.ts +++ b/packages/plugin/src/parser.ts @@ -47,7 +47,13 @@ export function parseForESLint(code: string, options: ParserOptions): GraphQLESL if (typeof window === 'undefined') { const gqlConfig = loadGraphQLConfig(options); const realFilepath = filePath.replace(VIRTUAL_DOCUMENT_REGEX, ''); - project = gqlConfig.getProjectForFile(realFilepath); + const projectMatch = filePath.match(VIRTUAL_DOCUMENT_REGEX); + if (projectMatch) { + const projectName = projectMatch[1]; + project = gqlConfig.projects[projectName]; + } else { + project = gqlConfig.getProjectForFile(realFilepath); + } documents = getDocuments(project); } else { documents = [ diff --git a/packages/plugin/src/processor.ts b/packages/plugin/src/processor.ts index acc3787be902..16cd2de21dbb 100644 --- a/packages/plugin/src/processor.ts +++ b/packages/plugin/src/processor.ts @@ -30,44 +30,54 @@ export const processor = { } let keywords: ReadonlyArray = RELEVANT_KEYWORDS; - const pluckConfig: GraphQLTagPluckOptions = - onDiskConfig?.getProjectForFile(filePath).extensions.pluckConfig; - - if (pluckConfig) { - const { - modules = [], - globalGqlIdentifierName = ['gql', 'graphql'], - gqlMagicComment = 'GraphQL', - } = pluckConfig; - - keywords = [ - ...new Set( - [ - ...modules.map(({ identifier }) => identifier), - ...asArray(globalGqlIdentifierName), - gqlMagicComment, - ].filter(truthy), - ), - ]; - } - - if (keywords.every(keyword => !code.includes(keyword))) { - return [code]; - } try { - const sources = gqlPluckFromCodeStringSync(filePath, code, { - skipIndent: true, - ...pluckConfig, - }); - - const blocks: Block[] = sources.map(item => ({ - filename: 'document.graphql', - text: item.body, - lineOffset: item.locationOffset.line - 1, - // @ts-expect-error -- `index` field exist but show ts error - offset: item.locationOffset.index + 1, - })); + const blocks: Block[] = []; + + // TODO: generate a list of matching projects to consider + const projects = onDiskConfig?.projects; + + for (const [projectName, project] of Object.entries(projects)) { + const pluckConfig: GraphQLTagPluckOptions = project.extensions.pluckConfig; + + if (pluckConfig) { + const { + modules = [], + globalGqlIdentifierName = ['gql', 'graphql'], + gqlMagicComment = 'GraphQL', + } = pluckConfig; + + keywords = [ + ...new Set( + [ + ...modules.map(({ identifier }) => identifier), + ...asArray(globalGqlIdentifierName), + gqlMagicComment, + ].filter(truthy), + ), + ]; + } + + if (keywords.every(keyword => !code.includes(keyword))) { + continue; + } + + const sources = gqlPluckFromCodeStringSync(filePath, code, { + skipIndent: true, + ...pluckConfig, + }); + + for (const item of sources) { + blocks.push({ + filename: `${projectName || 'unknown'}_document.graphql`, + text: item.body, + lineOffset: item.locationOffset.line - 1, + // @ts-expect-error -- `index` field exist but show ts error + offset: item.locationOffset.index + 1, + }); + } + } + blocksMap.set(filePath, blocks); return [...blocks, code /* source code must be provided and be last */]; diff --git a/packages/plugin/src/utils.ts b/packages/plugin/src/utils.ts index db2906049684..19d576e1866e 100644 --- a/packages/plugin/src/utils.ts +++ b/packages/plugin/src/utils.ts @@ -48,7 +48,7 @@ export const logger = { export const normalizePath = (path: string): string => (path || '').replace(/\\/g, '/'); -export const VIRTUAL_DOCUMENT_REGEX = /\/\d+_document.graphql$/; +export const VIRTUAL_DOCUMENT_REGEX = /\/\d+_([a-zA-Z0-9]*)_document.graphql$/; export const CWD = process.cwd();