Skip to content

Commit 6b6cd5e

Browse files
coreyfarrellbcoe
authored andcommitted
fix: add flag to allow control of intrumenter esModules option, default to looser parsing (#863)
1 parent c325799 commit 6b6cd5e

File tree

5 files changed

+86
-3
lines changed

5 files changed

+86
-3
lines changed

index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ NYC.prototype._createInstrumenter = function () {
139139
ignoreClassMethods: [].concat(this.config.ignoreClassMethod).filter(a => a),
140140
produceSourceMap: this.config.produceSourceMap,
141141
compact: this.config.compact,
142-
preserveComments: this.config.preserveComments
142+
preserveComments: this.config.preserveComments,
143+
esModules: this.config.esModules
143144
})
144145
}
145146

@@ -280,8 +281,9 @@ NYC.prototype._transformFactory = function (cacheDir) {
280281
try {
281282
instrumented = instrumenter.instrumentSync(code, filename, sourceMap)
282283
} catch (e) {
283-
debugLog('failed to instrument ' + filename + 'with error: ' + e.stack)
284+
debugLog('failed to instrument ' + filename + ' with error: ' + e.stack)
284285
if (this.config.exitOnError) {
286+
console.error('Failed to instrument ' + filename)
285287
process.exit(1)
286288
} else {
287289
instrumented = code

lib/config-util.js

+6
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ Config.buildYargs = function (cwd) {
115115
describe: 'cache babel transpilation results for improved performance',
116116
global: false
117117
})
118+
.option('es-modules', {
119+
default: false,
120+
type: 'boolean',
121+
describe: 'tell the instrumenter to treat files as ES Modules',
122+
global: false
123+
})
118124
.option('extension', {
119125
alias: 'e',
120126
default: [],

lib/instrumenters/istanbul.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function InstrumenterIstanbul (cwd, options) {
1313
preserveComments: options.preserveComments,
1414
produceSourceMap: options.produceSourceMap,
1515
ignoreClassMethods: options.ignoreClassMethods,
16-
esModules: true
16+
esModules: options.esModules
1717
})
1818

1919
return {

test/fixtures/cli/not-strict.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function package() {
2+
return 1;
3+
}
4+
5+
package();

test/nyc-bin.js

+70
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,76 @@ describe('the nyc cli', function () {
10301030
})
10311031
})
10321032

1033+
describe('es-modules', () => {
1034+
it('allows reserved word when es-modules is disabled', (done) => {
1035+
const args = [
1036+
bin,
1037+
'--cache', 'false',
1038+
process.execPath, './not-strict.js'
1039+
]
1040+
1041+
const proc = spawn(process.execPath, args, {
1042+
cwd: fixturesCLI,
1043+
env: env
1044+
})
1045+
1046+
var stdout = ''
1047+
proc.stdout.on('data', function (chunk) {
1048+
stdout += chunk
1049+
})
1050+
1051+
proc.on('close', function (code) {
1052+
code.should.equal(0)
1053+
stdoutShouldEqual(stdout, `
1054+
---------------|----------|----------|----------|----------|-------------------|
1055+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
1056+
---------------|----------|----------|----------|----------|-------------------|
1057+
All files | 100 | 100 | 100 | 100 | |
1058+
not-strict.js | 100 | 100 | 100 | 100 | |
1059+
---------------|----------|----------|----------|----------|-------------------|`)
1060+
done()
1061+
})
1062+
})
1063+
1064+
it('forbids reserved word when es-modules is not disabled', (done) => {
1065+
const args = [
1066+
bin,
1067+
'--cache', 'false',
1068+
'--es-modules', 'true',
1069+
'--exit-on-error', 'true',
1070+
process.execPath, './not-strict.js'
1071+
]
1072+
1073+
const proc = spawn(process.execPath, args, {
1074+
cwd: fixturesCLI,
1075+
env: env
1076+
})
1077+
1078+
var stdout = ''
1079+
proc.stdout.on('data', function (chunk) {
1080+
stdout += chunk
1081+
})
1082+
1083+
var stderr = ''
1084+
proc.stderr.on('data', function (chunk) {
1085+
stderr += chunk
1086+
})
1087+
1088+
proc.on('close', function (code) {
1089+
code.should.equal(1)
1090+
stdoutShouldEqual(stderr, `
1091+
Failed to instrument ${path.join(fixturesCLI, 'not-strict.js')}`)
1092+
stdoutShouldEqual(stdout, `
1093+
----------|----------|----------|----------|----------|-------------------|
1094+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
1095+
----------|----------|----------|----------|----------|-------------------|
1096+
All files | 0 | 0 | 0 | 0 | |
1097+
----------|----------|----------|----------|----------|-------------------|`)
1098+
done()
1099+
})
1100+
})
1101+
})
1102+
10331103
describe('merge', () => {
10341104
it('combines multiple coverage reports', (done) => {
10351105
const args = [

0 commit comments

Comments
 (0)