Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including parse-function with all npm packages installed. Try it out:

var parseFunction = require("parse-function")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

parse-function v5.6.10

Parse a function into an object using espree, acorn or babylon parsers. Extensible through Smart Plugins

Parse a function

parse-function npm version License Libera Manifesto

Parse a function into an object using espree, acorn or babylon parsers. Extensible through Smart Plugins

Please consider following this project's author, Charlike Mike Reagent, and :star: the project to show your :heart: and support.

Code style CircleCI linux build CodeCov coverage status Renovate App Status Make A Pull Request Time Since Last Commit

If you have any how-to kind of questions, please read the Contributing Guide and Code of Conduct documents. For bugs reports and feature requests, please create an issue or ping @tunnckoCore at Twitter.

Conventional Commits Minimum Required Nodejs NPM Downloads Monthly NPM Downloads Total Share Love Tweet Twitter

Project is semantically versioned & automatically released from GitHub Actions with Lerna.

Become a Patron Buy me a Kofi PayPal Donation Bitcoin Coinbase Keybase PGP

TopicContact
Any legal or licensing questions, like private or commerical usetunnckocore_legal
For any critical problems and security reportstunnckocore_security
Consulting, professional support, personal or team trainingtunnckocore_consulting
For any questions about Open Source, partnerships and sponsoringtunnckocore_opensource

Generated using jest-runner-docs.

parseFunction

Initializes with optional opts object which is passed directly to the desired parser and returns an object with .use and .parse methods. The default parse which is used is babylon's .parseExpression method from v7.

Signature

function(opts = {})

Params

  • opts {object} - optional, merged with options passed to .parse method
  • returns {object} - app object with .use and .parse methods

Examples

const parseFunction = require('parse-function');

const app = parseFunction({
  ecmaVersion: 2017,
});

const fixtureFn = (a, b, c) => {
  a = b + c;
  return a + 2;
};

const result = app.parse(fixtureFn);
console.log(result);

// see more
console.log(result.name); // => null
console.log(result.isNamed); // => false
console.log(result.isArrow); // => true
console.log(result.isAnonymous); // => true

// array of names of the arguments
console.log(result.args); // => ['a', 'b', 'c']

// comma-separated names of the arguments
console.log(result.params); // => 'a, b, c'

.parse

Parse a given code and returns a result object with useful properties - such as name, body and args. By default it uses Babylon parser, but you can switch it by passing options.parse - for example options.parse: acorn.parse. In the below example will show how to use acorn parser, instead of the default one.

Params

  • code {Function|string} - any kind of function or string to be parsed
  • options {object} - directly passed to the parser babylon, acorn, espree
  • options.parse {Function} - by default babylon.parseExpression, all options are passed as second argument
  • returns {object} - result see result section for more info

Examples

const acorn = require('acorn');
const parseFn = require('parse-function');
const app = parseFn();

const fn = function foo(bar, baz) {
  return bar * baz;
};
const result = app.parse(fn, {
  parse: acorn.parse,
  ecmaVersion: 2017,
});

console.log(result.name); // => 'foo'
console.log(result.args); // => ['bar', 'baz']
console.log(result.body); // => ' return bar * baz '
console.log(result.isNamed); // => true
console.log(result.isArrow); // => false
console.log(result.isAnonymous); // => false
console.log(result.isGenerator); // => false

.use

Add a plugin fn function for extending the API or working on the AST nodes. The fn is immediately invoked and passed with app argument which is instance of parseFunction() call. That fn may return another function that accepts (node, result) signature, where node is an AST node and result is an object which will be returned result from the .parse method. This retuned function is called on each node only when .parse method is called.

Params

  • fn {Function} - plugin to be called
  • returns {object} - app instance for chaining

See Plugins Architecture section.

Examples

// plugin extending the `app`
app.use((app) => {
  app.define(app, 'hello', (place) => `Hello ${place}!`);
});

const hi = app.hello('World');
console.log(hi); // => 'Hello World!'

// or plugin that works on AST nodes
app.use((app) => (node, result) => {
  if (node.type === 'ArrowFunctionExpression') {
    result.thatIsArrow = true;
  }
  return result;
});

const result = app.parse((a, b) => a + b + 123);
console.log(result.name); // => null
console.log(result.isArrow); // => true
console.log(result.thatIsArrow); // => true

const result = app.parse(function foo() {
  return 123;
});
console.log(result.name); // => 'foo'
console.log(result.isArrow); // => false
console.log(result.thatIsArrow); // => undefined

.define

Define a non-enumerable property on an object. Just a convenience mirror of the define-property library, so check out its docs. Useful to be used in plugins.

Params

  • obj {object} - the object on which to define the property
  • prop {string} - the name of the property to be defined or modified
  • val {any} - the descriptor for the property being defined or modified
  • returns {object} - obj the passed object, but modified

Examples

const parseFunction = require('parse-function');
const app = parseFunction();

// use it like `define-property` lib
const obj = {};
app.define(obj, 'hi', 'world');
console.log(obj); // => { hi: 'world' }

// or define a custom plugin that adds `.foo` property
// to the end result, returned from `app.parse`
app.use((app) => {
  return (node, result) => {
    // this function is called
    // only when `.parse` is called

    app.define(result, 'foo', 123);

    return result;
  };
});

// fixture function to be parsed
const asyncFn = async (qux) => {
  const bar = await Promise.resolve(qux);
  return bar;
};

const result = app.parse(asyncFn);

console.log(result.name); // => null
console.log(result.foo); // => 123
console.log(result.args); // => ['qux']

console.log(result.isAsync); // => true
console.log(result.isArrow); // => true
console.log(result.isNamed); // => false
console.log(result.isAnonymous); // => true

back to top

Contributing

Guides and Community

Please read the Contributing Guide and Code of Conduct documents for advices.

For bug reports and feature requests, please join our community forum and open a thread there with prefixing the title of the thread with the name of the project if there's no separate channel for it.

Consider reading the Support and Release Policy guide if you are interested in what are the supported Node.js versions and how we proceed. In short, we support latest two even-numbered Node.js release lines.

Support the project

Become a Partner or Sponsor? :dollar: Check the OpenSource Commision (tier). :tada: You can get your company logo, link & name on this file. It's also rendered on package's page in npmjs.com and yarnpkg.com sites too! :rocket:

Not financial support? Okey! Pull requests, stars and all kind of contributions are always welcome. :sparkles:

Contributors

This project follows the all-contributors specification. Contributions of any kind are welcome!

Thanks goes to these wonderful people (emoji key), consider showing your support to them:


Charlike Mike Reagent

🚇 💻 📖 🤔 🚧 ⚠️

back to top

License

Copyright (c) 2016-present, Charlike Mike Reagent <opensource@tunnckocore.com> & contributors.
Released under the MPL-2.0 License.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free