From fed590c504a19c992dc0f18001da271b87a43847 Mon Sep 17 00:00:00 2001 From: Azat Serikov Date: Fri, 1 Sep 2023 19:14:46 +0600 Subject: [PATCH 1/4] feat: prepend _ to directory name with leading digit --- packages/typechain/src/codegen/createBarrelFiles.ts | 5 +++-- packages/typechain/src/codegen/normalizeDirName.ts | 13 +++++++++++++ .../typechain/test/codegen/normalizeDirName.test.ts | 13 +++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 packages/typechain/src/codegen/normalizeDirName.ts create mode 100644 packages/typechain/test/codegen/normalizeDirName.test.ts diff --git a/packages/typechain/src/codegen/createBarrelFiles.ts b/packages/typechain/src/codegen/createBarrelFiles.ts index 2c4b0f85b..02eb5d73a 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..51099a038 --- /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..9856bfc53 --- /dev/null +++ b/packages/typechain/test/codegen/normalizeDirName.test.ts @@ -0,0 +1,13 @@ +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') + }) +}) From dda95e4466e8d0844ec38744ec9a0c5a3023cbec Mon Sep 17 00:00:00 2001 From: Azat Serikov Date: Mon, 4 Sep 2023 13:41:11 +0600 Subject: [PATCH 2/4] fix: update comment --- packages/typechain/src/codegen/normalizeDirName.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/typechain/src/codegen/normalizeDirName.ts b/packages/typechain/src/codegen/normalizeDirName.ts index 51099a038..e61fdb7e7 100644 --- a/packages/typechain/src/codegen/normalizeDirName.ts +++ b/packages/typechain/src/codegen/normalizeDirName.ts @@ -1,7 +1,7 @@ import { camelCase } from 'lodash' /** - * Converts valid directory name to valid variable name. Example: 0directory-name becomes _0directoryName + * Converts valid directory name to valid variable name. Example: 0directory-name becomes _0DirectoryName */ export function normalizeDirName(rawName: string): string { const transformations: ((s: string) => string)[] = [ From 33ab0f08f6ef128c1ff86042bee1bb3c7bb7be1a Mon Sep 17 00:00:00 2001 From: Azat Serikov Date: Mon, 4 Sep 2023 13:45:46 +0600 Subject: [PATCH 3/4] fix: import formatting --- packages/typechain/test/codegen/normalizeDirName.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/typechain/test/codegen/normalizeDirName.test.ts b/packages/typechain/test/codegen/normalizeDirName.test.ts index 9856bfc53..713b5f856 100644 --- a/packages/typechain/test/codegen/normalizeDirName.test.ts +++ b/packages/typechain/test/codegen/normalizeDirName.test.ts @@ -1,4 +1,5 @@ import { expect } from 'earljs' + import {normalizeDirName} from "../../src/codegen/normalizeDirName"; describe('dir name normalizer', () => { From 1f52e5907885433dcd484a2b43b28ea8b82633d5 Mon Sep 17 00:00:00 2001 From: Krzysztof Kaczor Date: Fri, 6 Oct 2023 16:38:32 +0200 Subject: [PATCH 4/4] Fix formatting and add changeset --- .changeset/cold-baboons-pull.md | 5 +++++ .../typechain/src/codegen/createBarrelFiles.ts | 2 +- .../typechain/src/codegen/normalizeDirName.ts | 10 +++++----- .../test/codegen/normalizeDirName.test.ts | 18 +++++++++--------- 4 files changed, 20 insertions(+), 15 deletions(-) create mode 100644 .changeset/cold-baboons-pull.md 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 02eb5d73a..f1e764db6 100644 --- a/packages/typechain/src/codegen/createBarrelFiles.ts +++ b/packages/typechain/src/codegen/createBarrelFiles.ts @@ -3,7 +3,7 @@ import { posix } from 'path' import { normalizeName } from '../parser/normalizeName' import { FileDescription } from '../typechain/types' -import {normalizeDirName} from "./normalizeDirName"; +import { normalizeDirName } from './normalizeDirName' /** * returns barrel files with reexports for all given paths diff --git a/packages/typechain/src/codegen/normalizeDirName.ts b/packages/typechain/src/codegen/normalizeDirName.ts index e61fdb7e7..d83e03963 100644 --- a/packages/typechain/src/codegen/normalizeDirName.ts +++ b/packages/typechain/src/codegen/normalizeDirName.ts @@ -4,10 +4,10 @@ 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 - ] + 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) + 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 index 713b5f856..033895065 100644 --- a/packages/typechain/test/codegen/normalizeDirName.test.ts +++ b/packages/typechain/test/codegen/normalizeDirName.test.ts @@ -1,14 +1,14 @@ import { expect } from 'earljs' -import {normalizeDirName} from "../../src/codegen/normalizeDirName"; +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') - }) + 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') + }) })