Coder Social home page Coder Social logo

egg-ast-utils's Introduction

egg-ast-utils

NPM version Build Status Appveyor status Coverage Status

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

egg-ast-utils's People

Contributors

whxaxes avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.