Parse a function into an object using espree, acorn or babylon parsers. Extensible through Smart Plugins
You might also be interested in function-arguments library if you need more lightweight solution and need for just getting the names of the function arguments.
If you have any how-to kind of questions, please read Code of Conduct and join the chat room or open an issue.
You may also read the Contributing Guide. There, beside "How to contribute?", we describe everything stated by the badges.
- Always up-to-date: auto-publish new version when new version of dependency is out, Renovate
- Standard: using StandardJS, Prettier, SemVer, Semantic Release and conventional commits
- Smart Plugins: for extending the core API or the end Result, see .use method and Plugins Architecture
- Extensible: using plugins for working directly on AST nodes, see the Plugins Architecture
- ES2017 Ready: by using
.parseExpression
method of the babylonv7.x
parser - Customization: allows switching the parser, through
options.parse
- Support for: arrow functions, default parameters, generators and async/await functions
- Stable: battle-tested in production and against all parsers - espree, acorn, babylon
- Tested: with 275+ tests for 200% coverage
(TOC generated by verb using markdown-toc)
This project requires Node.js v6 and above. Use npm to install it.
$ npm install parse-function
Review carefully the provided examples and the working tests.
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 fromv7
.
Params
opts
{Object}: optional, merged with options passed to.parse
methodreturns
{Object}
Example
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 a given
code
and returns aresult
object with useful properties - such asname
,body
andargs
. By default it uses Babylon parser, but you can switch it by passingoptions.parse
- for exampleoptions.parse: acorn.parse
. In the below example will show how to useacorn
parser, instead of the default one.
Params
code
{Function|String}: any kind of function or string to be parsedoptions
{Object}: directly passed to the parser - babylon, acorn, espreeoptions.parse
{Function}: by defaultbabylon.parseExpression
, alloptions
are passed as second argument to that provided functionreturns
{Object}
Example
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
Add a plugin
fn
function for extending the API or working on the AST nodes. Thefn
is immediately invoked and passed withapp
argument which is instance ofparseFunction()
call. Thatfn
may return another function that accepts(node, result)
signature, wherenode
is an AST node andresult
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.
See Plugins Architecture section.
Params
fn
{Function}: plugin to be calledreturns
{Object}
Example
// 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 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 propertyprop
{String}: the name of the property to be defined or modifiedval
{Any}: the descriptor for the property being defined or modifiedreturns
{Object}
Example
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
- acorn: ECMAScript parser | homepage
- babylon: A JavaScript parser | homepage
- charlike-cli: Command line interface for the charlike project scaffolder. | homepage
- espree: An Esprima-compatible JavaScript parser built on Acorn | homepage
- hela: Task runner based on execa. Includes few predefined tasks for linting, testing… more | homepage
- parse-semver: Parse, normalize and validate given semver shorthand (e.g. gulp@v3.8.10) to object. | homepage
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Please read the Contributing Guide and Code of Conduct documents for advices.
Copyright © 2016-2017, Charlike Mike Reagent. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on August 11, 2017.
Project scaffolded using charlike-cli.