|
| 1 | +/** |
| 2 | + * Copyright (c) 2013-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +const graphql = require('graphql'); |
| 13 | + |
| 14 | +function getGraphQLTagName(tag) { |
| 15 | + if (tag.type === 'Identifier' && tag.name === 'graphql') { |
| 16 | + return 'graphql'; |
| 17 | + } else if ( |
| 18 | + tag.type === 'MemberExpression' && |
| 19 | + tag.object.type === 'Identifier' && |
| 20 | + tag.object.name === 'graphql' && |
| 21 | + tag.property.type === 'Identifier' && |
| 22 | + tag.property.name === 'experimental' |
| 23 | + ) { |
| 24 | + return 'graphql.experimental'; |
| 25 | + } else { |
| 26 | + return null; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +function getGraphQLAST(taggedTemplateExpression) { |
| 31 | + if (!getGraphQLTagName(taggedTemplateExpression.tag)) { |
| 32 | + return null; |
| 33 | + } |
| 34 | + if (taggedTemplateExpression.quasi.quasis.length !== 1) { |
| 35 | + // has substitutions, covered by graphql-syntax rule |
| 36 | + return null; |
| 37 | + } |
| 38 | + const quasi = taggedTemplateExpression.quasi.quasis[0]; |
| 39 | + try { |
| 40 | + return graphql.parse(quasi.value.cooked); |
| 41 | + } catch (error) { |
| 42 | + // Invalid syntax, covered by graphql-syntax rule |
| 43 | + return null; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +const MODULE_NAME_REGEX = /(?:\/|^)(\w+)(?:\.[\w\.]+|\/index\.\w+)$/; |
| 48 | +function getModuleName(context) { |
| 49 | + const match = context.getFilename().match(MODULE_NAME_REGEX); |
| 50 | + if (match) { |
| 51 | + return match[1]; |
| 52 | + } |
| 53 | + return null; |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Returns a loc object for error reporting. |
| 58 | + */ |
| 59 | +function getLoc(context, templateNode, graphQLNode) { |
| 60 | + const [start, end] = getRange(context, templateNode, graphQLNode); |
| 61 | + return { |
| 62 | + start: context.getSourceCode().getLocFromIndex(start), |
| 63 | + end: context.getSourceCode().getLocFromIndex(end), |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Returns a range object for auto fixers. |
| 69 | + */ |
| 70 | +function getRange(context, templateNode, graphQLNode) { |
| 71 | + const graphQLStart = templateNode.quasi.quasis[0].start; |
| 72 | + return [ |
| 73 | + graphQLStart + graphQLNode.loc.start, |
| 74 | + graphQLStart + graphQLNode.loc.end, |
| 75 | + ]; |
| 76 | +} |
| 77 | + |
| 78 | +const CREATE_CONTAINER_FUNCTIONS = new Set([ |
| 79 | + 'createFragmentContainer', |
| 80 | + 'createPaginationContainer', |
| 81 | + 'createRefetchContainer', |
| 82 | +]); |
| 83 | + |
| 84 | +function isCreateContainerCall(node) { |
| 85 | + const callee = node.callee; |
| 86 | + // prettier-ignore |
| 87 | + return ( |
| 88 | + callee.type === 'Identifier' && |
| 89 | + CREATE_CONTAINER_FUNCTIONS.has(callee.name) |
| 90 | + ) || ( |
| 91 | + callee.kind === 'MemberExpression' && |
| 92 | + callee.object.type === 'Identifier' && |
| 93 | + // Relay, relay, RelayCompat, etc. |
| 94 | + /relay/i.test(callee.object.value) && |
| 95 | + callee.property.type === 'Identifier' && |
| 96 | + CREATE_CONTAINER_FUNCTIONS.has(callee.property.name) |
| 97 | + ); |
| 98 | +} |
| 99 | + |
| 100 | +function calleeToString(callee) { |
| 101 | + if (callee.type) { |
| 102 | + return callee.name; |
| 103 | + } |
| 104 | + if ( |
| 105 | + callee.kind === 'MemberExpression' && |
| 106 | + callee.object.type === 'Identifier' && |
| 107 | + callee.property.type === 'Identifier' |
| 108 | + ) { |
| 109 | + return callee.object.value + '.' + callee.property.name; |
| 110 | + } |
| 111 | + return null; |
| 112 | +} |
| 113 | + |
| 114 | +function validateTemplate(context, taggedTemplateExpression, keyName) { |
| 115 | + const ast = getGraphQLAST(taggedTemplateExpression); |
| 116 | + if (!ast) { |
| 117 | + return; |
| 118 | + } |
| 119 | + const moduleName = getModuleName(context); |
| 120 | + ast.definitions.forEach(def => { |
| 121 | + if (!def.name) { |
| 122 | + // no name, covered by graphql-naming/TaggedTemplateExpression |
| 123 | + return; |
| 124 | + } |
| 125 | + const definitionName = def.name.value; |
| 126 | + if (def.kind === 'FragmentDefinition') { |
| 127 | + if (keyName) { |
| 128 | + const expectedName = moduleName + '_' + keyName; |
| 129 | + if (definitionName !== expectedName) { |
| 130 | + context.report({ |
| 131 | + loc: getLoc(context, taggedTemplateExpression, def.name), |
| 132 | + message: |
| 133 | + 'Container fragment names must be `<ModuleName>_<propName>`. Got `{{actual}}`, expected `{{expected}}`.', |
| 134 | + data: { |
| 135 | + actual: definitionName, |
| 136 | + expected: expectedName, |
| 137 | + }, |
| 138 | + fix: fixer => |
| 139 | + fixer.replaceTextRange( |
| 140 | + getRange(context, taggedTemplateExpression, def.name), |
| 141 | + expectedName |
| 142 | + ), |
| 143 | + }); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + }); |
| 148 | +} |
| 149 | + |
| 150 | +module.exports.rules = { |
| 151 | + 'graphql-syntax': { |
| 152 | + meta: { |
| 153 | + docs: { |
| 154 | + description: |
| 155 | + 'Validates the syntax of all graphql`...` and ' + |
| 156 | + 'graphql.experimental`...` templates.', |
| 157 | + }, |
| 158 | + }, |
| 159 | + create(context) { |
| 160 | + return { |
| 161 | + TaggedTemplateExpression(node) { |
| 162 | + if (!getGraphQLTagName(node.tag)) { |
| 163 | + return; |
| 164 | + } |
| 165 | + const quasi = node.quasi.quasis[0]; |
| 166 | + if (node.quasi.quasis.length !== 1) { |
| 167 | + context.report({ |
| 168 | + node: node, |
| 169 | + message: |
| 170 | + 'graphql tagged templates do not support ${...} substitutions.', |
| 171 | + }); |
| 172 | + return; |
| 173 | + } |
| 174 | + try { |
| 175 | + const ast = graphql.parse(quasi.value.cooked); |
| 176 | + ast.definitions.forEach(definition => { |
| 177 | + if (!definition.name) { |
| 178 | + context.report({ |
| 179 | + message: 'Operations in graphql tags require a name.', |
| 180 | + loc: getLoc(context, node, definition), |
| 181 | + }); |
| 182 | + } |
| 183 | + }); |
| 184 | + } catch (error) { |
| 185 | + context.report({ |
| 186 | + node: node, |
| 187 | + message: '{{message}}', |
| 188 | + data: {message: error.message}, |
| 189 | + }); |
| 190 | + } |
| 191 | + }, |
| 192 | + }; |
| 193 | + }, |
| 194 | + }, |
| 195 | + 'graphql-naming': { |
| 196 | + meta: { |
| 197 | + fixable: 'code', |
| 198 | + docs: { |
| 199 | + description: 'Validates naming conventions of graphql tags', |
| 200 | + }, |
| 201 | + }, |
| 202 | + create(context) { |
| 203 | + return { |
| 204 | + TaggedTemplateExpression(node) { |
| 205 | + const ast = getGraphQLAST(node); |
| 206 | + if (!ast) { |
| 207 | + return; |
| 208 | + } |
| 209 | + |
| 210 | + ast.definitions.forEach(definition => { |
| 211 | + switch (definition.kind) { |
| 212 | + case 'OperationDefinition': |
| 213 | + const moduleName = getModuleName(context); |
| 214 | + const name = definition.name; |
| 215 | + if (!name) { |
| 216 | + return; |
| 217 | + } |
| 218 | + const operationName = name.value; |
| 219 | + |
| 220 | + if (operationName.indexOf(moduleName) !== 0) { |
| 221 | + context.report({ |
| 222 | + message: |
| 223 | + 'Operations should start with the module name. Expected prefix `{{expected}}`, got `{{actual}}`.', |
| 224 | + data: { |
| 225 | + expected: moduleName, |
| 226 | + actual: operationName, |
| 227 | + }, |
| 228 | + loc: getLoc(context, node, name), |
| 229 | + }); |
| 230 | + } |
| 231 | + break; |
| 232 | + default: |
| 233 | + } |
| 234 | + }); |
| 235 | + }, |
| 236 | + CallExpression(node) { |
| 237 | + if (!isCreateContainerCall(node)) { |
| 238 | + return; |
| 239 | + } |
| 240 | + const fragments = node.arguments[1]; |
| 241 | + if (fragments.type === 'ObjectExpression') { |
| 242 | + fragments.properties.forEach(property => { |
| 243 | + if ( |
| 244 | + property.type === 'Property' && |
| 245 | + property.key.type === 'Identifier' && |
| 246 | + property.computed === false && |
| 247 | + property.value.type === 'TaggedTemplateExpression' |
| 248 | + ) { |
| 249 | + const tagName = getGraphQLTagName(property.value.tag); |
| 250 | + |
| 251 | + if (!tagName) { |
| 252 | + context.report({ |
| 253 | + node: property.value.tag, |
| 254 | + message: |
| 255 | + '`{{callee}}` expects GraphQL to be tagged with graphql`...` or graphql.experimental`...`.', |
| 256 | + data: { |
| 257 | + callee: calleeToString(node.callee), |
| 258 | + }, |
| 259 | + }); |
| 260 | + return; |
| 261 | + } |
| 262 | + validateTemplate(context, property.value, property.key.name); |
| 263 | + } else { |
| 264 | + context.report({ |
| 265 | + node: property, |
| 266 | + message: |
| 267 | + '`{{callee}}` expects fragment definitions to be `key: graphql`.', |
| 268 | + data: { |
| 269 | + callee: calleeToString(node.callee), |
| 270 | + }, |
| 271 | + }); |
| 272 | + } |
| 273 | + }); |
| 274 | + } |
| 275 | + }, |
| 276 | + }; |
| 277 | + }, |
| 278 | + }, |
| 279 | +}; |
0 commit comments