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

overlookmotel / livepack / 6956398548

22 Nov 2023 11:03AM UTC coverage: 90.321% (-0.001%) from 90.322%
6956398548

push

github

overlookmotel
WIP 1

4746 of 5103 branches covered (0.0%)

Branch coverage included in aggregate %.

163 of 193 new or added lines in 11 files covered. (84.46%)

92 existing lines in 8 files now uncovered.

12667 of 14176 relevant lines covered (89.36%)

12765.82 hits per line

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

99.04
/lib/instrument/blocks.js
1
/* --------------------
62✔
2
 * livepack module
62✔
3
 * Code instrumentation block functions
62✔
4
 * ------------------*/
62✔
5

62✔
6
'use strict';
62✔
7

62✔
8
// Export
62✔
9
module.exports = {
62✔
10
        createBlock,
62✔
11
        createBlockWithId,
62✔
12
        createAndEnterBlock,
62✔
13
        createBlockId,
62✔
14
        createBinding,
62✔
15
        createBindingWithoutNameCheck,
62✔
16
        createThisBinding,
62✔
17
        createArgumentsBinding,
62✔
18
        createNewTargetBinding,
62✔
19
        getOrCreateExternalVar,
62✔
20
        activateBlock,
62✔
21
        activateBinding,
62✔
22
        createBlockTempVar
62✔
23
};
62✔
24

62✔
25
// Modules
62✔
26
const t = require('@babel/types');
62✔
27

62✔
28
// Imports
62✔
29
const {
62✔
30
                createScopeIdVarNode, createTempVarNode, checkInternalVarNameClash
62✔
31
        } = require('./internalVars.js'),
62✔
32
        {createArrayOrPush} = require('../shared/functions.js');
62✔
33

62✔
34
// Exports
62✔
35

62✔
36
/**
62✔
37
 * Create block.
62✔
38
 * @param {string} [name] - Block name
62✔
39
 * @param {boolean} isVarsBlock - `true` if is a vars block
62✔
40
 * @param {Object} state - State object
62✔
41
 * @returns {Object} - Block object
62✔
42
 */
62✔
43
function createBlock(name, isVarsBlock, state) {
91,432✔
44
        return createBlockWithId(createBlockId(state), name, isVarsBlock, state);
91,432✔
45
}
91,432✔
46

62✔
47
/**
62✔
48
 * Create block with specified ID.
62✔
49
 * @param {number} id - Block ID
62✔
50
 * @param {string} [name] - Block name
62✔
51
 * @param {boolean} isVarsBlock - `true` if is a vars block
62✔
52
 * @param {Object} state - State object
156✔
53
 * @returns {Object} - Block object
156✔
54
 */
156✔
55
function createBlockWithId(id, name, isVarsBlock, state) {
101,074✔
56
        const parent = state.currentBlock;
101,074✔
57
        const block = {
×
58
                id,
×
59
                name,
101,074✔
60
                parent,
101,074✔
61
                bindings: Object.create(null),
101,074✔
62
                varsBlock: undefined,
101,074✔
63
                scopeIdVarNode: undefined,
101,074✔
64
                tempVarNodes: undefined
101,074✔
65
        };
101,074✔
66
        block.varsBlock = isVarsBlock ? block : parent.varsBlock;
101,074✔
67
        return block;
101,074✔
68
}
101,074✔
69

62✔
70
/**
62✔
71
 * Create block and enter it.
62✔
72
 * @param {string} [name] - Block name
62✔
73
 * @param {boolean} isVarsBlock - `true` if is a vars block
62✔
74
 * @param {Object} state - State object
156✔
75
 * @returns {Object} - Block object
156✔
76
 */
156✔
77
function createAndEnterBlock(name, isVarsBlock, state) {
66,932✔
78
        // eslint-disable-next-line no-return-assign
66,932✔
79
        return state.currentBlock = createBlock(name, isVarsBlock, state);
66,932✔
80
}
66,932✔
81

156✔
82
/**
156✔
83
 * Create unique block ID.
156✔
84
 * @param {Object} state - State object
156✔
85
 * @returns {number} - Block ID
156✔
86
 */
156✔
87
function createBlockId(state) {
99,154✔
88
        return state.nextBlockId++;
99,154✔
89
}
99,154✔
90

156✔
91
/**
156✔
92
 * Create binding for variable in block.
156✔
93
 * Also check if `varName` potentially clashes with Livepack's internal vars.
62✔
94
 *
62✔
95
 * @param {Object} block - Block object
62✔
96
 * @param {string} varName - Variable name
62✔
97
 * @param {Object} props - Properties object
62✔
98
 * @param {boolean} [props.isConst=false] - `true` if is const
62✔
99
 * @param {boolean} [props.isSilentConst=false] - `true` if is function expression name,
98✔
100
 *   assignment to which fails silently in sloppy mode
98✔
101
 * @param {boolean} [props.isVar=false] - `true` if defined with `var` statement or function declaration
98✔
102
 * @param {boolean} [props.isFrozenName=false] - `true` if is var which is not renamed when used
62✔
103
 *   inside function in which it's bound
62✔
104
 * @param {Array<string>} [props.argNames] - Linked argument names (only relevant for `arguments`)
62✔
105
 * @param {Object} state - State object
190✔
106
 * @returns {Object} - Binding object
190✔
107
 */
190✔
108
function createBinding(block, varName, props, state) {
61,348✔
109
        checkInternalVarNameClash(varName, state);
61,348✔
110
        return createBindingWithoutNameCheck(block, varName, props);
61,348✔
111
}
61,348✔
112

62✔
113
/**
62✔
114
 * Create binding for variable in block.
62✔
115
 * Do not check if `varName` potentially clashes with Livepack's internal vars.
62✔
116
 * Intended to be called for vars which it's known are non-clashing e.g. `arguments`.
62✔
117
 *
62✔
118
 * @param {Object} block - Block object
62✔
119
 * @param {string} varName - Variable name
62✔
120
 * @param {Object} props - Properties object
62✔
121
 * @param {Object} [props.varNode] - Identifier AST node for var which will go in tracker function call
62✔
122
 * @param {boolean} [props.isConst=false] - `true` if is const
62✔
123
 * @param {boolean} [props.isSilentConst=false] - `true` if is function expression name,
62✔
124
 *   assignment to which fails silently in sloppy mode
62✔
125
 * @param {boolean} [props.isVar=false] - `true` if defined with `var` statement or function declaration
62✔
126
 * @param {boolean} [props.isFrozenName=false] - `true` if is var which is not renamed when used
64✔
127
 *   inside function in which it's bound
64✔
128
 * @param {Array<string>} [props.argNames] - Linked argument names (only relevant for `arguments`)
64✔
129
 * @returns {Object} - Binding object
64✔
130
 */
64✔
131
function createBindingWithoutNameCheck(block, varName, props) {
150,506✔
132
        return block.bindings[varName] = { // eslint-disable-line no-return-assign
150,506✔
133
                name: varName,
150,506✔
134
                varNode: props.varNode,
150,506✔
135
                isConst: !!props.isConst,
150,506✔
136
                isSilentConst: !!props.isSilentConst,
150,506✔
137
                isVar: !!props.isVar,
150,506✔
138
                isFrozenName: !!props.isFrozenName,
150,506✔
139
                argNames: props.argNames,
150,506✔
140
                trails: [] // Trails of usages within same function as binding, where var not function or frozen
150,506✔
141
        };
150,506✔
142
}
150,506✔
143

62✔
144
/**
62✔
145
 * Create binding for `this`.
62✔
146
 * @param {Object} block - Block object
62✔
147
 * @returns {Object} - Binding object
62✔
148
 */
62✔
149
function createThisBinding(block) {
21,706✔
150
        // `isFrozenName: true` because it cannot be renamed within function in which it's created.
21,706✔
151
        // If `this` has to be substituted for a temp var for transpiled `super()`,
21,706✔
152
        // `isFrozenName` will be set to false.
21,706✔
153
        return createBindingWithoutNameCheck(
21,706✔
154
                // eslint-disable-next-line no-use-before-define
21,706✔
155
                block, 'this', {varNode: thisNode, isConst: true, isFrozenName: true}
21,706✔
156
        );
21,706✔
157
}
21,706✔
158

62✔
159
const thisNode = t.thisExpression();
62✔
160

62✔
161
/**
62✔
162
 * Create binding for function `arguments`.
62✔
163
 * @param {Object} block - Block object
62✔
164
 * @param {boolean} isConst - `true` if is const (strict mode)
62✔
165
 * @param {Array<string>} argNames - Linked argument names
154✔
166
 * @returns {Object} - Binding object
154✔
167
 */
154✔
168
function createArgumentsBinding(block, isConst, argNames) {
19,346✔
169
        // `isFrozenName: true` because it cannot be renamed within function in which it's created
19,346✔
170
        return createBindingWithoutNameCheck(block, 'arguments', {isConst, isFrozenName: true, argNames});
19,346✔
171
}
19,346✔
172

154✔
173
/**
154✔
174
 * Create binding for `new.target`.
154✔
175
 * @param {Object} block - Block object
154✔
176
 * @returns {Object} - Binding object
154✔
177
 */
154✔
178
function createNewTargetBinding(block) {
21,706✔
179
        // `isFrozenName: true` because it cannot be renamed within function in which it's created
21,706✔
180
        /* eslint-disable no-use-before-define */
21,706✔
181
        newTargetNode ||= t.metaProperty(t.identifier('new'), t.identifier('target'));
21,706✔
182
        return createBindingWithoutNameCheck(
28✔
183
                block, 'new.target', {varNode: newTargetNode, isConst: true, isFrozenName: true}
28✔
NEW
184
        );
×
185
        /* eslint-enable no-use-before-define */
28✔
186
}
28✔
187

28✔
188
let newTargetNode;
28✔
189

28✔
190
/**
28✔
191
 * Get or create an external var in a function.
28✔
192
 * @param {Map} externalVars - External vars for function. Map keyed by block object.
28✔
193
 * @param {Object} block - Block object
28✔
194
 * @param {string} varName - Var name
28✔
195
 * @param {Object} binding - Binding object
28✔
196
 * @returns {Object} - External var object
28✔
197
 */
28✔
198
function getOrCreateExternalVar(externalVars, block, varName, binding) {
53,660✔
199
        let blockVars = externalVars.get(block),
53,660✔
200
                externalVar;
53,660✔
201
        if (blockVars) {
53,660✔
202
                externalVar = blockVars[varName];
30,312✔
203
        } else {
53,660✔
204
                blockVars = Object.create(null);
23,348✔
205
                externalVars.set(block, blockVars);
23,348✔
206
        }
23,348✔
207

53,660✔
208
        if (!externalVar) {
53,660✔
209
                externalVar = {
40,188✔
210
                        binding,
40,188✔
211
                        isReadFrom: false,
40,188✔
212
                        isAssignedTo: false,
40,188✔
213
                        isFrozenName: false,
24✔
214
                        trails: []
24✔
215
                };
24✔
216
                blockVars[varName] = externalVar;
24✔
217
        }
24✔
218
        return externalVar;
24✔
219
}
24✔
220

24✔
221
/**
24✔
222
 * Activate block.
24✔
223
 * Called when block contains a binding which is referenced within a function.
24✔
224
 * @param {Object} block - Block object
24✔
225
 * @param {Object} state - State object
24✔
226
 * @returns {undefined}
24✔
227
 */
24✔
228
function activateBlock(block, state) {
41,382✔
229
        const {varsBlock} = block;
41,382✔
230
        if (!varsBlock.scopeIdVarNode) varsBlock.scopeIdVarNode = createScopeIdVarNode(varsBlock.id, state);
41,382✔
231
}
41,382✔
232

62✔
233
/**
62✔
234
 * Activate binding.
62✔
235
 * Called when a binding is accessed from within a function.
62✔
236
 * @param {Object} binding - Binding object
28✔
237
 * @param {string} varName - Var name
28✔
238
 * @returns {undefined}
28✔
239
 */
28✔
240
function activateBinding(binding, varName) {
39,798✔
241
        if (!binding.varNode) binding.varNode = t.identifier(varName);
39,798✔
242
}
39,798✔
243

28✔
244
/**
28✔
245
 * Create temp var in block.
28✔
246
 * @param {Object} block - Block object
28✔
247
 * @param {Object} state - State object
28✔
248
 * @returns {undefined}
28✔
249
 */
28✔
250
function createBlockTempVar(block, state) {
882✔
251
        const tempVarNode = createTempVarNode(state);
882✔
252
        createArrayOrPush(block.varsBlock, 'tempVarNodes', tempVarNode);
882✔
253
        return tempVarNode;
882✔
254
}
882✔
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

© 2024 Coveralls, Inc