tsearch
tsearch copied to clipboard
Do not reimplement AST
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.