1
1
import { Else , ElseIf , If , SwitchIf } from '@directives' ;
2
2
import { LogicErrors } from '@fixtures' ;
3
3
4
- import { isValidElement , ReactNode } from 'react' ;
4
+ import { ReactNode } from 'react' ;
5
5
6
6
type ValidatorFn = ( children : Array < Exclude < ReactNode , boolean | null | undefined > > ) => number [ ] ;
7
7
@@ -14,11 +14,11 @@ const validateSwitchIfChildren: ValidatorFn = (children) => {
14
14
}
15
15
16
16
children . forEach ( ( child ) => {
17
- // @ts -expect-error type.name exists on the child
18
- const typeName = isValidElement ( child ) ? child . type . name : 'unknown' ;
17
+ // @ts -expect-error child.type exist
18
+ const { displayName } = child . type ?? { } ;
19
19
20
20
// If, ElseIf, Else cannot be direct children of If, ElseIf, Else
21
- if ( typeName === If . name || typeName === ElseIf . name || typeName === Else . name ) {
21
+ if ( displayName === If . displayName || displayName === ElseIf . displayName || displayName === Else . displayName ) {
22
22
errors . push ( LogicErrors . SwitchBlockExpected ) ;
23
23
}
24
24
} ) ;
@@ -28,27 +28,27 @@ const validateSwitchIfChildren: ValidatorFn = (children) => {
28
28
29
29
const validateSwitchIf : ValidatorFn = ( children ) => {
30
30
const errors : LogicErrors [ ] = [ ] ;
31
- const elements : Record < string , number > = { } ;
31
+ const elementLookup : Record < string , number > = { } ;
32
32
33
33
if ( children . length === 0 ) {
34
34
errors . push ( LogicErrors . ChildrenExpected , LogicErrors . IfBlockExpected ) ;
35
35
return errors ;
36
36
}
37
37
38
38
children . forEach ( ( child , index ) => {
39
- // @ts -expect-error type.name exists on the child
40
- const typeName = isValidElement ( child ) ? child . type . name : 'unknown' ;
39
+ // @ts -expect-error child.type exist
40
+ const { displayName = 'unknown' } = child . type ;
41
41
42
- const count = elements [ typeName ] ?? 0 ;
43
- elements [ typeName ] = count + 1 ;
42
+ const count = elementLookup [ displayName ] ?? 0 ;
43
+ elementLookup [ displayName ] = count + 1 ;
44
44
45
- validateIfBlock ( typeName , index , elements , errors ) ;
46
- validateElseBlock ( typeName , index , children . length , elements , errors ) ;
47
- validateElseIfBlock ( typeName , index , errors ) ;
48
- validateSwitchIfInvalidElement ( typeName , errors ) ;
45
+ validateIfBlock ( displayName , index , elementLookup , errors ) ;
46
+ validateElseBlock ( displayName , index , children . length , elementLookup , errors ) ;
47
+ validateElseIfBlock ( displayName , index , errors ) ;
48
+ validateSwitchIfInvalidElement ( displayName , errors ) ;
49
49
} ) ;
50
50
51
- if ( ! elements [ If . name ] ) {
51
+ if ( ! elementLookup [ If . name ] ) {
52
52
errors . push ( LogicErrors . IfBlockExpected ) ;
53
53
}
54
54
@@ -100,12 +100,12 @@ export class ValidationFactory {
100
100
static get ( validator : string ) {
101
101
let validatorFn : ValidatorFn ;
102
102
switch ( validator ) {
103
- case SwitchIf . name :
103
+ case SwitchIf . displayName :
104
104
validatorFn = validateSwitchIf ;
105
105
break ;
106
- case If . name :
107
- case ElseIf . name :
108
- case Else . name :
106
+ case If . displayName :
107
+ case ElseIf . displayName :
108
+ case Else . displayName :
109
109
validatorFn = validateSwitchIfChildren ;
110
110
break ;
111
111
default :
0 commit comments