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

overlookmotel / livepack / 7077920486

03 Dec 2023 04:01PM UTC coverage: 90.344% (-0.02%) from 90.361%
7077920486

push

github

overlookmotel
Don't transpile `super()` in class constructors [fix]

Fixes #541.
Fixes #540.
Fixes #341.

4611 of 4962 branches covered (0.0%)

Branch coverage included in aggregate %.

78 of 84 new or added lines in 8 files covered. (92.86%)

57 existing lines in 8 files now uncovered.

12446 of 13918 relevant lines covered (89.42%)

13081.48 hits per line

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

99.47
/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;
107,338✔
25
        }
107,338✔
26

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

102,072✔
31
                // Check if JS reserved var name
102,072✔
32
                return isReservedVarName(name);
102,072✔
33
        }
110,378✔
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'...
194✔
43
 *
62✔
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) {
100,068✔
50
                super(reservedNames);
53,870✔
51
                this.counter = 0;
53,870✔
52
                this.privateCounter = 0;
53,870✔
53
        }
53,870✔
54

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

53,756✔
63
                                name = `${CHARS[code]}${name}`;
53,756✔
64

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

53,756✔
69
                        if (isPrivate) name = `${PRIVATE_PREFIX}${name}`;
53,756✔
70
                } while (this.isReserved(name));
50,246✔
71

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

62✔
76
function createMangledVarNameTransform(reservedNames) {
53,870✔
77
        const factory = new MangledVarNameFactory(reservedNames);
53,870✔
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✔
UNCOV
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);
53,468✔
95
                this.used = new Map();
53,468✔
96
        }
53,468✔
97

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

50,290✔
103
                const usedNum = this.used.get(nameWithoutPostfix);
50,290✔
104
                if (usedNum !== undefined && num <= usedNum) num = usedNum + 1;
50,290✔
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) {
53,468✔
120
        const factory = new UnmangledVarNameFactory(reservedNames);
53,468✔
121
        return factory.transform.bind(factory);
53,468✔
122
}
53,468✔
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