Skip to content

Commit

Permalink
wip: multi project files
Browse files Browse the repository at this point in the history
  • Loading branch information
znarf committed Dec 7, 2023
1 parent d4c236f commit a887c3d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 38 deletions.
4 changes: 3 additions & 1 deletion packages/plugin/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ 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);
const projectName = projectMatch ? projectMatch[1] : 'unknown';
project = gqlConfig.projects[projectName] ?? gqlConfig.getProjectForFile(realFilepath);
documents = getDocuments(project);
} else {
documents = [
Expand Down
82 changes: 46 additions & 36 deletions packages/plugin/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,44 +30,54 @@ export const processor = {
}

let keywords: ReadonlyArray<string> = 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 */];
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit a887c3d

Please sign in to comment.