Skip to content

Commit

Permalink
fix: Fix incorrect gobal export alias in buildFlatBarrel
Browse files Browse the repository at this point in the history
This fixes a bug where an invalid export alias was generated when filenames
had segments that started with a number preceded by a dash or ".".

Examples using dashes and dots:
```typescript
export { default as testFile-1 } from "./directory1/test-file-1";
```
and
```typescript
export { default as testFile.1 } from "./directory1/test.file.1";
```

While underscores are allowed in aliases, dashes and dots is not.

Fix is to extend the regex to also include numbers.
  • Loading branch information
Christian Probst committed Sep 6, 2023
1 parent 9917083 commit 3a120b5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/builders/flat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Sinon from 'sinon';
import * as TestUtilities from '../testUtilities';
import * as Flat from './flat';
import { Signale } from 'signale';
import { Directory } from '../interfaces/directory.interface';

describe('builder/flat module has a', () => {
describe('buildFlatBarrel function that', () => {
Expand Down Expand Up @@ -196,6 +197,39 @@ export * from "./directory3/program";
`
);
});

it('should produce the correct output when a part of the filename starts with a number', () => {
const directory: Directory = {
directories: [],
files: [
{
name: 'file-with-number-1.ts',
path: 'directory1/file-with-number-1.ts',
},
],
name: 'directory1',
path: './directory1',
};

const output = Flat.buildFlatBarrel(
directory,
TestUtilities.mockModules(directory),
'"',
';',
signale,
undefined,
true,
false
);

TestUtilities.assertMultiLine(
output,
`export { default as fileWithNumber1 } from "./file-with-number-1";
export * from "./file-with-number-1";
`
);
});

it('should produce output compatible with the recommended tslint ruleset', () => {
TestUtilities.tslint(output, '"');
});
Expand Down
2 changes: 1 addition & 1 deletion src/builders/flat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { FileTreeLocation } from '../interfaces/location.interface';

function dotOrDashStrToCamelCase(str: string): string {
// massage any `example.file.name` to `exampleFileName`
return str.replace(/[-_.]([a-z])/g, (_, group) => group.toUpperCase());
return str.replace(/[-_.]([a-z0-9])/g, (_, group) => group.toUpperCase());
}

function arrayToCamelCase(arr: string[]) {
Expand Down

0 comments on commit 3a120b5

Please sign in to comment.