Skip to content

convert @babel/parser to typescript#13370

Closed
zxbodya wants to merge 16 commits intobabel:mainfrom
zxbodya:ts-babel-parser
Closed

convert @babel/parser to typescript#13370
zxbodya wants to merge 16 commits intobabel:mainfrom
zxbodya:ts-babel-parser

Conversation

@zxbodya
Copy link
Copy Markdown
Contributor

@zxbodya zxbodya commented May 25, 2021

Q                       A
License MIT

Originally this was a part of #11578 however it was decided better to do it separately, because it looks to be bigger refactoring(not just converting it to typescript).

This is still very much in progress - and is just a starting point, to start experimenting with next steps.

Remaining issues, to solve:

  1. Decide how to type incomplete nodes

The problem is that currently parser typically starts with node which does not yet have a specific node type, and then adds more properties to it or removes some of them, to eventually get final node type.

Often it looks like this:

 // creates `Node` which is not expected to have additional props, T also in many cases is not yet clear
const node = this.startNode<T extends Node>();
if() {
  node.someNode1SpecificProp = ;
  return this.finishNode<T>(node, "Node1");
} else {
  node.someNode2SpecificProp = ;
  delete node.somethingNotUseful;
  return this.finishNode<T>(node, "Node2");
} 

TypeScript does not work well with that approach… Also, there might be some space for optimization, not sure but I guess it might be faster to have constructor or object literal

  1. Decide what to do about src/types

Node types there are incomplete, and in some cases were effectively same as any for type-checks with flow… I tried to fix it, however I guess - @babel/types probably should be used instead or to add some code generation for parser as well.


Suggestion - to try doing something like this instead:

 // creates builder which stores some information about context where node was started
const nodeBuilder = this.startNodeBuilder();
if() {
  const someNode1SpecificProp = ;
  // inside to use builder from @babel/types
  return nodeBuilder.node1(someNode1SpecificProp);
} else {
  const someNode2SpecificProps = ;
  // use builder from @babel/types
  return nodeBuilder.node2(someNode1SpecificProp);
} 

This would be much better for type-checking, and will require less manual type annotations.

However about builders - probably this might require some changes for generated builders in @babel/types if to used them - I worry, they might be slow, In particular - I think we might want to remove or disable runtime parameter validation(not actually sure how beneficial that would be…), and generally make them less dynamic instead generating more code.

Drafted changes for @babel/types here #13369

@codesandbox-ci
Copy link
Copy Markdown

codesandbox-ci bot commented May 25, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit c5c78ab:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@babel-bot
Copy link
Copy Markdown
Collaborator

babel-bot commented Aug 11, 2021

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/48162/

@zxbodya zxbodya force-pushed the ts-babel-parser branch 2 times, most recently from 3b8b2f3 to c5c78ab Compare August 13, 2021 10:33
@JLHwung
Copy link
Copy Markdown
Contributor

JLHwung commented Aug 18, 2021

Also, there might be some space for optimization, not sure but I guess it might be faster to have constructor or object literal

I agree, object literals are faster than a class, but we can not use object literal in Babel 7 because of the undocumented __clone method:

NodePrototype.__clone = function (): Node {

@babel/types probably should be used instead or to add some code generation for parser as well.

Agreed. The fastest approach is to generate AST node from object literals, like kataw did in AST builders: https://github.com/kataw/kataw/blob/main/src/ast/expressions/array-literal.ts.

@JLHwung JLHwung added the Flow -> TS Tracking repository migration from Flow to TS label Nov 25, 2021
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 22, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Flow -> TS Tracking repository migration from Flow to TS outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants