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

overlookmotel / livepack / 6864299412

14 Nov 2023 01:27PM UTC coverage: 90.441% (-0.09%) from 90.526%
6864299412

push

github

overlookmotel
Avoid string concatenation in passing assertions [perf]

4644 of 4995 branches covered (0.0%)

Branch coverage included in aggregate %.

19 of 23 new or added lines in 5 files covered. (82.61%)

16 existing lines in 4 files now uncovered.

12585 of 14055 relevant lines covered (89.54%)

12924.85 hits per line

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

95.95
/lib/serialize/split.js
1
/* --------------------
62✔
2
 * livepack module
62✔
3
 * `split` + `splitAsync` functions, and split methods
62✔
4
 * ------------------*/
62✔
5

62✔
6
'use strict';
62✔
7

62✔
8
// Modules
62✔
9
const assert = require('simple-invariant'),
62✔
10
        {isFullString} = require('is-it-type'),
62✔
11
        t = require('@babel/types');
62✔
12

62✔
13
// Imports
62✔
14
const {splitPoints, functions: specialFunctions} = require('../shared/internal.js'),
62✔
15
        createModulePromise = require('./createModule.js'),
62✔
16
        {isPrimitive} = require('../shared/functions.js');
62✔
17

62✔
18
// Exports
62✔
19

62✔
20
module.exports = {
62✔
21
        split,
62✔
22
        splitAsync,
62✔
23
        // `methods` is merged into `Serializer` class prototype
62✔
24
        methods: {
62✔
25
                initSplits,
62✔
26
                serializeSplitAsyncFunction
62✔
27
        }
62✔
28
};
62✔
29

62✔
30
/**
62✔
31
 * Create a split point (code splitting).
62✔
32
 * @param {*} val - Value to split at
62✔
33
 * @param {string} [name] - Split point name
62✔
34
 * @returns {*} - Input
62✔
35
 */
62✔
36
function split(val, name) {
1,176✔
37
        name = validateInput(val, name, 'split');
1,176✔
38
        getOrCreateSplitPoint(val, name);
1,176✔
39
        return val;
1,176✔
40
}
1,176✔
41

62✔
42
/**
62✔
43
 * Create a split point (code splitting) for async loading.
62✔
44
 * Returns a function with same behavior as `import()` - it returns
62✔
45
 * a Promise of a module namespace object with the value as default export.
62✔
46
 * Each call returns a new function, and each call to that function returns a new promise,
62✔
47
 * but the module object is always the same for same value.
62✔
48
 * @param {*} val - Value to split at
62✔
49
 * @param {string} [name] - Split point name
62✔
50
 * @returns {Function} - Import function
62✔
51
 */
62✔
52
function splitAsync(val, name) {
2,336✔
53
        name = validateInput(val, name, 'splitAsync');
2,336✔
54

2,336✔
55
        const splitPoint = getOrCreateSplitPoint(val, name);
2,336✔
56

2,336✔
57
        // Create import function
2,336✔
58
        const importFn = (0, () => {
2,336✔
59
                const {moduleObj, modulePromise} = splitPoint;
2,240✔
60
                if (!moduleObj && !modulePromise) return createModulePromise(val, splitPoint);
2,240✔
61
                if (moduleObj) return Promise.resolve(moduleObj);
272✔
62
                return modulePromise.then(mod => mod);
24✔
63
        });
2,336✔
64

2,336✔
65
        // Record import function in special functions
2,336✔
66
        specialFunctions.set(importFn, {type: 'splitAsync', val, splitPoint});
2,336✔
67

2,336✔
68
        return importFn;
2,336✔
69
}
2,336✔
70

62✔
71
function validateInput(val, name, fnName) {
3,512✔
72
        assert(!isPrimitive(val), 'Cannot split on primitive values');
3,512✔
73

3,512✔
74
        if (name == null) {
3,512✔
75
                name = undefined;
1,434✔
76
        } else if (name !== undefined && !isFullString(name)) {
3,512!
NEW
77
                throw new Error(`\`name\` argument to \`${fnName}\` must be a non-empty string if provided`);
×
UNCOV
78
        }
×
79
        return name;
3,512✔
80
}
3,512✔
81

62✔
82
function getOrCreateSplitPoint(val, name) {
3,512✔
83
        let splitPoint = splitPoints.get(val);
3,512✔
84
        if (!splitPoint) {
3,512✔
85
                splitPoint = {
3,360✔
86
                        name: name || undefined,
3,360✔
87
                        moduleObj: undefined,
3,360✔
88
                        modulePromise: undefined
3,360✔
89
                };
3,360✔
90
                splitPoints.set(val, splitPoint);
3,360✔
91
        } else if (!splitPoint.name && name) {
3,512!
92
                splitPoint.name = name;
×
93
        }
×
94
        return splitPoint;
3,512✔
95
}
3,512✔
96

62✔
97
/*
62✔
98
 * Methods to merge into `Serializer` class prototype
62✔
99
 */
62✔
100

62✔
101
/* eslint-disable no-invalid-this */
62✔
102
function initSplits() {
26,990✔
103
        this.splitImportFns = [];
26,990✔
104
}
26,990✔
105

62✔
106
function serializeSplitAsyncFunction({val, splitPoint}, importFnRecord) {
2,066✔
107
        this.splitImportFns.push({importFnRecord, val, splitPoint});
2,066✔
108
        importFnRecord.scope = this.rootScope; // Ensure does not get implicitly named
2,066✔
109
        return t.identifier('x'); // Will be replaced later
2,066✔
110
}
2,066✔
111
/* eslint-enable no-invalid-this */
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