babel-plugin-transform-comment-to-assert

Replace commented expressions with assert statements

MIT 22 个版本
安装
npm install babel-plugin-transform-comment-to-assert
yarn add babel-plugin-transform-comment-to-assert
pnpm add babel-plugin-transform-comment-to-assert
bun add babel-plugin-transform-comment-to-assert
README

babel-plugin-transform-comment-to-assert travis npm

replace commented expressions with assert statements

Install

$ npm install --save babel-plugin-transform-comment-to-assert

Usage

import * as babel from 'babel-core'
import plugin from 'babel-plugin-transform-comment-to-assert'

function replace (code) {
  return babel.transform(code, { babelrc: false, plugins: [plugin] }).code.trim();
};

replace('1 //=> 1')
//=> 'assert.deepEqual(1, 1);'

Objects:


replace('a = { a: 1 }; a //=> {a: 1}')
//=> 'a = { a: 1 };assert.deepEqual(a, { a: 1 });'

Results of function calls:

replace("(() => 'foo')() //=> 'bar'")
//=> "assert.deepEqual((() => 'foo')(), 'bar');"

It also supports console.log:

replace("console.log('foo') //=> 'bar'")
//=> "console.log('foo');assert.deepEqual('foo', 'bar');"

Throws:

replace(`
const a = () => {
  throw new Error('fail');
};
a() // throws Error
`);
/*=>
`const a = () => {
  throw new Error('fail');
};
assert.throws(() => a(), Error);`
*/
replace(`
const a = () => {
  throw new Error('fail');
};
a() // throws /fail/
`);
/*=>
`const a = () => {
  throw new Error('fail');
};
assert.throws(() => a(), /fail/);`
*/

License

MIT © Sigurd Fosseng

版本列表
4.1.0 2019-06-07
4.0.0 2019-06-07
3.1.2 2017-09-07
3.1.1 2017-05-15
3.1.0 2017-04-18
3.0.1 2017-04-11
3.0.0 2016-09-09
2.0.2 2016-08-15
2.0.1 2016-07-21
2.0.0 2016-07-12
1.0.12 2016-05-08
1.0.11 2016-05-08
1.0.10 2016-04-30
1.0.9 2016-04-23
1.0.8 2016-04-22
1.0.7 2016-04-14
1.0.6 2016-04-12
1.0.5 2016-04-12
1.0.4 2016-04-11
1.0.2 2016-02-04
1.0.1 2016-01-31
1.0.0 2016-01-29