solidity-parser / parser

A Solidity parser for JS built on top of a robust ANTLR4 grammar
MIT License
152 stars 43 forks source link

Typescript node types #71

Open robinstraub opened 2 years ago

robinstraub commented 2 years ago

Thank you for your work, the project is working great.

I am using typescript and would like to make use of node typing. As the project is a fork of @federicobond project, in which there are node typings (https://github.com/federicobond/solidity-parser-antlr/blob/master/index.d.ts) I was wondering if typing were implemented as well in this project or if it was something planned ?

fvictorio commented 2 years ago

This project is now in typescript, so types should work out of the box. Are they not working for you?

robinstraub commented 2 years ago

I did not note the project was now using typescript my bad.

Typings work correctly for the exported functions (parse, tokenize, visit). The module is not however (to my understanding) exporting the node typings, which I would like to reuse.

Trying to import a node type fails with a "no exported member" error. for instance the following line

import { parse, SourceUnit } from '@solidity-parser/parser';

causes the following build issue:

src/parser/parser.service.ts:2:17 - error TS2305: Module '"@solidity-parser/parser"' has no exported member 'SourceUnit'.

2 import { parse, SourceUnit } from '@solidity-parser/parser';

I am able to import a node type, but not from the index:

import { SourceUnit } from '@solidity-parser/parser/dist/src/ast-types';

Is this the expected behavior ? the package.json references the types entry point to the src/index.d.ts ("types": "dist/src/index.d.ts",) which in turn only exports types for the core parsing functions, not the nodes.

Considering you did handle building the type files to the dist, it would make sense to me that we can import them directly from the main module.

fvictorio commented 2 years ago

Oh I see. I guess I could export them too. But since there are a lot of them, I believe it would be good to export them either under some path (e.g. @solidity-parser/parser/ast) or under some namespace (import { AST } from "@solidity-parser/parser" and then AST.SourceUnit)