TypeChain icon indicating copy to clipboard operation
TypeChain copied to clipboard

contract names with underscores do not get type definitions

Open moodysalem opened this issue 5 years ago • 9 comments

If you have a contract Math5_0 typechain will not generate type definitions for it

I experienced this using hardhat + hardhat-typechain

moodysalem avatar Nov 30 '20 17:11 moodysalem

@moodysalem which target are you using, and which package versions? Is it only about underscores in contract names, or in property and function names too?

quezak avatar Nov 30 '20 18:11 quezak

ethers v5 target seems to be contract names/solidity file names only haven't tried anything else

hardhat-typechain 0.3.3 hardhat 2.0.3

moodysalem avatar Nov 30 '20 19:11 moodysalem

@moodysalem repository with reproduction example is worth a thousand words and for sure it will allow us to fix it faster.

krzkaczor avatar Nov 30 '20 20:11 krzkaczor

@moodysalem repository with reproduction example is worth a thousand words and for sure it will allow us to fix it faster.

it should be pretty easy to reproduce this as a test case, if it's actually a problem. i can easily repro by renaming a contract with an underscore and running yarn compile, i cant imagine it's specific to my setup

moodysalem avatar Nov 30 '20 20:11 moodysalem

Well, if it's as simple as our name normalizing function messing up this particular name then it's easy but if it's not repro would be useful. That being said, I think there is a pretty high chance it's the former so maybe repro won't be needed after all.

krzkaczor avatar Nov 30 '20 20:11 krzkaczor

I just checked implementation and as for the current rules Math5_0 will end up as Math50.

https://github.com/ethereum-ts/TypeChain/blob/master/packages/typechain/test/parser/normalizeName.test.ts#L12

@quezak I am not sure what should be done in this case. It seems like name mangling is quite a complicated problem b/c people often have some unexpected naming schemes... currently we try to generate most natural ts symbols that's why we remove _ and make next character uppercase. It seems like it's hard to come up with something that fits everyone.

I see two solutions:

  1. Have a flag to turn off the name mangling entirely. This will force users to rename their files.
  2. Have a way to inject any arbitrary function that does mangling.

Both suck. For now, I would recommend simply renaming your file.

krzkaczor avatar Dec 04 '20 22:12 krzkaczor

why do we normalize the filename in the first place? Solidity naming conventions are mostly similar to TS/JS ones. If someone wants or needs an underscore in the solidity contract name, my opinion is let'em have it in the typescript class and file too.

Have a flag to turn off the name mangling entirely. This will force users to rename their files.

Why would they need to rename the files? What's wrong with having the same filename as the contract name?

quezak avatar Dec 04 '20 22:12 quezak

As far as I remember the main reason for this feature is that symbols need to be normalized b/c filenames often are not valid JS symbols. Note that we don't have access to a contract name - only it's filename.

From the docs for this function:

Converts valid file names to valid javascript symbols and does best effort to make them readable. Example: ds-token.test becomes DsTokenTest

Note that:

  1. We are not strictly required to do filenames normalization (only symbols in generated files).
  2. We could simplify rules for normalization

But this was all done to improve DX afaik...

krzkaczor avatar Dec 04 '20 22:12 krzkaczor

I'm fine with normalization... but you could just leave in the underscore

FWIW I didn't see any file generated at all. But we are no longer using this naming convention so this is really low prio for us. Feel free to close it

moodysalem avatar Dec 05 '20 04:12 moodysalem