From 3a120b57c69b9c92db4c19d9ce9aa62de21bc532 Mon Sep 17 00:00:00 2001 From: Christian Probst Date: Wed, 6 Sep 2023 22:54:55 +0200 Subject: [PATCH] fix: Fix incorrect gobal export alias in buildFlatBarrel 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. --- src/builders/flat.test.ts | 34 ++++++++++++++++++++++++++++++++++ src/builders/flat.ts | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/builders/flat.test.ts b/src/builders/flat.test.ts index bc96372..f7287a8 100644 --- a/src/builders/flat.test.ts +++ b/src/builders/flat.test.ts @@ -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', () => { @@ -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, '"'); }); diff --git a/src/builders/flat.ts b/src/builders/flat.ts index ee2838b..4f7d40f 100644 --- a/src/builders/flat.ts +++ b/src/builders/flat.ts @@ -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[]) {