This repository has been archived by the owner on Nov 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
migrate-to-modern-1.0.js
104 lines (91 loc) · 2.83 KB
/
migrate-to-modern-1.0.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
/**
* Copyright (c) 2016-present, 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 printOptions =
options.printOptions || {quote: 'single', trailingComma: true};
const RELAY_REQUIRE = /(\w+) Relay = require\(\'react-relay\/classic\'\)/;
const RELAY_IMPORT = /import Relay from \'react-relay\/classic\'/;
let source = file.source;
// Currently only handles upgrading fragment containers, bail if none found
if (source.search('Relay.createContainer') < 0) {
return source;
}
// update requires or imports
if (source.search(RELAY_REQUIRE) >= 0) {
source = source.replace(
RELAY_REQUIRE,
'$1 {\n' +
' createFragmentContainer,\n' +
' graphql,\n' +
'} = require(\'react-relay/compat\')'
);
} else if (source.search(RELAY_IMPORT) >= 0) {
source = source.replace(
RELAY_IMPORT,
'import {\n' +
' createFragmentContainer,\n' +
' graphql,\n' +
'} from \'react-relay/compat\''
);
} else {
// Does not meet current assumptions, skip
return source;
}
// update getFragment calls
source = source.replace(
/\$\{(\w+).getFragment\('(\w+)'\)\}/g,
'...$1_$2'
);
// add warning to props.relay
source = source.replace(
/(^.*props\.relay)/gm,
'// TODO props.relay.* APIs do not exist on compat containers\n$1'
);
// add warning to setVariables
source = source.replace(
/(^.*setVariables)/gm,
'// TODO needs manual handling\n$1'
);
source = j(source)
.find(j.CallExpression)
.filter(p =>
p.node.callee.type === 'MemberExpression' &&
p.node.callee.object.name === 'Relay'
).forEach(p => {
p.node.callee = j.identifier('createFragmentContainer');
const comments = [];
let fragments;
const spec = p.node.arguments[1];
spec.properties.forEach(prop => {
if (prop.key.name === 'fragments') {
fragments = prop.value;
} else {
comments.push(
j.commentBlock(
' TODO manually deal with:\n' +
j(prop).toSource() +
'\n'
)
);
}
});
p.node.arguments[1] = fragments;
fragments.properties[0].comments = comments;
}).toSource(printOptions);
const match = file.path.match(/(\w+)\./);
const moduleName = match && match[1] || 'UNKNOWN';
// add fragment names
source = source.replace(
/\b(\w+): \(\w*\) => Relay\.QL(\s*)`\n( +)fragment( \w+)? on/g,
'$1: graphql`\n$2$3fragment ' + moduleName + '_$1 on'
);
return source;
};