1 | "use strict";
|
2 |
|
3 | const rename = require('./renamer');
|
4 |
|
5 | const t = require('@babel/types');
|
6 |
|
7 | const CHARSET = ('abcdefghijklmnopqrstuvwxyz' + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ$_').split('');
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function mangleScope(scope) {
|
18 | let newNames = new Set();
|
19 |
|
20 | let sortedBindings = Object.keys(scope.bindings).sort((a, b) => scope.bindings[b].referencePaths.length - scope.bindings[a].referencePaths.length);
|
21 | var _iteratorNormalCompletion = true;
|
22 | var _didIteratorError = false;
|
23 | var _iteratorError = undefined;
|
24 |
|
25 | try {
|
26 | for (var _iterator = sortedBindings[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
27 | let oldName = _step.value;
|
28 | let i = 0;
|
29 | let newName = '';
|
30 |
|
31 | do {
|
32 | newName = getIdentifier(i++);
|
33 | } while (newNames.has(newName) || !canRename(scope, scope.bindings[oldName], newName));
|
34 |
|
35 | rename(scope, oldName, newName);
|
36 | newNames.add(newName);
|
37 | }
|
38 | } catch (err) {
|
39 | _didIteratorError = true;
|
40 | _iteratorError = err;
|
41 | } finally {
|
42 | try {
|
43 | if (!_iteratorNormalCompletion && _iterator.return != null) {
|
44 | _iterator.return();
|
45 | }
|
46 | } finally {
|
47 | if (_didIteratorError) {
|
48 | throw _iteratorError;
|
49 | }
|
50 | }
|
51 | }
|
52 | }
|
53 |
|
54 | function getIdentifier(num) {
|
55 | let ret = '';
|
56 | num++;
|
57 |
|
58 | do {
|
59 | num--;
|
60 | ret += CHARSET[num % CHARSET.length];
|
61 | num = Math.floor(num / CHARSET.length);
|
62 | } while (num > 0);
|
63 |
|
64 | return ret;
|
65 | }
|
66 |
|
67 | function canRename(scope, binding, newName) {
|
68 | if (!t.isValidIdentifier(newName)) {
|
69 | return false;
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 | for (let i = 0; i < binding.referencePaths.length; i++) {
|
75 | const ref = binding.referencePaths[i];
|
76 |
|
77 | if (ref.scope.hasBinding(newName) || ref.scope.hasReference(newName)) {
|
78 | return false;
|
79 | }
|
80 | }
|
81 |
|
82 | return true;
|
83 | }
|
84 |
|
85 | module.exports = mangleScope; |
\ | No newline at end of file |