UNPKG

3.42 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 *
8 * @format
9 */
10'use strict';
11
12var cache = new (require("./RelayCompilerCache"))('RelayFindGraphQLTags', 'v1');
13
14function memoizedFind(tagFinder, text, baseDir, file, options) {
15 !file.exists ? process.env.NODE_ENV !== "production" ? invariant(false, 'RelayFindGraphQLTags: Called with non-existent file `%s`', file.relPath) : invariant(false) : void 0;
16 return cache.getOrCompute(file.hash + (options.validateNames ? '1' : '0'), find.bind(null, tagFinder, text, require("path").join(baseDir, file.relPath), options));
17}
18
19function find(tagFinder, text, absPath, _ref) {
20 var validateNames = _ref.validateNames;
21 var tags = tagFinder(text, absPath);
22
23 if (validateNames) {
24 var moduleName = require("./getModuleName")(absPath);
25
26 tags.forEach(function (tag) {
27 return validateTemplate(tag, moduleName, absPath);
28 });
29 }
30
31 return tags.map(function (tag) {
32 return tag.template;
33 });
34}
35
36function validateTemplate(_ref2, moduleName, filePath) {
37 var template = _ref2.template,
38 keyName = _ref2.keyName,
39 sourceLocationOffset = _ref2.sourceLocationOffset;
40
41 var ast = require("graphql").parse(new (require("graphql").Source)(template, filePath, sourceLocationOffset));
42
43 ast.definitions.forEach(function (def) {
44 !def.name ? process.env.NODE_ENV !== "production" ? invariant(false, 'RelayFindGraphQLTags: In module `%s`, a definition of kind `%s` requires a name.', moduleName, def.kind) : invariant(false) : void 0;
45 var definitionName = def.name.value;
46
47 if (def.kind === 'OperationDefinition') {
48 var operationNameParts = definitionName.match(/^(.*)(Mutation|Query|Subscription)$/);
49 !(operationNameParts && definitionName.startsWith(moduleName)) ? process.env.NODE_ENV !== "production" ? invariant(false, 'RelayFindGraphQLTags: Operation names in graphql tags must be prefixed ' + 'with the module name and end in "Mutation", "Query", or ' + '"Subscription". Got `%s` in module `%s`.', definitionName, moduleName) : invariant(false) : void 0;
50 } else if (def.kind === 'FragmentDefinition') {
51 if (keyName) {
52 !(definitionName === moduleName + '_' + keyName) ? process.env.NODE_ENV !== "production" ? invariant(false, 'RelayFindGraphQLTags: Container fragment names must be ' + '`<ModuleName>_<propName>`. Got `%s`, expected `%s`.', definitionName, moduleName + '_' + keyName) : invariant(false) : void 0;
53 } else {
54 !definitionName.startsWith(moduleName) ? process.env.NODE_ENV !== "production" ? invariant(false, 'RelayFindGraphQLTags: Fragment names in graphql tags must be prefixed ' + 'with the module name. Got `%s` in module `%s`.', definitionName, moduleName) : invariant(false) : void 0;
55 }
56 }
57 });
58} // TODO: Not sure why this is defined here rather than imported, is it so that it doesn’t get stripped in prod?
59
60
61function invariant(condition, msg) {
62 if (!condition) {
63 for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
64 args[_key - 2] = arguments[_key];
65 }
66
67 throw new Error(require("util").format.apply(require("util"), [msg].concat(args)));
68 }
69}
70
71module.exports = {
72 find: find,
73 // Exported for testing only.
74 memoizedFind: memoizedFind
75};
\No newline at end of file