Skip to content

Commit

Permalink
Recombine a trivial $dotCall case
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdz committed Aug 2, 2024
1 parent 981b243 commit 4e14c74
Show file tree
Hide file tree
Showing 268 changed files with 996 additions and 1,377 deletions.
39 changes: 39 additions & 0 deletions src/normalize/normalize.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2912,6 +2912,45 @@ export function phaseNormalize(fdata, fname, { allowEval = true }) {
return true;
}

if (node.callee.type === 'Identifier' && node.callee.name === BUILTIN_FUNC_CALL_NAME) {
// This is $dotCall()
// Lowest hanging fruit: `const x = a.b; $dotCall(x, a, 1, 2); -> a.b(1, 2)
// Check previous statement. If it is a const binding to the first arg of the $dotCall
// and it is a member expression where the object equals the second $dotCall arg... simplify!

if (
i > 0 &&
body[i-1].type === 'VariableDeclaration' &&
body[i-1].kind === 'const' &&
node.arguments.length &&
node.arguments[0].type === 'Identifier' &&
body[i-1].declarations[0].id.name === node.arguments[0].name &&
// `const a = ???; $dotCall(a, b, c)` where a is defined in the previous statement. check context
body[i-1].declarations[0].init.type === 'MemberExpression' &&
!body[i-1].declarations[0].init.computed &&
body[i-1].declarations[0].init.object.type === 'Identifier' &&
node.arguments[1].type === 'Identifier' &&
body[i-1].declarations[0].init.object.name === node.arguments[1].name
// `const a = b.?; $dotCall(a, b, c)` where a is defined in the previous statement. we should be good now
) {
// (We must support method calls, anyways. This makes some things easier.)
rule('If the first $dotCall arg is found to be declared by a simple member expression then recombine it');
example('const a = b.c; $dotCall(a, b, 1, 2);', 'b.c(1, 2);')
before(body[i-1])
before(body[i]);

node.callee = body[i-1].declarations[0].init;
node.arguments.shift(); // Remove the function being called (that is the callee now)
node.arguments.shift(); // Remove the context arg
body[i-1] = AST.emptyStatement();

before(body[i-1])
before(body[i]);
assertNoDupeNodes(AST.blockStatement(body), 'body');
return true;
}
}

if (
AST.isPrimitive(node.callee) ||
node.callee.type === 'TemplateLiteral' || // Any template literal is a string and is uncallable
Expand Down
3 changes: 1 addition & 2 deletions tests/cases/dot_call/array_push.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ $(x, arr);

`````js filename=intro
const arr = [1, 2];
const tmpCallVal = arr.push;
const x = $dotCall(tmpCallVal, arr, 3);
const x = arr.push(3);
$(x, arr);
`````

Expand Down
3 changes: 1 addition & 2 deletions tests/cases/dot_call/base.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ $(x);

`````js filename=intro
const arr = [1, 2];
const tmpCallVal = arr.push;
const x = $dotCall(tmpCallVal, arr, 3);
const x = arr.push(3);
$(x);
`````

Expand Down
3 changes: 1 addition & 2 deletions tests/cases/dot_call/false_tostring.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ $(x);

`````js filename=intro
const bool = false;
const tmpCallVal = bool.toString;
const x = $dotCall(tmpCallVal, bool);
const x = bool.toString();
$(x);
`````

Expand Down
3 changes: 1 addition & 2 deletions tests/cases/dot_call/float_tostring.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ $(x);

`````js filename=intro
const flt = 3.48;
const tmpCallVal = flt.toString;
const x = $dotCall(tmpCallVal, flt);
const x = flt.toString();
$(x);
`````

Expand Down
3 changes: 1 addition & 2 deletions tests/cases/dot_call/infinity_tostring.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ $(x);

`````js filename=intro
const inf = Infinity;
const tmpCallVal = inf.toString;
const x = $dotCall(tmpCallVal, inf);
const x = inf.toString();
$(x);
`````

Expand Down
3 changes: 1 addition & 2 deletions tests/cases/dot_call/nan_tostring.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ $(x);

`````js filename=intro
const naN = NaN;
const tmpCallVal = naN.toString;
const x = $dotCall(tmpCallVal, naN);
const x = naN.toString();
$(x);
`````

Expand Down
3 changes: 1 addition & 2 deletions tests/cases/dot_call/nan_tostring_multi_arg.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ $(x);

`````js filename=intro
const naN = NaN;
const tmpCallVal = naN.toString;
const x = $dotCall(tmpCallVal, naN, 2, $, unknown);
const x = naN.toString(2, $, unknown);
$(x);
`````

Expand Down
3 changes: 1 addition & 2 deletions tests/cases/dot_call/neg_tostring.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ $(x);

`````js filename=intro
const num = -500;
const tmpCallVal = num.toString;
const x = $dotCall(tmpCallVal, num);
const x = num.toString();
$(x);
`````

Expand Down
3 changes: 1 addition & 2 deletions tests/cases/dot_call/regex_test.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ $(x);

`````js filename=intro
const rex = /foo/;
const tmpCallVal = rex.test;
const x = $dotCall(tmpCallVal, rex, `why is foo always used`);
const x = rex.test(`why is foo always used`);
$(x);
`````

Expand Down
3 changes: 1 addition & 2 deletions tests/cases/dot_call/regex_test_multi_arg.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ $(x);

`````js filename=intro
const rex = /foo/;
const tmpCallVal = rex.test;
const x = $dotCall(tmpCallVal, rex, `why is foo always used`, $, unknown);
const x = rex.test(`why is foo always used`, $, unknown);
$(x);
`````

Expand Down
3 changes: 1 addition & 2 deletions tests/cases/dot_call/regex_test_no_arg.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ $(x);

`````js filename=intro
const rex = /foo/;
const tmpCallVal = rex.test;
const x = $dotCall(tmpCallVal, rex);
const x = rex.test();
$(x);
`````

Expand Down
3 changes: 1 addition & 2 deletions tests/cases/dot_call/string_slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ $(x);

`````js filename=intro
const str = `worldy`;
const tmpCallVal = str.slice;
const x = $dotCall(tmpCallVal, str, 2, 4);
const x = str.slice(2, 4);
$(x);
`````

Expand Down
3 changes: 1 addition & 2 deletions tests/cases/dot_call/true_tostring.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ $(x);

`````js filename=intro
const bool = true;
const tmpCallVal = bool.toString;
const x = $dotCall(tmpCallVal, bool);
const x = bool.toString();
$(x);
`````

Expand Down
3 changes: 1 addition & 2 deletions tests/cases/dot_call/true_tostring_multi_arg.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ $(x);

`````js filename=intro
const bool = true;
const tmpCallVal = bool.toString;
const x = $dotCall(tmpCallVal, bool, $, unknown);
const x = bool.toString($, unknown);
$(x);
`````

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ const tmpIfTest = tmpChainRootProp != null;
if (tmpIfTest) {
const tmpChainElementObject = tmpChainRootProp.c;
const tmpChainElementObject$1 = tmpChainElementObject.d;
const tmpChainElementObject$3 = tmpChainElementObject$1.e;
const tmpChainElementCall = $dotCall(tmpChainElementObject$3, tmpChainElementObject$1, 1);
const tmpChainElementCall = tmpChainElementObject$1.e(1);
a = tmpChainElementCall;
} else {
}
Expand All @@ -53,8 +52,7 @@ const tmpIfTest$1 = tmpChainRootProp$1 != null;
if (tmpIfTest$1) {
const tmpChainElementObject$5 = tmpChainRootProp$1.c;
const tmpChainElementObject$7 = tmpChainElementObject$5.d;
const tmpChainElementObject$9 = tmpChainElementObject$7.e;
const tmpChainElementCall$1 = $dotCall(tmpChainElementObject$9, tmpChainElementObject$7, 1);
const tmpChainElementCall$1 = tmpChainElementObject$7.e(1);
a = tmpChainElementCall$1;
} else {
}
Expand All @@ -69,9 +67,8 @@ $(a);
`````js filename=intro
const tmpObjLitVal$1 = { e: $ };
const tmpChainElementCall = $dotCall($, tmpObjLitVal$1, 1);
const tmpChainElementObject$9 = tmpObjLitVal$1.e;
const tmpChainElementCall$1 = $dotCall(tmpChainElementObject$9, tmpObjLitVal$1, 1);
const tmpChainElementCall = tmpObjLitVal$1.e(1);
const tmpChainElementCall$1 = tmpObjLitVal$1.e(1);
const tmpCalleeParam = tmpChainElementCall + tmpChainElementCall$1;
$(tmpCalleeParam);
$(tmpChainElementCall$1);
Expand All @@ -83,12 +80,11 @@ With rename=true
`````js filename=intro
const a = { e: $ };
const b = $dotCall( $, a, 1 );
const c = a.e;
const d = $dotCall( c, a, 1 );
const e = b + d;
$( e );
const b = a.e( 1 );
const c = a.e( 1 );
const d = b + c;
$( d );
$( c );
`````
## Globals
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ a = undefined;
const tmpChainRootProp = b;
const tmpIfTest = tmpChainRootProp != null;
if (tmpIfTest) {
const tmpChainElementObject = tmpChainRootProp.c;
const tmpChainElementCall = $dotCall(tmpChainElementObject, tmpChainRootProp, 1);
const tmpChainElementCall = tmpChainRootProp.c(1);
a = tmpChainElementCall;
} else {
}
Expand All @@ -47,8 +46,7 @@ a = undefined;
const tmpChainRootProp$1 = b;
const tmpIfTest$1 = tmpChainRootProp$1 != null;
if (tmpIfTest$1) {
const tmpChainElementObject$1 = tmpChainRootProp$1.c;
const tmpChainElementCall$1 = $dotCall(tmpChainElementObject$1, tmpChainRootProp$1, 1);
const tmpChainElementCall$1 = tmpChainRootProp$1.c(1);
a = tmpChainElementCall$1;
} else {
}
Expand All @@ -63,9 +61,8 @@ $(a);
`````js filename=intro
const b = { c: $ };
const tmpChainElementCall = $dotCall($, b, 1);
const tmpChainElementObject$1 = b.c;
const tmpChainElementCall$1 = $dotCall(tmpChainElementObject$1, b, 1);
const tmpChainElementCall = b.c(1);
const tmpChainElementCall$1 = b.c(1);
const tmpCalleeParam = tmpChainElementCall + tmpChainElementCall$1;
$(tmpCalleeParam);
$(tmpChainElementCall$1);
Expand All @@ -77,12 +74,11 @@ With rename=true
`````js filename=intro
const a = { c: $ };
const b = $dotCall( $, a, 1 );
const c = a.c;
const d = $dotCall( c, a, 1 );
const e = b + d;
$( e );
const b = a.c( 1 );
const c = a.c( 1 );
const d = b + c;
$( d );
$( c );
`````
## Globals
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ const tmpIfTest = tmpChainRootProp != null;
if (tmpIfTest) {
const tmpChainElementObject = tmpChainRootProp.c;
const tmpChainElementObject$1 = tmpChainElementObject.d;
const tmpChainElementObject$3 = tmpChainElementObject$1.e;
const tmpChainElementCall = $dotCall(tmpChainElementObject$3, tmpChainElementObject$1, 1);
const tmpChainElementCall = tmpChainElementObject$1.e(1);
a = tmpChainElementCall;
} else {
}
Expand All @@ -57,7 +56,7 @@ $(a);
`````js filename=intro
const tmpObjLitVal$1 = { e: $ };
const tmpChainElementCall = $dotCall($, tmpObjLitVal$1, 1);
const tmpChainElementCall = tmpObjLitVal$1.e(1);
const tmpCalleeParam = [...tmpChainElementCall];
$(tmpCalleeParam);
$(tmpChainElementCall);
Expand All @@ -69,7 +68,7 @@ With rename=true
`````js filename=intro
const a = { e: $ };
const b = $dotCall( $, a, 1 );
const b = a.e( 1 );
const c = [ ... b ];
$( c );
$( b );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ a = undefined;
const tmpChainRootProp = b;
const tmpIfTest = tmpChainRootProp != null;
if (tmpIfTest) {
const tmpChainElementObject = tmpChainRootProp.c;
const tmpChainElementCall = $dotCall(tmpChainElementObject, tmpChainRootProp, 1);
const tmpChainElementCall = tmpChainRootProp.c(1);
a = tmpChainElementCall;
} else {
}
Expand All @@ -53,7 +52,7 @@ $(a);
`````js filename=intro
const b = { c: $ };
const tmpChainElementCall = $dotCall($, b, 1);
const tmpChainElementCall = b.c(1);
const tmpCalleeParam = [...tmpChainElementCall];
$(tmpCalleeParam);
$(tmpChainElementCall);
Expand All @@ -65,7 +64,7 @@ With rename=true
`````js filename=intro
const a = { c: $ };
const b = $dotCall( $, a, 1 );
const b = a.c( 1 );
const c = [ ... b ];
$( c );
$( b );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ const tmpIfTest = tmpChainRootProp != null;
if (tmpIfTest) {
const tmpChainElementObject = tmpChainRootProp.c;
const tmpChainElementObject$1 = tmpChainElementObject.d;
const tmpChainElementObject$3 = tmpChainElementObject$1.e;
const tmpChainElementCall = $dotCall(tmpChainElementObject$3, tmpChainElementObject$1, 1);
const tmpChainElementCall = tmpChainElementObject$1.e(1);
a = tmpChainElementCall;
} else {
}
Expand All @@ -53,8 +52,7 @@ const tmpIfTest$1 = tmpChainRootProp$1 != null;
if (tmpIfTest$1) {
const tmpChainElementObject$5 = tmpChainRootProp$1.c;
const tmpChainElementObject$7 = tmpChainElementObject$5.d;
const tmpChainElementObject$9 = tmpChainElementObject$7.e;
const tmpChainElementCall$1 = $dotCall(tmpChainElementObject$9, tmpChainElementObject$7, 1);
const tmpChainElementCall$1 = tmpChainElementObject$7.e(1);
a = tmpChainElementCall$1;
} else {
}
Expand All @@ -69,9 +67,8 @@ $(a);
`````js filename=intro
const tmpObjLitVal$1 = { e: $ };
const tmpChainElementCall = $dotCall($, tmpObjLitVal$1, 1);
const tmpChainElementObject$9 = tmpObjLitVal$1.e;
const tmpChainElementCall$1 = $dotCall(tmpChainElementObject$9, tmpObjLitVal$1, 1);
const tmpChainElementCall = tmpObjLitVal$1.e(1);
const tmpChainElementCall$1 = tmpObjLitVal$1.e(1);
const tmpCalleeParam = tmpChainElementCall + tmpChainElementCall$1;
$(tmpCalleeParam);
$(tmpChainElementCall$1);
Expand All @@ -83,12 +80,11 @@ With rename=true
`````js filename=intro
const a = { e: $ };
const b = $dotCall( $, a, 1 );
const c = a.e;
const d = $dotCall( c, a, 1 );
const e = b + d;
$( e );
const b = a.e( 1 );
const c = a.e( 1 );
const d = b + c;
$( d );
$( c );
`````
## Globals
Expand Down
Loading

0 comments on commit 4e14c74

Please sign in to comment.