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.
@@ -189,181 +186,6 @@ you can add more properties if you want.
189
186
## API
190
187
Review carefully the provided examples and the working [tests](./test/index.js).
191
188
192
-
### [parseFunction](src/index.js#L59)
193
-
194
-
> Initializes with optional `opts` object which is passed directly
195
-
to the desired parser and returns an object
196
-
with `.use` and `.parse` methods. The default parse which
197
-
is used is [babylon][]'s `.parseExpression` method from `v7`.
198
-
199
-
**Params**
200
-
201
-
*`opts`**{Object}**: optional, merged with options passed to `.parse` method
202
-
*`returns`**{Object}**: `app` object with `.use` and `.parse` methods
203
-
204
-
**Example**
205
-
206
-
```jsx
207
-
constparseFunction=require('parse-function')
208
-
209
-
constapp=parseFunction({
210
-
ecmaVersion:2017
211
-
})
212
-
213
-
constfixtureFn= (a, b, c) => {
214
-
a = b + c
215
-
return a +2
216
-
}
217
-
218
-
constresult=app.parse(fixtureFn)
219
-
console.log(result)
220
-
221
-
// see more
222
-
console.log(result.name) // => null
223
-
console.log(result.isNamed) // => false
224
-
console.log(result.isArrow) // => true
225
-
console.log(result.isAnonymous) // => true
226
-
227
-
// array of names of the arguments
228
-
console.log(result.args) // => ['a', 'b', 'c']
229
-
230
-
// comma-separated names of the arguments
231
-
console.log(result.params) // => 'a, b, c'
232
-
```
233
-
234
-
### [.parse](src/index.js#L98)
235
-
236
-
> Parse a given `code` and returns a `result` object
237
-
with useful properties - such as `name`, `body` and `args`.
238
-
By default it uses Babylon parser, but you can switch it by
239
-
passing `options.parse` - for example `options.parse: acorn.parse`.
240
-
In the below example will show how to use `acorn` parser, instead
241
-
of the default one.
242
-
243
-
**Params**
244
-
245
-
*`code`**{Function|String}**: any kind of function or string to be parsed
246
-
*`options`**{Object}**: directly passed to the parser - babylon, acorn, espree
247
-
*`options.parse`**{Function}**: by default `babylon.parseExpression`, all `options` are passed as second argument to that provided function
248
-
*`returns`**{Object}**: `result` see [result section](#result) for more info
249
-
250
-
**Example**
251
-
252
-
```jsx
253
-
constacorn=require('acorn')
254
-
constparseFn=require('parse-function')
255
-
constapp=parseFn()
256
-
257
-
constfn=functionfoo (bar, baz) { return bar * baz }
258
-
constresult=app.parse(fn, {
259
-
parse:acorn.parse,
260
-
ecmaVersion:2017
261
-
})
262
-
263
-
console.log(result.name) // => 'foo'
264
-
console.log(result.args) // => ['bar', 'baz']
265
-
console.log(result.body) // => ' return bar * baz '
266
-
console.log(result.isNamed) // => true
267
-
console.log(result.isArrow) // => false
268
-
console.log(result.isAnonymous) // => false
269
-
console.log(result.isGenerator) // => false
270
-
```
271
-
272
-
### [.use](src/index.js#L168)
273
-
> 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.
-[babylon](https://www.npmjs.com/package/babylon): A JavaScript parser | [homepage](https://babeljs.io/"A JavaScript parser")
390
212
-[charlike-cli](https://www.npmjs.com/package/charlike-cli): Command line interface for the [charlike][] project scaffolder. | [homepage](https://github.com/tunnckoCore/charlike-cli#readme"Command line interface for the [charlike][] project scaffolder.")
391
213
-[espree](https://www.npmjs.com/package/espree): An Esprima-compatible JavaScript parser built on Acorn | [homepage](https://github.com/eslint/espree"An Esprima-compatible JavaScript parser built on Acorn")
392
-
-[hela](https://www.npmjs.com/package/hela): Powerful & flexible task runner framework in 80 lines, based on execa… [more](https://github.com/tunnckoCore/hela#readme) | [homepage](https://github.com/tunnckoCore/hela#readme"Powerful & flexible task runner framework in 80 lines, based on execa. Supports presets, a la ESLint but for tasks & npm scripts")
214
+
-[hela](https://www.npmjs.com/package/hela): Powerful & flexible task runner framework in 80 lines, based on [execa… [more](https://github.com/tunnckoCore/hela#readme) | [homepage](https://github.com/tunnckoCore/hela#readme"Powerful & flexible task runner framework in 80 lines, based on [execa][]. Supports shareable configs, a la ESLint")
393
215
-[parse-semver](https://www.npmjs.com/package/parse-semver): Parse, normalize and validate given semver shorthand (e.g. [email protected]) to object. | [homepage](https://github.com/tunnckocore/parse-semver#readme"Parse, normalize and validate given semver shorthand (e.g. [email protected]) to object.")
394
216
395
217
## Contributing
@@ -402,14 +224,14 @@ Please read the [Contributing Guide](./CONTRIBUTING.md) and [Code of Conduct](./
0 commit comments