-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
61 lines (49 loc) · 1.5 KB
/
index.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
// @flow
'use strict';
function privateUnderscores({ types: t, template } /*: any */) {
let buildSym = template(`
let ID = Symbol(SYM);
`);
return {
name: "private-underscores",
visitor: {
Class(classPath /*: any */) {
let members = {};
let program = classPath.hub.file.path;
function createReference(name) {
let ref = classPath.scope.generateUidIdentifier(name);
program.get('body')[0].insertBefore(buildSym({
ID: ref,
SYM: t.stringLiteral(name)
}));
return ref;
}
classPath.traverse({
'ClassMethod|ClassProperty'(memberPath) {
if (memberPath.node.computed) return;
let name = memberPath.node.key.name;
if (!name.startsWith('_')) return;
let ref = createReference(name);
members[name] = ref;
memberPath.node.computed = true;
memberPath.get('key').replaceWith(ref);
},
});
classPath.traverse({
ThisExpression(path) {
let parent = path.parentPath;
if (parent.isMemberExpression() && !parent.node.computed) {
let property = parent.get('property');
let ref = members[property.node.name];
if (ref) {
parent.node.computed = true;
property.replaceWith(ref)
}
}
},
});
},
},
};
}
module.exports = privateUnderscores;