Skip to content

Commit 3469800

Browse files
Normalize dirname (#877)
Co-authored-by: Azat Serikov <[email protected]>
1 parent c8ac1aa commit 3469800

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

.changeset/cold-baboons-pull.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'typechain': patch
3+
---
4+
5+
Escape dirs starting with digits

packages/typechain/src/codegen/createBarrelFiles.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { camelCase, groupBy, mapValues, uniq } from 'lodash'
1+
import { groupBy, mapValues, uniq } from 'lodash'
22
import { posix } from 'path'
33

44
import { normalizeName } from '../parser/normalizeName'
55
import { FileDescription } from '../typechain/types'
6+
import { normalizeDirName } from './normalizeDirName'
67

78
/**
89
* returns barrel files with reexports for all given paths
@@ -51,7 +52,7 @@ export function createBarrelFiles(
5152

5253
const namespacesExports = nestedDirs
5354
.map((p) => {
54-
const namespaceIdentifier = camelCase(p)
55+
const namespaceIdentifier = normalizeDirName(p)
5556

5657
if (typeOnly)
5758
return [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { camelCase } from 'lodash'
2+
3+
/**
4+
* Converts valid directory name to valid variable name. Example: 0directory-name becomes _0DirectoryName
5+
*/
6+
export function normalizeDirName(rawName: string): string {
7+
const transformations: ((s: string) => string)[] = [
8+
(s) => camelCase(s), // convert to camelCase
9+
(s) => s.replace(/^\d/g, (match) => '_' + match), // prepend '_' if contains a leading number
10+
]
11+
12+
return transformations.reduce((s, t) => t(s), rawName)
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { expect } from 'earljs'
2+
3+
import { normalizeDirName } from '../../src/codegen/normalizeDirName'
4+
5+
describe('dir name normalizer', () => {
6+
it('should work', () => {
7+
expect(normalizeDirName('dirname')).toEqual('dirname')
8+
expect(normalizeDirName('dir_name')).toEqual('dirName')
9+
expect(normalizeDirName('0.4.24')).toEqual('_0424')
10+
expect(normalizeDirName('0-4-24')).toEqual('_0424')
11+
expect(normalizeDirName('0424')).toEqual('_0424')
12+
expect(normalizeDirName('0dir-name.1')).toEqual('_0DirName1')
13+
})
14+
})

0 commit comments

Comments
 (0)