-
Notifications
You must be signed in to change notification settings - Fork 2
/
setter-deprecation.js
89 lines (78 loc) · 2.24 KB
/
setter-deprecation.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
const chalk = require('chalk');
const diff = require('jest-diff');
const path = require('path');
const { findTiAppAndSdkVersion, lookupCall, loadJSCA } = require('./utils/transform-utils');
module.exports = function (file, api, options) {
let madeChanges = false;
let sdkInfo;
let relativePath;
if (!options.sdkPath) {
const projectInfo = findTiAppAndSdkVersion(file.path);
relativePath = path.resolve(projectInfo.projectDirectory, file.path);
sdkInfo = projectInfo.sdkInfo;
}
// Check and read in our JSCA file for checking against;
loadJSCA(sdkInfo, options);
const j = api.jscodeshift;
const root = j(file.source);
const original = root.toSource();
try {
root.find(j.CallExpression)
.forEach(convertToPropertyAccess);
} catch (error) {
console.error(`Ran into problem transforming ${file.path}`);
console.error(error);
}
function convertToPropertyAccess(node) {
// Get the object that the function is called on
const object = node.value.callee.object;
// Get the function call
const call = node.value.callee.property;
// If it's just a function call like openWindow() that has not object
if (!call) {
return;
}
// If it's like $.placeholder[sections.length > 0 ? 'hide' : 'show']();
if (!call.name) {
return;
}
// We only care about getters
if (!call.name.startsWith('set')) {
return;
}
// Look for a potential replacement by looping through the api.jsca file
const replacement = lookupCall(object, call.name);
if (!replacement) {
return;
}
// Construct our new expression
const statement = j.memberExpression(
object,
j.assignmentExpression(
'=',
j.identifier(replacement),
...node.value.arguments
)
);
// Copy over comments to the new expression to ensure code stays the same
statement.comments = node.value.comments;
// Replace the statement
j(node)
.replaceWith(
statement
);
madeChanges = true;
}
const changedSource = root.toSource();
if (madeChanges && options.dry) {
const changes = diff(changedSource, original, {
aAnnotation: 'After',
bAnnotation: 'Before',
expand: false,
contextLines: 1
});
console.log(`Showing changes for ${chalk.cyan(relativePath)}`);
console.log(changes);
}
return madeChanges ? changedSource : null;
};