Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[must-colocate-fragment-spreads] Handle importing relative index.js files which are direct siblings #104

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/rule-must-colocate-fragment-spreads.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ function getGraphQLFragmentDefinitionName(graphQLAst) {
function rule(context) {
const foundImportedModules = [];
const graphqlLiterals = [];
const contextFilename = context.getFilename();

return {
'Program:exit'(_node) {
Expand Down Expand Up @@ -145,15 +146,23 @@ function rule(context) {

ImportDeclaration(node) {
if (node.importKind === 'value') {
foundImportedModules.push(utils.getModuleName(node.source.value));
const moduleName = utils.getModuleName(
node.source.value,
contextFilename
);
foundImportedModules.push(moduleName);
}
},

ImportExpression(node) {
if (node.source.type === 'Literal') {
// Allow dynamic imports like import(`test/${fileName}`); and (path) => import(path);
// These would have node.source.value undefined
foundImportedModules.push(utils.getModuleName(node.source.value));
const moduleName = utils.getModuleName(
node.source.value,
contextFilename
);
foundImportedModules.push(moduleName);
}
},

Expand All @@ -163,7 +172,8 @@ function rule(context) {
}
const [source] = node.arguments;
if (source && source.type === 'Literal') {
foundImportedModules.push(utils.getModuleName(source.value));
const moduleName = utils.getModuleName(source.value, contextFilename);
foundImportedModules.push(moduleName);
}
},

Expand Down
10 changes: 9 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function getLocFromIndex(sourceCode, index) {
}

// Copied directly from Relay
function getModuleName(filePath) {
function getModuleName(filePath, contextFilename) {
// index.js -> index
// index.js.flow -> index.js
let filename = path.basename(filePath, path.extname(filePath));
Expand All @@ -80,6 +80,14 @@ function getModuleName(filePath) {
let moduleName =
filename === 'index' ? path.basename(path.dirname(filePath)) : filename;

// Special case to handle relative sibling index imports such as:
// import MyComponent from './'
if (moduleName === '.' && contextFilename) {
moduleName = path.basename(
path.dirname(path.join(moduleName, contextFilename))
);
}

// foo-bar -> fooBar
// Relay compatibility mode splits on _, so we can't use that here.
moduleName = moduleName.replace(/[^a-zA-Z0-9]+(\w?)/g, (match, next) =>
Expand Down
21 changes: 20 additions & 1 deletion test/must-colocate-fragment-spreads.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,16 @@ ruleTester.run(
const getOperation = (reference) => {\
return import(reference);\
};\
'
',
{
filename: '/Container/MyComponent/AnotherComponent.js',
code: `
import MyComponent from './';
graphql\`fragment foo on Page {
...MyComponent_foo
}\`;
`
}
],
invalid: [
{
Expand Down Expand Up @@ -152,6 +161,16 @@ ruleTester.run(
}\`;
`,
errors: [unusedFieldsWarning('component_fragment')]
},
{
filename: '/Container/NotMyComponent/AnotherComponent.js',
code: `
import MyComponent from './';
graphql\`fragment foo on Page {
...MyComponent_foo
}\`;
`,
errors: [unusedFieldsWarning('MyComponent_foo')]
}
]
}
Expand Down