Add ReactUtils and array polyfills.
cpojer committed
Mar 25, 2015 at 01:29 UTC
328274bbba44aa6d75dfa21242ac9f0609c5948f
2 files changed
+169
npm-react-codemod/transforms/utils/ReactUtils.js
new
+133
@@ -0,0 +1,133 @@
1
+/*eslint-disable no-comma-dangle*/
2
+
3
+'use strict';
4
+
5
+module.exports = function(j) {
6
+ const REACT_CREATE_CLASS_MEMBER_EXPRESSION = {
7
+ type: 'MemberExpression',
8
+ object: {
9
+ name: 'React',
10
+ },
11
+ property: {
12
+ name: 'createClass',
13
+ },
14
+ };
15
+
16
+ // ---------------------------------------------------------------------------
17
+ // Checks if the file requires a certain module
18
+ const hasModule = (path, module) =>
19
+ path
20
+ .findVariableDeclarators()
21
+ .filter(j.filters.VariableDeclarator.requiresModule(module))
22
+ .size() === 1;
23
+
24
+ const hasReact = path => (
25
+ hasModule(path, 'React') ||
26
+ hasModule(path, 'react') ||
27
+ hasModule(path, 'react/addons')
28
+ );
29
+
30
+ // ---------------------------------------------------------------------------
31
+ // Finds all variable declarations that call React.createClass
32
+ const findReactCreateClassCallExpression = path =>
33
+ j(path).find(j.CallExpression, {
34
+ callee: REACT_CREATE_CLASS_MEMBER_EXPRESSION,
35
+ });
36
+
37
+ const findReactCreateClass = path =>
38
+ path
39
+ .findVariableDeclarators()
40
+ .filter(path => findReactCreateClassCallExpression(path).size() > 0);
41
+
42
+ const findReactCreateClassModuleExports = path =>
43
+ path
44
+ .find(j.AssignmentExpression, {
45
+ left: {
46
+ type: 'MemberExpression',
47
+ object: {
48
+ type: 'Identifier',
49
+ name: 'module',
50
+ },
51
+ property: {
52
+ type: 'Identifier',
53
+ name: 'exports',
54
+ },
55
+ },
56
+ right: {
57
+ type: 'CallExpression',
58
+ callee: REACT_CREATE_CLASS_MEMBER_EXPRESSION,
59
+ },
60
+ });
61
+
62
+ // ---------------------------------------------------------------------------
63
+ // Finds all classes that extend React.Component
64
+ const findReactES6ClassDeclaration = path =>
65
+ path
66
+ .find(j.ClassDeclaration, {
67
+ superClass: {
68
+ type: 'MemberExpression',
69
+ object: {
70
+ type: 'Identifier',
71
+ name: 'React',
72
+ },
73
+ property: {
74
+ type: 'Identifier',
75
+ name: 'Component',
76
+ },
77
+ },
78
+ });
79
+
80
+ // ---------------------------------------------------------------------------
81
+ // Checks if the React class has mixins
82
+ const isMixinProperty = property => {
83
+ const key = property.key;
84
+ const value = property.value;
85
+ return (
86
+ key.name === 'mixins' &&
87
+ value.type === 'ArrayExpression' &&
88
+ Array.isArray(value.elements) &&
89
+ value.elements.length
90
+ );
91
+ };
92
+
93
+ const hasMixins = classPath => {
94
+ const spec = getReactCreateClassSpec(classPath);
95
+ return spec && spec.properties.some(isMixinProperty);
96
+ };
97
+
98
+ // ---------------------------------------------------------------------------
99
+ // Others
100
+ const getReactCreateClassSpec = classPath => {
101
+ const spec = (classPath.value.init || classPath.value.right).arguments[0];
102
+ if (spec.type === 'ObjectExpression' && Array.isArray(spec.properties)) {
103
+ return spec;
104
+ }
105
+ };
106
+
107
+ const createCreateReactClassCallExpression = properties =>
108
+ j.callExpression(
109
+ j.memberExpression(
110
+ j.identifier('React'),
111
+ j.identifier('createClass'),
112
+ false
113
+ ),
114
+ [j.objectExpression(properties)]
115
+ );
116
+
117
+ const getComponentName =
118
+ classPath => classPath.node.id && classPath.node.id.name;
119
+
120
+ return {
121
+ createCreateReactClassCallExpression,
122
+ findReactES6ClassDeclaration,
123
+ findReactCreateClass,
124
+ findReactCreateClassCallExpression,
125
+ findReactCreateClassModuleExports,
126
+ getComponentName,
127
+ getReactCreateClassSpec,
128
+ hasMixins,
129
+ hasModule,
130
+ hasReact,
131
+ isMixinProperty,
132
+ };
133
+};
npm-react-codemod/transforms/utils/array-polyfills.js
new
+36
@@ -0,0 +1,36 @@
1
+/*eslint-disable no-extend-native*/
2
+
3
+'use strict';
4
+
5
+function findIndex(predicate, context) {
6
+ if (this == null) {
7
+ throw new TypeError(
8
+ 'Array.prototype.findIndex called on null or undefined'
9
+ );
10
+ }
11
+ if (typeof predicate !== 'function') {
12
+ throw new TypeError('predicate must be a function');
13
+ }
14
+ var list = Object(this);
15
+ var length = list.length >>> 0;
16
+ for (var i = 0; i < length; i++) {
17
+ if (predicate.call(context, list[i], i, list)) {
18
+ return i;
19
+ }
20
+ }
21
+ return -1;
22
+}
23
+
24
+if (!Array.prototype.findIndex) {
25
+ Array.prototype.findIndex = findIndex;
26
+}
27
+
28
+if (!Array.prototype.find) {
29
+ Array.prototype.find = function(predicate, context) {
30
+ if (this == null) {
31
+ throw new TypeError('Array.prototype.find called on null or undefined');
32
+ }
33
+ var index = findIndex.call(this, predicate, context);
34
+ return index === -1 ? undefined : this[index];
35
+ };
36
+}