Security News
NVD Backlog Tops 20,000 CVEs Awaiting Analysis as NIST Prepares System Updates
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
parse-function
Advanced tools
Parse a function into an object using espree, acorn or babylon parsers. Extensible through Smart Plugins
The parse-function npm package is a utility for parsing JavaScript functions to extract their name, parameters, and body. It is useful for analyzing and manipulating functions programmatically.
Parse Function
This feature allows you to parse a JavaScript function to extract its name, parameters, and body. The code sample demonstrates how to parse a simple function and log the parsed details.
const parseFunction = require('parse-function')();
const parsed = parseFunction.parse(function example(a, b) { return a + b; });
console.log(parsed);
Parse Arrow Function
This feature allows you to parse an arrow function. The code sample demonstrates how to parse an arrow function and log the parsed details.
const parseFunction = require('parse-function')();
const parsed = parseFunction.parse((a, b) => a + b);
console.log(parsed);
Parse Async Function
This feature allows you to parse an async function. The code sample demonstrates how to parse an async function and log the parsed details.
const parseFunction = require('parse-function')();
const parsed = parseFunction.parse(async function example(a, b) { return a + b; });
console.log(parsed);
Esprima is a high-performance, standard-compliant ECMAScript parser. It can parse JavaScript code into an abstract syntax tree (AST), which can then be analyzed or transformed. Compared to parse-function, Esprima provides a more comprehensive parsing capability but requires more effort to extract specific function details.
Acorn is a small, fast, JavaScript-based JavaScript parser. It generates an abstract syntax tree (AST) from JavaScript code. Similar to Esprima, Acorn offers a broader parsing capability but requires additional steps to extract function-specific information compared to parse-function.
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.
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.
Project is semantically versioned & automatically released from GitHub Actions with Lerna.
Topic | Contact |
---|---|
Any legal or licensing questions, like private or commerical use | |
For any critical problems and security reports | |
Consulting, professional support, personal or team training | |
For any questions about Open Source, partnerships and sponsoring |
.parseExpression
method of the Babel v7.x
parseroptions.parse
(TOC generated by verb using markdown-toc)
This project requires Node.js >=8.11 (see Support & Release Policy). Install it using
yarn or npm.
We highly recommend to use Yarn when you think to contribute to this project.
$ yarn add parse-function
There's no breaking changes between the v2.x
version. The only breaking is v2.1
which also is not
working properly, so no use it.
Use v2.0.x
When you don't need support for arrow functions
and es6 default params
. This version
uses a RegExp expression to work.
Use v2.2.x
Only when you need a basic support for es6 features
like arrow functions. This version
uses a RegExp expression to work.
Use v2.3.x
When you want full* support for arrow functions
and es6 default params
. Where this "full",
means "almost full", because it has bugs. This version also uses (acorn.parse
) real parser
to do the parsing.
Use v3.x
When you want to use different parser instead of the default babylon.parse
, by passing custom
parse function to the options.parse
option. From this version we require node >= 4
.
Use v4.x
When you want full customization and most stable support for old and modern features. This version
uses babylon.parseExpression
for parsing and provides a Plugins API.
See the Features section for more info.
Use v5.x
It is basically the same as v4
, but requires Node 6 & npm 5. Another is boilerplate stuff.
see: issue #3 and test/index.js#L229-L235
It may throw in one specific case, otherwise it won't throw, so you should
relay on the result.isValid
for sure.
see: test/index.js#L319-L324 and Result section
If you pass a function which is named "anonymous" the result.name
will be 'anonymous'
,
but the result.isAnonymous
will be false
and result.isNamed
will be true
, because
in fact it's a named function.
see: test/index.js#L326-L331 and Result section
Only if you pass really an anonymous function you will get result.name
equal to null
,
result.isAnonymous
equal to true
and result.isNamed
equal to false
.
see: the .use method, test/index.js#L305-L317 and test/index.js#L396-L414
A more human description of the plugin mechanism. Plugins are synchronous - no support and no need for async plugins here, but notice that you can do that manually, because that exact architecture.
The first function that is passed to the .use method is used for extending the core API,
for example adding a new method to the app
instance. That function is immediately invoked.
const parseFunction = require('parse-function');
const app = parseFunction();
app.use((self) => {
// self is same as `app`
console.log(self.use);
console.log(self.parse);
console.log(self.define);
self.define(self, 'foo', (bar) => bar + 1);
});
console.log(app.foo(2)); // => 3
On the other side, if you want to access the AST of the parser, you should return a function
from that plugin, which function is passed with (node, result)
signature.
This function is lazy plugin, it is called only when the .parse method is called.
const parseFunction = require('parse-function');
const app = parseFunction();
app.use((self) => {
console.log('immediately called');
return (node, result) => {
console.log('called only when .parse is invoked');
console.log(node);
console.log(result);
};
});
Where 1) the node
argument is an object - actual and real AST Node coming from the parser
and 2) the result
is an object too - the end Result, on which
you can add more properties if you want.
Generated using jest-runner-docs.
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
.
Signature
function(opts = {})
Params
opts
- optional, merged with options passed to .parse
methodreturns
- app object with .use
and .parse
methodsExample
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
- any kind of function or string to be parsedoptions
- directly passed to the parser babylon, acorn, espreeoptions.parse
- by default babylon.parseExpression
,
all options
are passed as second argumentreturns
- result see result section for more infoExample
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.
Params
fn
- plugin to be calledreturns
- app instance for chainingSee Plugins Architecture section.
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
- the object on which to define the propertyprop
- the name of the property to be defined or modifiedval
- the descriptor for the property being defined or modifiedreturns
- obj the passed object, but modifiedExample
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
In the result object you have
name
,args
,params
,body
and few hidden properties that can be useful to determine what the function is - arrow, regular, async/await or generator.
name
{String|null}: name of the passed function or null
if anonymousargs
{Array}: arguments of the functionparams
{String}: comma-separated list representing the args
defaults
{Object}: key/value pairs, useful when use ES2015 default argumentsbody
{String}: actual body of the function, respects trailing newlines and whitespacesisValid
{Boolean}: is the given value valid or not, that's because it never throws!isAsync
{Boolean}: true
if function is ES2015 async/await functionisArrow
{Boolean}: true
if the function is arrow functionisNamed
{Boolean}: true
if function has name, or false
if is anonymousisGenerator
{Boolean}: true
if the function is ES2015 generator functionisAnonymous
{Boolean}: true
if the function don't have namePlease 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.
Become a Partner or Sponsor? :dollar: Check the Partner, Sponsor or Omega-level tiers! :tada: You can get your company logo, link & name on this file. It's also rendered on package 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:
Thanks to the hard work of these wonderful people this project is alive! It follows the all-contributors specification. Don't hesitate to add yourself to that list if you have made any contribution! ;) See how, here.
Charlike Mike Reagent 💻 📖 💬 👀 🔍 |
---|
Consider showing your support to them. :sparkling_heart:
Copyright (c) 2016-present, Charlike Mike Reagent <opensource@tunnckocore.com>
& contributors.
Released under the MPL-2.0 License.
FAQs
Parse a function into an object using espree, acorn or babylon parsers. Extensible through Smart Plugins
The npm package parse-function receives a total of 100,667 weekly downloads. As such, parse-function popularity was classified as popular.
We found that parse-function demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.