• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

overlookmotel / livepack / 6937739311

21 Nov 2023 12:48AM UTC coverage: 90.481% (+0.01%) from 90.467%
6937739311

push

github

overlookmotel
Dev: Update dev dependencies

4700 of 5049 branches covered (0.0%)

Branch coverage included in aggregate %.

12609 of 14081 relevant lines covered (89.55%)

12859.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.35
/lib/serialize/varNames.js
1
/* --------------------
62✔
2
 * livepack module
62✔
3
 * Functions to create unique var names.
62✔
4
 * ------------------*/
62✔
5

62✔
6
'use strict';
62✔
7

62✔
8
// Imports
62✔
9
const {isReservedVarName} = require('../shared/functions.js');
62✔
10

62✔
11
// Exports
62✔
12

62✔
13
const CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
62✔
14
        NUM_CHARS = CHARS.length,
62✔
15
        PRIVATE_PREFIX = '_';
62✔
16

62✔
17
/**
62✔
18
 * Factory superclass.
62✔
19
 * Extended by `MangledVarNameFactory` and `UnmangledVarNameFactory`.
62✔
20
 * @param {Set<string>} reservedNames - Set of reserved names
62✔
21
 */
62✔
22
class VarNameFactory {
62✔
23
        constructor(reservedNames) {
62✔
24
                this.reservedNames = reservedNames;
104,906✔
25
        }
104,906✔
26

62✔
27
        isReserved(name) {
62✔
28
                // Check if reserved name
111,878✔
29
                if (this.reservedNames?.has(name)) return true;
111,878✔
30

103,232✔
31
                // Check if JS reserved var name
103,232✔
32
                return isReservedVarName(name);
103,232✔
33
        }
111,878✔
34
}
62✔
35

62✔
36
/**
62✔
37
 * Factory for mangled var names.
62✔
38
 *   - First name is 'a'
62✔
39
 *   - Then 'b', 'c', ...'z', 'A', 'B', 'C', ...'Z'
62✔
40
 *   - Then 'aa', 'ab', ...'aZ'
62✔
41
 *   - Then 'ba', 'bb', ...'bZ', ...'ZZ'
194✔
42
 *   - Then 'aaa', 'aab'...
194✔
43
 *
194✔
44
 * Names present in reserved names set and JS reserved words are avoided.
62✔
45
 *
100,068✔
46
 * @param {Set<string>} reservedNames - Set of reserved names
100,068✔
47
 */
100,068✔
48
class MangledVarNameFactory extends VarNameFactory {
100,068✔
49
        constructor(reservedNames) {
✔
50
                super(reservedNames);
52,654✔
51
                this.counter = 0;
52,654✔
52
                this.privateCounter = 0;
52,654✔
53
        }
52,654✔
54

100,068✔
55
        transform(originalName, isPrivate) {
100,068✔
56
                let name;
50,610✔
57
                do {
50,610✔
58
                        let remainder = isPrivate ? this.privateCounter++ : this.counter++;
54,648✔
59
                        name = '';
54,648✔
60
                        while (true) { // eslint-disable-line no-constant-condition
54,648✔
61
                                const code = remainder % NUM_CHARS;
54,648✔
62

54,648✔
63
                                name = `${CHARS[code]}${name}`;
54,648✔
64

54,648✔
65
                                remainder = (remainder - code) / NUM_CHARS - 1;
54,648✔
66
                                if (remainder === -1) break;
54,648✔
67
                        }
54,648✔
68

54,648✔
69
                        if (isPrivate) name = `${PRIVATE_PREFIX}${name}`;
54,648✔
70
                } while (this.isReserved(name));
50,610✔
71

50,610✔
72
                return name;
50,610✔
73
        }
50,610✔
74
}
62✔
75

62✔
76
function createMangledVarNameTransform(reservedNames) {
52,654✔
77
        const factory = new MangledVarNameFactory(reservedNames);
52,654✔
78
        return factory.transform.bind(factory);
52,654✔
79
}
138✔
80

138✔
81
/**
138✔
82
 * Factory for unmangled var names.
138✔
83
 * Aims to not change var names if possible, but ensure all names are unique.
138✔
84
 * Where a name has been used before, name is postpended with '$0', '$1', '$2' etc
62✔
85
 *
99,408✔
86
 * Names present in reserved names set and JS reserved words are avoided.
99,408✔
87
 *
99,408✔
88
 * @param {Set<string>} reservedNames - Set of reserved names
99,408✔
89
 */
99,408✔
90
const NAME_REGEX = /^(.*?)(?:\$(\d+))?$/;
99,408✔
91

×
92
class UnmangledVarNameFactory extends VarNameFactory {
99,408✔
93
        constructor(reservedNames) {
99,812✔
94
                super(reservedNames);
52,252✔
95
                this.used = new Map();
52,252✔
96
        }
52,252✔
97

99,812✔
98
        transform(name, isPrivate) {
99,812✔
99
                if (isPrivate) name = `${PRIVATE_PREFIX}${name}`;
50,654✔
100
                const [, nameWithoutPostfix, numStr] = name.match(NAME_REGEX);
50,654✔
101
                let num = numStr ? numStr * 1 : -1;
50,654✔
102

50,654✔
103
                const usedNum = this.used.get(nameWithoutPostfix);
50,654✔
104
                if (usedNum !== undefined && num <= usedNum) num = usedNum + 1;
50,654✔
105

50,654✔
106
                let outName = nameWithoutPostfix;
50,654✔
107
                while (true) { // eslint-disable-line no-constant-condition
50,654✔
108
                        if (num > -1) outName = `${nameWithoutPostfix}$${num}`;
114✔
109
                        if (!this.isReserved(outName)) break;
114✔
110
                        num++;
114✔
111
                }
114✔
112

114✔
113
                this.used.set(nameWithoutPostfix, num);
114✔
114

×
115
                return outName;
×
116
        }
×
117
}
×
118

×
119
function createUnmangledVarNameTransform(reservedNames) {
52,252✔
120
        const factory = new UnmangledVarNameFactory(reservedNames);
52,252✔
121
        return factory.transform.bind(factory);
52,252✔
122
}
52,252✔
123

62✔
124
module.exports = {
62✔
125
        createMangledVarNameTransform,
62✔
126
        createUnmangledVarNameTransform,
62✔
127
        // Classes only exported for unit testing
62✔
128
        MangledVarNameFactory,
62✔
129
        UnmangledVarNameFactory
62✔
130
};
62✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc