FormidableLabs / babel-plugin-transform-define

Compile time code replacement for babel similar to Webpack's DefinePlugin
MIT License
245 stars 31 forks source link

fix the sort comparator #48

Closed wirthi closed 5 years ago

wirthi commented 6 years ago

The comparator function of Array.prototype.sort expects two arguments to compare (https://tc39.github.io/ecma262/#sec-array.prototype.sort)

The previous implementation used a 1-element comparator, which always returns the length of the first element. That this resulted in the correct ordering in a test is pure luck.

ryan-roemer commented 5 years ago

Great find and thanks!

ryan-roemer commented 5 years ago

Released in babel-plugin-transform-define@1.3.2

Published differences:

$ publish-diff -o babel-plugin-transform-define@1.3.1 -n babel-plugin-transform-define@1.3.2 --filter='lib/index.js'
Index: lib/index.js
===================================================================
--- lib/index.js    babel-plugin-transform-define@1.3.1
+++ lib/index.js    babel-plugin-transform-define@1.3.2
@@ -76,10 +76,10 @@
   return traverse(obj).paths().filter(function (arr) {
     return arr.length;
   }).map(function (arr) {
     return arr.join(".");
-  }).sort(function (elem) {
-    return elem.length;
+  }).sort(function (a, b) {
+    return b.length - a.length;
   });
 };

 /**