@@ -36,6 +36,9 @@ function create(context) {
3636 const constructor = clazz . body . body . find ( node => node . kind === 'constructor' && node . value . type === 'FunctionExpression' ) ??
3737 clazz . body . body . find ( node => node . kind === 'constructor' ) ;
3838 // MethodDefinition > FunctionExpression > BlockStatement > []
39+ const isReact = clazz . superClass ?. name === 'Component'
40+ || ( clazz . superClass ?. object ?. name === 'React' && clazz . superClass ?. property . name === 'Component' ) ;
41+ const unsupportedSuper = ! isReact && ! ! clazz . superClass ;
3942 const isMakeObservable = node => node . expression ?. callee ?. name === 'makeObservable' && node . expression ?. arguments [ 0 ] ?. type === 'ThisExpression' ;
4043 const makeObservable = constructor ?. value . body ?. body . find ( isMakeObservable ) ?. expression ;
4144
@@ -50,6 +53,10 @@ function create(context) {
5053 }
5154 } else {
5255 const fix = fixer => {
56+ // The class extends a another unknown class so we can not safely create a super call.
57+ if ( unsupportedSuper && ! constructor ) {
58+ return ;
59+ }
5360
5461 const fixes = [ ] ;
5562 let makeObservableExpr = 'makeObservable' ;
@@ -71,7 +78,15 @@ function create(context) {
7178 // constructor() {}
7279 const closingBracket = sourceCode . getLastToken ( constructor . value . body ) ;
7380 fixes . push ( fixer . insertTextBefore ( closingBracket , `;${ makeObservableExpr } (this);` ) ) ;
74- } else {
81+ } else if ( isReact ) {
82+ // class C extends Component<{ foo: string }> {}
83+ let propsType = '' ;
84+ if ( clazz . superTypeParameters ?. params . length ) {
85+ propsType = `: ${ sourceCode . getText ( clazz . superTypeParameters . params [ 0 ] ) } ` ;
86+ }
87+ const openingBracket = sourceCode . getFirstToken ( clazz . body ) ;
88+ fixes . push ( fixer . insertTextAfter ( openingBracket , `\nconstructor(props${ propsType } ) { super(props); ${ makeObservableExpr } (this); }` ) ) ;
89+ } else if ( ! unsupportedSuper ) {
7590 // class C {}
7691 const openingBracket = sourceCode . getFirstToken ( clazz . body ) ;
7792 fixes . push ( fixer . insertTextAfter ( openingBracket , `\nconstructor() { ${ makeObservableExpr } (this); }` ) ) ;
@@ -82,7 +97,7 @@ function create(context) {
8297
8398 context . report ( {
8499 node : clazz ,
85- messageId : 'missingMakeObservable' ,
100+ messageId : unsupportedSuper ? 'missingMakeObservableSuper' : 'missingMakeObservable' ,
86101 fix,
87102 } )
88103 }
@@ -101,6 +116,7 @@ module.exports = {
101116 } ,
102117 messages : {
103118 missingMakeObservable : "Constructor is missing `makeObservable(this)`." ,
119+ missingMakeObservableSuper : "Constructor is missing `makeObservable(this)`. Can not fix because of missing super call." ,
104120 secondArgMustBeNullish : "`makeObservable`'s second argument must be nullish or not provided when using decorators."
105121 } ,
106122 } ,
0 commit comments