egg-ast-utils
v1.0.0
Published
[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Appveyor status][appveyor-image]][appveyor-url] [![Coverage Status][coveralls-image]][coveralls-url]
Downloads
6
Readme
egg-ast-utils
Install
npm install egg-ast-utils --save
Or
yarn add egg-ast-utils --save
Methods
parseConfig
file
// config.default.js
module.exports = appInfo => {
const config = {};
config.keys = appInfo.name + '_1501832752131_9495';
config.view = {
defaultViewEngine: 'nunjucks',
};
config.security = {
csrf: false,
};
config.security.csrf = true;
return config;
};
parse
const astUtil = require('egg-ast-utils');
const result = astUtil.parseConfig(fs.readFileSync('config.default.js').toString());
console.log(result.find('view'))
// [
// {
// key: { Identify }, // view
// value: { ObjectExpression },
// children: {
// defaultViewEngine: [{
// key: { Identify }, // defaultViewEngine
// value: { Literal }, // nunjucks
// }]
// }
// }
// ]
console.log(result.find('view.defaultViewEngine'))
// [
// {
// key: { Identify }, // defaultViewEngine
// value: { Literal } // nunjucks
// }
// ]
console.log(result.find('security.csrf'))
// [
// {
// key: { Identify }, // csrf
// value: { Identify } // false
// },
// {
// key: { Identify }, // csrf
// value: { Identify } // true
// }
// ]
parseClass
file
// Test.js
module.exports = app => {
return class Test extends app.Service {
constructor(ctx) {
super(ctx);
}
userInfo(page) {
return this.ctx.httpClient.request('xxxx');
}
'user.search'(data, page) {
return this.ctx.httpClient.request('xxxx');
}
}
};
parse
const astUtil = require('egg-ast-utils');
const result = astUtil.parseClass(fs.readFileSync('Test.js').toString());
console.log(result.find('userInfo'))
// [
// { key: { Identify }, value: { FunctionExpression } }
// ]
console.log(result.find('"user.search"'))
// [
// { key: { Identify }, value: { FunctionExpression } }
// ]
parseUnittest
file
// test/util.test.js
describe('lib#utils#utils.js', () => {
before(function* b() {
...
});
it('forEach should run without error', () => {
...
});
describe('sub describe 2', function() {
it('should throw friendly error', () => {
...
});
it('should throw error correctly without el', () => {
...
});
it('should run without error if no el', (done) => {
...
});
});
});
parse
const astUtil = require('egg-ast-utils');
const list = astUtil.parseUnittest(fs.readFileSync('test/util.test.js').toString());
console.log(list);
// [
// {
// node: { CallExpression },
// type: 'describe',
// describe: 'lib#utils#utils.js',
// children: [
// { node: { CallExpression }, type: 'before' },
// { node: { CallExpression }, type: 'it', describe: 'forEach should run without error' },
// { node: { CallExpression }, type: 'describe', describe: 'sub describe 2', children: [ ... ] },
// ]
// }
// ]
Author
wanghx
License
MIT