From 312f81f88b5904176a04a8e02314b34099376ee8 Mon Sep 17 00:00:00 2001 From: Walter Zalazar Date: Mon, 27 Aug 2018 19:34:30 -0300 Subject: [PATCH] feat: Add option plugins (#205) * add option plugins * export defaultOpts --- packages/istanbul-lib-instrument/src/index.js | 3 ++ .../src/instrumenter.js | 22 +++++----- .../test/plugins.test.js | 44 +++++++++++++++++++ 3 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 packages/istanbul-lib-instrument/test/plugins.test.js diff --git a/packages/istanbul-lib-instrument/src/index.js b/packages/istanbul-lib-instrument/src/index.js index 4338fe92..e06dca4a 100644 --- a/packages/istanbul-lib-instrument/src/index.js +++ b/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 @@ -15,3 +16,5 @@ function createInstrumenter(opts) { export {createInstrumenter}; export {programVisitor}; export {readInitialCoverage}; +export {defaultOpts}; + diff --git a/packages/istanbul-lib-instrument/src/instrumenter.js b/packages/istanbul-lib-instrument/src/instrumenter.js index d563799c..b1a2c656 100644 --- a/packages/istanbul-lib-instrument/src/instrumenter.js +++ b/packages/istanbul-lib-instrument/src/instrumenter.js @@ -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, @@ -18,7 +18,15 @@ function defaultOpts() { produceSourceMap: false, ignoreClassMethods: [], sourceMapUrlCallback: null, - debug: false + debug: false, + plugins: [ + 'asyncGenerators', + 'dynamicImport', + 'objectRestSpread', + 'optionalCatchBinding', + 'flow', + 'jsx' + ] }; } /** @@ -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()) { @@ -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, diff --git a/packages/istanbul-lib-instrument/test/plugins.test.js b/packages/istanbul-lib-instrument/test/plugins.test.js new file mode 100644 index 00000000..b15830e7 --- /dev/null +++ b/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'); + }); + }); + }); +});