Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/empty-horses-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': patch
---

fix issue #2276
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fragment BazFields on Bar {
id
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Imports could have extra whitespace and double/single quotes

# import BazFields from ".\other-path\baz-fragment.gql"

query {
baz {
...BazFields
}
}
4 changes: 2 additions & 2 deletions packages/plugin/src/documents.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve } from 'node:path';
import path from 'node:path';
import debugFactory from 'debug';
import fg from 'fast-glob';
import { GraphQLProjectConfig } from 'graphql-config';
Expand All @@ -21,7 +21,7 @@ const handleVirtualPath = (documents: Source[]): Source[] => {
const index = (filepathMap[location] += 1);
return {
...source,
location: resolve(location, `${index}_document.graphql`),
location: path.resolve(location, `${index}_document.graphql`),
};
});
};
Expand Down
18 changes: 17 additions & 1 deletion packages/plugin/src/rules/require-import-fragment/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@ import { CWD } from '@/utils.js';
import { ParserOptionsForTests, ruleTester } from '../../../__tests__/test-utils.js';
import { rule } from './index.js';

function withMocks({ name, filename, errors }: { name: string; filename: string; errors?: any }) {
function withMocks({
name,
filename,
errors,
only = false,
}: {
name: string;
filename: string;
errors?: any;
only?: boolean;
}) {
return {
name,
filename,
Expand All @@ -14,10 +24,12 @@ function withMocks({ name, filename, errors }: { name: string; filename: string;
filename,
join(CWD, '__tests__', 'mocks', 'import-fragments', 'foo-fragment.gql'),
join(CWD, '__tests__', 'mocks', 'import-fragments', 'bar-fragment.gql'),
join(CWD, '__tests__', 'mocks', 'import-fragments', 'other-path', 'baz-fragment.gql'),
],
},
} satisfies ParserOptionsForTests,
errors,
only,
};
}

Expand All @@ -35,6 +47,10 @@ ruleTester.run('require-import-fragment', rule, {
name: 'should not report fragments from the same file',
filename: join(CWD, '__tests__', 'mocks', 'import-fragments', 'same-file.gql'),
}),
withMocks({
name: 'should not report with correct relative path import',
filename: join(CWD, '__tests__', 'mocks', 'import-fragments', 'valid-baz-query.gql'),
}),
],
invalid: [
withMocks({
Expand Down
7 changes: 5 additions & 2 deletions packages/plugin/src/rules/require-import-fragment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,16 @@ export const rule: GraphQLESLintRule = {
const extractedImportPath = comment.value.match(/(["'])((?:\1|.)*?)\1/)?.[2];
if (!extractedImportPath) continue;

const importPath = path.join(path.dirname(filePath), extractedImportPath);
const importPath = path.join(
path.dirname(filePath),
extractedImportPath.replaceAll(/[/\\]/g, path.sep),
);

const hasInSiblings = fragmentsFromSiblings.some(
source => source.filePath === importPath,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previously you added path.resolve(source.filePath) but why?

source.filePath comes from graphql-config and contains slashes according to user's os

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought so too, but when I printed out the filePath in fragmentsFromSiblings by adding

console.log(fragmentsFromSiblings.map(x=>x.filePath));

I saw this syntax. On Windows.

C:/Dev/my-project/client/logic/experiments/experimentFragment.graphql

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange because I added console log on ci and get backslash on windows

Copy link
Contributor

@dimaMachina dimaMachina Nov 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here I commented d9f5b4e and got

IMG_2887

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder maybe it happens when you specify glob syntax for documents? e.g. **/*.gql

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I was right, specifying glob pattern returns forward slash always

image

);
if (hasInSiblings) return;
}

const fragmentInSameFile = fragmentsFromSiblings.some(
source => source.filePath === filePath,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same, source.filePath comes from graphql-config and contains slashes according to user's OS, and filePath is ESLint's context.filename which also contains slashed according user's OS

);
Expand Down
3 changes: 1 addition & 2 deletions packages/plugin/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ export const logger = {
export const slash = (path: string): string => path.replaceAll('\\', '/');

// Match slash or backslash for Windows
// eslint-disable-next-line no-useless-escape
export const VIRTUAL_DOCUMENT_REGEX = /[\/\\]\d+_document.graphql$/;
export const VIRTUAL_DOCUMENT_REGEX = /[/\\]\d+_document.graphql$/;

export const CWD = process.cwd();

Expand Down