@@ -83,27 +83,47 @@ const getChange = (
83
83
} ;
84
84
}
85
85
86
- // we want to correctly remove 'default' when its a default export so we get the syntaxList node instead of the exportKeyword node
87
- // note: the first syntaxList node should contain the export keyword
86
+ /**
87
+ * The syntax list contains the keywords that can be found before the actual declaration.
88
+ * We want to remove everything that is not a decorator.
89
+ */
88
90
const syntaxListIndex = node
89
91
. getChildren ( )
90
92
. findIndex ( ( n ) => n . kind === ts . SyntaxKind . SyntaxList ) ;
91
93
92
94
const syntaxList = node . getChildren ( ) [ syntaxListIndex ] ;
95
+
96
+ if ( ! syntaxList ) {
97
+ throw new Error ( 'syntaxList missing' ) ;
98
+ }
99
+
100
+ const firstKeywordToDeleteIndex = syntaxList
101
+ . getChildren ( )
102
+ . findIndex ( ( n ) => n . kind !== ts . SyntaxKind . Decorator ) ;
103
+
104
+ const firstKeywordToDelete =
105
+ syntaxList . getChildren ( ) [ firstKeywordToDeleteIndex ] ;
106
+
107
+ if ( ! firstKeywordToDelete ) {
108
+ throw new Error (
109
+ 'Unexpected syntax list when looking for keywords after decorators' ,
110
+ ) ;
111
+ }
112
+
93
113
const nextSibling = node . getChildren ( ) [ syntaxListIndex + 1 ] ;
94
114
95
- if ( ! syntaxList || ! nextSibling ) {
96
- throw new Error ( 'Unexpected syntax' ) ;
115
+ if ( ! nextSibling ) {
116
+ throw new Error ( 'No sibling after syntax list ' ) ;
97
117
}
98
118
99
119
return {
100
120
code : node
101
121
. getSourceFile ( )
102
122
. getFullText ( )
103
- . slice ( syntaxList . getStart ( ) , nextSibling . getStart ( ) ) ,
123
+ . slice ( firstKeywordToDelete . getStart ( ) , nextSibling . getStart ( ) ) ,
104
124
span : {
105
- start : syntaxList . getStart ( ) ,
106
- length : nextSibling . getStart ( ) - syntaxList . getStart ( ) ,
125
+ start : firstKeywordToDelete . getStart ( ) ,
126
+ length : nextSibling . getStart ( ) - firstKeywordToDelete . getStart ( ) ,
107
127
} ,
108
128
} ;
109
129
} ;
0 commit comments