1
- import { flatOne } from '@utils' ;
2
1
import ts from 'typescript' ;
3
2
4
- import type * as d from '../../../declarations' ;
5
- import { convertValueToLiteral , createStaticGetter , retrieveTsDecorators } from '../transform-utils' ;
3
+ import { convertValueToLiteral , createStaticGetter , retrieveTsDecorators , tsPropDeclName } from '../transform-utils' ;
6
4
import { getDecoratorParameters , isDecoratorNamed } from './decorator-utils' ;
7
5
8
6
export const serializeDecoratorsToStatic = (
@@ -11,37 +9,56 @@ export const serializeDecoratorsToStatic = (
11
9
newMembers : ts . ClassElement [ ] ,
12
10
decoratorName : string ,
13
11
translateType : 'PropSerialize' | 'AttrDeserialize' ,
12
+ propDecoratorName : string ,
14
13
) => {
14
+ // we only care about `@Prop` decorated properties
15
+ const props : string [ ] = decoratedProps
16
+ . filter ( ( prop ) => ts . isPropertyDeclaration ( prop ) || ts . isGetAccessor ( prop ) )
17
+ . flatMap ( ( prop ) => {
18
+ if ( retrieveTsDecorators ( prop ) ?. find ( isDecoratorNamed ( propDecoratorName ) ) ) {
19
+ const { staticName } = tsPropDeclName ( prop , typeChecker ) ;
20
+ return [ staticName ] ;
21
+ }
22
+ return [ ] ;
23
+ } ) ;
24
+
15
25
const serializers = decoratedProps
16
26
. filter ( ts . isMethodDeclaration )
17
- . map ( ( method ) => parseSerializeDecorator ( typeChecker , method , decoratorName ) ) ;
18
-
19
- const flatSerializers = flatOne ( serializers ) ;
27
+ . flatMap ( ( method ) => parseSerializeDecorator ( typeChecker , method , decoratorName , props ) ) ;
20
28
21
- if ( flatSerializers . length > 0 ) {
29
+ if ( serializers . length > 0 ) {
22
30
if ( translateType === 'PropSerialize' ) {
23
- newMembers . push ( createStaticGetter ( 'serializers' , convertValueToLiteral ( flatSerializers ) ) ) ;
31
+ newMembers . push ( createStaticGetter ( 'serializers' , convertValueToLiteral ( serializers ) ) ) ;
24
32
} else {
25
- newMembers . push ( createStaticGetter ( 'deserializers' , convertValueToLiteral ( flatSerializers ) ) ) ;
33
+ newMembers . push ( createStaticGetter ( 'deserializers' , convertValueToLiteral ( serializers ) ) ) ;
26
34
}
27
35
}
28
36
29
- return flatSerializers ;
37
+ return serializers ;
30
38
} ;
31
39
32
40
const parseSerializeDecorator = (
33
41
typeChecker : ts . TypeChecker ,
34
42
method : ts . MethodDeclaration ,
35
43
decoratorName : string ,
36
- ) : d . ComponentCompilerChangeHandler [ ] => {
44
+ props : string [ ] ,
45
+ ) => {
37
46
const methodName = method . name . getText ( ) ;
38
47
const decorators = retrieveTsDecorators ( method ) ?? [ ] ;
39
- return decorators . filter ( isDecoratorNamed ( decoratorName ) ) . map ( ( decorator ) => {
48
+
49
+ return decorators . filter ( isDecoratorNamed ( decoratorName ) ) . flatMap ( ( decorator ) => {
40
50
const [ propName ] = getDecoratorParameters < string > ( decorator , typeChecker ) ;
41
51
42
- return {
43
- propName,
44
- methodName,
45
- } ;
52
+ if ( ! props . includes ( propName ) ) {
53
+ // ignore if there's no corresponding @Prop decorated property
54
+ return [ ] ;
55
+ }
56
+
57
+ return [
58
+ {
59
+ propName,
60
+ methodName,
61
+ } ,
62
+ ] ;
46
63
} ) ;
47
64
} ;
0 commit comments