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

Conversation

tunnckoCore
Copy link
Owner

@tunnckoCore tunnckoCore commented Oct 19, 2019

resolves #65 + TypeScript. Really recommended to always return Result from the plugins.

The new API is the looks like this

import { parse as acornParse } from 'acorn';
import { parseFunction } from 'parse-function';

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

  // Really recommended to always return `Result` from the plugins.
  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);

The console log output is

{
  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'
}

TypeScript typings

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;
}

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 <opensource@tunnckocore.com>
Signed-off-by: Charlike Mike Reagent <opensource@tunnckocore.com>
Signed-off-by: Charlike Mike Reagent <opensource@tunnckocore.com>
Signed-off-by: Charlike Mike Reagent <opensource@tunnckocore.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Next Major: Simplifying the API, docs and the plugins, smaller codebase, TypeScript support
1 participant