Skip to content

Commit

Permalink
Centralize File Extension Matcher
Browse files Browse the repository at this point in the history
Remove the duplicate code that the file matcher logic uses and have it at the top of the entry file instead of declared inline.
Make sure that all regular expressions are Unicode compatible and that they are case insensitive matches.
This behavior that should be caught on the Windows platform, as Windows is case insensitive and this could cause a namespace collision if not insensitive matching.
While possible to have two different files with the same name only differing on linux and MacOS, this is generally agreed upon as a bad practice.
  • Loading branch information
elliot-huffman committed Jun 5, 2024
1 parent 6e2a4a9 commit c0129a4
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import {

const GENERATED_WARNING = 'WARNING: Do not manually change this file.'

/** Matches the supported file extensions of this project. */
const fileExtensionRegex = /\.(ts|mts|cts|tsx|d\.ts)$/iu

// -- Helpers --

function reportError(message: string, ...args: unknown[]) {
Expand Down Expand Up @@ -85,7 +88,7 @@ function outFilePath(sourcePath: string, guardFileName: string): string {

/** Name of the file to output the generated type guard. */
const outPath = sourcePath.replace(
/\.(ts|mts|cts|tsx|d\.ts)$/u,
fileExtensionRegex,
`.${guardFileName}.${cjsModuleMode ? 'cts' : esmModuleMode ? 'mts' : 'ts'}`
)

Expand Down Expand Up @@ -1262,7 +1265,7 @@ export function processProject(
.getFilePath()
.split('/')
.reverse()[0]
.replace(/\.(ts|mts|cts|tsx|d\.ts)$/, '')
.replace(fileExtensionRegex, '')
const importStatement = `import * as ${options.importGuards} from "${relativeOutPath}${importExtension}";`
const exportStatement = `export { ${options.importGuards} };`
const { hasImport, hasExport, statements } = sourceFile
Expand Down

0 comments on commit c0129a4

Please sign in to comment.