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

overlookmotel / livepack / 7305779414

23 Dec 2023 02:56AM UTC coverage: 90.551% (-0.03%) from 90.578%
7305779414

push

github

overlookmotel
Capture `module` objects when function declaration called `module` at top level [fix]

Fixes #566.

4680 of 5034 branches covered (0.0%)

Branch coverage included in aggregate %.

15 of 15 new or added lines in 3 files covered. (100.0%)

35 existing lines in 15 files now uncovered.

12473 of 13909 relevant lines covered (89.68%)

8705.64 hits per line

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

98.95
/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;
54,492✔
25
        }
54,492✔
26

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

26,140✔
31
                // Check if JS reserved var name
26,140✔
32
                return isReservedVarName(name);
26,140✔
33
        }
29,198✔
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'
194✔
41
 *   - Then 'ba', 'bb', ...'bZ', ...'ZZ'
194✔
42
 *   - Then 'aaa', 'aab'...
62✔
43
 *
100,068✔
44
 * Names present in reserved names set and JS reserved words are avoided.
100,068✔
45
 *
100,068✔
46
 * @param {Set<string>} reservedNames - Set of reserved names
100,068✔
UNCOV
47
 */
×
48
class MangledVarNameFactory extends VarNameFactory {
100,068✔
49
        constructor(reservedNames) {
100,068✔
50
                super(reservedNames);
27,522✔
51
                this.counter = 0;
27,522✔
52
                this.privateCounter = 0;
27,522✔
53
        }
27,522✔
54

99,794✔
55
        transform(originalName, isPrivate) {
99,794✔
56
                let name;
12,834✔
57
                do {
12,834✔
58
                        let remainder = isPrivate ? this.privateCounter++ : this.counter++;
14,608✔
59
                        name = '';
14,608✔
60
                        while (true) { // eslint-disable-line no-constant-condition
14,608✔
61
                                const code = remainder % NUM_CHARS;
14,608✔
62

14,608✔
63
                                name = `${CHARS[code]}${name}`;
14,608✔
64

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

14,608✔
69
                        if (isPrivate) name = `${PRIVATE_PREFIX}${name}`;
14,608✔
70
                } while (this.isReserved(name));
12,834✔
71

12,834✔
72
                return name;
12,834✔
73
        }
12,834✔
74
}
62✔
75

62✔
76
function createMangledVarNameTransform(reservedNames) {
27,522✔
77
        const factory = new MangledVarNameFactory(reservedNames);
27,522✔
78
        return factory.transform.bind(factory);
138✔
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.
62✔
84
 * Where a name has been used before, name is postpended with '$0', '$1', '$2' etc
99,408✔
85
 *
99,408✔
86
 * Names present in reserved names set and JS reserved words are avoided.
99,408✔
87
 *
×
88
 * @param {Set<string>} reservedNames - Set of reserved names
99,408✔
89
 */
99,812✔
90
const NAME_REGEX = /^(.*?)(?:\$(\d+))?$/;
99,812✔
91

99,794✔
92
class UnmangledVarNameFactory extends VarNameFactory {
99,812✔
93
        constructor(reservedNames) {
199,088✔
94
                super(reservedNames);
26,970✔
95
                this.used = new Map();
26,970✔
96
        }
26,970✔
97

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

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

114✔
106
                let outName = nameWithoutPostfix;
114✔
107
                while (true) { // eslint-disable-line no-constant-condition
114✔
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

114✔
115
                return outName;
114✔
116
        }
114✔
117
}
114✔
118

114✔
119
function createUnmangledVarNameTransform(reservedNames) {
26,970✔
120
        const factory = new UnmangledVarNameFactory(reservedNames);
26,970✔
121
        return factory.transform.bind(factory);
26,970✔
122
}
26,970✔
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