Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

major: v6 of parse-function #72

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore(docs): update plugins docs
Signed-off-by: Charlike Mike Reagent <[email protected]>
tunnckoCore committed Oct 24, 2019
commit 4b486fc3ac38c1f12c7398bd4c5c6ca7582f0445
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -14,14 +14,14 @@
"clean:dist": "rm -rf configs/*/dist packages/*/dist @tunnckocore/*/dist",
"clean:fresh": "lerna clean -y && rm -rf node_modules",
"cleanup": "yarn clean:dist && yarn clean:fresh",
"cmt": "LOCAL_TESTING=0 CI=1 yarn start test -u && yarn testy && git status --porcelain",
"cr": "node create-package.js",
"patch:hela": "cp patches/hela-cli/* node_modules/@hela/cli/dist/build/cjs/",
"patch:sade": "cp patches/sade/* node_modules/sade/lib/",
"patch:verb": "cp patches/verb-repo-helpers/index.js node_modules/verb-repo-helpers/index.js",
"postsetup": "yarn patch:verb && yarn patch:hela && yarn patch:sade",
"postsetup:ci": "yarn patch:verb && yarn patch:hela && yarn patch:sade",
"pre-commit": "echo nothing",
"cmt": "LOCAL_TESTING=0 CI=1 yarn start test -u && yarn testy && git status --porcelain",
"refresh": "yarn cleanup && yarn setup",
"release": "lerna version && lerna publish from-package",
"setup": "yarn && yarn bootstrap",
51 changes: 21 additions & 30 deletions packages/parse-function/.verb.md
Original file line number Diff line number Diff line change
@@ -67,44 +67,35 @@ Only if you pass really an anonymous function you will get `result.name` equal t
> _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)_

A more human description of the plugin mechanism. Plugins are **synchronous** - no support
and no need for **async** plugins here.
and no need for **async** plugins here. Plugins MAY or MAY NOT return Result object, so
don't worry if you miss to return something.

```js
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.
import { parseFunction } from 'parse-function';

This function is lazy plugin, it is called only when the [.parse](#parse) method is called.
function someFn(foo, bar) {
return foo + bar;
}

```js
const parseFunction = require('parse-function');
const app = parseFunction();
const plugins = [
(node, result) => {
return { ...result, zaz: 111 };
},
(node, result) => {
return { qux: result.zaz + 202 };
},
];

app.use((self) => {
console.log('immediately called');
const res = parseFunction(someFn, { plugins });
console.log(res);

return (node, result) => {
console.log('called only when .parse is invoked');
console.log(node);
console.log(result);
};
});
console.log(res.name); // => 'someFn'
console.log(res.zaz); // => 111
console.log(res.qux); // => 313
```

If you want to access the AST of the parser, use the `node` parameter of the plugin.

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](#result), on which
you can add more properties if you want.
51 changes: 21 additions & 30 deletions packages/parse-function/README.md
Original file line number Diff line number Diff line change
@@ -165,44 +165,35 @@ Only if you pass really an anonymous function you will get `result.name` equal t
> _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)_

A more human description of the plugin mechanism. Plugins are **synchronous** - no support
and no need for **async** plugins here.
and no need for **async** plugins here. Plugins MAY or MAY NOT return Result object, so
don't worry if you miss to return something.

```js
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.
import { parseFunction } from 'parse-function';

This function is lazy plugin, it is called only when the [.parse](#parse) method is called.
function someFn(foo, bar) {
return foo + bar;
}

```js
const parseFunction = require('parse-function');
const app = parseFunction();
const plugins = [
(node, result) => {
return { ...result, zaz: 111 };
},
(node, result) => {
return { qux: result.zaz + 202 };
},
];

app.use((self) => {
console.log('immediately called');
const res = parseFunction(someFn, { plugins });
console.log(res);

return (node, result) => {
console.log('called only when .parse is invoked');
console.log(node);
console.log(result);
};
});
console.log(res.name); // => 'someFn'
console.log(res.zaz); // => 111
console.log(res.qux); // => 313
```

If you want to access the AST of the parser, use the `node` parameter of the plugin.

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](#result), on which
you can add more properties if you want.