You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 8, 2020. It is now read-only.
@@ -186,6 +189,168 @@ you can add more properties if you want.
186
189
## API
187
190
Review carefully the provided examples and the working [tests](./test/index.js).
188
191
192
+
### [parseFunction](src/index.js#L60)
193
+
> 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`.
194
+
195
+
**Params**
196
+
197
+
*`opts`**{Object}**: optional, merged with options passed to `.parse` method
198
+
*`returns`**{Object}**`app`: object with `.use` and `.parse` methods
199
+
200
+
**Example**
201
+
202
+
```js
203
+
constparseFunction=require('parse-function')
204
+
205
+
constapp=parseFunction({
206
+
ecmaVersion:2017
207
+
})
208
+
209
+
constfixtureFn= (a, b, c) => {
210
+
a = b + c
211
+
return a +2
212
+
}
213
+
214
+
constresult=app.parse(fixtureFn)
215
+
console.log(result)
216
+
217
+
// see more
218
+
console.log(result.name) // => null
219
+
console.log(result.isNamed) // => false
220
+
console.log(result.isArrow) // => true
221
+
console.log(result.isAnonymous) // => true
222
+
223
+
// array of names of the arguments
224
+
console.log(result.args) // => ['a', 'b', 'c']
225
+
226
+
// comma-separated names of the arguments
227
+
console.log(result.params) // => 'a, b, c'
228
+
```
229
+
230
+
### [.parse](src/index.js#L101)
231
+
> 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.
232
+
233
+
**Params**
234
+
235
+
*`code`**{Function|String}**: any kind of function or string to be parsed
236
+
*`options`**{Object}**: directly passed to the parser - babylon, acorn, espree
237
+
*`options.parse`**{Function}**: by default `babylon.parseExpression`, all `options` are passed as second argument to that provided function
238
+
*`returns`**{Object}**`result`: see [result section](#result) for more info
239
+
240
+
**Example**
241
+
242
+
```js
243
+
constacorn=require('acorn')
244
+
constparseFn=require('parse-function')
245
+
constapp=parseFn()
246
+
247
+
constfn=functionfoo (bar, baz) { return bar * baz }
248
+
constresult=app.parse(fn, {
249
+
parse:acorn.parse,
250
+
ecmaVersion:2017
251
+
})
252
+
253
+
console.log(result.name) // => 'foo'
254
+
console.log(result.args) // => ['bar', 'baz']
255
+
console.log(result.body) // => ' return bar * baz '
256
+
console.log(result.isNamed) // => true
257
+
console.log(result.isArrow) // => false
258
+
console.log(result.isAnonymous) // => false
259
+
console.log(result.isGenerator) // => false
260
+
```
261
+
262
+
### [.use](src/index.js#L173)
263
+
> 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.
> 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.
304
+
305
+
**Params**
306
+
307
+
*`obj`**{Object}**: the object on which to define the property
308
+
*`prop`**{String}**: the name of the property to be defined or modified
309
+
*`val`**{Any}**: the descriptor for the property being defined or modified
310
+
*`returns`**{Object}**`obj`: the passed object, but modified
311
+
312
+
**Example**
313
+
314
+
```js
315
+
constparseFunction=require('parse-function')
316
+
constapp=parseFunction()
317
+
318
+
// use it like `define-property` lib
319
+
constobj= {}
320
+
app.define(obj, 'hi', 'world')
321
+
console.log(obj) // => { hi: 'world' }
322
+
323
+
// or define a custom plugin that adds `.foo` property
324
+
// to the end result, returned from `app.parse`
325
+
app.use((app) => {
326
+
return (node, result) => {
327
+
// this function is called
328
+
// only when `.parse` is called
329
+
330
+
app.define(result, 'foo', 123)
331
+
332
+
return result
333
+
}
334
+
})
335
+
336
+
// fixture function to be parsed
337
+
constasyncFn=async (qux) => {
338
+
constbar=awaitPromise.resolve(qux)
339
+
return bar
340
+
}
341
+
342
+
constresult=app.parse(asyncFn)
343
+
344
+
console.log(result.name) // => null
345
+
console.log(result.foo) // => 123
346
+
console.log(result.args) // => ['qux']
347
+
348
+
console.log(result.isAsync) // => true
349
+
console.log(result.isArrow) // => true
350
+
console.log(result.isNamed) // => false
351
+
console.log(result.isAnonymous) // => true
352
+
```
353
+
189
354
**[back to top](#thetop)**
190
355
191
356
### Result
@@ -249,7 +414,7 @@ Project scaffolded and managed with [hela][].
0 commit comments