Skip to content

Commit 0eb8085

Browse files
eintxaurtietaEndika Intxaurtieta
and
Endika Intxaurtieta
authored
fix: ensure a valid name for exports (#489)
Co-authored-by: Endika Intxaurtieta <endika@MacBook-Pro-de-Endika.local>
1 parent 16a58d6 commit 0eb8085

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

packages/cli/src/dirCommand.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import { promises as fs } from 'fs'
33
import path from 'path'
44
import chalk from 'chalk'
55
import { loadConfig } from '@svgr/core'
6-
import { convertFile, transformFilename, CASE, politeWrite } from './util'
6+
import {
7+
convertFile,
8+
transformFilename,
9+
CASE,
10+
politeWrite,
11+
formatExportName,
12+
} from './util'
713

814
async function exists(file) {
915
try {
@@ -33,7 +39,7 @@ export function isCompilable(filename) {
3339
function defaultIndexTemplate(filePaths) {
3440
const exportEntries = filePaths.map((filePath) => {
3541
const basename = path.basename(filePath, path.extname(filePath))
36-
const exportName = /^\d/.test(basename) ? `Svg${basename}` : basename
42+
const exportName = formatExportName(basename)
3743
return `export { default as ${exportName} } from './${basename}'`
3844
})
3945
return exportEntries.join('\n')

packages/cli/src/util.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,19 @@ export function politeWrite(program, data) {
5252
process.stdout.write(data)
5353
}
5454
}
55+
56+
export function formatExportName(name) {
57+
if (/[-]/g.test(name) && /^\d/.test(name)) {
58+
return `Svg${camelcase(name, { pascalCase: true })}`
59+
}
60+
61+
if (/^\d/.test(name)) {
62+
return `Svg${name}`
63+
}
64+
65+
if (/[-]/g.test(name)) {
66+
return camelcase(name, { pascalCase: true })
67+
}
68+
69+
return name
70+
}

packages/cli/src/util.test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'path'
2-
import { convertFile, transformFilename, CASE } from './util'
2+
import { convertFile, transformFilename, CASE, formatExportName } from './util'
33

44
const FIXTURES = path.join(__dirname, '../../../__fixtures__')
55

@@ -31,4 +31,12 @@ describe('util', () => {
3131
expect(transformFilename('foo_bar', CASE.PASCAL)).toBe('FooBar')
3232
})
3333
})
34+
35+
describe('#formatExportName', () => {
36+
it('should ensure a valid export name', () => {
37+
expect(formatExportName('foo-bar')).toBe('FooBar')
38+
expect(formatExportName('2foo')).toBe('Svg2foo')
39+
expect(formatExportName('2foo-bar')).toBe('Svg2FooBar')
40+
})
41+
})
3442
})

0 commit comments

Comments
 (0)