File tree Expand file tree Collapse file tree 4 files changed +125
-0
lines changed Expand file tree Collapse file tree 4 files changed +125
-0
lines changed Original file line number Diff line number Diff line change @@ -216,6 +216,31 @@ const create = context => {
216216 }
217217 } ) ;
218218
219+ // `foo ?? undefined`
220+ context . on ( 'Identifier' , node => {
221+ if (
222+ isUndefined ( node )
223+ && node . parent . type === 'LogicalExpression'
224+ && node . parent . operator === '??'
225+ && node . parent . right === node
226+ ) {
227+ return getProblem (
228+ node ,
229+ fixer => {
230+ const logicalExpression = node . parent ;
231+ const operatorToken = sourceCode . getTokenBefore ( node , token => token . value === logicalExpression . operator ) ;
232+ const tokenBeforeOperatorToken = sourceCode . getTokenBefore ( operatorToken ) ;
233+
234+ return fixer . removeRange ( [
235+ sourceCode . getRange ( tokenBeforeOperatorToken ) [ 1 ] ,
236+ sourceCode . getRange ( logicalExpression ) [ 1 ] ,
237+ ] ) ;
238+ } ,
239+ /* CheckFunctionReturnType */ false ,
240+ ) ;
241+ }
242+ } ) ;
243+
219244 if ( ! options . checkArguments ) {
220245 return ;
221246 }
Original file line number Diff line number Diff line change @@ -497,3 +497,19 @@ test.snapshot({
497497 } ,
498498 ] ,
499499} ) ;
500+
501+ // `foo ?? undefined`
502+ test . snapshot ( {
503+ valid : [
504+ 'foo || undefined' ,
505+ 'foo && undefined' ,
506+ 'foo ?? null' ,
507+ 'undefined ?? foo' ,
508+ ] ,
509+ invalid : [
510+ 'foo ?? undefined' ,
511+ '(( (( foo )) ?? (( undefined )) ))' ,
512+ 'foo ?? null ?? undefined' ,
513+ 'call(foo ?? undefined,)' ,
514+ ] ,
515+ } ) ;
Original file line number Diff line number Diff line change @@ -790,3 +790,87 @@ Generated by [AVA](https://avajs.dev).
790790 > 1 | function a({foo} = undefined) {}␊
791791 | ^^^^^^^^^ Do not use useless \`undefined\`.␊
792792 `
793+
794+ ## invalid(1): foo ?? undefined
795+
796+ > Input
797+
798+ `␊
799+ 1 | foo ?? undefined␊
800+ `
801+
802+ > Output
803+
804+ `␊
805+ 1 | foo␊
806+ `
807+
808+ > Error 1/1
809+
810+ `␊
811+ > 1 | foo ?? undefined␊
812+ | ^^^^^^^^^ Do not use useless \`undefined\`.␊
813+ `
814+
815+ ## invalid(2): (( (( foo )) ?? (( undefined )) ))
816+
817+ > Input
818+
819+ `␊
820+ 1 | (( (( foo )) ?? (( undefined )) ))␊
821+ `
822+
823+ > Output
824+
825+ `␊
826+ 1 | (( (( foo )) ))␊
827+ `
828+
829+ > Error 1/1
830+
831+ `␊
832+ > 1 | (( (( foo )) ?? (( undefined )) ))␊
833+ | ^^^^^^^^^ Do not use useless \`undefined\`.␊
834+ `
835+
836+ ## invalid(3): foo ?? null ?? undefined
837+
838+ > Input
839+
840+ `␊
841+ 1 | foo ?? null ?? undefined␊
842+ `
843+
844+ > Output
845+
846+ `␊
847+ 1 | foo ?? null␊
848+ `
849+
850+ > Error 1/1
851+
852+ `␊
853+ > 1 | foo ?? null ?? undefined␊
854+ | ^^^^^^^^^ Do not use useless \`undefined\`.␊
855+ `
856+
857+ ## invalid(4): call(foo ?? undefined,)
858+
859+ > Input
860+
861+ `␊
862+ 1 | call(foo ?? undefined,)␊
863+ `
864+
865+ > Output
866+
867+ `␊
868+ 1 | call(foo,)␊
869+ `
870+
871+ > Error 1/1
872+
873+ `␊
874+ > 1 | call(foo ?? undefined,)␊
875+ | ^^^^^^^^^ Do not use useless \`undefined\`.␊
876+ `
You can’t perform that action at this time.
0 commit comments