• 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

79.61
/lib/shared/functions.js
1
/* --------------------
62✔
2
 * livepack module
62✔
3
 * Shared functions
62✔
4
 * ------------------*/
62✔
5

62✔
6
'use strict';
62✔
7

62✔
8
// Modules
62✔
9
const checkReservedWord = require('reserved-words').check,
62✔
10
        {VISITOR_KEYS, COMMENT_KEYS} = require('@babel/types'),
62✔
11
        {isArray, isSymbol} = require('is-it-type');
62✔
12

62✔
13
// Exports
62✔
14

62✔
15
module.exports = {
62✔
16
        isPrimitive,
62✔
17
        isReservedWord,
62✔
18
        isReservedVarName,
62✔
19
        createArrayOrPush,
62✔
20
        combineArraysWithDedup,
62✔
21
        getProp,
62✔
22
        getProps,
62✔
23
        setProp,
62✔
24
        traverseAll
62✔
25
};
62✔
26

62✔
27
/**
62✔
28
 * Determine if value is a primitive.
62✔
29
 * @param {*} val - Value
62✔
30
 * @returns {boolean} - `true` if value is a primitive
62✔
31
 */
62✔
32
function isPrimitive(val) {
402,826✔
33
        // Symbols are not considered primitives as they need to be uniquely referenced.
402,826✔
34
        // `undefined` + `NaN` + `Infinity` not considered primitives as they are global vars.
402,826✔
35
        return val !== undefined
402,826✔
36
                && !isSymbol(val)
402,826✔
37
                && !Number.isNaN(val)
402,826✔
38
                && val !== Infinity && val !== -Infinity
✔
39
                && Object(val) !== val;
✔
40
}
×
41

×
42
/**
×
43
 * Determine if var name is a JS reserved word e.g. 'break', 'class'.
×
44
 * Includes names which are reserved only in strict mode e.g. `package`.
×
45
 * @param {string} name - Variable name
×
46
 * @returns {boolean} - `true` if reserved word, `false` if not
×
47
 */
×
48
function isReservedWord(name) {
33,482✔
49
        return checkReservedWord(name, 'es6', true);
33,482✔
50
}
33,482✔
51

×
52
/**
×
53
 * Determine if name should not be used as a var name.
×
54
 * Includes all JS reserved words, plus 'arguments' + 'eval' are additionally prohibited
62✔
55
 * as cannot initialize vars with these names in strict mode.
62✔
56
 * @param {string} name - Variable name
62✔
57
 * @returns {boolean} - `true` if reserved var name, `false` if not
62✔
58
 */
62✔
59
function isReservedVarName(name) {
30,192✔
60
        return name === 'arguments' || name === 'eval' || isReservedWord(name);
99,794✔
61
}
99,794✔
62

99,794✔
63
/**
99,794✔
64
 * Create an array as `obj[key]` or push elements to array if already exists.
99,794✔
65
 * @param {Object} obj - Object
99,794✔
66
 * @param {string} key - Key
99,794✔
67
 * @param {...any} values - Values to add to array
62✔
68
 * @returns {undefined}
62✔
69
 */
62✔
70
function createArrayOrPush(obj, key, ...values) {
1,058✔
71
        const arr = obj[key];
1,058✔
72
        if (arr) {
1,058✔
73
                arr.push(...values);
46✔
74
        } else {
1,058✔
75
                obj[key] = values;
1,012✔
76
        }
1,012✔
77
}
1,058✔
78

62✔
79
/**
62✔
80
 * Combine two arrays, removing duplicates.
62✔
81
 * If either array is `undefined` / `null`, treat it as an empty array.
62✔
82
 * @param {Array} [arr1] - Array 1
99,794✔
83
 * @param {Array} [arr2] - Array 2
99,794✔
84
 * @returns {Array} - New array combining elements of both arrays with duplicates removed
99,794✔
85
 */
×
86
function combineArraysWithDedup(arr1, arr2) {
8✔
87
        if (!arr1) return arr2 ? [...arr2] : [];
8!
88
        if (!arr2) return [...arr1];
8✔
89

×
90
        return [...new Set([...arr1, ...arr2])];
×
91
}
8✔
92

62✔
93
/**
62✔
94
 * Get deep property of object, specified by key trail.
62✔
95
 * Simplified version of `lodash.get()` (https://lodash.com/docs/4.17.15#get).
62✔
96
 * e.g. `getProp({x: [{y: 1}]}, ['x', 0, 'y'])` returns `1`.
62✔
97
 * @param {Object} obj - Object
×
98
 * @param {Array<string|number>} trail - Array of trail segements
×
99
 * @param {number} [len=trail.length] - Stop after `len` number of keys
×
100
 * @returns {*} - Value
×
101
 */
×
102
function getProp(obj, trail, len) {
18,430✔
103
        if (len == null) len = trail.length;
18,430✔
104
        for (let i = 0; i < len; i++) {
18,430✔
105
                obj = obj[trail[i]];
72,174✔
106
        }
72,174✔
107
        return obj;
18,430✔
108
}
18,430✔
109

62✔
110
/**
62✔
111
 * Get deep properties of object, specified by key trail.
62✔
112
 * Returns all properties on trail, including the original object.
62✔
113
 * e.g. `getProps({x: {y: 1}}, ['x', 'y'])` returns `[{x: {y: 1}}, {y: 1}, 1]`.
62✔
114
 * @param {Object} obj - Object
×
115
 * @param {Array<string|number>} trail - Array of trail segements
×
116
 * @param {number} [len=trail.length] - Stop after `len` number of keys
×
117
 * @returns {Array<*>} - Array of values
×
118
 */
×
119
function getProps(obj, trail, len) {
1,224✔
120
        if (len == null) len = trail.length;
1,224✔
121
        const trailObjs = [obj];
1,224✔
122
        let i = 0;
1,224✔
123
        while (i < len) {
1,224✔
124
                obj = obj[trail[i]];
6,120✔
125
                i++;
6,120✔
126
                trailObjs[i] = obj;
6,120✔
127
        }
6,120✔
128
        return trailObjs;
1,224✔
129
}
1,224✔
130

62✔
131
/**
62✔
132
 * Set deep property of object, specified by key trail.
62✔
133
 * Simplified version of `lodash.set()` (https://lodash.com/docs/4.17.15#set).
62✔
134
 * e.g. `setProp({x: [{y: 1}]}, ['x', 0, 'y'], 2)` mutates object to `{x: [{y: 2}]}`.
62✔
135
 * @param {Object} obj - Object
62✔
UNCOV
136
 * @param {Array<string|number>} trail - Array of trail segements
×
137
 * @param {*} value - Value to set
×
138
 * @param {number} [len=trail.length] - Stop after `len` number of keys
×
139
 * @returns {undefined}
×
140
 */
×
141
function setProp(obj, trail, value, len) {
2,852✔
142
        len = (len == null ? trail.length : len) - 1;
2,852!
143
        getProp(obj, trail, len)[trail[len]] = value;
2,852✔
144
}
2,852✔
145

62✔
146
/**
62✔
147
 * Traverse Babel AST, calling callback `visit()` with every node.
62✔
148
 * Unlike Babel's `traverse()`, also traverses over comment nodes.
62✔
149
 * Comments are visited before child nodes.
62✔
150
 * @param {Object} ast - Babel AST
62✔
151
 * @param {Function} visit - Visitor function, called with each node
62✔
152
 * @returns {undefined}
62✔
153
 */
62✔
154
function traverseAll(ast, visit) {
15,682✔
155
        const queue = [];
15,682✔
UNCOV
156
        let node = ast;
×
157
        do {
✔
158
                visit(node);
×
159

×
160
                for (let i = NUM_COMMENT_KEYS_MINUS_ONE; i >= 0; i--) { // eslint-disable-line no-use-before-define
✔
161
                        const arr = node[COMMENT_KEYS[i]];
×
162
                        if (arr) {
✔
163
                                for (let j = arr.length - 1; j >= 0; j--) {
✔
164
                                        visit(arr[j]);
×
165
                                }
×
166
                        }
×
167
                }
×
168

×
169
                const keys = VISITOR_KEYS[node.type];
×
170
                if (keys) {
×
171
                        for (let i = keys.length - 1; i >= 0; i--) {
544,122✔
172
                                const childNode = node[keys[i]];
1,061,916✔
173
                                if (childNode) {
1,061,916✔
174
                                        if (isArray(childNode)) {
514,424✔
175
                                                for (let j = childNode.length - 1; j >= 0; j--) {
143,348✔
176
                                                        const containerChildNode = childNode[j];
158,730✔
177
                                                        if (containerChildNode) queue.push(containerChildNode);
158,730✔
178
                                                }
158,730✔
179
                                        } else {
514,424✔
180
                                                queue.push(childNode);
371,076✔
181
                                        }
371,076✔
182
                                }
514,424✔
183
                        }
1,061,916✔
184
                }
544,122✔
185
        } while (node = queue.pop()); // eslint-disable-line no-cond-assign
15,682✔
186
}
15,682✔
187

62✔
188
const NUM_COMMENT_KEYS_MINUS_ONE = COMMENT_KEYS.length - 1;
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

© 2025 Coveralls, Inc