Skip to content

Suggest Code Style & Refactoring #5

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

Open
wants to merge 2 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/gqlRules/gqlOperationName/gqlOperationName.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isGqlFile } from "../utils";
import { isGqlFile, isGqlTemplateElement } from "../utils";
import { isQuery, isMutation } from "../utils/isGQLOperation";
import { relativePathToFile } from "../../utils";
import { operationName } from "../utils/operationName";
Expand Down Expand Up @@ -37,10 +37,6 @@ const create = context => {
const isGqlObjectFile = isGqlFile(context);
const { namespaceOperationPrefix, namespaceIgnoreList } = context.options[0];

const isGqlTemplateElement = node => {
return node.tag && node.tag.name === "gql";
};

const getNamespaceAndPrefix = () => {
const pathToFile = relativePathToFile(context);
const relativeJsPath = pathToFile.replace("app/javascript/", "");
Expand Down
59 changes: 25 additions & 34 deletions src/gqlRules/gqlVariableNameMatch/gqlVariableNameMatch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isGqlFile } from "../utils";
import { isGqlFile, isGqlTemplateElement } from "../utils";
import { isQuery } from "../utils/isGQLOperation";
import { relativePathToFile } from "../../utils";
import { operationName } from "../utils/operationName";
Expand Down Expand Up @@ -29,44 +29,35 @@ const meta = {
],
};

const create = context => {
const isGqlObjectFile = isGqlFile(context);
const { namespaceIgnoreList } = context.options[0];

const isGqlTemplateElement = node => {
return node.tag && node.tag.name === "gql";
};

const isInIgnoreList = () => {
const pathToFile = relativePathToFile(context);
return namespaceIgnoreList.some(ignoredNamespace => pathToFile.startsWith(ignoredNamespace));
};

const isOperationNameAndVariableNameSame = (gqlOperationText, node) => {
const operationType = isQuery(gqlOperationText) ? "Query" : "Mutation";
const gqlOperationName = operationName(gqlOperationText, operationType);
const { id } = node;
const variableName = id.name;
if (isInIgnoreList()) {
return;
}
const isInIgnoreList = (ignoreList, pathToFile) =>
ignoreList.some(name => pathToFile.startsWith(name));

const isOperationNameValid = !!gqlOperationName.match(variableName);
const isOperationNameAndVariableNameSame = (gqlOperationText, node) => {
const operationType = isQuery(gqlOperationText) ? "Query" : "Mutation";
const gqlOperationName = operationName(gqlOperationText, operationType);
const variableName = node.id.name;
return !!gqlOperationName.match(variableName);
};

if (!isOperationNameValid) {
context.report({
node: node,
message: `The variable name "${variableName}" should match with the GQL operation name, please use "${gqlOperationName}"`,
});
}
};
const create = context => {
const pathToFile = relativePathToFile(context);
const isGqlObjectFile = isGqlFile(context);
const [{ namespaceIgnoreList }] = context.options;

const VariableDeclarator = node => {
if (!isGqlObjectFile || !isGqlTemplateElement(node.init)) return;
const { init } = node;
const templateElementNode = init.quasi;
if (!isGqlObjectFile) return;
if (!isGqlTemplateElement(node.init)) return;
if (isInIgnoreList(namespaceIgnoreList, pathToFile)) return;

const templateElementNode = node.init.quasi;
const gqlOperationText = sanitizeGqlOperationText(templateElementNode, context);
isOperationNameAndVariableNameSame(gqlOperationText, node);
const isOperationNameValid = isOperationNameAndVariableNameSame(gqlOperationText, node);
if (isOperationNameValid) return;

context.report({
node: node,
message: `The variable name "${variableName}" should match with the GQL operation name, please use "${gqlOperationName}"`,
});
};

return {
Expand Down
1 change: 1 addition & 0 deletions src/gqlRules/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from "./isGqlFile";
export * from "./isGQLOperation";
export * from "./operationName";
export * from "./sanitizeGqlOperationText";
export * from "./isGqlTemplateElement";
1 change: 1 addition & 0 deletions src/gqlRules/utils/isGqlTemplateElement/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { isGqlTemplateElement } from "./isGqlTemplateElement";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const isGqlTemplateElement = node => node.tag && node.tag.name === "gql";

export { isGqlTemplateElement };