Skip to content

Commit

Permalink
Normalize dirname (#877)
Browse files Browse the repository at this point in the history
Co-authored-by: Azat Serikov <[email protected]>
  • Loading branch information
krzkaczor and mymphe authored Oct 7, 2023
1 parent c8ac1aa commit 3469800
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-baboons-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'typechain': patch
---

Escape dirs starting with digits
5 changes: 3 additions & 2 deletions packages/typechain/src/codegen/createBarrelFiles.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -51,7 +52,7 @@ export function createBarrelFiles(

const namespacesExports = nestedDirs
.map((p) => {
const namespaceIdentifier = camelCase(p)
const namespaceIdentifier = normalizeDirName(p)

if (typeOnly)
return [
Expand Down
13 changes: 13 additions & 0 deletions packages/typechain/src/codegen/normalizeDirName.ts
Original file line number Diff line number Diff line change
@@ -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)
}
14 changes: 14 additions & 0 deletions packages/typechain/test/codegen/normalizeDirName.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})

0 comments on commit 3469800

Please sign in to comment.