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

feat!(parse-function): simpler API, rewrite to TypeScript #67

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions @types/undeclared.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare module 'espree';
declare module 'acorn-loose';
declare module 'for-in';
declare module 'define-property' {
export default function(obj: any, name: string, value: any): void;
}
23 changes: 13 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,28 @@
"@babel/plugin-transform-runtime": "^7.6.2",
"@commitlint/cli": "^8.2.0",
"@commitlint/config-conventional": "^8.2.0",
"@types/jest": "^24.0.19",
"@types/node": "^12.11.1",
"@types/react": "^16.9.9",
"@types/react-dom": "^16.9.2",
"@wessberg/rollup-plugin-ts": "^1.1.73",
"builtin-modules": "^3.1.0",
"enquirer": "^2.3.2",
"eslint": "^6.4.0",
"eslint": "^6.5.1",
"esm": "^3.2.25",
"husky": "^3.0.9",
"jest": "^24.9.0",
"lerna": "^3.16.4",
"lerna": "^3.18.1",
"prettier": "^1.18.2",
"prettier-plugin-pkg": "^0.4.4",
"prettier-plugin-sh": "^0.2.0",
"react": "^16.10.1",
"rollup": "^1.23.1",
"prettier-plugin-sh": "^0.2.1",
"react": "^16.10.2",
"rollup": "^1.25.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-terser": "^5.1.2",
"semver": "^6.3.0",
"typescript": "^3.7.0-beta",
"typescript": "^3.6.4",
"verb": "verbose/verb#dev",
"verb-generate-readme": "^0.8.0"
},
Expand All @@ -69,11 +72,11 @@
},
"meta": {
"build": [
"koa-better-body"
"koa-better-body",
"parse-function"
],
"bundle": [
"@tunnckocore/execa",
"parse-function"
"@tunnckocore/execa"
]
},
"renovate": {
Expand Down
41 changes: 41 additions & 0 deletions packages/parse-function/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { parse as acornParse } from 'acorn';
import { parseFunction } from './dist/module';

// `node` is an AST Node
function bobbyPlugin(node, result) {
const bobby = 'bobby';

return { ...result, bobby };
}

function barryPlugin(node, result) {
return { ...result, barry: 'barry barry' };
}

const result = parseFunction(bobbyPlugin.toString(), {
parse: acornParse,
plugins: [bobbyPlugin, barryPlugin], // supports array of plugins
parserOptions: {},
});

console.log(result);

/* {
name: 'bobbyPlugin',
body: "\n const bobby = 'bobby';\n\n return { ...result, bobby };\n",
args: [ 'node', 'result' ],
params: 'node, result',
defaults: { node: undefined, result: undefined },
value: '(function bobbyPlugin(node, result) {\n const ' +
"bobby = 'bobby';\n\n return { ...result, bobby };\n" +
'})',
isValid: true,
isArrow: false,
isAsync: false,
isNamed: true,
isAnonymous: false,
isGenerator: false,
isExpression: false,
bobby: 'bobby',
barry: 'barry barry'
} */
8 changes: 5 additions & 3 deletions packages/parse-function/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"engines": {
"node": ">=8.11"
},
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"main": "dist/main/index.js",
"module": "dist/module/index.js",
"types": "dist/types/index.d.ts",
"files": [
"dist"
Expand Down Expand Up @@ -48,7 +48,9 @@
],
"scripts": {},
"dependencies": {
"@babel/parser": "^7.6.4"
"@types/babel__core": "^7.1.3",
"@babel/parser": "^7.6.4",
"@babel/types": "^7.6.3"
},
"devDependencies": {
"acorn": "^7.1.0",
Expand Down
228 changes: 0 additions & 228 deletions packages/parse-function/src/index.js

This file was deleted.

41 changes: 41 additions & 0 deletions packages/parse-function/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import arrayify from 'arrify';
import { parse as babelParse } from '@babel/parser';

import { setDefaults, getNode } from './utils';
import { Input, Options, Plugin, Result } from './types';

import basePlugin from './plugins/initial';

// eslint-disable-next-line import/prefer-default-export
export function parseFunction(code: Input, options?: Options) {
const opts: Options = { parse: babelParse, ...options };
const result: Result = setDefaults(code);

if (!result.isValid) {
return result;
}

const isFunction = result.value.startsWith('function');
const isAsyncFn = result.value.startsWith('async function');
const isAsync = result.value.startsWith('async');
const isArrow = result.value.includes('=>');
const isAsyncArrow = isAsync && isArrow;

const isMethod = /^\*?.+\([\s\S\w\W]*\)\s*\{/i.test(result.value);

if (!(isFunction || isAsyncFn || isAsyncArrow) && isMethod) {
result.value = `{ ${result.value} }`;
}

const node = getNode(result, opts);
const plugins = arrayify(opts.plugins);

return [basePlugin, ...plugins]
.filter(Boolean)
.reduce((res: any, fn: Plugin) => {
const pluginResult = fn(node, { ...res }) || res;

return pluginResult;
}, result);
}
Loading