Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kk/naming fix #293

Merged
merged 7 commits into from
Nov 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

import { BigNumber } from "bignumber.js";

export interface Name12ManglingContract
extends Truffle.Contract<Name12ManglingInstance> {
"new"(meta?: Truffle.TransactionDetails): Promise<Name12ManglingInstance>;
export interface NAME12manglingContract
extends Truffle.Contract<NAME12manglingInstance> {
"new"(meta?: Truffle.TransactionDetails): Promise<NAME12manglingInstance>;
}

type AllEvents = never;

export interface Name12ManglingInstance extends Truffle.ContractInstance {
export interface NAME12manglingInstance extends Truffle.ContractInstance {
works(txDetails?: Truffle.TransactionDetails): Promise<boolean>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { DataTypesInputContract } from "./DataTypesInput";
import { DataTypesPureContract } from "./DataTypesPure";
import { DataTypesViewContract } from "./DataTypesView";
import { EventsContract } from "./Events";
import { Name12ManglingContract } from "./Name12Mangling";
import { NAME12manglingContract } from "./NAME12mangling";
import { OverloadsContract } from "./Overloads";
import { PayableContract } from "./Payable";

Expand All @@ -17,7 +17,7 @@ declare global {
require(name: "DataTypesPure"): DataTypesPureContract;
require(name: "DataTypesView"): DataTypesViewContract;
require(name: "Events"): EventsContract;
require(name: "NAME12mangling"): Name12ManglingContract;
require(name: "NAME12mangling"): NAME12manglingContract;
require(name: "Overloads"): OverloadsContract;
require(name: "Payable"): PayableContract;
}
Expand All @@ -32,8 +32,8 @@ export { DataTypesPureContract, DataTypesPureInstance } from "./DataTypesPure";
export { DataTypesViewContract, DataTypesViewInstance } from "./DataTypesView";
export { EventsContract, EventsInstance } from "./Events";
export {
Name12ManglingContract,
Name12ManglingInstance,
} from "./Name12Mangling";
NAME12manglingContract,
NAME12manglingInstance,
} from "./NAME12mangling";
export { OverloadsContract, OverloadsInstance } from "./Overloads";
export { PayableContract, PayableInstance } from "./Payable";
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import BN from "bn.js";
import { EventData, PastEventOptions } from "web3-eth-contract";

export interface Name12ManglingContract
extends Truffle.Contract<Name12ManglingInstance> {
"new"(meta?: Truffle.TransactionDetails): Promise<Name12ManglingInstance>;
export interface NAME12manglingContract
extends Truffle.Contract<NAME12manglingInstance> {
"new"(meta?: Truffle.TransactionDetails): Promise<NAME12manglingInstance>;
}

type AllEvents = never;

export interface Name12ManglingInstance extends Truffle.ContractInstance {
export interface NAME12manglingInstance extends Truffle.ContractInstance {
works(txDetails?: Truffle.TransactionDetails): Promise<boolean>;

methods: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { DataTypesViewContract } from "./DataTypesView";
import { EventsContract } from "./Events";
import { LibContract } from "./Lib";
import { LibraryConsumerContract } from "./LibraryConsumer";
import { Name12ManglingContract } from "./Name12Mangling";
import { NAME12manglingContract } from "./NAME12mangling";
import { OverloadsContract } from "./Overloads";
import { PayableContract } from "./Payable";

Expand All @@ -21,7 +21,7 @@ declare global {
require(name: "Events"): EventsContract;
require(name: "Lib"): LibContract;
require(name: "LibraryConsumer"): LibraryConsumerContract;
require(name: "NAME12mangling"): Name12ManglingContract;
require(name: "NAME12mangling"): NAME12manglingContract;
require(name: "Overloads"): OverloadsContract;
require(name: "Payable"): PayableContract;
}
Expand All @@ -41,8 +41,8 @@ export {
LibraryConsumerInstance,
} from "./LibraryConsumer";
export {
Name12ManglingContract,
Name12ManglingInstance,
} from "./Name12Mangling";
NAME12manglingContract,
NAME12manglingInstance,
} from "./NAME12mangling";
export { OverloadsContract, OverloadsInstance } from "./Overloads";
export { PayableContract, PayableInstance } from "./Payable";
20 changes: 14 additions & 6 deletions packages/typechain/src/parser/normalizeName.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import { upperFirst, camelCase } from 'lodash'
import { upperFirst } from 'lodash'

/**
* Converts valid file names to valid javascript symbols and does best effort to make them readable. Example: ds-token.test becomes DsTokenTest
*/
export function normalizeName(rawName: string): string {
const t1 = rawName.split(' ').join('-') // spaces to - so later we can automatically convert them
const t2 = t1.replace(/^\d+/, '') // removes leading digits
const result = upperFirst(camelCase(t2))
const transformations: ((s: string) => string)[] = [
(s) => s.replace(/\s+/g, '-'), // spaces to - so later we can automatically convert them
(s) => s.replace(/\./g, '-'), // replace "."
(s) => s.replace(/_/g, '-'), // replace "_"
(s) => s.replace(/-[a-z]/g, (match) => match.substr(-1).toUpperCase()), // delete '-' and capitalize the letter after them
(s) => s.replace(/-/g, ''), // delete any '-' left
(s) => s.replace(/^\d+/, ''), // removes leading digits
(s) => upperFirst(s),
]

if (result === '') {
const finalName = transformations.reduce((s, t) => t(s), rawName)

if (finalName === '') {
throw new Error(`Can't guess class name, please rename file: ${rawName}`)
}

return result
return finalName
}
2 changes: 1 addition & 1 deletion packages/typechain/src/parser/parseEvmType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function parseEvmType(rawType: string, components?: EvmSymbol[], internal
}

if (internalType?.startsWith('enum')) {
return parseEvmType('uint8')
return parseEvmType('uint8') // this is a best effort approach. Sometimes enums can be uint16 too. Read more: https://github.com/ethereum-ts/TypeChain/pull/281#discussion_r513303099
}

throw new Error('Unknown type: ' + rawType)
Expand Down
1 change: 1 addition & 0 deletions packages/typechain/test/parser/normalizeName.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ describe('name normalizer', () => {
expect(normalizeName('ds token')).to.be.eq('DsToken')
expect(normalizeName('name.abi')).to.be.eq('NameAbi')
expect(normalizeName('1234name.abi')).to.be.eq('NameAbi')
expect(normalizeName('ERC20.abi')).to.be.eq('ERC20Abi')
})
})