Skip to content

Commit

Permalink
Merge pull request #17711 from colemanw/useStrict
Browse files Browse the repository at this point in the history
CRM_Utils_JS - also dedupe 'use strict' directive when deduping closures
  • Loading branch information
seamuslee001 authored Jun 29, 2020
2 parents fcb1a2d + df4b6c3 commit 151b3ad
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
4 changes: 3 additions & 1 deletion CRM/Utils/JS.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public static function parseStrings($jsCode) {
* Note that you can only dedupe closures if they are directly adjacent and
* have exactly the same parameters.
*
* Also dedupes the "use strict" directive as it is only meaningful at the beginning of a closure.
*
* @param array $scripts
* Javascript source.
* @param array $localVars
Expand All @@ -70,7 +72,7 @@ public static function dedupeClosures($scripts, $localVars, $inputVals) {
return preg_quote($v, '/');
}, $localVars));
$opening .= '\)\s*\{';
$opening = '/^' . $opening . '/';
$opening = '/^' . $opening . '\s*(?:"use strict";\s|\'use strict\';\s)?/';

// Example closing: })(angular, CRM.$, CRM._);
$closing = '\}\s*\)\s*\(\s*';
Expand Down
12 changes: 7 additions & 5 deletions tests/phpunit/CRM/Utils/JSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,19 @@ public function testParseStrings($jsCode, $expectedStrings) {

public function dedupeClosureExamples() {
// Each example string here is named for its body, eg the body of $a calls "a()".
$a = "(function (angular, $, _) {\na();\n})(angular, CRM.$, CRM._);";
$b = "(function(angular,$,_){\nb();\n})(angular,CRM.$,CRM._);";
$a = "(function (angular, $, _) {\n 'use strict';\n a();\n})(angular, CRM.$, CRM._);";
$b = "(function(angular,$,_){\n \"use strict\";\n b();\n})(angular,CRM.$,CRM._);";
$c = "(function( angular, $,_) {\nc();\n})(angular,CRM.$, CRM._);";
$d = "(function (angular, $, _, whiz) {\nd();\n})(angular, CRM.$, CRM._, CRM.whizbang);";
$m = "alert('i is the trickster (function( angular, $,_) {\nm();\n})(angular,CRM.$, CRM._);)'";
// Note: $d has a fundamentally different closure.

// Each example string here is a deduped combination of others,
// eg "$ab" is the deduping of $a+$b.
$ab = "(function (angular, $, _) {\na();\n\nb();\n})(angular,CRM.$,CRM._);";
$abc = "(function (angular, $, _) {\na();\n\nb();\n\nc();\n})(angular,CRM.$, CRM._);";
$cb = "(function( angular, $,_) {\nc();\n\nb();\n})(angular,CRM.$,CRM._);";
$ab = "(function (angular, $, _) {\n 'use strict';\n a();\n b();\n})(angular,CRM.$,CRM._);";
$ba = "(function(angular,$,_){\n \"use strict\";\n b();\n a();\n})(angular, CRM.$, CRM._);";
$abc = "(function (angular, $, _) {\n 'use strict';\n a();\n b();\nc();\n})(angular,CRM.$, CRM._);";
$cb = "(function( angular, $,_) {\nc();\n b();\n})(angular,CRM.$,CRM._);";

$cases = [];
$cases[] = [[$a], "$a"];
Expand All @@ -127,6 +128,7 @@ public function dedupeClosureExamples() {
$cases[] = [[$d], "$d"];
$cases[] = [[$m], "$m"];
$cases[] = [[$a, $b], "$ab"];
$cases[] = [[$b, $a], "$ba"];
$cases[] = [[$a, $m, $b], "$a$m$b"];
$cases[] = [[$a, $d], "$a$d"];
$cases[] = [[$a, $d, $b], "$a$d$b"];
Expand Down

0 comments on commit 151b3ad

Please sign in to comment.