Skip to content

Do not reimplement AST#37

Draft
fredefox wants to merge 2 commits intomasterfrom
fredefox/do-not-reimplement-ast
Draft

Do not reimplement AST#37
fredefox wants to merge 2 commits intomasterfrom
fredefox/do-not-reimplement-ast

Conversation

@fredefox
Copy link
Collaborator

This isn't really in a usable state, but my thought was that it should
not be necessary to try and re-implement the AST of TypeScript types.
In stead we should be able to pull out the representation as defined
by the typescript/lib module (which ts-morph wraps). Now,
unfortunately ts-morph is infected with OOP and so the structures
are circular and cannot directly be serialized. So a little logic
is required to flatten the data.[see footnote 1] This is what I've tried to do.

My implementation uses unsafe coercions in two places. I'm not very
happy with this.

Below is an example of how the exported representation will differ
with this patch. Note that this will break your Haskell package
which is not expecting this structure.

One things that would need a bit of extra work is to convert the
returnType field into something more structured. Really it's a
Type node and does have a well-defined structure - but for some
reason when it's converted to JSON only a string representation is
output.

Before:

> node bin/ts-earch . | jq '.[0:1]'
[
  {
    "name": "findFunctions",
    "text": "\nexport default function findFunctions(directory: string, options: ts.ProjectOptions): FunctionRecord[];",
    "docs": "",
    "signature": {
      "parameters": [
        {
          "name": "directory",
          "type": {
            "__tag": "StringT"
          }
        },
        {
          "name": "options",
          "type": {
            "__tag": "Other",
            "values": "import(\"/home/fredefox/git/tsearch-io/indexer/node_modules/ts-morph/lib/ts-morph\").ProjectOptions"
          }
        }
      ],
      "returnType": {
        "__tag": "Other",
        "values": "import(\"/home/fredefox/git/tsearch-io/indexer/build/types\").FunctionRecord[]"
      }
    },
    "module": "indexer",
    "location": {
      "path": "build/index.d.ts",
      "lines": {
        "from": 3,
        "to": 3
      }
    }
  }
]

After:

> node bin/ts-earch . | jq '.[0:1]'
[
  {
    "name": "findFunctions",
    "type": {
      "parameters": [
        {
          "name": "directory",
          "type": "string",
          "isReadonly": false,
          "decorators": [],
          "hasQuestionToken": false,
          "kind": 28,
          "isRestParameter": false
        },
        {
          "name": "options",
          "type": "ts.ProjectOptions",
          "isReadonly": false,
          "decorators": [],
          "hasQuestionToken": false,
          "kind": 28,
          "isRestParameter": false
        }
      ],
      "returnType": "{\n    name: string;\n    type: ts.Structure;\n}[]",
      "typeParameters": []
    }
  }
]

[1]: Alternatively some function that I have yet to find allows for exporting the structure whole-sale.

@fredefox fredefox marked this pull request as draft June 14, 2020 14:18
@fredefox fredefox mentioned this pull request Jun 14, 2020
`~` is expanded by the shell before being passed to the program.
Programs don't need to re implement this.  Compare:

    > file ~/.profile
    /home/fredefox/.profile: ASCII text
    > file "~/.profile"
    ~/.profile: cannot open `~/.profile' (No such file or directory)

You can test what something expands to with `echo`:

    > echo ~/.profile
    /home/fredefox/.profile
@fredefox fredefox force-pushed the fredefox/do-not-reimplement-ast branch from dda037c to 45415f3 Compare June 14, 2020 14:19
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.

1 participant