-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
235 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import {inject, INJECTOR} from '@angular/core'; | ||
import type {TuiDialogContext, TuiDialogOptions} from '@taiga-ui/core/components/dialog'; | ||
import {TuiDialogService} from '@taiga-ui/core/components/dialog'; | ||
import {PolymorpheusComponent} from '@taiga-ui/polymorpheus'; | ||
import type {Observable} from 'rxjs'; | ||
|
||
type ReplaceNever<T> = [T] extends [never] ? void : T; | ||
|
||
type ReplaceAny<T> = 0 extends T & 1 ? void : T; | ||
|
||
type ExtractDialogData<T> = ReplaceNever< | ||
{ | ||
[K in keyof T]: ReplaceAny<T[K]> extends TuiDialogContext<any, infer D> | ||
? D | ||
: never; | ||
}[keyof T] | ||
>; | ||
|
||
type ExtractDialogResult<T> = ReplaceNever< | ||
{ | ||
[K in keyof T]: ReplaceAny<T[K]> extends TuiDialogContext<infer R, any> | ||
? R | ||
: never; | ||
}[keyof T] | ||
>; | ||
|
||
export function tuiDialog< | ||
T, | ||
D extends ExtractDialogData<T>, | ||
R extends ExtractDialogResult<T>, | ||
>( | ||
component: new () => T, | ||
options?: Partial<Omit<TuiDialogOptions<D>, 'data'>>, | ||
): (data: D) => Observable<R> { | ||
const dialogService = inject(TuiDialogService); | ||
const injector = inject(INJECTOR); | ||
|
||
return (data) => | ||
dialogService.open(new PolymorpheusComponent(component, injector), { | ||
...options, | ||
data, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
projects/core/components/dialog/test/dialog.factory.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import * as fs from 'node:fs'; | ||
import * as path from 'node:path'; | ||
|
||
import {TestBed} from '@angular/core/testing'; | ||
import {TuiDialogService} from '@taiga-ui/core'; | ||
import ts from 'typescript'; | ||
|
||
import tsconfig from '../../../../../tsconfig.json'; | ||
|
||
describe('tuiDialog', () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [ | ||
{ | ||
provide: TuiDialogService, | ||
useValue: { | ||
open: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
describe('typing tests', () => { | ||
let checker: ts.TypeChecker; | ||
let sourceFile: ts.SourceFile; | ||
|
||
function findVariable(variableName: string): ts.VariableDeclaration | undefined { | ||
function findVariableCb(node: ts.Node): ts.VariableDeclaration | undefined { | ||
if ( | ||
ts.isVariableDeclaration(node) && | ||
ts.isIdentifier(node.name) && | ||
node.name.text === variableName | ||
) { | ||
return node; | ||
} | ||
|
||
return ts.forEachChild(node, findVariableCb); | ||
} | ||
|
||
return findVariableCb(sourceFile); | ||
} | ||
|
||
function getVariableType(variableName: string): string { | ||
const variableNode = findVariable(variableName); | ||
|
||
if (!variableNode) { | ||
throw new Error(`Cannot find variable: ${variableName}`); | ||
} | ||
|
||
// Получаем тип переменной через TypeChecker | ||
const type = checker.getTypeAtLocation(variableNode); | ||
|
||
return checker.typeToString(type); | ||
} | ||
|
||
beforeAll(() => { | ||
// language=TypeScript | ||
const sourceCode = `; | ||
import {tuiDialog} from "../dialog.factory"; | ||
import {TuiDialogContext} from "../dialog.interfaces"; | ||
class Test1Component { | ||
} | ||
const dialog1 = tuiDialog(Test1Component); | ||
class Test2Component { | ||
context!: TuiDialogContext<string, number>; | ||
} | ||
const dialog2 = tuiDialog(Test2Component); | ||
class Test3Component { | ||
context!: TuiDialogContext<string, number>; | ||
otherMember: string; | ||
} | ||
const dialog3 = tuiDialog(Test3Component); | ||
class Test4Component { | ||
context!: TuiDialogContext<string, number>; | ||
otherMember: any; | ||
} | ||
const dialog4 = tuiDialog(Test4Component); | ||
`; | ||
|
||
const sourceFilePath = path.join(__dirname, 'input.ts'); | ||
const options: ts.CompilerOptions = { | ||
noEmit: true, | ||
target: ts.ScriptTarget.ESNext, | ||
module: ts.ModuleKind.ES2020, | ||
lib: ['lib.esnext.d.ts', 'lib.dom.d.ts'], | ||
typeRoots: ['node_modules/@types'], | ||
types: ['node'], | ||
moduleResolution: ts.ModuleResolutionKind.Node10, | ||
paths: tsconfig.compilerOptions.paths, | ||
baseUrl: './', | ||
strict: true, | ||
}; | ||
const host = ts.createCompilerHost(options); | ||
const program = ts.createProgram([sourceFilePath], options, { | ||
...host, | ||
getSourceFile: (filePath, languageVersion) => { | ||
if (filePath === sourceFilePath) { | ||
return ts.createSourceFile( | ||
filePath, | ||
sourceCode, | ||
languageVersion, | ||
true, | ||
); | ||
} | ||
|
||
const fileContent = fs.readFileSync(filePath, 'utf8'); | ||
|
||
return ts.createSourceFile( | ||
filePath, | ||
fileContent, | ||
languageVersion, | ||
true, | ||
); | ||
}, | ||
getCurrentDirectory: () => process.cwd(), | ||
}); | ||
|
||
checker = program.getTypeChecker(); | ||
sourceFile = program.getSourceFile(sourceFilePath)!; | ||
}); | ||
|
||
it('should have void data and void result for empty component', () => { | ||
expect(getVariableType('dialog1')).toBe('(data: void) => Observable<void>'); | ||
}); | ||
|
||
it('should have number data and string result for component with TuiDialogContext<string, number>', () => { | ||
expect(getVariableType('dialog2')).toBe( | ||
'(data: number) => Observable<string>', | ||
); | ||
}); | ||
|
||
it('should have number data and string result for component with TuiDialogContext<string, number> and other member', () => { | ||
expect(getVariableType('dialog3')).toBe( | ||
'(data: number) => Observable<string>', | ||
); | ||
}); | ||
|
||
it('should have number data and string result for component with TuiDialogContext<string, number> and other member that any', () => { | ||
expect(getVariableType('dialog4')).toBe( | ||
'(data: number) => Observable<string>', | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters