Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add option to preserve comments, thanks to @arikon

  • Loading branch information...
commit b1ad9ead1c48b163604cfc2f5608ae86cb92969f 1 parent f7eb537
@gotwarlost authored
View
7 lib/command/instrument.js
@@ -147,6 +147,7 @@ Command.mix(InstrumentCommand, {
'default value of `__coverage__` to something else'),
formatOption('--embed-source', 'embed source code into the coverage object, defaults to false'),
formatOption('--[no-]compact', 'produce [non]compact output, defaults to compact'),
+ formatOption('--[no-]preserve-comments', 'remove / preserve comments in the output, defaults to false'),
formatOption('--[no-]complete-copy', 'also copy non-javascript files to the ouput directory as is, defaults to false'),
formatOption('--save-baseline', 'produce a baseline coverage.json file out of all files instrumented'),
formatOption('--baseline-file <file>', 'filename of baseline file, defaults to coverage/coverage-baseline.json')
@@ -165,7 +166,8 @@ Command.mix(InstrumentCommand, {
verbose: Boolean,
'save-baseline': Boolean,
'baseline-file': path,
- 'embed-source': Boolean
+ 'embed-source': Boolean,
+ 'preserve-comments': Boolean
},
opts = nopt(config, { v : '--verbose' }, args, 0),
cmdArgs = opts.argv.remain,
@@ -199,7 +201,8 @@ Command.mix(InstrumentCommand, {
instrumenter = new Instrumenter({
coverageVariable: opts.variable,
embedSource: opts['embed-source'],
- noCompact: !opts.compact
+ noCompact: !opts.compact,
+ preserveComments: opts['preserve-comments']
});
if (needBaseline) {
View
15 lib/instrumenter.js
@@ -287,6 +287,7 @@
* tracking coverage. Defaults to `__coverage__`
* @param {Boolean} [options.embedSource] whether to embed the source code of every
* file as an array in the file coverage object for that file. Defaults to `false`
+ * @param {Boolean} [options.preserveComments] whether comments should be preserved in the output. Defaults to `false`
* @param {Boolean} [options.noCompact] emit readable code when set. Defaults to `false`
* @param {Boolean} [options.noAutoWrap] do not automatically wrap the source in
* an anonymous function before covering it. By default, code is wrapped in
@@ -314,7 +315,8 @@
codeGenerationOptions: undefined,
noAutoWrap: false,
noCompact: false,
- embedSource: false
+ embedSource: false,
+ preserveComments: false
};
this.walker = new Walker({
@@ -368,7 +370,15 @@
if (!this.opts.noAutoWrap) {
code = LEADER_WRAP + code + TRAILER_WRAP;
}
- program = ESP.parse(code, { loc: true });
+ program = ESP.parse(code, {
+ loc: true,
+ range: this.opts.preserveComments,
+ tokens: this.opts.preserveComments,
+ comment: this.opts.preserveComments
+ });
+ if (this.opts.preserveComments) {
+ program = ESPGEN.attachComments(program, program.comments, program.tokens);
+ }
if (!this.opts.noAutoWrap) {
program = { type: SYNTAX.Program.name, body: program.body[0].expression.callee.body.body };
}
@@ -413,6 +423,7 @@
}
this.walker.startWalk(program);
codegenOptions = this.opts.codeGenerationOptions || { format: { compact: !this.opts.noCompact }};
+ codegenOptions.comment = this.opts.preserveComments;
//console.log(JSON.stringify(program, undefined, 2));
return this.getPreamble(originalCode || '', usingStrict) + '\n' + ESPGEN.generate(program, codegenOptions) + '\n';
},
View
1  test/cli/sample-project/lib/foo.js
@@ -2,6 +2,7 @@ var dep = require('dependency'),
vendor = require('../vendor/dummy_vendor_lib'),
generator = require('./util/generate-names');
+// export what we need
module.exports = function (input, useDep) {
var base = generator.generateName(),
output = useDep
View
11 test/cli/test-instrument-command.js
@@ -42,6 +42,17 @@ module.exports = {
test.done();
});
},
+ "should preserve comments in output": function (test) {
+ run([ 'lib/foo.js', '--preserve-comments' ], function (results) {
+ var code = results.stdout().join('\n');
+ test.ok(results.succeeded());
+ test.doesNotThrow(function () {
+ vm.createScript(code, path.resolve(DIR, 'lib', 'foo.js'));
+ }, "Invalid code generated; logging interference perhaps?");
+ test.ok(code.match(/\/\/ export what we need/), 'Could not find comment that should have been preserved');
+ test.done();
+ });
+ },
"should work with compact as default": function (test) {
run([ 'lib/foo.js' ], function (results) {
test.ok(results.succeeded());
View
4 test/helper.js
@@ -76,13 +76,15 @@ function setup(file, codeArray, opts) {
//exercise the case where RE substitutions for the preamble have $ signs
coverageVariable = typeof opts.coverageVariable === 'undefined' ? '$$coverage$$' : opts.coverageVariable,
ps = opts.embedSource || false,
+ pc = opts.preserveComments || false,
verifier,
cover = new Instrumenter({
debug: opts.debug,
walkDebug: opts.walkDebug,
noAutoWrap: opts.noAutoWrap,
coverageVariable: coverageVariable,
- embedSource: ps
+ embedSource: ps,
+ preserveComments: pc
}),
args = [ codeArray.join("\n")],
callback = function (err, generated) {
View
26 test/instrumentation/test-statement.js
@@ -177,6 +177,32 @@ module.exports = {
verifier.verify(test, [ 1 ], "undef", { lines: { 2: 1, 3: 1 }, branches: { 1: [ 0, 1 ]}, functions: {}, statements: { 1: 1, 2: 1 } });
test.done();
}
+ },
+ "with code having comments": {
+ setUp: function (cb) {
+ code = [
+ '#!/usr/bin/env node',
+ 'var x = args[0] > 5 ? args[0] : "undef";',
+ '/* set the output */',
+ 'output = x;'
+ ];
+ verifier = helper.verifier(__filename, code, { noAutoWrap: true, preserveComments: true });
+ cb();
+ },
+
+ "should preserve comments in generated code": function (test) {
+ test.ok(verifier.generatedCode.match(/\/\* set the output \*\//));
+ test.done();
+ },
+
+ "should cover line and one branch": function (test) {
+ verifier.verify(test, [ 10 ], 10, { lines: { 2: 1, 4: 1 }, branches: { 1: [1, 0 ]}, functions: {}, statements: { 1: 1, 2: 1 } });
+ test.done();
+ },
+ "should cover line and other branch": function (test) {
+ verifier.verify(test, [ 1 ], "undef", { lines: { 2: 1, 4: 1 }, branches: { 1: [ 0, 1 ]}, functions: {}, statements: { 1: 1, 2: 1 } });
+ test.done();
+ }
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.