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.
Copy file name to clipboardExpand all lines: .verb.md
+145-6Lines changed: 145 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,9 @@
6
6
7
7
> {%= description %}
8
8
9
-
_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._
9
+
<divid="thetop"></div>
10
+
11
+
{%= include('highlight') %}
10
12
11
13
## Quality Assurance :100:
12
14
@@ -44,17 +46,154 @@ You may also read the [Contributing Guide](./CONTRIBUTING.md). There, beside _"H
44
46
45
47
## Install
46
48
47
-
This project requires [Node.js][nodeversion-url] v{%= engines.node.slice(2) %} and above. Use [npm](https://www.npmjs.com) to install it.
49
+
This project requires [**Node.js**][nodeversion-url]**v{%= engines.node.slice(2) %}** and above. Use [**yarn**](https://yarnpkg.com)**v{%= engines.yarn.slice(2) %}** / [**npm**](https://npmjs.com)**v{%= engines.npm.slice(2) %}** or above to install it.
50
+
51
+
```
52
+
$ yarn add {%= name %}
53
+
```
54
+
55
+
## Which version to use?
56
+
57
+
There's no breaking changes between the `v2.x` version. The only breaking is `v2.1` which also is not
58
+
working properly, so no use it.
59
+
60
+
**Use v2.0.x**
61
+
62
+
When you don't need support for `arrow functions` and `es6 default params`. This version
63
+
uses a RegExp expression to work.
64
+
65
+
**Use v2.2.x**
66
+
67
+
Only when you need a _basic_ support for `es6 features` like arrow functions. This version
68
+
uses a RegExp expression to work.
69
+
70
+
**Use v2.3.x**
71
+
72
+
When you want _full*_ support for `arrow functions` and `es6 default params`. Where this "full",
73
+
means "almost full", because it has bugs. This version also uses (`acorn.parse`) real parser
74
+
to do the parsing.
75
+
76
+
**Use v3.x**
77
+
78
+
When you want to use different parser instead of the default `babylon.parse`, by passing custom
79
+
parse function to the `options.parse` option. **From this version we require `node >= 4`**.
80
+
81
+
**Use v4.x**
82
+
83
+
When you want full customization and most stable support for old and modern features. This version
84
+
uses `babylon.parseExpression` for parsing and provides a [Plugins API](#plugins-architecture).
85
+
See the [Features](#features) section for more info.
86
+
87
+
**Use v5.x**
88
+
89
+
It is basically the same as `v4`, but requires Node 6 & npm 5. Another is boilerplate stuff.
90
+
91
+
**[back to top](#thetop)**
92
+
93
+
## Notes
94
+
95
+
### Throws in one specific case
96
+
97
+
> _see: [issue #3](https://github.com/tunnckoCore/parse-function/issues/3) and [test/index.js#L229-L235](https://github.com/tunnckoCore/parse-function/blob/master/test/index.js#L229-L235)_
98
+
99
+
It may throw in one specific case, otherwise it won't throw, so you should
100
+
relay on the `result.isValid` for sure.
101
+
102
+
### Function named _"anonymous"_
48
103
104
+
> _see: [test/index.js#L319-L324](https://github.com/tunnckoCore/parse-function/blob/master/test/index.js#L319-L324) and [Result](#result) section_
105
+
106
+
If you pass a function which is named _"anonymous"_ the `result.name` will be `'anonymous'`,
107
+
but the `result.isAnonymous` will be `false` and `result.isNamed` will be `true`, because
108
+
in fact it's a named function.
109
+
110
+
### Real anonymous function
111
+
112
+
> _see: [test/index.js#L326-L331](https://github.com/tunnckoCore/parse-function/blob/master/test/index.js#L326-L331) and [Result](#result) section_
113
+
114
+
Only if you pass really an anonymous function you will get `result.name` equal to `null`,
115
+
`result.isAnonymous` equal to `true` and `result.isNamed` equal to `false`.
116
+
117
+
**[back to top](#thetop)**
118
+
119
+
### Plugins Architecture
120
+
121
+
> _see: the [.use](#use) method, [test/index.js#L305-L317](https://github.com/tunnckoCore/parse-function/blob/master/test/index.js#L305-L317) and [test/index.js#L396-L414](https://github.com/tunnckoCore/parse-function/blob/master/test/index.js#L396-L414)_
122
+
123
+
A more human description of the plugin mechanism. Plugins are **synchronous** - no support
124
+
and no need for **async** plugins here, but notice that you can do that manually, because
125
+
that exact architecture.
126
+
127
+
The first function that is passed to the [.use](#use) method is used for extending the core API,
128
+
for example adding a new method to the `app` instance. That function is immediately invoked.
129
+
130
+
```js
131
+
constparseFunction=require('parse-function')
132
+
constapp=parseFunction()
133
+
134
+
app.use((self) => {
135
+
// self is same as `app`
136
+
console.log(self.use)
137
+
console.log(self.parse)
138
+
console.log(self.define)
139
+
140
+
self.define(self, 'foo', (bar) => bar +1)
141
+
})
142
+
143
+
console.log(app.foo(2)) // => 3
49
144
```
50
-
$ npm install {%= name %}
145
+
146
+
On the other side, if you want to access the AST of the parser, you should return a function
147
+
from that plugin, which function is passed with `(node, result)` signature.
148
+
149
+
This function is lazy plugin, it is called only when the [.parse](#parse) method is called.
150
+
151
+
```js
152
+
constparseFunction=require('parse-function')
153
+
constapp=parseFunction()
154
+
155
+
app.use((self) => {
156
+
console.log('immediately called')
157
+
158
+
return (node, result) => {
159
+
console.log('called only when .parse is invoked')
160
+
console.log(node)
161
+
console.log(result)
162
+
}
163
+
})
51
164
```
52
165
166
+
Where **1)** the `node` argument is an object - actual and real AST Node coming from the parser
167
+
and **2)** the `result` is an object too - the end [Result](#result), on which
168
+
you can add more properties if you want.
169
+
170
+
**[back to top](#thetop)**
171
+
53
172
## API
54
-
Review carefully the provided examples and the working [tests](./test.js).
173
+
Review carefully the provided examples and the working [tests](./test/index.js).
55
174
56
175
{%= apidocs('src/index.js') %}
57
176
177
+
**[back to top](#thetop)**
178
+
179
+
### Result
180
+
> In the result object you have `name`, `args`, `params`, `body` and few hidden properties
181
+
that can be useful to determine what the function is - arrow, regular, async/await or generator.
182
+
183
+
*`name`**{String|null}**: name of the passed function or `null` if anonymous
184
+
*`args`**{Array}**: arguments of the function
185
+
*`params`**{String}**: comma-separated list representing the `args`
186
+
*`defaults`**{Object}**: key/value pairs, useful when use ES2015 default arguments
187
+
*`body`**{String}**: actual body of the function, respects trailing newlines and whitespaces
188
+
*`isValid`**{Boolean}**: is the given value valid or not, that's because it never throws!
189
+
*`isAsync`**{Boolean}**: `true` if function is ES2015 async/await function
190
+
*`isArrow`**{Boolean}**: `true` if the function is arrow function
191
+
*`isNamed`**{Boolean}**: `true` if function has name, or `false` if is anonymous
192
+
*`isGenerator`**{Boolean}**: `true` if the function is ES2015 generator function
193
+
*`isAnonymous`**{Boolean}**: `true` if the function don't have name
194
+
195
+
**[back to top](#thetop)**
196
+
58
197
{% if (verb.related && verb.related.list && verb.related.list.length) { %}
59
198
## Related
60
199
{%= related(verb.related.list, { words: 12 }) %}
@@ -70,12 +209,12 @@ Please read the [Contributing Guide](./CONTRIBUTING.md) and [Code of Conduct](./
0 commit comments