Skip to content

Commit

Permalink
feat: Use @istanbuljs/schema to pull defaults (#485)
Browse files Browse the repository at this point in the history
Fixes #460

BREAKING CHANGE: The defaults for `autoWrap`, `preserveComments`,
`esModules` and `produceSourceMap` are now true.  This applies only to
the stand-alone instrumenter, the visitor does not use these options.

BREAKING CHANGE: The `flow` and `jsx` parser plugins are no longer
enabled by default.  This applies only to the stand-alone instrumenter,
the visitor does not use this option.

BREAKING CHANGE: The `plugins` option of the stand-alone instrumenter
has been renamed to `parserPlugins` to match nyc.
  • Loading branch information
coreyfarrell committed Oct 5, 2019
1 parent 8dfbcba commit 87e27f3
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 56 deletions.
4 changes: 2 additions & 2 deletions packages/istanbul-lib-instrument/api.md
Expand Up @@ -45,7 +45,7 @@ instead.
- `opts.sourceMapUrlCallback` **[Function][17]** a callback function that is called when a source map URL
is found in the original code. This function is called with the source file name and the source map URL. (optional, default `null`)
- `opts.debug` **[boolean][15]** turn debugging on (optional, default `false`)
- `opts.plugins` **[array][16]** set plugins (optional, default `['asyncGenerators','dynamicImport','objectRestSpread','optionalCatchBinding','flow','jsx']`)
- `opts.parserPlugins` **[array][16]?** set babel parser plugins, see @istanbuljs/schema for defaults.

### instrumentSync

Expand Down Expand Up @@ -109,7 +109,7 @@ The exit function returns an object that currently has the following keys:

- `types` **[Object][13]** an instance of babel-types
- `sourceFilePath` **[string][14]** the path to source file (optional, default `'unknown.js'`)
- `opts` **[Object][13]** additional options (optional, default `defaultProgramVisitorOpts`)
- `opts` **[Object][13]** additional options (optional, default `{}`)
- `opts.coverageVariable` **[string][14]** the global coverage variable name. (optional, default `__coverage__`)
- `opts.coverageGlobalScope` **[string][14]** the global coverage variable scope. (optional, default `this`)
- `opts.coverageGlobalScopeFunc` **[boolean][15]** use an evaluated function to find coverageGlobalScope. (optional, default `true`)
Expand Down
1 change: 1 addition & 0 deletions packages/istanbul-lib-instrument/package.json
Expand Up @@ -18,6 +18,7 @@
"@babel/template": "^7.4.4",
"@babel/traverse": "^7.4.5",
"@babel/types": "^7.4.4",
"@istanbuljs/schema": "^0.1.0",
"istanbul-lib-coverage": "^3.0.0-alpha.0",
"semver": "^6.1.1"
},
Expand Down
29 changes: 0 additions & 29 deletions packages/istanbul-lib-instrument/src/default-opts.js

This file was deleted.

5 changes: 3 additions & 2 deletions packages/istanbul-lib-instrument/src/index.js
@@ -1,5 +1,5 @@
import { defaults } from '@istanbuljs/schema';
import Instrumenter from './instrumenter';
import defaultOpts from './default-opts';
import programVisitor from './visitor';
import readInitialCoverage from './read-coverage';

Expand All @@ -16,4 +16,5 @@ function createInstrumenter(opts) {
export { createInstrumenter };
export { programVisitor };
export { readInitialCoverage };
export { defaultOpts };

export const defaultOpts = defaults.instrumenter;
8 changes: 4 additions & 4 deletions packages/istanbul-lib-instrument/src/instrumenter.js
Expand Up @@ -6,8 +6,8 @@ import * as parser from '@babel/parser';
import * as t from '@babel/types';
import traverse from '@babel/traverse';
import generate from '@babel/generator';
import { defaults } from '@istanbuljs/schema';
import programVisitor from './visitor';
import defaultOpts from './default-opts';
import readInitialCoverage from './read-coverage';

/**
Expand All @@ -26,12 +26,12 @@ import readInitialCoverage from './read-coverage';
* @param {Function} [opts.sourceMapUrlCallback=null] a callback function that is called when a source map URL
* is found in the original code. This function is called with the source file name and the source map URL.
* @param {boolean} [opts.debug=false] - turn debugging on
* @param {array} [opts.plugins=['asyncGenerators','dynamicImport','objectRestSpread','optionalCatchBinding','flow','jsx']] - set plugins
* @param {array} [opts.parserPlugins] - set babel parser plugins, see @istanbuljs/schema for defaults.
*/
class Instrumenter {
constructor(opts = {}) {
this.opts = {
...defaultOpts(),
...defaults.instrumenter,
...opts
};
this.fileCoverage = null;
Expand Down Expand Up @@ -59,7 +59,7 @@ class Instrumenter {
const ast = parser.parse(code, {
allowReturnOutsideFunction: opts.autoWrap,
sourceType: opts.esModules ? 'module' : 'script',
plugins: opts.plugins
plugins: opts.parserPlugins
});
const ee = programVisitor(t, filename, {
coverageVariable: opts.coverageVariable,
Expand Down
4 changes: 2 additions & 2 deletions packages/istanbul-lib-instrument/src/read-coverage.js
@@ -1,8 +1,8 @@
import { parse } from '@babel/parser';
import traverse from '@babel/traverse';
import * as t from '@babel/types';
import { defaults } from '@istanbuljs/schema';
import { MAGIC_KEY, MAGIC_VALUE } from './constants';
import defaultOpts from './default-opts';

const astClass = parse('').constructor;

Expand All @@ -22,7 +22,7 @@ function getAst(code) {
allowReturnOutsideFunction: true,
allowSuperOutsideMethod: true,
sourceType: 'script',
plugins: defaultOpts().plugins
plugins: defaults.instrumenter.parserPlugins
});
}

Expand Down
15 changes: 3 additions & 12 deletions packages/istanbul-lib-instrument/src/visitor.js
@@ -1,8 +1,8 @@
import { createHash } from 'crypto';
import template from '@babel/template';
import { defaults } from '@istanbuljs/schema';
import { SourceCoverage } from './source-coverage';
import { SHA, MAGIC_KEY, MAGIC_VALUE } from './constants';
import defaultOpts from './default-opts';

// pattern for istanbul to ignore a section
const COMMENT_RE = /^\s*istanbul\s+ignore\s+(if|else|next)(?=\W|$)/;
Expand Down Expand Up @@ -571,9 +571,6 @@ function shouldIgnoreFile(programNode) {
);
}

const defaultProgramVisitorOpts = {
inputSourceMap: undefined
};
/**
* programVisitor is a `babel` adaptor for instrumentation.
* It returns an object with two methods `enter` and `exit`.
Expand All @@ -597,16 +594,10 @@ const defaultProgramVisitorOpts = {
* @param {object} [opts.inputSourceMap=undefined] the input source map, that maps the uninstrumented code back to the
* original code.
*/
function programVisitor(
types,
sourceFilePath = 'unknown.js',
opts = defaultProgramVisitorOpts
) {
function programVisitor(types, sourceFilePath = 'unknown.js', opts = {}) {
const T = types;
// This sets some unused options but ensures all required options are initialized
opts = {
...defaultOpts(),
...defaultProgramVisitorOpts,
...defaults.instrumentVisitor,
...opts
};
const visitState = new VisitState(
Expand Down
Expand Up @@ -4,7 +4,8 @@ import { assert } from 'chai';
import Instrumenter from '../src/instrumenter';

function instrument(code) {
const instrumenter = new Instrumenter({});
// XXX produceSourceMap: true produces an altered source-map for the second run.
const instrumenter = new Instrumenter({ produceSourceMap: false });
const result = instrumenter.instrumentSync(code, __filename);
return {
code: result,
Expand Down
6 changes: 5 additions & 1 deletion packages/istanbul-lib-instrument/test/negative.test.js
Expand Up @@ -19,7 +19,11 @@ describe('negative tests', () => {
});

it('should barf on mainline returns with no auto-wrap', () => {
const v = verifier.create('return 10;', { quiet: true });
const v = verifier.create(
'return 10;',
{ quiet: true },
{ autoWrap: false }
);
const err = v.compileError();
assert.ok(err);
assert.ok(err.message.match(/'return' outside/));
Expand Down
4 changes: 2 additions & 2 deletions packages/istanbul-lib-instrument/test/plugins.test.js
Expand Up @@ -8,11 +8,11 @@ const codeNeedDecoratorPlugin = `
class MyClass {}
`;

const generateCode = (code, plugins) => {
const generateCode = (code, parserPlugins) => {
const opts = {
esModules: true,
produceSourceMap: true,
plugins
parserPlugins
};
const instrumenter = new Instrumenter(opts);
return instrumenter.instrumentSync(code, __filename);
Expand Down
3 changes: 3 additions & 0 deletions packages/istanbul-lib-instrument/test/specs/jsx.yaml
Expand Up @@ -7,5 +7,8 @@ code: |
</div>;
opts:
generateOnly: true
instrumentOpts:
parserPlugins:
- "jsx"
tests:
- name: jsx syntax
4 changes: 4 additions & 0 deletions packages/istanbul-lib-instrument/test/specs/with.yaml
Expand Up @@ -7,6 +7,8 @@ tests:
out: 1
lines: {'1': 1}
statements: {'0': 1, '1': 1}
instrumentOpts:
esModules: false

---
name: with statement with block
Expand All @@ -17,3 +19,5 @@ tests:
out: 1
lines: {'1': 1}
statements: {'0': 1, '1': 1}
instrumentOpts:
esModules: false
2 changes: 1 addition & 1 deletion packages/istanbul-lib-instrument/test/varia.test.js
Expand Up @@ -72,7 +72,7 @@ describe('varia', () => {
'/* istanbul ignore next*/ export function fn1() {}' +
'/* istanbul ignore next*/ export default function() {}',
{ generateOnly: true },
{ esModules: true }
{ esModules: true, preserveComments: false }
);
assert.ok(!v.err);

Expand Down

0 comments on commit 87e27f3

Please sign in to comment.