Skip to content

Commit e151a61

Browse files
committed
feat!(parse-function): simpler API, rewrite to TypeScript
BREAKING CHANGE: exposes named function `parseFunction(Input, Options): Result` The `options.parse` can be any parser `.parse` method, tested against Babel, ESPree & Acorn ```ts type Input = string | (...args: any) => any; interface Options { parse?(input: string, options?: ParserOptions): import('@babel/types').File; parserOptions?: ParserOptions; plugins?: Plugins; } type Plugin = (node: any, result: Result) => Result | undefined; type Plugins = Plugin | Array<Plugin>; interface Result { name: string | null; body: string; args: Array<string>; params: string; defaults: { [key: string]: string | undefined }; value: string; isValid: boolean; isArrow: boolean; isAsync: boolean; isNamed: boolean; isAnonymous: boolean; isGenerator: boolean; isExpression: boolean; } ``` Signed-off-by: Charlike Mike Reagent <[email protected]>
1 parent 2b529c1 commit e151a61

File tree

19 files changed

+10478
-5789
lines changed

19 files changed

+10478
-5789
lines changed

@types/undeclared.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
declare module 'espree';
2+
declare module 'acorn-loose';
3+
declare module 'for-in';
4+
declare module 'define-property' {
5+
export default function(obj: any, name: string, value: any): void;
6+
}

package.json

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,29 @@
3434
"@babel/plugin-transform-runtime": "^7.6.2",
3535
"@commitlint/cli": "^8.2.0",
3636
"@commitlint/config-conventional": "^8.2.0",
37+
"@types/jest": "^24.0.19",
38+
"@types/node": "^12.11.1",
39+
"@types/react": "^16.9.9",
40+
"@types/react-dom": "^16.9.2",
3741
"@wessberg/rollup-plugin-ts": "^1.1.73",
42+
"acorn-loose": "^7.0.0",
3843
"builtin-modules": "^3.1.0",
3944
"enquirer": "^2.3.2",
40-
"eslint": "^6.4.0",
45+
"eslint": "^6.5.1",
4146
"esm": "^3.2.25",
4247
"husky": "^3.0.9",
4348
"jest": "^24.9.0",
44-
"lerna": "^3.16.4",
49+
"lerna": "^3.18.1",
4550
"prettier": "^1.18.2",
4651
"prettier-plugin-pkg": "^0.4.4",
47-
"prettier-plugin-sh": "^0.2.0",
48-
"react": "^16.10.1",
49-
"rollup": "^1.23.1",
52+
"prettier-plugin-sh": "^0.2.1",
53+
"react": "^16.10.2",
54+
"rollup": "^1.25.0",
5055
"rollup-plugin-commonjs": "^10.1.0",
5156
"rollup-plugin-json": "^4.0.0",
5257
"rollup-plugin-node-resolve": "^5.2.0",
5358
"rollup-plugin-terser": "^5.1.2",
54-
"semver": "^6.3.0",
55-
"typescript": "^3.7.0-beta",
59+
"typescript": "^3.6.4",
5660
"verb": "verbose/verb#dev",
5761
"verb-generate-readme": "^0.8.0"
5862
},
@@ -69,11 +73,11 @@
6973
},
7074
"meta": {
7175
"build": [
72-
"koa-better-body"
76+
"koa-better-body",
77+
"parse-function"
7378
],
7479
"bundle": [
75-
"@tunnckocore/execa",
76-
"parse-function"
80+
"@tunnckocore/execa"
7781
]
7882
},
7983
"renovate": {

packages/parse-function/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"engines": {
1414
"node": ">=8.11"
1515
},
16-
"main": "dist/cjs/index.js",
17-
"module": "dist/esm/index.js",
16+
"main": "dist/main/index.js",
17+
"module": "dist/module/index.js",
1818
"types": "dist/types/index.d.ts",
1919
"files": [
2020
"dist"
@@ -48,7 +48,9 @@
4848
],
4949
"scripts": {},
5050
"dependencies": {
51-
"@babel/parser": "^7.6.4"
51+
"@types/babel__core": "^7.1.3",
52+
"@babel/parser": "^7.6.4",
53+
"@babel/types": "^7.6.3"
5254
},
5355
"devDependencies": {
5456
"acorn": "^7.1.0",

packages/parse-function/src/index.js

Lines changed: 0 additions & 228 deletions
This file was deleted.

packages/parse-function/src/index.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import arrayify from 'arrify';
3+
import { parse as babelParse } from '@babel/parser';
4+
5+
import { setDefaults, getNode } from './utils';
6+
import { Input, Options, Plugin, Result } from './types';
7+
8+
import basePlugin from './plugins/initial';
9+
10+
// eslint-disable-next-line import/prefer-default-export
11+
export function parseFunction(code: Input, options?: Options) {
12+
const opts: Options = { parse: babelParse, ...options };
13+
const result: Result = setDefaults(code);
14+
15+
if (!result.isValid) {
16+
return result;
17+
}
18+
19+
const isFunction = result.value.startsWith('function');
20+
const isAsyncFn = result.value.startsWith('async function');
21+
const isAsync = result.value.startsWith('async');
22+
const isArrow = result.value.includes('=>');
23+
const isAsyncArrow = isAsync && isArrow;
24+
25+
const isMethod = /^\*?.+\([\s\S\w\W]*\)\s*\{/i.test(result.value);
26+
27+
if (!(isFunction || isAsyncFn || isAsyncArrow) && isMethod) {
28+
result.value = `{ ${result.value} }`;
29+
}
30+
31+
const node = getNode(result, opts);
32+
const plugins = arrayify(opts.plugins);
33+
34+
return [basePlugin, ...plugins]
35+
.filter(Boolean)
36+
.reduce((res: any, fn: Plugin) => {
37+
const pluginResult = fn(node, { ...res }) || res;
38+
39+
return pluginResult;
40+
}, result);
41+
}

packages/parse-function/src/plugins/body.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)