Error in r.js for recursive function?

I'm currently trying to optimize the current jQuery (development version 1.8.1) using r.js. This happens when compiling assets in a rails project using gem requirejs-rails.

I think I encountered an error in the optimizer. Around line 999 in the jquery source you will find the following code:

(function add(args) { jQuery.each(args, function (_, arg) { var type = jQuery.type(arg); if (type === "function" && (!options.unique || !self.has(arg))) { list.push(arg); } else if (arg && arg.length && type !== "string") { // Inspect recursively add(arg); } }); })(arguments); 

When optimized jquery gets into a string containing add(arg); , an error will be thrown that add not detected. This is because the optimizer renamed the add function to e , and the function call remained add(...) as follows:

 (function e(args) { jQuery.each(args, function (_, arg) { var type = jQuery.type(arg); if (type === "function" && (!options.unique || !self.has(arg))) { list.push(arg); } else if (arg && arg.length && type !== "string") { // Inspect recursively add(arg); } }); })(arguments); 

I managed to fix the code by rewriting it:

 var fnAdd = function (args) { jQuery.each(args, function (_, arg) { var type = jQuery.type(arg); if (type === "function" && (!options.unique || !self.has(arg))) { list.push(arg); } else if (arg && arg.length && type !== "string") { // Inspect recursively console.log("inspecting", fnAdd); fnAdd(arg); } }); }; fnAdd(arguments); 

Can this be considered a mistake in r.js? Or is javascript not allowed? I wonder why I'm the first to have a problem (at least Google didn't have any solutions).

+4
source share
1 answer

I think it will be like a mistake. Although I personally would not use the syntax you use, for me it is valid JavaScript. It seems that r.js is not intended to handle recursive calls to functions of this type.

Out of interest, what happens if you add the add () call outside of you? Is r.js correctly renaming it to e () correctly?

+1
source

Source: https://habr.com/ru/post/1433575/


All Articles