1- import * as ts from 'typescript' ;
21import * as fs from 'fs' ;
32import * as path from 'path' ;
3+ import * as ts from 'typescript' ;
44
55interface MatcherInfo {
66 name : string ;
@@ -46,11 +46,11 @@ function extractMatcherInfo(sourceFile: ts.SourceFile): MatcherInfo[] {
4646 const isOptional = param . initializer !== undefined || param . questionToken !== undefined ;
4747 // Check if parameter is a rest parameter
4848 const isRest = param . dotDotDotToken !== undefined ;
49- return {
50- name : paramName ,
51- type : paramType ,
49+ return {
50+ name : paramName ,
51+ type : paramType ,
5252 optional : isOptional ,
53- rest : isRest
53+ rest : isRest ,
5454 } ;
5555 } ) ;
5656 const returnType = node . type ? node . type . getText ( sourceFile ) : 'unknown' ;
@@ -67,43 +67,52 @@ function generateTypeDefinition(matcher: MatcherInfo): string {
6767 const lines = matcher . docComment . split ( '\n' ) . map ( line => ` * ${ line . trim ( ) } ` ) ;
6868 docBlock = [ ' /**' , ...lines , ' */' ] . join ( '\n' ) ;
6969 }
70- const params = matcher . parameters . map ( p => {
71- const prefix = p . rest ? '...' : '' ;
72- const suffix = p . optional ? '?' : '' ;
73- return `${ prefix } ${ p . name } ${ suffix } : ${ p . type } ` ;
74- } ) . join ( ', ' ) ;
75-
70+
71+ const params = matcher . parameters
72+ . map ( p => {
73+ const prefix = p . rest ? '...' : '' ;
74+ const suffix = p . optional ? '?' : '' ;
75+ return `${ prefix } ${ p . name } ${ suffix } : ${ p . type } ` ;
76+ } )
77+ . join ( ', ' ) ;
78+
7679 // Check if the function uses the E type parameter
77- const needsGenericE = matcher . parameters . some ( p => p . type . includes ( 'E' ) ) ||
78- matcher . returnType . includes ( 'E' ) ;
79-
80+ const needsGenericE = matcher . parameters . some ( p => p . type . includes ( 'E' ) ) || matcher . returnType . includes ( 'E' ) ;
81+
8082 // Add generic type parameter if needed
8183 const genericParams = needsGenericE ? '<E>' : '' ;
82-
84+
8385 // Add two newlines after each method for clarity
8486 return `\n${ docBlock } \n ${ matcher . name } ${ genericParams } (${ params } ): R;\n` ;
8587}
8688
8789function generateTypeFile ( matchers : MatcherInfo [ ] ) : string {
88- return `interface CustomMatchers<R> extends Record<string, any> { ${ matchers
89- . map ( generateTypeDefinition )
90- . join ( '' ) }
90+ return `export {};
91+
92+ interface CustomMatchers<R> extends Record<string, any> { ${ matchers . map ( generateTypeDefinition ) . join ( '' ) }
9193}
9294
93- declare namespace jest {
94- interface Matchers<R> {${ matchers
95- . map ( generateTypeDefinition )
96- . join ( '' ) }
95+ declare global {
96+ namespace jest {
97+ interface Matchers<R> extends CustomMatchers<R> {}
98+ interface Expect extends CustomMatchers<any> {}
99+ interface InverseAsymmetricMatchers extends Expect {}
97100 }
101+ }
98102
99- interface Expect extends CustomMatchers<any> {}
100- interface InverseAsymmetricMatchers extends Expect {}
103+ declare module '@jest/expect' {
104+ interface Matchers<R> extends CustomMatchers<R> {}
105+ }
106+
107+ declare module '@jest/globals' {
108+ interface Matchers<R> extends CustomMatchers<R> {}
101109}
102110
103111declare module 'jest-extended' {
104112 const matchers: CustomMatchers<any>;
105113 export = matchers;
106- }` ;
114+ }
115+ ` ;
107116}
108117
109118function main ( ) {
@@ -112,8 +121,7 @@ function main() {
112121 const outputFile = path . join ( typesDir , 'index.d.ts' ) ;
113122
114123 // Read all matcher files
115- const matcherFiles = fs . readdirSync ( matchersDir )
116- . filter ( file => file . endsWith ( '.ts' ) && file !== 'index.ts' ) ;
124+ const matcherFiles = fs . readdirSync ( matchersDir ) . filter ( file => file . endsWith ( '.ts' ) && file !== 'index.ts' ) ;
117125
118126 const matchers : MatcherInfo [ ] = [ ] ;
119127
@@ -124,7 +132,7 @@ function main() {
124132 filePath ,
125133 fs . readFileSync ( filePath , 'utf8' ) . replace ( / \r / g, '' ) ,
126134 ts . ScriptTarget . Latest ,
127- true
135+ true ,
128136 ) ;
129137
130138 const matcherInfos = extractMatcherInfo ( sourceFile ) ;
@@ -138,4 +146,4 @@ function main() {
138146 console . log ( `Generated type definitions in ${ outputFile } ` ) ;
139147}
140148
141- main ( ) ;
149+ main ( ) ;
0 commit comments