forked from reactjs/react-codemod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
React-PropTypes-to-prop-types.js
210 lines (178 loc) · 5.64 KB
/
React-PropTypes-to-prop-types.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
* Copyright 2013-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
'use strict';
module.exports = function(file, api, options) {
const j = api.jscodeshift;
const root = j(file.source);
const MODULE_NAME = options['module-name'] || 'prop-types';
// Find alpha-sorted import that would follow prop-types
function findImportAfterPropTypes(j, root) {
let target, targetName;
root
.find(j.ImportDeclaration)
.forEach(path => {
const name = path.value.source.value.toLowerCase();
if (
name > MODULE_NAME &&
(!target || name < targetName)
) {
targetName = name;
target = path;
}
});
return target;
}
// Find alpha-sorted require that would follow prop-types
function findRequireAfterPropTypes(j, root) {
let target, targetName;
root
.find(j.CallExpression, {callee: {name: 'require'}})
.forEach(path => {
const name = path.node.arguments[0].value.toLowerCase();
if (
name > MODULE_NAME &&
(!target || name < targetName)
) {
targetName = name;
target = path;
}
});
return target;
}
// React.PropTypes
const isReactPropTypes = path => (
path.node.name === 'PropTypes' &&
path.parent.node.type === 'MemberExpression' &&
path.parent.node.object.name === 'React'
);
// Program uses ES import syntax
function useImportSyntax(j, root) {
return root
.find(j.ImportDeclaration)
.length > 0;
}
// Program uses var keywords
function useVar(j, root) {
return root
.find(j.VariableDeclaration, {kind: 'const'})
.length === 0;
}
// If any PropTypes references exist, add a 'prop-types' import (or require)
function addPropTypesImport(j, root) {
if (useImportSyntax(j, root)) {
const path = findImportAfterPropTypes(j, root);
if (path) {
const importStatement = j.importDeclaration(
[j.importDefaultSpecifier(j.identifier('PropTypes'))],
j.literal(MODULE_NAME)
);
j(path).insertBefore(importStatement);
return;
}
}
const path = findRequireAfterPropTypes(j, root);
if (path) {
const requireStatement = useVar(j, root)
? j.template.statement([`var PropTypes = require('${MODULE_NAME}');\n`])
: j.template.statement([`const PropTypes = require('${MODULE_NAME}');\n`]);
j(path.parent.parent).insertBefore(requireStatement);
return;
}
throw new Error('No PropTypes import found!');
}
// Remove PropTypes destructure statements (eg const { ProptTypes } = React)
function removeDestructuredPropTypeStatements(j, root) {
let hasModifications = false;
root
.find(j.ObjectPattern)
.filter(path => (
path.parent.node.init &&
path.parent.node.init.name === 'React' &&
path.node.properties.some(
property => property.key.name === 'PropTypes'
)
))
.forEach(path => {
hasModifications = true;
// Remove the PropTypes key
path.node.properties = path.node.properties.filter(
property => property.key.name !== 'PropTypes'
);
// If this was the only property, remove the entire statement.
if (path.node.properties.length === 0) {
path.parent.parent.replace('');
}
});
return hasModifications;
}
// Remove old { PropTypes } imports
function removePropTypesImport(j, root) {
let hasModifications = false;
root
.find(j.Identifier)
.filter(path => (
path.node.name === 'PropTypes' &&
path.parent.node.type === 'ImportSpecifier'
))
.forEach(path => {
hasModifications = true;
const importDeclaration = path.parent.parent.node;
importDeclaration.specifiers = importDeclaration.specifiers.filter(
specifier => (
!specifier.imported ||
specifier.imported.name !== 'PropTypes'
)
);
});
return hasModifications;
}
// Replace all React.PropTypes instances with PropTypes
function replacePropTypesReferences(j, root) {
let hasModifications = false;
root
.find(j.Identifier)
.filter(isReactPropTypes)
.forEach(path => {
hasModifications = true;
// VariableDeclarator should be removed entirely
// eg 'PropTypes = React.PropTypes'
if (path.parent.parent.node.type === 'VariableDeclarator') {
j(path.parent.parent).remove();
} else {
// MemberExpression should be updated
// eg 'foo = React.PropTypes.string'
j(path.parent).replaceWith(
j.identifier('PropTypes')
);
}
});
return hasModifications;
}
function removeEmptyReactImport(j, root) {
root
.find(j.ImportDeclaration)
.filter(path => (
path.node.specifiers.length === 0 &&
path.node.source.value === 'react'
))
.replaceWith();
}
let hasModifications = false;
hasModifications = replacePropTypesReferences(j, root) || hasModifications;
hasModifications = removePropTypesImport(j, root) || hasModifications;
hasModifications = removeDestructuredPropTypeStatements(j, root) || hasModifications;
if (hasModifications) {
addPropTypesImport(j, root);
removeEmptyReactImport(j, root);
}
return hasModifications
? root.toSource({ quote: 'single' })
: null;
};