Skip to content

Commit

Permalink
fix: Add flag to allow control of intrumenter esModules option.
Browse files Browse the repository at this point in the history
nyc was hard-coded to use `esModules: true` when running the
instrumenter.  This could result in certain scripts not being
instrumented.

```js
function package() {
  return 'package is a reserved keyword'
}

console.log(package())
```

Add command-line option `--es-modules true` to force strict parsing.

Issue #796
  • Loading branch information
coreyfarrell committed Jun 29, 2018
1 parent a5818f5 commit 3578a2b
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 3 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ NYC.prototype._createInstrumenter = function () {
ignoreClassMethods: [].concat(this.config.ignoreClassMethod).filter(a => a),
produceSourceMap: this.config.produceSourceMap,
compact: this.config.compact,
preserveComments: this.config.preserveComments
preserveComments: this.config.preserveComments,
esModules: this.config.esModules
})
}

Expand Down Expand Up @@ -282,8 +283,9 @@ NYC.prototype._transformFactory = function (cacheDir) {
try {
instrumented = instrumenter.instrumentSync(code, filename, sourceMap)
} catch (e) {
debugLog('failed to instrument ' + filename + 'with error: ' + e.stack)
debugLog('failed to instrument ' + filename + ' with error: ' + e.stack)
if (this.config.exitOnError) {
console.error('Failed to instrument ' + filename)
process.exit(1)
} else {
instrumented = code
Expand Down
6 changes: 6 additions & 0 deletions lib/config-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ Config.buildYargs = function (cwd) {
describe: 'cache babel transpilation results for improved performance',
global: false
})
.option('es-modules', {
default: false,
type: 'boolean',
describe: 'tell the instrumenter to treat files as ES Modules',
global: false
})
.option('extension', {
alias: 'e',
default: [],
Expand Down
2 changes: 1 addition & 1 deletion lib/instrumenters/istanbul.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function InstrumenterIstanbul (cwd, options) {
preserveComments: options.preserveComments,
produceSourceMap: options.produceSourceMap,
ignoreClassMethods: options.ignoreClassMethods,
esModules: true
esModules: options.esModules
})

return {
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/cli/not-strict.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function package() {
return 1;
}

package();
70 changes: 70 additions & 0 deletions test/nyc-bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,76 @@ describe('the nyc cli', function () {
})
})

describe('es-modules', () => {
it('allows reserved word when es-modules is disabled', (done) => {
const args = [
bin,
'--cache', 'false',
process.execPath, './not-strict.js'
]

const proc = spawn(process.execPath, args, {
cwd: fixturesCLI,
env: env
})

var stdout = ''
proc.stdout.on('data', function (chunk) {
stdout += chunk
})

proc.on('close', function (code) {
code.should.equal(0)
stdoutShouldEqual(stdout, `
---------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
not-strict.js | 100 | 100 | 100 | 100 | |
---------------|----------|----------|----------|----------|-------------------|`)
done()
})
})

it('forbids reserved word when es-modules is not disabled', (done) => {
const args = [
bin,
'--cache', 'false',
'--es-modules', 'true',
'--exit-on-error', 'true',
process.execPath, './not-strict.js'
]

const proc = spawn(process.execPath, args, {
cwd: fixturesCLI,
env: env
})

var stdout = ''
proc.stdout.on('data', function (chunk) {
stdout += chunk
})

var stderr = ''
proc.stderr.on('data', function (chunk) {
stderr += chunk
})

proc.on('close', function (code) {
code.should.equal(1)
stdoutShouldEqual(stderr, `
Failed to instrument ${path.join(fixturesCLI, 'not-strict.js')}`)
stdoutShouldEqual(stdout, `
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 0 | 0 | 0 | 0 | |
----------|----------|----------|----------|----------|-------------------|`)
done()
})
})
})

describe('merge', () => {
it('combines multiple coverage reports', (done) => {
const args = [
Expand Down

0 comments on commit 3578a2b

Please sign in to comment.