diff --git a/.changeset/cold-baboons-pull.md b/.changeset/cold-baboons-pull.md new file mode 100644 index 000000000..107bcc310 --- /dev/null +++ b/.changeset/cold-baboons-pull.md @@ -0,0 +1,5 @@ +--- +'typechain': patch +--- + +Escape dirs starting with digits diff --git a/packages/typechain/src/codegen/createBarrelFiles.ts b/packages/typechain/src/codegen/createBarrelFiles.ts index 2c4b0f85b..f1e764db6 100644 --- a/packages/typechain/src/codegen/createBarrelFiles.ts +++ b/packages/typechain/src/codegen/createBarrelFiles.ts @@ -1,8 +1,9 @@ -import { camelCase, groupBy, mapValues, uniq } from 'lodash' +import { groupBy, mapValues, uniq } from 'lodash' import { posix } from 'path' import { normalizeName } from '../parser/normalizeName' import { FileDescription } from '../typechain/types' +import { normalizeDirName } from './normalizeDirName' /** * returns barrel files with reexports for all given paths @@ -51,7 +52,7 @@ export function createBarrelFiles( const namespacesExports = nestedDirs .map((p) => { - const namespaceIdentifier = camelCase(p) + const namespaceIdentifier = normalizeDirName(p) if (typeOnly) return [ diff --git a/packages/typechain/src/codegen/normalizeDirName.ts b/packages/typechain/src/codegen/normalizeDirName.ts new file mode 100644 index 000000000..d83e03963 --- /dev/null +++ b/packages/typechain/src/codegen/normalizeDirName.ts @@ -0,0 +1,13 @@ +import { camelCase } from 'lodash' + +/** + * Converts valid directory name to valid variable name. Example: 0directory-name becomes _0DirectoryName + */ +export function normalizeDirName(rawName: string): string { + const transformations: ((s: string) => string)[] = [ + (s) => camelCase(s), // convert to camelCase + (s) => s.replace(/^\d/g, (match) => '_' + match), // prepend '_' if contains a leading number + ] + + return transformations.reduce((s, t) => t(s), rawName) +} diff --git a/packages/typechain/test/codegen/normalizeDirName.test.ts b/packages/typechain/test/codegen/normalizeDirName.test.ts new file mode 100644 index 000000000..033895065 --- /dev/null +++ b/packages/typechain/test/codegen/normalizeDirName.test.ts @@ -0,0 +1,14 @@ +import { expect } from 'earljs' + +import { normalizeDirName } from '../../src/codegen/normalizeDirName' + +describe('dir name normalizer', () => { + it('should work', () => { + expect(normalizeDirName('dirname')).toEqual('dirname') + expect(normalizeDirName('dir_name')).toEqual('dirName') + expect(normalizeDirName('0.4.24')).toEqual('_0424') + expect(normalizeDirName('0-4-24')).toEqual('_0424') + expect(normalizeDirName('0424')).toEqual('_0424') + expect(normalizeDirName('0dir-name.1')).toEqual('_0DirName1') + }) +})