@@ -4,34 +4,48 @@ export default {
44 fixable : "code" ,
55 schema : [ ] ,
66 messages : {
7- default : "Always use `T | null` for optional property" ,
7+ optionalWithoutNull : "Always use `T | null` for optional property" ,
8+ nullWithoutOptional : "Mark nullable parameter as optional" ,
89 } ,
910 } ,
1011
1112 create ( context ) {
1213 return {
1314 TSPropertySignature ( node ) {
14- if ( ! node . optional ) {
15- return ;
16- }
17-
18- if ( node . typeAnnotation . type !== "TSTypeAnnotation" ) {
19- return ;
20- }
15+ if ( node . optional ) {
16+ if ( node . typeAnnotation . type !== "TSTypeAnnotation" ) {
17+ return ;
18+ }
2119
22- if (
23- node . typeAnnotation . typeAnnotation . type !== "TSUnionType" ||
24- node . typeAnnotation . typeAnnotation . types . every (
25- ( type ) => type . type !== "TSNullKeyword" ,
26- )
27- ) {
28- context . report ( {
29- node,
30- messageId : "default" ,
31- fix : function * ( fixer ) {
32- yield fixer . insertTextAfter ( node . typeAnnotation , " | null" ) ;
33- } ,
34- } ) ;
20+ if (
21+ node . typeAnnotation . typeAnnotation . type !== "TSUnionType" ||
22+ node . typeAnnotation . typeAnnotation . types . every (
23+ ( type ) => type . type !== "TSNullKeyword" ,
24+ )
25+ ) {
26+ context . report ( {
27+ node,
28+ messageId : "optionalWithoutNull" ,
29+ fix : function * ( fixer ) {
30+ yield fixer . insertTextAfter ( node . typeAnnotation , " | null" ) ;
31+ } ,
32+ } ) ;
33+ }
34+ } else {
35+ if (
36+ node . typeAnnotation . typeAnnotation . type === "TSUnionType" &&
37+ node . typeAnnotation . typeAnnotation . types . some (
38+ ( type ) => type . type === "TSNullKeyword" ,
39+ )
40+ ) {
41+ context . report ( {
42+ node,
43+ messageId : "nullWithoutOptional" ,
44+ fix : function * ( fixer ) {
45+ yield fixer . insertTextAfter ( node . key , "?" ) ;
46+ } ,
47+ } ) ;
48+ }
3549 }
3650 } ,
3751 } ;
0 commit comments