parse-function
Advanced tools
Comparing version 5.6.3 to 5.6.4
@@ -6,2 +6,14 @@ # Change Log | ||
## [5.6.4](https://github.com/tunnckoCore/opensource/compare/parse-function@5.6.3...parse-function@5.6.4) (2020-02-04) | ||
### Bug Fixes | ||
* docs runner, regen docs, and create-jest-runner updates ([d854e3d](https://github.com/tunnckoCore/opensource/commit/d854e3d335fa1d2c82d87321a07c6659fe6dcee1)) | ||
* dooh, readmes and bugs ([871666e](https://github.com/tunnckoCore/opensource/commit/871666e7eabbca6bf65cbc257311f0a46d410752)) | ||
## [5.6.3](https://github.com/tunnckoCore/opensource/compare/parse-function@5.6.2...parse-function@5.6.3) (2020-02-04) | ||
@@ -8,0 +20,0 @@ |
{ | ||
"name": "parse-function", | ||
"version": "5.6.3", | ||
"version": "5.6.4", | ||
"licenseStart": 2016, | ||
@@ -114,3 +114,3 @@ "license": "MPL-2.0", | ||
}, | ||
"gitHead": "36720fdd18247b0ce9134b9631a6d5848e6c6c0d" | ||
"gitHead": "66acb69b616e923f601e632bfe398c07d86fa739" | ||
} |
181
README.md
@@ -99,14 +99,5 @@ <p align="center"> | ||
* [parseFunction](#parsefunction) | ||
+ [Signature](#signature) | ||
+ [Params](#params) | ||
+ [Examples](#examples) | ||
* [.parse](#parse) | ||
+ [Params](#params-1) | ||
+ [Examples](#examples-1) | ||
* [.use](#use) | ||
+ [Params](#params-2) | ||
+ [Examples](#examples-2) | ||
* [.define](#define) | ||
+ [Params](#params-3) | ||
+ [Examples](#examples-3) | ||
- [Contributing](#contributing) | ||
@@ -273,6 +264,6 @@ * [Guides and Community](#guides-and-community) | ||
> 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`. | ||
> 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`. | ||
@@ -299,27 +290,27 @@ <span id="parsefunction-signature"></span> | ||
```js | ||
const parseFunction = require('parse-function'); | ||
const parseFunction = require('parse-function') | ||
const app = parseFunction({ | ||
ecmaVersion: 2017, | ||
}); | ||
ecmaVersion: 2017 | ||
}) | ||
const fixtureFn = (a, b, c) => { | ||
a = b + c; | ||
return a + 2; | ||
}; | ||
a = b + c | ||
return a + 2 | ||
} | ||
const result = app.parse(fixtureFn); | ||
console.log(result); | ||
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 | ||
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'] | ||
console.log(result.args) // => ['a', 'b', 'c'] | ||
// comma-separated names of the arguments | ||
console.log(result.params); // => 'a, b, c' | ||
console.log(result.params) // => 'a, b, c' | ||
``` | ||
@@ -329,7 +320,8 @@ | ||
> 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. | ||
> 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. | ||
@@ -342,4 +334,4 @@ <span id="parse-params"></span> | ||
- `options` **{object}** - directly passed to the parser babylon, acorn, espree | ||
- `options.parse` **{Function}** - by default `babylon.parseExpression`, all | ||
`options` are passed as second argument | ||
- `options.parse` **{Function}** - by default `babylon.parseExpression`, | ||
all `options` are passed as second argument | ||
- `returns` **{object}** - result see [result section](#result) for more info | ||
@@ -352,21 +344,19 @@ | ||
```js | ||
const acorn = require('acorn'); | ||
const parseFn = require('parse-function'); | ||
const app = parseFn(); | ||
const acorn = require('acorn') | ||
const parseFn = require('parse-function') | ||
const app = parseFn() | ||
const fn = function foo(bar, baz) { | ||
return bar * baz; | ||
}; | ||
const fn = function foo (bar, baz) { return bar * baz } | ||
const result = app.parse(fn, { | ||
parse: acorn.parse, | ||
ecmaVersion: 2017, | ||
}); | ||
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 | ||
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 | ||
``` | ||
@@ -376,9 +366,10 @@ | ||
> 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](#result) from the `.parse` | ||
> method. This retuned function is called on each node only when `.parse` method | ||
> is called. | ||
> 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](#result) | ||
from the `.parse` method. This retuned function is called on each | ||
node only when `.parse` method is called. | ||
@@ -401,7 +392,7 @@ <span id="use-params"></span> | ||
app.use((app) => { | ||
app.define(app, 'hello', (place) => `Hello ${place}!`); | ||
}); | ||
app.define(app, 'hello', (place) => `Hello ${place}!`) | ||
}) | ||
const hi = app.hello('World'); | ||
console.log(hi); // => 'Hello World!' | ||
const hi = app.hello('World') | ||
console.log(hi) // => 'Hello World!' | ||
@@ -411,18 +402,16 @@ // or plugin that works on AST nodes | ||
if (node.type === 'ArrowFunctionExpression') { | ||
result.thatIsArrow = true; | ||
result.thatIsArrow = true | ||
} | ||
return result; | ||
}); | ||
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((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 | ||
const result = app.parse(function foo () { return 123 }) | ||
console.log(result.name) // => 'foo' | ||
console.log(result.isArrow) // => false | ||
console.log(result.thatIsArrow) // => undefined | ||
``` | ||
@@ -432,5 +421,5 @@ | ||
> 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. | ||
> 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. | ||
@@ -451,9 +440,9 @@ <span id="define-params"></span> | ||
```js | ||
const parseFunction = require('parse-function'); | ||
const app = parseFunction(); | ||
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' } | ||
const obj = {} | ||
app.define(obj, 'hi', 'world') | ||
console.log(obj) // => { hi: 'world' } | ||
@@ -467,24 +456,24 @@ // or define a custom plugin that adds `.foo` property | ||
app.define(result, 'foo', 123); | ||
app.define(result, 'foo', 123) | ||
return result; | ||
}; | ||
}); | ||
return result | ||
} | ||
}) | ||
// fixture function to be parsed | ||
const asyncFn = async (qux) => { | ||
const bar = await Promise.resolve(qux); | ||
return bar; | ||
}; | ||
const bar = await Promise.resolve(qux) | ||
return bar | ||
} | ||
const result = app.parse(asyncFn); | ||
const result = app.parse(asyncFn) | ||
console.log(result.name); // => null | ||
console.log(result.foo); // => 123 | ||
console.log(result.args); // => ['qux'] | ||
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 | ||
console.log(result.isAsync) // => true | ||
console.log(result.isArrow) // => true | ||
console.log(result.isNamed) // => false | ||
console.log(result.isAnonymous) // => true | ||
``` | ||
@@ -491,0 +480,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
61287
636