diff --git a/__testfixtures__/nestedObjects.input.js b/__testfixtures__/nestedObjects.input.js index e5c99a0..6744cb0 100644 --- a/__testfixtures__/nestedObjects.input.js +++ b/__testfixtures__/nestedObjects.input.js @@ -17,3 +17,8 @@ const foo16 = foo[0] && foo[0].bar; const foo17 = a && b.c && b.c.d; const foo18 = a && b.c && b.c.d.e; this.a.b && this.b.c; +this.a.b && this.a.b.c; +this.a && this.a.b && this.a.b.c; +this && this.a; +x.a.b && this.a.b.c; +this.a.b && x.a.b.c; diff --git a/__testfixtures__/nestedObjects.output.js b/__testfixtures__/nestedObjects.output.js index ac23c6f..c010666 100644 --- a/__testfixtures__/nestedObjects.output.js +++ b/__testfixtures__/nestedObjects.output.js @@ -17,3 +17,8 @@ const foo16 = foo[0]?.bar; const foo17 = a && b.c?.d; const foo18 = a && b.c?.d.e; this.a.b && this.b.c; +this.a.b?.c; +this.a?.b?.c; +this?.a; +x.a.b && this.a.b.c; +this.a.b && x.a.b.c; diff --git a/transform.js b/transform.js index aff05a8..a342846 100644 --- a/transform.js +++ b/transform.js @@ -246,6 +246,8 @@ const mangleLodashGets = ( }; const nameEquals = (a, b) => a.name && b.name && a.name === b.name; +const areThisExpressions = (a, b) => + a.type === "ThisExpression" && b.type === "ThisExpression"; const valueEquals = (a, b) => a.value !== undefined && b.value !== undefined && a.value === b.value; @@ -257,14 +259,23 @@ const isPropertyMatch = (a, b) => { valueEquals(a.property, b.property)) ); } - return !!(a && b && (nameEquals(a, b) || valueEquals(a, b))); + return !!( + a && + b && + (nameEquals(a, b) || valueEquals(a, b) || areThisExpressions(a, b)) + ); }; const getDepth = (node, depth) => node.object ? getDepth(node.object, depth + 1) : depth; const dive = (node, compare, j) => { - if (node.object.type === "MemberExpression") { + if (node.type === "ThisExpression") { + return node; + } else if ( + node.object.type === "MemberExpression" || + node.object.type === "ThisExpression" + ) { const d1 = getDepth(node, 0); const d2 = getDepth(compare, 0); const toCompare = d1 <= d2 ? compare.object : compare;