create()

in src/rule-graphql-naming.js [98:178]


  create(context) {
    if (!shouldLint(context)) {
      return {};
    }
    return {
      TaggedTemplateExpression(node) {
        const ast = getGraphQLAST(node);
        if (!ast) {
          return;
        }

        ast.definitions.forEach(definition => {
          switch (definition.kind) {
            case 'OperationDefinition': {
              const moduleName = getModuleName(context.getFilename());
              const name = definition.name;
              if (!name) {
                return;
              }
              const operationName = name.value;

              if (operationName.indexOf(moduleName) !== 0) {
                context.report({
                  message:
                    'Operations should start with the module name. ' +
                    'Expected prefix `{{expected}}`, got `{{actual}}`.',
                  data: {
                    expected: moduleName,
                    actual: operationName
                  },
                  loc: getLoc(context, node, name)
                });
              }
              break;
            }
            default:
          }
        });
      },
      CallExpression(node) {
        if (!isCreateContainerCall(node)) {
          return;
        }
        const fragments = node.arguments[1];
        if (fragments.type === 'ObjectExpression') {
          fragments.properties.forEach(property => {
            if (
              property.type === 'Property' &&
              property.key.type === 'Identifier' &&
              property.computed === false &&
              property.value.type === 'TaggedTemplateExpression'
            ) {
              if (!isGraphQLTag(property.value.tag)) {
                context.report({
                  node: property.value.tag,
                  message:
                    '`{{callee}}` expects GraphQL to be tagged with ' +
                    'graphql`...`.',
                  data: {
                    callee: calleeToString(node.callee)
                  }
                });
                return;
              }
              validateTemplate(context, property.value, property.key.name);
            } else {
              context.report({
                node: property,
                message:
                  '`{{callee}}` expects fragment definitions to be ' +
                  '`key: graphql`.',
                data: {
                  callee: calleeToString(node.callee)
                }
              });
            }
          });
        }
      }
    };
  }