Skip to content

Commit 956b9cb

Browse files
committed
Skip parsing if file doesn't contain assertions
Unassertify is quite an expensive transform, because it does full parsing and code generation. That only needs to be done for files that contain assertions, though. This patch adds a quick check: if the string `assert` does not even occur in the file, we'll just skip it, saving some time.
1 parent 37b3ae8 commit 956b9cb

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ function shouldProduceSourceMap (options) {
8787
return (options && options._flags && options._flags.debug);
8888
}
8989

90+
function containsAssertions (src) {
91+
// Matches both `assert` and `power-assert`.
92+
return src.indexOf('assert') !== -1;
93+
}
94+
9095
module.exports = function unassertify (filepath, options) {
9196
if (path.extname(filepath) === '.json') {
9297
return through();
@@ -100,11 +105,13 @@ module.exports = function unassertify (filepath, options) {
100105
}
101106

102107
function end() {
103-
if (shouldProduceSourceMap(options)) {
108+
if (!containsAssertions(data)) {
109+
stream.queue(data);
110+
} else if (shouldProduceSourceMap(options)) {
104111
stream.queue(applyUnassertWithSourceMap(data, filepath));
105112
} else {
106113
stream.queue(applyUnassertWithoutSourceMap(data));
107-
}
114+
}
108115
stream.queue(null);
109116
}
110117

test/fixtures/func/no-assert.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
contains some invalid code to make sure that this did not get parsed

test/test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ describe('unassertify', function () {
4343
done();
4444
}));
4545
});
46+
it('skips files that do not contain assertions', function (done) {
47+
var filename = path.join(__dirname, 'fixtures', 'func', 'no-assert.js');
48+
fs.createReadStream(filename)
49+
.pipe(unassertify(filename, {}))
50+
.pipe(es.wait(function(err, data) {
51+
assert(!err);
52+
var code = data.toString('utf-8');
53+
assert(! /assert/.test(code));
54+
done();
55+
}));
56+
});
4657
});
4758

4859

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy