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

overlookmotel / livepack / 6449138139

08 Oct 2023 05:55PM UTC coverage: 67.988% (+0.01%) from 67.977%
6449138139

push

github

overlookmotel
Test changes

Merge as many of these changes as possible into `dev` branch.

1743 of 2005 branches covered (0.0%)

Branch coverage included in aggregate %.

10677 of 16263 relevant lines covered (65.65%)

2187.65 hits per line

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

63.04
/lib/instrument/sourceMaps.js
1
/* --------------------
31✔
2
 * livepack module
31✔
3
 * Code instrumentation source maps functions
31✔
4
 * ------------------*/
31✔
5

31✔
6
'use strict';
31✔
7

31✔
8
// Modules
31✔
9
const {join: pathJoin, dirname, isAbsolute: pathIsAbsolute} = require('path'),
31✔
10
        {readFileSync} = require('fs'),
31✔
11
        {
31✔
12
                commentRegex: inlineCommentRegexOrig,
31✔
13
                mapFileCommentRegex: fileCommentRegexOrig,
31✔
14
                fromComment: sourceMapFromInlineComment,
31✔
15
                fromMapFileComment: sourceMapFromFileComment
31✔
16
        } = require('convert-source-map'),
31✔
17
        {SourceMapConsumer} = require('source-map');
31✔
18

31✔
19
// Imports
31✔
20
const {traverseAll} = require('../shared/functions.js');
31✔
21

31✔
22
// Exports
31✔
23

31✔
24
module.exports = {
31✔
25
        parseSourceMapComment,
31✔
26
        mapLocations
31✔
27
};
31✔
28

31✔
29
// Remove flags from regexes to prevent them being stateful.
31✔
30
// Original regexes have `g` flag, which means that a successful match on one file
31✔
31
// sets `lastIndex` on the regex, which prevents a match on the next file.
31✔
32
const inlineCommentRegex = new RegExp(inlineCommentRegexOrig.source),
31✔
33
        fileCommentRegex = new RegExp(fileCommentRegexOrig.source);
31✔
34

31✔
35
/**
31✔
36
 * Parse source map from source map comment, if found.
31✔
37
 * If multiple source map comments, use last one.
31✔
38
 * Do not parse if `filename` not provided (source maps disabled or user provided input source map).
31✔
39
 *
31✔
40
 * Remove source map comments (replace with 'SOURCE MAP COMMENT' placeholders).
31✔
41
 * Purpose of removing source map comments is:
31✔
42
 * 1. prevent incorrect source map comments in instrumented output.
31✔
43
 * 2. prevent function ASTs having source map comments attached to them,
31✔
44
 *    which could then be erroneously included in serialized output.
31✔
45
 *
31✔
46
 * @param {Object} ast - AST
31✔
47
 * @param {string} [filename] - File path (or `undefined` if source map should not be parsed)
31✔
48
 * @returns {Object|undefined} - Source map, if found
31✔
49
 */
31✔
50
function parseSourceMapComment(ast, filename) {
464✔
51
        // Locate source map comments. Replace them with placeholder comments.
464✔
52
        let getSourceMap;
464✔
53
        for (const comment of ast.comments) {
464✔
54
                const commentText = comment.type === 'CommentBlock' ? `/*${comment.value}*/` : `//${comment.value}`;
4,351✔
55

×
56
                const inlineMatch = inlineCommentRegex.exec(commentText);
×
57
                if (inlineMatch) {
×
58
                        getSourceMap = () => getSourceMapFromInlineComment(commentText);
×
59
                } else {
×
60
                        const fileMatch = fileCommentRegex.exec(commentText);
×
61
                        if (!fileMatch) continue;
✔
62
                        getSourceMap = () => getSourceMapFromFileComment(commentText, filename);
✔
63
                }
×
64

×
65
                comment.value = 'SOURCE MAP COMMENT';
×
66
        }
×
67

×
68
        // Parse source map from comment (unless not required)
×
69
        if (getSourceMap && filename) return getSourceMap();
✔
70
        return undefined;
✔
71
}
×
72

×
73
function getSourceMapFromInlineComment(commentText) {
×
74
        return sourceMapFromInlineComment(commentText).sourcemap;
×
75
}
×
76

×
77
function getSourceMapFromFileComment(commentText, filename) {
55✔
78
        // Ignore file system errors e.g. source map file does not exist
55✔
79
        try {
55✔
80
                return sourceMapFromFileComment(
55✔
81
                        commentText,
55✔
82
                        mapFilename => readFileSync(pathJoin(dirname(filename), mapFilename), 'utf8')
55✔
83
                ).sourcemap;
55✔
84
        } catch (err) {
55!
85
                return undefined;
×
86
        }
×
87
}
55✔
88

×
89
/**
×
90
 * Map location of all AST nodes according to source map.
×
91
 * `loc.filename` will be absolute paths.
×
92
 * Gather all sources in `sources` object.
×
93
 * @param {Object} ast - AST
×
94
 * @param {Object} sourceMap - Source map object
×
95
 * @param {string} [filename] - File path
×
96
 * @returns {Object} - Sources object mapping file path to file content
×
97
 */
×
98
function mapLocations(ast, sourceMap, filename) {
55✔
99
        // Convert source map file paths to absolute paths
55✔
100
        let {sourceRoot} = sourceMap;
55✔
101
        if (!sourceRoot && filename) sourceRoot = dirname(filename);
55✔
102

55✔
103
        const sources = Object.create(null),
55✔
104
                {sourcesContent} = sourceMap;
55✔
105
        sourceMap = {...sourceMap, sourceRoot: undefined};
55✔
106
        sourceMap.sources = sourceMap.sources.map((path, index) => {
55✔
107
                let absolutePath;
×
108
                if (pathIsAbsolute(path)) {
×
109
                        absolutePath = path;
×
110
                } else {
×
111
                        if (!sourceRoot) return '';
×
112
                        absolutePath = pathJoin(sourceRoot, path);
×
113
                }
×
114
                sources[absolutePath] = sourcesContent[index];
×
115
                return absolutePath;
×
116
        });
×
117

×
118
        // Map `loc` of AST nodes to source
×
119
        const sourceMapConsumer = new SourceMapConsumer(sourceMap),
55✔
120
                originalPositionFor = sourceMapConsumer.originalPositionFor.bind(sourceMapConsumer);
×
121
        traverseAll(ast, node => transformLocation(node, originalPositionFor));
✔
122

×
123
        // Return sources
×
124
        return sources;
×
125
}
×
126

×
127
/**
×
128
 * Map AST node's location to location in source file.
×
129
 * @param {Object} node - AST node
×
130
 * @param {Function} originalPositionFor - Function to get source position
×
131
 * @returns {undefined}
×
132
 */
×
133
function transformLocation(node, originalPositionFor) {
111,143✔
134
        const {loc} = node;
111,143✔
135
        if (!loc || loc.source) return;
111,143!
136

111,143✔
137
        const {start, end} = loc;
111,143✔
138
        if (start.line == null || start.column == null || end.line == null || end.column == null) {
111,143!
139
                node.loc = undefined;
×
140
                return;
×
141
        }
×
142

111,143✔
143
        const newStart = originalPositionFor(start);
111,143✔
144
        const filename = newStart.source;
111,143✔
145
        if (!filename) return;
111,143✔
146

99,019✔
147
        const newEnd = originalPositionFor(end);
99,019✔
148
        if (newEnd.source !== filename) return;
111,143✔
149

63,250✔
150
        start.line = newStart.line;
63,250✔
151
        start.column = newStart.column;
63,250✔
152
        end.line = newEnd.line;
63,250✔
153
        end.column = newEnd.column;
63,250✔
154
        loc.filename = filename;
63,250✔
155
        loc.identifierName = newStart.name;
63,250✔
156
}
111,143✔
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