Skip to content

Commit

Permalink
feat: Add option plugins (#205)
Browse files Browse the repository at this point in the history
* add option plugins
* export defaultOpts
  • Loading branch information
wzalazar authored and coreyfarrell committed Aug 27, 2018
1 parent 829e658 commit 312f81f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
3 changes: 3 additions & 0 deletions packages/istanbul-lib-instrument/src/index.js
@@ -1,6 +1,7 @@
import Instrumenter from './instrumenter';
import programVisitor from './visitor';
import readInitialCoverage from './read-coverage';
import {defaultOpts} from './instrumenter';

/**
* createInstrumenter creates a new instrumenter with the
Expand All @@ -15,3 +16,5 @@ function createInstrumenter(opts) {
export {createInstrumenter};
export {programVisitor};
export {readInitialCoverage};
export {defaultOpts};

22 changes: 12 additions & 10 deletions packages/istanbul-lib-instrument/src/instrumenter.js
Expand Up @@ -8,7 +8,7 @@ import traverse from '@babel/traverse';
import generate from '@babel/generator';
import programVisitor from './visitor';

function defaultOpts() {
export function defaultOpts() {
return {
coverageVariable: "__coverage__",
preserveComments: false,
Expand All @@ -18,7 +18,15 @@ function defaultOpts() {
produceSourceMap: false,
ignoreClassMethods: [],
sourceMapUrlCallback: null,
debug: false
debug: false,
plugins: [
'asyncGenerators',
'dynamicImport',
'objectRestSpread',
'optionalCatchBinding',
'flow',
'jsx'
]
};
}
/**
Expand All @@ -37,6 +45,7 @@ function defaultOpts() {
* @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
*/
class Instrumenter {
constructor(opts=defaultOpts()) {
Expand Down Expand Up @@ -83,14 +92,7 @@ class Instrumenter {
const ast = parser.parse(code, {
allowReturnOutsideFunction: opts.autoWrap,
sourceType: opts.esModules ? "module" : "script",
plugins: [
'asyncGenerators',
'dynamicImport',
'objectRestSpread',
'optionalCatchBinding',
'flow',
'jsx'
]
plugins: opts.plugins
});
const ee = programVisitor(t, filename, {
coverageVariable: opts.coverageVariable,
Expand Down
44 changes: 44 additions & 0 deletions packages/istanbul-lib-instrument/test/plugins.test.js
@@ -0,0 +1,44 @@
/* globals describe, it, context */

import Instrumenter from '../src/instrumenter';
import {assert} from 'chai';

const codeNeedDecoratorPlugin = `
@decorator
class MyClass {}
`;

const generateCode = (code, plugins) => {
const opts = {
esModules: true,
produceSourceMap: true,
plugins
};
const instrumenter = new Instrumenter(opts);
return instrumenter.instrumentSync(code, __filename);
};

describe('plugins', function () {
context('when the code has a decorator', function() {
context('without decorator plugin', function() {
it('should fail', function (done) {
try {
generateCode(codeNeedDecoratorPlugin);
} catch(e) {
const expected =
`This experimental syntax requires enabling one of the following parser plugin(s): 'decorators-legacy, decorators'`;
assert.ok(e.message.includes(expected));
done();
}
});
});

context('with decorator plugin', function() {
it('should success', function () {
const generated = generateCode(codeNeedDecoratorPlugin, ['decorators']);
assert.ok(generated);
assert.ok(typeof generated === 'string');
});
});
});
});

0 comments on commit 312f81f

Please sign in to comment.