From be3d8e3ed7ea243ca1e3254d7aa1dfbdfbf59d83 Mon Sep 17 00:00:00 2001 From: Valentyn Patsera Date: Fri, 10 Mar 2023 14:28:31 -0500 Subject: [PATCH 1/2] shared util --- src/gqlRules/gqlOperationName/gqlOperationName.js | 6 +----- src/gqlRules/gqlVariableNameMatch/gqlVariableNameMatch.js | 8 ++------ src/gqlRules/utils/index.js | 1 + src/gqlRules/utils/isGqlTemplateElement/index.js | 1 + .../utils/isGqlTemplateElement/isGqlTemplateElement.js | 3 +++ 5 files changed, 8 insertions(+), 11 deletions(-) create mode 100644 src/gqlRules/utils/isGqlTemplateElement/index.js create mode 100644 src/gqlRules/utils/isGqlTemplateElement/isGqlTemplateElement.js diff --git a/src/gqlRules/gqlOperationName/gqlOperationName.js b/src/gqlRules/gqlOperationName/gqlOperationName.js index 9972cce..47beecf 100644 --- a/src/gqlRules/gqlOperationName/gqlOperationName.js +++ b/src/gqlRules/gqlOperationName/gqlOperationName.js @@ -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"; @@ -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/", ""); diff --git a/src/gqlRules/gqlVariableNameMatch/gqlVariableNameMatch.js b/src/gqlRules/gqlVariableNameMatch/gqlVariableNameMatch.js index fe703a6..117299d 100644 --- a/src/gqlRules/gqlVariableNameMatch/gqlVariableNameMatch.js +++ b/src/gqlRules/gqlVariableNameMatch/gqlVariableNameMatch.js @@ -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"; @@ -31,11 +31,7 @@ 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 [{ namespaceIgnoreList }] = context.options; const isInIgnoreList = () => { const pathToFile = relativePathToFile(context); diff --git a/src/gqlRules/utils/index.js b/src/gqlRules/utils/index.js index fa5eddc..17c90f0 100644 --- a/src/gqlRules/utils/index.js +++ b/src/gqlRules/utils/index.js @@ -3,3 +3,4 @@ export * from "./isGqlFile"; export * from "./isGQLOperation"; export * from "./operationName"; export * from "./sanitizeGqlOperationText"; +export * from "./isGqlTemplateElement"; diff --git a/src/gqlRules/utils/isGqlTemplateElement/index.js b/src/gqlRules/utils/isGqlTemplateElement/index.js new file mode 100644 index 0000000..d0efe05 --- /dev/null +++ b/src/gqlRules/utils/isGqlTemplateElement/index.js @@ -0,0 +1 @@ +export { isGqlTemplateElement } from "./isGqlTemplateElement"; diff --git a/src/gqlRules/utils/isGqlTemplateElement/isGqlTemplateElement.js b/src/gqlRules/utils/isGqlTemplateElement/isGqlTemplateElement.js new file mode 100644 index 0000000..5512e30 --- /dev/null +++ b/src/gqlRules/utils/isGqlTemplateElement/isGqlTemplateElement.js @@ -0,0 +1,3 @@ +const isGqlTemplateElement = node => node.tag && node.tag.name === "gql"; + +export { isGqlTemplateElement }; From 2790c37f34767f531c96902f058c5c0236723b29 Mon Sep 17 00:00:00 2001 From: Valentyn Patsera Date: Sat, 11 Mar 2023 12:03:32 -0500 Subject: [PATCH 2/2] refactor gql variable name --- .../gqlVariableNameMatch.js | 51 +++++++++---------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/src/gqlRules/gqlVariableNameMatch/gqlVariableNameMatch.js b/src/gqlRules/gqlVariableNameMatch/gqlVariableNameMatch.js index 117299d..3c14e2e 100644 --- a/src/gqlRules/gqlVariableNameMatch/gqlVariableNameMatch.js +++ b/src/gqlRules/gqlVariableNameMatch/gqlVariableNameMatch.js @@ -29,40 +29,35 @@ const meta = { ], }; +const isInIgnoreList = (ignoreList, pathToFile) => + ignoreList.some(name => pathToFile.startsWith(name)); + +const isOperationNameAndVariableNameSame = (gqlOperationText, node) => { + const operationType = isQuery(gqlOperationText) ? "Query" : "Mutation"; + const gqlOperationName = operationName(gqlOperationText, operationType); + const variableName = node.id.name; + return !!gqlOperationName.match(variableName); +}; + const create = context => { + const pathToFile = relativePathToFile(context); const isGqlObjectFile = isGqlFile(context); const [{ namespaceIgnoreList }] = context.options; - 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 isOperationNameValid = !!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 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 {