UNPKG

3.77 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 * strict-local
8 * @format
9 */
10'use strict';
11
12var _objectSpread2 = require("@babel/runtime/helpers/interopRequireDefault")(require("@babel/runtime/helpers/objectSpread"));
13
14/**
15 * Refines the argument definitions for operations to remove unused arguments
16 * due to statically pruned conditional branches (e.g. because of overriding
17 * a variable used in `@include()` to be false) and checks that all variables
18 * referenced in each operation are defined. Reports aggregated errors for all
19 * operations.
20 */
21function stripUnusedVariablesTransform(context) {
22 var contextWithUsedArguments = require("./inferRootArgumentDefinitions")(context);
23
24 var nextContext = context;
25
26 var errors = require("./RelayCompilerError").eachWithErrors(context.documents(), function (node) {
27 if (node.kind !== 'Root') {
28 return;
29 }
30
31 var nodeWithUsedArguments = contextWithUsedArguments.getRoot(node.name);
32 var definedArguments = argumentDefinitionsToMap(node.argumentDefinitions);
33 var usedArguments = argumentDefinitionsToMap(nodeWithUsedArguments.argumentDefinitions); // All used arguments must be defined
34
35 var undefinedVariables = [];
36 var _iteratorNormalCompletion = true;
37 var _didIteratorError = false;
38 var _iteratorError = undefined;
39
40 try {
41 for (var _iterator = usedArguments.values()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
42 var argDef = _step.value;
43
44 if (!definedArguments.has(argDef.name)) {
45 undefinedVariables.push(argDef);
46 }
47 }
48 } catch (err) {
49 _didIteratorError = true;
50 _iteratorError = err;
51 } finally {
52 try {
53 if (!_iteratorNormalCompletion && _iterator["return"] != null) {
54 _iterator["return"]();
55 }
56 } finally {
57 if (_didIteratorError) {
58 throw _iteratorError;
59 }
60 }
61 }
62
63 if (undefinedVariables.length !== 0) {
64 throw require("./RelayCompilerError").createUserError("Operation '".concat(node.name, "' references undefined variable(s):\n").concat(undefinedVariables.map(function (argDef) {
65 return "- $".concat(argDef.name, ": ").concat(String(argDef.type));
66 }).join('\n'), "."), undefinedVariables.map(function (argDef) {
67 return argDef.loc;
68 }));
69 } // Remove unused argument definitions
70
71
72 nextContext = nextContext.replace((0, _objectSpread2["default"])({}, node, {
73 argumentDefinitions: node.argumentDefinitions.filter(function (argDef) {
74 return usedArguments.has(argDef.name);
75 })
76 }));
77 });
78
79 if (errors != null && errors.length !== 0) {
80 throw require("./RelayCompilerError").createCombinedError(errors);
81 }
82
83 return nextContext;
84}
85
86function argumentDefinitionsToMap(argDefs) {
87 var map = new Map();
88 var _iteratorNormalCompletion2 = true;
89 var _didIteratorError2 = false;
90 var _iteratorError2 = undefined;
91
92 try {
93 for (var _iterator2 = argDefs[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
94 var argDef = _step2.value;
95 map.set(argDef.name, argDef);
96 }
97 } catch (err) {
98 _didIteratorError2 = true;
99 _iteratorError2 = err;
100 } finally {
101 try {
102 if (!_iteratorNormalCompletion2 && _iterator2["return"] != null) {
103 _iterator2["return"]();
104 }
105 } finally {
106 if (_didIteratorError2) {
107 throw _iteratorError2;
108 }
109 }
110 }
111
112 return map;
113}
114
115module.exports = {
116 transform: stripUnusedVariablesTransform
117};
\No newline at end of file