babel-schmooze-sprockets 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. checksums.yaml +4 -4
  2. data/bin/console +14 -0
  3. data/bin/export-external-helpers +3 -0
  4. data/bin/setup +6 -0
  5. data/bin/update-npm-deps +8 -0
  6. data/examples/myapp/Gemfile +4 -5
  7. data/examples/myapp/Gemfile.lock +69 -93
  8. data/examples/myapp/app/assets/javascripts/code/hello.es6 +5 -1
  9. data/examples/myapp/app/views/layouts/application.html.erb +1 -0
  10. data/examples/myapp/app/views/site/home.html.erb +0 -1
  11. data/lib/babel-schmooze-sprockets/babel_processor.rb +6 -4
  12. data/lib/babel-schmooze-sprockets/engine.rb +1 -4
  13. data/lib/babel-schmooze-sprockets/version.rb +1 -1
  14. data/node_modules/amdefine/package.json +8 -1
  15. data/node_modules/babel-core/node_modules/babel-runtime/package.json +9 -5
  16. data/node_modules/babel-core/node_modules/babel-traverse/.npmignore +3 -0
  17. data/node_modules/babel-core/node_modules/babel-traverse/README.md +1 -0
  18. data/node_modules/babel-core/node_modules/babel-traverse/lib/cache.js +20 -0
  19. data/node_modules/babel-core/node_modules/babel-traverse/lib/context.js +219 -0
  20. data/node_modules/babel-core/node_modules/babel-traverse/lib/hub.js +19 -0
  21. data/node_modules/babel-core/node_modules/babel-traverse/lib/index.js +250 -0
  22. data/node_modules/babel-core/node_modules/babel-traverse/lib/path/ancestry.js +317 -0
  23. data/node_modules/babel-core/node_modules/babel-traverse/lib/path/comments.js +55 -0
  24. data/node_modules/babel-core/node_modules/babel-traverse/lib/path/context.js +297 -0
  25. data/node_modules/babel-core/node_modules/babel-traverse/lib/path/conversion.js +48 -0
  26. data/node_modules/babel-core/node_modules/babel-traverse/lib/path/evaluation.js +406 -0
  27. data/node_modules/babel-core/node_modules/babel-traverse/lib/path/family.js +168 -0
  28. data/node_modules/babel-core/node_modules/babel-traverse/lib/path/index.js +245 -0
  29. data/node_modules/babel-core/node_modules/babel-traverse/lib/path/inference/index.js +151 -0
  30. data/node_modules/babel-core/node_modules/babel-traverse/lib/path/inference/inferer-reference.js +225 -0
  31. data/node_modules/babel-core/node_modules/babel-traverse/lib/path/inference/inferers.js +198 -0
  32. data/node_modules/babel-core/node_modules/babel-traverse/lib/path/introspection.js +518 -0
  33. data/node_modules/babel-core/node_modules/babel-traverse/lib/path/lib/hoister.js +166 -0
  34. data/node_modules/babel-core/node_modules/babel-traverse/lib/path/lib/removal-hooks.js +64 -0
  35. data/node_modules/babel-core/node_modules/babel-traverse/lib/path/lib/virtual-types.js +141 -0
  36. data/node_modules/babel-core/node_modules/babel-traverse/lib/path/modification.js +293 -0
  37. data/node_modules/babel-core/node_modules/babel-traverse/lib/path/removal.js +72 -0
  38. data/node_modules/babel-core/node_modules/babel-traverse/lib/path/replacement.js +302 -0
  39. data/node_modules/babel-core/node_modules/babel-traverse/lib/scope/binding.js +105 -0
  40. data/node_modules/babel-core/node_modules/babel-traverse/lib/scope/index.js +1222 -0
  41. data/node_modules/babel-core/node_modules/babel-traverse/lib/scope/lib/renamer.js +154 -0
  42. data/node_modules/babel-core/node_modules/babel-traverse/lib/visitors.js +360 -0
  43. data/node_modules/babel-core/node_modules/babel-traverse/package.json +107 -0
  44. data/node_modules/babel-core/node_modules/core-js/package.json +20 -2
  45. data/node_modules/babel-core/package.json +31 -24
  46. data/node_modules/babel-generator/lib/buffer.js +69 -70
  47. data/node_modules/babel-generator/lib/generators/base.js +16 -12
  48. data/node_modules/babel-generator/lib/generators/classes.js +28 -19
  49. data/node_modules/babel-generator/lib/generators/expressions.js +76 -99
  50. data/node_modules/babel-generator/lib/generators/flow.js +178 -119
  51. data/node_modules/babel-generator/lib/generators/jsx.js +38 -29
  52. data/node_modules/babel-generator/lib/generators/methods.js +34 -25
  53. data/node_modules/babel-generator/lib/generators/modules.js +67 -40
  54. data/node_modules/babel-generator/lib/generators/statements.js +93 -76
  55. data/node_modules/babel-generator/lib/generators/template-literals.js +12 -11
  56. data/node_modules/babel-generator/lib/generators/types.js +56 -49
  57. data/node_modules/babel-generator/lib/index.js +52 -39
  58. data/node_modules/babel-generator/lib/node/index.js +12 -18
  59. data/node_modules/babel-generator/lib/node/parentheses.js +23 -27
  60. data/node_modules/babel-generator/lib/node/whitespace.js +20 -26
  61. data/node_modules/babel-generator/lib/position.js +5 -5
  62. data/node_modules/babel-generator/lib/printer.js +52 -81
  63. data/node_modules/babel-generator/lib/source-map.js +9 -11
  64. data/node_modules/babel-generator/lib/whitespace.js +13 -13
  65. data/node_modules/babel-generator/node_modules/babel-runtime/package.json +19 -6
  66. data/node_modules/babel-generator/node_modules/babel-types/.npmignore +3 -0
  67. data/node_modules/babel-generator/node_modules/babel-types/README.md +1141 -0
  68. data/node_modules/babel-generator/node_modules/babel-types/lib/constants.js +40 -0
  69. data/node_modules/babel-generator/node_modules/babel-types/lib/converters.js +378 -0
  70. data/node_modules/babel-generator/node_modules/babel-types/lib/definitions/core.js +704 -0
  71. data/node_modules/babel-generator/node_modules/babel-types/lib/definitions/es2015.js +354 -0
  72. data/node_modules/babel-generator/node_modules/babel-types/lib/definitions/experimental.js +85 -0
  73. data/node_modules/babel-generator/node_modules/babel-types/lib/definitions/flow.js +319 -0
  74. data/node_modules/babel-generator/node_modules/babel-types/lib/definitions/index.js +247 -0
  75. data/node_modules/babel-generator/node_modules/babel-types/lib/definitions/init.js +15 -0
  76. data/node_modules/babel-generator/node_modules/babel-types/lib/definitions/jsx.js +137 -0
  77. data/node_modules/babel-generator/node_modules/babel-types/lib/definitions/misc.js +21 -0
  78. data/node_modules/babel-generator/node_modules/babel-types/lib/flow.js +129 -0
  79. data/node_modules/babel-generator/node_modules/babel-types/lib/index.js +848 -0
  80. data/node_modules/babel-generator/node_modules/babel-types/lib/react.js +83 -0
  81. data/node_modules/babel-generator/node_modules/babel-types/lib/retrievers.js +123 -0
  82. data/node_modules/babel-generator/node_modules/babel-types/lib/validators.js +276 -0
  83. data/node_modules/babel-generator/node_modules/babel-types/package.json +103 -0
  84. data/node_modules/babel-generator/node_modules/core-js/package.json +10 -5
  85. data/node_modules/babel-generator/package.json +19 -16
  86. data/node_modules/babel-helpers/node_modules/babel-runtime/package.json +19 -3
  87. data/node_modules/babel-helpers/node_modules/core-js/package.json +20 -4
  88. data/node_modules/babel-helpers/package.json +8 -1
  89. data/node_modules/babel-register/node_modules/babel-runtime/package.json +19 -3
  90. data/node_modules/babel-register/node_modules/core-js/package.json +20 -4
  91. data/node_modules/babel-register/package.json +8 -1
  92. data/node_modules/balanced-match/package.json +8 -1
  93. data/node_modules/brace-expansion/package.json +19 -12
  94. data/node_modules/concat-map/package.json +9 -2
  95. data/node_modules/convert-source-map/package.json +8 -1
  96. data/node_modules/detect-indent/package.json +8 -1
  97. data/node_modules/get-stdin/package.json +8 -1
  98. data/node_modules/home-or-tmp/package.json +9 -2
  99. data/node_modules/is-finite/package.json +8 -2
  100. data/node_modules/json5/package.json +8 -1
  101. data/node_modules/minimatch/README.md +2 -9
  102. data/node_modules/minimatch/minimatch.js +15 -3
  103. data/node_modules/minimatch/package.json +29 -21
  104. data/node_modules/minimist/package.json +8 -1
  105. data/node_modules/mkdirp/node_modules/minimist/package.json +8 -1
  106. data/node_modules/mkdirp/package.json +8 -1
  107. data/node_modules/number-is-nan/package.json +8 -1
  108. data/node_modules/os-tmpdir/package.json +8 -1
  109. data/node_modules/path-exists/package.json +8 -1
  110. data/node_modules/path-is-absolute/package.json +8 -1
  111. data/node_modules/repeating/package.json +15 -11
  112. data/node_modules/shebang-regex/package.json +8 -1
  113. data/node_modules/slash/package.json +8 -1
  114. data/node_modules/source-map-support/node_modules/source-map/package.json +8 -1
  115. data/node_modules/source-map-support/package.json +8 -1
  116. data/node_modules/source-map/package.json +8 -1
  117. data/node_modules/user-home/package.json +8 -1
  118. data/package.json +1 -1
  119. metadata +54 -6
  120. data/node_modules/brace-expansion/.npmignore +0 -3
  121. data/node_modules/brace-expansion/example.js +0 -8
  122. data/node_modules/minimatch/browser.js +0 -1159
@@ -0,0 +1,225 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+
5
+ var _getIterator2 = require("babel-runtime/core-js/get-iterator");
6
+
7
+ var _getIterator3 = _interopRequireDefault(_getIterator2);
8
+
9
+ exports.default = function (node) {
10
+ if (!this.isReferenced()) return;
11
+
12
+ // check if a binding exists of this value and if so then return a union type of all
13
+ // possible types that the binding could be
14
+ var binding = this.scope.getBinding(node.name);
15
+ if (binding) {
16
+ if (binding.identifier.typeAnnotation) {
17
+ return binding.identifier.typeAnnotation;
18
+ } else {
19
+ return getTypeAnnotationBindingConstantViolations(this, node.name);
20
+ }
21
+ }
22
+
23
+ // built-in values
24
+ if (node.name === "undefined") {
25
+ return t.voidTypeAnnotation();
26
+ } else if (node.name === "NaN" || node.name === "Infinity") {
27
+ return t.numberTypeAnnotation();
28
+ } else if (node.name === "arguments") {
29
+ // todo
30
+ }
31
+ };
32
+
33
+ var _babelTypes = require("babel-types");
34
+
35
+ var t = _interopRequireWildcard(_babelTypes);
36
+
37
+ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
38
+
39
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
40
+
41
+ function getTypeAnnotationBindingConstantViolations(path, name) {
42
+ var binding = path.scope.getBinding(name);
43
+
44
+ var types = [];
45
+ path.typeAnnotation = t.unionTypeAnnotation(types);
46
+
47
+ var functionConstantViolations = [];
48
+ var constantViolations = getConstantViolationsBefore(binding, path, functionConstantViolations);
49
+
50
+ var testType = getConditionalAnnotation(path, name);
51
+ if (testType) {
52
+ (function () {
53
+ var testConstantViolations = getConstantViolationsBefore(binding, testType.ifStatement);
54
+
55
+ // remove constant violations observed before the IfStatement
56
+ constantViolations = constantViolations.filter(function (path) {
57
+ return testConstantViolations.indexOf(path) < 0;
58
+ });
59
+
60
+ // clear current types and add in observed test type
61
+ types.push(testType.typeAnnotation);
62
+ })();
63
+ }
64
+
65
+ if (constantViolations.length) {
66
+ // pick one constant from each scope which will represent the last possible
67
+ // control flow path that it could've taken/been
68
+ /* This code is broken for the following problems:
69
+ * It thinks that assignments can only happen in scopes.
70
+ * What about conditionals, if statements without block,
71
+ * or guarded assignments.
72
+ * It also checks to see if one of the assignments is in the
73
+ * same scope and uses that as the only "violation". However,
74
+ * the binding is returned by `getConstantViolationsBefore` so we for
75
+ * sure always going to return that as the only "violation".
76
+ let rawConstantViolations = constantViolations.reverse();
77
+ let visitedScopes = [];
78
+ constantViolations = [];
79
+ for (let violation of (rawConstantViolations: Array<NodePath>)) {
80
+ let violationScope = violation.scope;
81
+ if (visitedScopes.indexOf(violationScope) >= 0) continue;
82
+ visitedScopes.push(violationScope);
83
+ constantViolations.push(violation);
84
+ if (violationScope === path.scope) {
85
+ constantViolations = [violation];
86
+ break;
87
+ }
88
+ }*/
89
+
90
+ // add back on function constant violations since we can't track calls
91
+ constantViolations = constantViolations.concat(functionConstantViolations);
92
+
93
+ // push on inferred types of violated paths
94
+ for (var _iterator = constantViolations, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
95
+ var _ref;
96
+
97
+ if (_isArray) {
98
+ if (_i >= _iterator.length) break;
99
+ _ref = _iterator[_i++];
100
+ } else {
101
+ _i = _iterator.next();
102
+ if (_i.done) break;
103
+ _ref = _i.value;
104
+ }
105
+
106
+ var violation = _ref;
107
+
108
+ types.push(violation.getTypeAnnotation());
109
+ }
110
+ }
111
+
112
+ if (types.length) {
113
+ return t.createUnionTypeAnnotation(types);
114
+ }
115
+ }
116
+
117
+ function getConstantViolationsBefore(binding, path, functions) {
118
+ var violations = binding.constantViolations.slice();
119
+ violations.unshift(binding.path);
120
+ return violations.filter(function (violation) {
121
+ violation = violation.resolve();
122
+ var status = violation._guessExecutionStatusRelativeTo(path);
123
+ if (functions && status === "function") functions.push(violation);
124
+ return status === "before";
125
+ });
126
+ }
127
+
128
+ function inferAnnotationFromBinaryExpression(name, path) {
129
+ var operator = path.node.operator;
130
+
131
+ var right = path.get("right").resolve();
132
+ var left = path.get("left").resolve();
133
+
134
+ var target = void 0;
135
+ if (left.isIdentifier({ name: name })) {
136
+ target = right;
137
+ } else if (right.isIdentifier({ name: name })) {
138
+ target = left;
139
+ }
140
+ if (target) {
141
+ if (operator === "===") {
142
+ return target.getTypeAnnotation();
143
+ } else if (t.BOOLEAN_NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
144
+ return t.numberTypeAnnotation();
145
+ } else {
146
+ return;
147
+ }
148
+ } else {
149
+ if (operator !== "===") return;
150
+ }
151
+
152
+ //
153
+ var typeofPath = void 0;
154
+ var typePath = void 0;
155
+ if (left.isUnaryExpression({ operator: "typeof" })) {
156
+ typeofPath = left;
157
+ typePath = right;
158
+ } else if (right.isUnaryExpression({ operator: "typeof" })) {
159
+ typeofPath = right;
160
+ typePath = left;
161
+ }
162
+ if (!typePath && !typeofPath) return;
163
+
164
+ // ensure that the type path is a Literal
165
+ typePath = typePath.resolve();
166
+ if (!typePath.isLiteral()) return;
167
+
168
+ // and that it's a string so we can infer it
169
+ var typeValue = typePath.node.value;
170
+ if (typeof typeValue !== "string") return;
171
+
172
+ // and that the argument of the typeof path references us!
173
+ if (!typeofPath.get("argument").isIdentifier({ name: name })) return;
174
+
175
+ // turn type value into a type annotation
176
+ return t.createTypeAnnotationBasedOnTypeof(typePath.node.value);
177
+ }
178
+
179
+ function getParentConditionalPath(path) {
180
+ var parentPath = void 0;
181
+ while (parentPath = path.parentPath) {
182
+ if (parentPath.isIfStatement() || parentPath.isConditionalExpression()) {
183
+ if (path.key === "test") {
184
+ return;
185
+ } else {
186
+ return parentPath;
187
+ }
188
+ } else {
189
+ path = parentPath;
190
+ }
191
+ }
192
+ }
193
+
194
+ function getConditionalAnnotation(path, name) {
195
+ var ifStatement = getParentConditionalPath(path);
196
+ if (!ifStatement) return;
197
+
198
+ var test = ifStatement.get("test");
199
+ var paths = [test];
200
+ var types = [];
201
+
202
+ do {
203
+ var _path = paths.shift().resolve();
204
+
205
+ if (_path.isLogicalExpression()) {
206
+ paths.push(_path.get("left"));
207
+ paths.push(_path.get("right"));
208
+ }
209
+
210
+ if (_path.isBinaryExpression()) {
211
+ var type = inferAnnotationFromBinaryExpression(name, _path);
212
+ if (type) types.push(type);
213
+ }
214
+ } while (paths.length);
215
+
216
+ if (types.length) {
217
+ return {
218
+ typeAnnotation: t.createUnionTypeAnnotation(types),
219
+ ifStatement: ifStatement
220
+ };
221
+ } else {
222
+ return getConditionalAnnotation(ifStatement, name);
223
+ }
224
+ }
225
+ module.exports = exports["default"];
@@ -0,0 +1,198 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.Class = exports.Function = exports.Identifier = undefined;
5
+
6
+ var _infererReference = require("./inferer-reference");
7
+
8
+ Object.defineProperty(exports, "Identifier", {
9
+ enumerable: true,
10
+ get: function get() {
11
+ return _interopRequireDefault(_infererReference).default;
12
+ }
13
+ });
14
+ exports.VariableDeclarator = VariableDeclarator;
15
+ exports.TypeCastExpression = TypeCastExpression;
16
+ exports.NewExpression = NewExpression;
17
+ exports.TemplateLiteral = TemplateLiteral;
18
+ exports.UnaryExpression = UnaryExpression;
19
+ exports.BinaryExpression = BinaryExpression;
20
+ exports.LogicalExpression = LogicalExpression;
21
+ exports.ConditionalExpression = ConditionalExpression;
22
+ exports.SequenceExpression = SequenceExpression;
23
+ exports.AssignmentExpression = AssignmentExpression;
24
+ exports.UpdateExpression = UpdateExpression;
25
+ exports.StringLiteral = StringLiteral;
26
+ exports.NumericLiteral = NumericLiteral;
27
+ exports.BooleanLiteral = BooleanLiteral;
28
+ exports.NullLiteral = NullLiteral;
29
+ exports.RegExpLiteral = RegExpLiteral;
30
+ exports.ObjectExpression = ObjectExpression;
31
+ exports.ArrayExpression = ArrayExpression;
32
+ exports.RestElement = RestElement;
33
+ exports.CallExpression = CallExpression;
34
+ exports.TaggedTemplateExpression = TaggedTemplateExpression;
35
+
36
+ var _babelTypes = require("babel-types");
37
+
38
+ var t = _interopRequireWildcard(_babelTypes);
39
+
40
+ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
41
+
42
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
43
+
44
+ function VariableDeclarator() {
45
+ var id = this.get("id");
46
+
47
+ if (id.isIdentifier()) {
48
+ return this.get("init").getTypeAnnotation();
49
+ } else {
50
+ return;
51
+ }
52
+ }
53
+
54
+ function TypeCastExpression(node) {
55
+ return node.typeAnnotation;
56
+ }
57
+
58
+ TypeCastExpression.validParent = true;
59
+
60
+ function NewExpression(node) {
61
+ if (this.get("callee").isIdentifier()) {
62
+ // only resolve identifier callee
63
+ return t.genericTypeAnnotation(node.callee);
64
+ }
65
+ }
66
+
67
+ function TemplateLiteral() {
68
+ return t.stringTypeAnnotation();
69
+ }
70
+
71
+ function UnaryExpression(node) {
72
+ var operator = node.operator;
73
+
74
+ if (operator === "void") {
75
+ return t.voidTypeAnnotation();
76
+ } else if (t.NUMBER_UNARY_OPERATORS.indexOf(operator) >= 0) {
77
+ return t.numberTypeAnnotation();
78
+ } else if (t.STRING_UNARY_OPERATORS.indexOf(operator) >= 0) {
79
+ return t.stringTypeAnnotation();
80
+ } else if (t.BOOLEAN_UNARY_OPERATORS.indexOf(operator) >= 0) {
81
+ return t.booleanTypeAnnotation();
82
+ }
83
+ }
84
+
85
+ function BinaryExpression(node) {
86
+ var operator = node.operator;
87
+
88
+ if (t.NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
89
+ return t.numberTypeAnnotation();
90
+ } else if (t.BOOLEAN_BINARY_OPERATORS.indexOf(operator) >= 0) {
91
+ return t.booleanTypeAnnotation();
92
+ } else if (operator === "+") {
93
+ var right = this.get("right");
94
+ var left = this.get("left");
95
+
96
+ if (left.isBaseType("number") && right.isBaseType("number")) {
97
+ // both numbers so this will be a number
98
+ return t.numberTypeAnnotation();
99
+ } else if (left.isBaseType("string") || right.isBaseType("string")) {
100
+ // one is a string so the result will be a string
101
+ return t.stringTypeAnnotation();
102
+ }
103
+
104
+ // unsure if left and right are strings or numbers so stay on the safe side
105
+ return t.unionTypeAnnotation([t.stringTypeAnnotation(), t.numberTypeAnnotation()]);
106
+ }
107
+ }
108
+
109
+ function LogicalExpression() {
110
+ return t.createUnionTypeAnnotation([this.get("left").getTypeAnnotation(), this.get("right").getTypeAnnotation()]);
111
+ }
112
+
113
+ function ConditionalExpression() {
114
+ return t.createUnionTypeAnnotation([this.get("consequent").getTypeAnnotation(), this.get("alternate").getTypeAnnotation()]);
115
+ }
116
+
117
+ function SequenceExpression() {
118
+ return this.get("expressions").pop().getTypeAnnotation();
119
+ }
120
+
121
+ function AssignmentExpression() {
122
+ return this.get("right").getTypeAnnotation();
123
+ }
124
+
125
+ function UpdateExpression(node) {
126
+ var operator = node.operator;
127
+ if (operator === "++" || operator === "--") {
128
+ return t.numberTypeAnnotation();
129
+ }
130
+ }
131
+
132
+ function StringLiteral() {
133
+ return t.stringTypeAnnotation();
134
+ }
135
+
136
+ function NumericLiteral() {
137
+ return t.numberTypeAnnotation();
138
+ }
139
+
140
+ function BooleanLiteral() {
141
+ return t.booleanTypeAnnotation();
142
+ }
143
+
144
+ function NullLiteral() {
145
+ return t.nullLiteralTypeAnnotation();
146
+ }
147
+
148
+ function RegExpLiteral() {
149
+ return t.genericTypeAnnotation(t.identifier("RegExp"));
150
+ }
151
+
152
+ function ObjectExpression() {
153
+ return t.genericTypeAnnotation(t.identifier("Object"));
154
+ }
155
+
156
+ function ArrayExpression() {
157
+ return t.genericTypeAnnotation(t.identifier("Array"));
158
+ }
159
+
160
+ function RestElement() {
161
+ return ArrayExpression();
162
+ }
163
+
164
+ RestElement.validParent = true;
165
+
166
+ function Func() {
167
+ return t.genericTypeAnnotation(t.identifier("Function"));
168
+ }
169
+
170
+ exports.Function = Func;
171
+ exports.Class = Func;
172
+ function CallExpression() {
173
+ return resolveCall(this.get("callee"));
174
+ }
175
+
176
+ function TaggedTemplateExpression() {
177
+ return resolveCall(this.get("tag"));
178
+ }
179
+
180
+ function resolveCall(callee) {
181
+ callee = callee.resolve();
182
+
183
+ if (callee.isFunction()) {
184
+ if (callee.is("async")) {
185
+ if (callee.is("generator")) {
186
+ return t.genericTypeAnnotation(t.identifier("AsyncIterator"));
187
+ } else {
188
+ return t.genericTypeAnnotation(t.identifier("Promise"));
189
+ }
190
+ } else {
191
+ if (callee.node.returnType) {
192
+ return callee.node.returnType;
193
+ } else {
194
+ // todo: get union type of all return arguments
195
+ }
196
+ }
197
+ }
198
+ }
@@ -0,0 +1,518 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.is = undefined;
5
+
6
+ var _typeof2 = require("babel-runtime/helpers/typeof");
7
+
8
+ var _typeof3 = _interopRequireDefault(_typeof2);
9
+
10
+ var _getIterator2 = require("babel-runtime/core-js/get-iterator");
11
+
12
+ var _getIterator3 = _interopRequireDefault(_getIterator2);
13
+
14
+ exports.matchesPattern = matchesPattern;
15
+ exports.has = has;
16
+ exports.isStatic = isStatic;
17
+ exports.isnt = isnt;
18
+ exports.equals = equals;
19
+ exports.isNodeType = isNodeType;
20
+ exports.canHaveVariableDeclarationOrExpression = canHaveVariableDeclarationOrExpression;
21
+ exports.canSwapBetweenExpressionAndStatement = canSwapBetweenExpressionAndStatement;
22
+ exports.isCompletionRecord = isCompletionRecord;
23
+ exports.isStatementOrBlock = isStatementOrBlock;
24
+ exports.referencesImport = referencesImport;
25
+ exports.getSource = getSource;
26
+ exports.willIMaybeExecuteBefore = willIMaybeExecuteBefore;
27
+ exports._guessExecutionStatusRelativeTo = _guessExecutionStatusRelativeTo;
28
+ exports._guessExecutionStatusRelativeToDifferentFunctions = _guessExecutionStatusRelativeToDifferentFunctions;
29
+ exports.resolve = resolve;
30
+ exports._resolve = _resolve;
31
+
32
+ var _includes = require("lodash/includes");
33
+
34
+ var _includes2 = _interopRequireDefault(_includes);
35
+
36
+ var _babelTypes = require("babel-types");
37
+
38
+ var t = _interopRequireWildcard(_babelTypes);
39
+
40
+ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
41
+
42
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
43
+
44
+ /**
45
+ * Match the current node if it matches the provided `pattern`.
46
+ *
47
+ * For example, given the match `React.createClass` it would match the
48
+ * parsed nodes of `React.createClass` and `React["createClass"]`.
49
+ */
50
+
51
+ function matchesPattern(pattern, allowPartial) {
52
+ // not a member expression
53
+ if (!this.isMemberExpression()) return false;
54
+
55
+ var parts = pattern.split(".");
56
+ var search = [this.node];
57
+ var i = 0;
58
+
59
+ function matches(name) {
60
+ var part = parts[i];
61
+ return part === "*" || name === part;
62
+ }
63
+
64
+ while (search.length) {
65
+ var node = search.shift();
66
+
67
+ if (allowPartial && i === parts.length) {
68
+ return true;
69
+ }
70
+
71
+ if (t.isIdentifier(node)) {
72
+ // this part doesn't match
73
+ if (!matches(node.name)) return false;
74
+ } else if (t.isLiteral(node)) {
75
+ // this part doesn't match
76
+ if (!matches(node.value)) return false;
77
+ } else if (t.isMemberExpression(node)) {
78
+ if (node.computed && !t.isLiteral(node.property)) {
79
+ // we can't deal with this
80
+ return false;
81
+ } else {
82
+ search.unshift(node.property);
83
+ search.unshift(node.object);
84
+ continue;
85
+ }
86
+ } else if (t.isThisExpression(node)) {
87
+ if (!matches("this")) return false;
88
+ } else {
89
+ // we can't deal with this
90
+ return false;
91
+ }
92
+
93
+ // too many parts
94
+ if (++i > parts.length) {
95
+ return false;
96
+ }
97
+ }
98
+
99
+ return i === parts.length;
100
+ }
101
+
102
+ /**
103
+ * Check whether we have the input `key`. If the `key` references an array then we check
104
+ * if the array has any items, otherwise we just check if it's falsy.
105
+ */
106
+
107
+ // This file contains methods responsible for introspecting the current path for certain values.
108
+
109
+ function has(key) {
110
+ var val = this.node && this.node[key];
111
+ if (val && Array.isArray(val)) {
112
+ return !!val.length;
113
+ } else {
114
+ return !!val;
115
+ }
116
+ }
117
+
118
+ /**
119
+ * Description
120
+ */
121
+
122
+ function isStatic() {
123
+ return this.scope.isStatic(this.node);
124
+ }
125
+
126
+ /**
127
+ * Alias of `has`.
128
+ */
129
+
130
+ var is = exports.is = has;
131
+
132
+ /**
133
+ * Opposite of `has`.
134
+ */
135
+
136
+ function isnt(key) {
137
+ return !this.has(key);
138
+ }
139
+
140
+ /**
141
+ * Check whether the path node `key` strict equals `value`.
142
+ */
143
+
144
+ function equals(key, value) {
145
+ return this.node[key] === value;
146
+ }
147
+
148
+ /**
149
+ * Check the type against our stored internal type of the node. This is handy when a node has
150
+ * been removed yet we still internally know the type and need it to calculate node replacement.
151
+ */
152
+
153
+ function isNodeType(type) {
154
+ return t.isType(this.type, type);
155
+ }
156
+
157
+ /**
158
+ * This checks whether or not we're in one of the following positions:
159
+ *
160
+ * for (KEY in right);
161
+ * for (KEY;;);
162
+ *
163
+ * This is because these spots allow VariableDeclarations AND normal expressions so we need
164
+ * to tell the path replacement that it's ok to replace this with an expression.
165
+ */
166
+
167
+ function canHaveVariableDeclarationOrExpression() {
168
+ return (this.key === "init" || this.key === "left") && this.parentPath.isFor();
169
+ }
170
+
171
+ /**
172
+ * This checks whether we are swapping an arrow function's body between an
173
+ * expression and a block statement (or vice versa).
174
+ *
175
+ * This is because arrow functions may implicitly return an expression, which
176
+ * is the same as containing a block statement.
177
+ */
178
+
179
+ function canSwapBetweenExpressionAndStatement(replacement) {
180
+ if (this.key !== "body" || !this.parentPath.isArrowFunctionExpression()) {
181
+ return false;
182
+ }
183
+
184
+ if (this.isExpression()) {
185
+ return t.isBlockStatement(replacement);
186
+ } else if (this.isBlockStatement()) {
187
+ return t.isExpression(replacement);
188
+ }
189
+
190
+ return false;
191
+ }
192
+
193
+ /**
194
+ * Check whether the current path references a completion record
195
+ */
196
+
197
+ function isCompletionRecord(allowInsideFunction) {
198
+ var path = this;
199
+ var first = true;
200
+
201
+ do {
202
+ var container = path.container;
203
+
204
+ // we're in a function so can't be a completion record
205
+ if (path.isFunction() && !first) {
206
+ return !!allowInsideFunction;
207
+ }
208
+
209
+ first = false;
210
+
211
+ // check to see if we're the last item in the container and if we are
212
+ // we're a completion record!
213
+ if (Array.isArray(container) && path.key !== container.length - 1) {
214
+ return false;
215
+ }
216
+ } while ((path = path.parentPath) && !path.isProgram());
217
+
218
+ return true;
219
+ }
220
+
221
+ /**
222
+ * Check whether or not the current `key` allows either a single statement or block statement
223
+ * so we can explode it if necessary.
224
+ */
225
+
226
+ function isStatementOrBlock() {
227
+ if (this.parentPath.isLabeledStatement() || t.isBlockStatement(this.container)) {
228
+ return false;
229
+ } else {
230
+ return (0, _includes2.default)(t.STATEMENT_OR_BLOCK_KEYS, this.key);
231
+ }
232
+ }
233
+
234
+ /**
235
+ * Check if the currently assigned path references the `importName` of `moduleSource`.
236
+ */
237
+
238
+ function referencesImport(moduleSource, importName) {
239
+ if (!this.isReferencedIdentifier()) return false;
240
+
241
+ var binding = this.scope.getBinding(this.node.name);
242
+ if (!binding || binding.kind !== "module") return false;
243
+
244
+ var path = binding.path;
245
+ var parent = path.parentPath;
246
+ if (!parent.isImportDeclaration()) return false;
247
+
248
+ // check moduleSource
249
+ if (parent.node.source.value === moduleSource) {
250
+ if (!importName) return true;
251
+ } else {
252
+ return false;
253
+ }
254
+
255
+ if (path.isImportDefaultSpecifier() && importName === "default") {
256
+ return true;
257
+ }
258
+
259
+ if (path.isImportNamespaceSpecifier() && importName === "*") {
260
+ return true;
261
+ }
262
+
263
+ if (path.isImportSpecifier() && path.node.imported.name === importName) {
264
+ return true;
265
+ }
266
+
267
+ return false;
268
+ }
269
+
270
+ /**
271
+ * Get the source code associated with this node.
272
+ */
273
+
274
+ function getSource() {
275
+ var node = this.node;
276
+ if (node.end) {
277
+ return this.hub.file.code.slice(node.start, node.end);
278
+ } else {
279
+ return "";
280
+ }
281
+ }
282
+
283
+ function willIMaybeExecuteBefore(target) {
284
+ return this._guessExecutionStatusRelativeTo(target) !== "after";
285
+ }
286
+
287
+ /**
288
+ * Given a `target` check the execution status of it relative to the current path.
289
+ *
290
+ * "Execution status" simply refers to where or not we **think** this will execuete
291
+ * before or after the input `target` element.
292
+ */
293
+
294
+ function _guessExecutionStatusRelativeTo(target) {
295
+ // check if the two paths are in different functions, we can't track execution of these
296
+ var targetFuncParent = target.scope.getFunctionParent();
297
+ var selfFuncParent = this.scope.getFunctionParent();
298
+
299
+ // here we check the `node` equality as sometimes we may have different paths for the
300
+ // same node due to path thrashing
301
+ if (targetFuncParent.node !== selfFuncParent.node) {
302
+ var status = this._guessExecutionStatusRelativeToDifferentFunctions(targetFuncParent);
303
+ if (status) {
304
+ return status;
305
+ } else {
306
+ target = targetFuncParent.path;
307
+ }
308
+ }
309
+
310
+ var targetPaths = target.getAncestry();
311
+ if (targetPaths.indexOf(this) >= 0) return "after";
312
+
313
+ var selfPaths = this.getAncestry();
314
+
315
+ // get ancestor where the branches intersect
316
+ var commonPath = void 0;
317
+ var targetIndex = void 0;
318
+ var selfIndex = void 0;
319
+ for (selfIndex = 0; selfIndex < selfPaths.length; selfIndex++) {
320
+ var selfPath = selfPaths[selfIndex];
321
+ targetIndex = targetPaths.indexOf(selfPath);
322
+ if (targetIndex >= 0) {
323
+ commonPath = selfPath;
324
+ break;
325
+ }
326
+ }
327
+ if (!commonPath) {
328
+ return "before";
329
+ }
330
+
331
+ // get the relationship paths that associate these nodes to their common ancestor
332
+ var targetRelationship = targetPaths[targetIndex - 1];
333
+ var selfRelationship = selfPaths[selfIndex - 1];
334
+ if (!targetRelationship || !selfRelationship) {
335
+ return "before";
336
+ }
337
+
338
+ // container list so let's see which one is after the other
339
+ if (targetRelationship.listKey && targetRelationship.container === selfRelationship.container) {
340
+ return targetRelationship.key > selfRelationship.key ? "before" : "after";
341
+ }
342
+
343
+ // otherwise we're associated by a parent node, check which key comes before the other
344
+ var targetKeyPosition = t.VISITOR_KEYS[targetRelationship.type].indexOf(targetRelationship.key);
345
+ var selfKeyPosition = t.VISITOR_KEYS[selfRelationship.type].indexOf(selfRelationship.key);
346
+ return targetKeyPosition > selfKeyPosition ? "before" : "after";
347
+ }
348
+
349
+ function _guessExecutionStatusRelativeToDifferentFunctions(targetFuncParent) {
350
+ var targetFuncPath = targetFuncParent.path;
351
+ if (!targetFuncPath.isFunctionDeclaration()) return;
352
+
353
+ // so we're in a completely different function, if this is a function declaration
354
+ // then we can be a bit smarter and handle cases where the function is either
355
+ // a. not called at all (part of an export)
356
+ // b. called directly
357
+ var binding = targetFuncPath.scope.getBinding(targetFuncPath.node.id.name);
358
+
359
+ // no references!
360
+ if (!binding.references) return "before";
361
+
362
+ var referencePaths = binding.referencePaths;
363
+
364
+ // verify that all of the references are calls
365
+ for (var _iterator = referencePaths, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
366
+ var _ref;
367
+
368
+ if (_isArray) {
369
+ if (_i >= _iterator.length) break;
370
+ _ref = _iterator[_i++];
371
+ } else {
372
+ _i = _iterator.next();
373
+ if (_i.done) break;
374
+ _ref = _i.value;
375
+ }
376
+
377
+ var path = _ref;
378
+
379
+ if (path.key !== "callee" || !path.parentPath.isCallExpression()) {
380
+ return;
381
+ }
382
+ }
383
+
384
+ var allStatus = void 0;
385
+
386
+ // verify that all the calls have the same execution status
387
+ for (var _iterator2 = referencePaths, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
388
+ var _ref2;
389
+
390
+ if (_isArray2) {
391
+ if (_i2 >= _iterator2.length) break;
392
+ _ref2 = _iterator2[_i2++];
393
+ } else {
394
+ _i2 = _iterator2.next();
395
+ if (_i2.done) break;
396
+ _ref2 = _i2.value;
397
+ }
398
+
399
+ var _path = _ref2;
400
+
401
+ // if a reference is a child of the function we're checking against then we can
402
+ // safelty ignore it
403
+ var childOfFunction = !!_path.find(function (path) {
404
+ return path.node === targetFuncPath.node;
405
+ });
406
+ if (childOfFunction) continue;
407
+
408
+ var status = this._guessExecutionStatusRelativeTo(_path);
409
+
410
+ if (allStatus) {
411
+ if (allStatus !== status) return;
412
+ } else {
413
+ allStatus = status;
414
+ }
415
+ }
416
+
417
+ return allStatus;
418
+ }
419
+
420
+ /**
421
+ * Resolve a "pointer" `NodePath` to it's absolute path.
422
+ */
423
+
424
+ function resolve(dangerous, resolved) {
425
+ return this._resolve(dangerous, resolved) || this;
426
+ }
427
+
428
+ function _resolve(dangerous, resolved) {
429
+ var _this = this;
430
+
431
+ // detect infinite recursion
432
+ // todo: possibly have a max length on this just to be safe
433
+ if (resolved && resolved.indexOf(this) >= 0) return;
434
+
435
+ // we store all the paths we've "resolved" in this array to prevent infinite recursion
436
+ resolved = resolved || [];
437
+ resolved.push(this);
438
+
439
+ if (this.isVariableDeclarator()) {
440
+ if (this.get("id").isIdentifier()) {
441
+ return this.get("init").resolve(dangerous, resolved);
442
+ } else {
443
+ // otherwise it's a request for a pattern and that's a bit more tricky
444
+ }
445
+ } else if (this.isReferencedIdentifier()) {
446
+ var binding = this.scope.getBinding(this.node.name);
447
+ if (!binding) return;
448
+
449
+ // reassigned so we can't really resolve it
450
+ if (!binding.constant) return;
451
+
452
+ // todo - lookup module in dependency graph
453
+ if (binding.kind === "module") return;
454
+
455
+ if (binding.path !== this) {
456
+ var _ret = function () {
457
+ var ret = binding.path.resolve(dangerous, resolved);
458
+ // If the identifier resolves to parent node then we can't really resolve it.
459
+ if (_this.find(function (parent) {
460
+ return parent.node === ret.node;
461
+ })) return {
462
+ v: void 0
463
+ };
464
+ return {
465
+ v: ret
466
+ };
467
+ }();
468
+
469
+ if ((typeof _ret === "undefined" ? "undefined" : (0, _typeof3.default)(_ret)) === "object") return _ret.v;
470
+ }
471
+ } else if (this.isTypeCastExpression()) {
472
+ return this.get("expression").resolve(dangerous, resolved);
473
+ } else if (dangerous && this.isMemberExpression()) {
474
+ // this is dangerous, as non-direct target assignments will mutate it's state
475
+ // making this resolution inaccurate
476
+
477
+ var targetKey = this.toComputedKey();
478
+ if (!t.isLiteral(targetKey)) return;
479
+
480
+ var targetName = targetKey.value;
481
+
482
+ var target = this.get("object").resolve(dangerous, resolved);
483
+
484
+ if (target.isObjectExpression()) {
485
+ var props = target.get("properties");
486
+ for (var _iterator3 = props, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : (0, _getIterator3.default)(_iterator3);;) {
487
+ var _ref3;
488
+
489
+ if (_isArray3) {
490
+ if (_i3 >= _iterator3.length) break;
491
+ _ref3 = _iterator3[_i3++];
492
+ } else {
493
+ _i3 = _iterator3.next();
494
+ if (_i3.done) break;
495
+ _ref3 = _i3.value;
496
+ }
497
+
498
+ var prop = _ref3;
499
+
500
+ if (!prop.isProperty()) continue;
501
+
502
+ var key = prop.get("key");
503
+
504
+ // { foo: obj }
505
+ var match = prop.isnt("computed") && key.isIdentifier({ name: targetName });
506
+
507
+ // { "foo": "obj" } or { ["foo"]: "obj" }
508
+ match = match || key.isLiteral({ value: targetName });
509
+
510
+ if (match) return prop.get("value").resolve(dangerous, resolved);
511
+ }
512
+ } else if (target.isArrayExpression() && !isNaN(+targetName)) {
513
+ var elems = target.get("elements");
514
+ var elem = elems[targetName];
515
+ if (elem) return elem.resolve(dangerous, resolved);
516
+ }
517
+ }
518
+ }