From 5a82ff0c487b83a11cf9dafb5f13aad329611c00 Mon Sep 17 00:00:00 2001 From: chee Date: Thu, 17 Jan 2019 18:23:12 +0000 Subject: [PATCH] Use toposort to sort the polyfills! --- lib/index.js | 18 +++++++++--------- polyfills/Array/prototype/copyWithin/tests.js | 2 +- polyfills/Array/prototype/includes/tests.js | 2 +- polyfills/Math/fround/polyfill.js | 2 +- tasks/node/buildsources.js | 2 +- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/index.js b/lib/index.js index 56d9538e1..3d5448a2b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,6 @@ "use strict"; -const tsort = require("tsort"); +const toposort = require("toposort").array; const createAliasResolver = require("./aliases"); const UA = require("./UA"); const Sources = require("./sources"); @@ -215,7 +215,8 @@ const PolyfillLibrary = class PolyfillLibrary { // Build a polyfill bundle of polyfill sources sorted in dependency order this.getPolyfills(options).then(targetedFeatures => { const warnings = { unknown: [] }; - const features = []; + const featureNodes = []; + const featureEdges = []; Promise.all(Object.keys(targetedFeatures).map(featureName => { const feature = targetedFeatures[featureName]; @@ -223,12 +224,12 @@ const PolyfillLibrary = class PolyfillLibrary { if (!polyfill) { warnings.unknown.push(featureName); } else { - features.push([featureName]); + featureNodes.push(featureName); if (polyfill.dependencies) { polyfill.dependencies.forEach(depName => { if (depName in targetedFeatures) { - features.push([depName, featureName]); + featureEdges.push([depName, featureName]); } }); } @@ -247,10 +248,10 @@ const PolyfillLibrary = class PolyfillLibrary { })).then(() => { // Sort the features alphabetically, so ones with no dependencies // turn up in the same order - const alphabeticalFeatures = features.sort(function ([a], [b]) { - return a.localeCompare(b); - }); - const sortedFeatures = tsort(alphabeticalFeatures).sort(); + featureNodes.sort((a, b) => a.localeCompare(b)); + featureEdges.sort(([, a], [, b]) => a.localeCompare(b)); + + const sortedFeatures = toposort(featureNodes, featureEdges); if (!options.minify) { explainerComment.push( @@ -290,7 +291,6 @@ const PolyfillLibrary = class PolyfillLibrary { output.add( streamFromString("/* " + explainerComment.join("\n * ") + " */\n\n") ); - if (sortedFeatures.length) { // Outer closure hides private features from global scope output.add(streamFromString("(function(undefined) {" + lf)); diff --git a/polyfills/Array/prototype/copyWithin/tests.js b/polyfills/Array/prototype/copyWithin/tests.js index 2d3be4438..5ccb10ff1 100644 --- a/polyfills/Array/prototype/copyWithin/tests.js +++ b/polyfills/Array/prototype/copyWithin/tests.js @@ -1,5 +1,5 @@ /* eslint-env mocha */ -/* globals proclaim, Symbol */ +/* globals proclaim */ it('is a function', function () { proclaim.isFunction(Array.prototype.copyWithin); diff --git a/polyfills/Array/prototype/includes/tests.js b/polyfills/Array/prototype/includes/tests.js index 13b4e2034..bc310ce0a 100644 --- a/polyfills/Array/prototype/includes/tests.js +++ b/polyfills/Array/prototype/includes/tests.js @@ -1,5 +1,5 @@ /* eslint-env mocha */ -/* globals proclaim, Symbol */ +/* globals proclaim */ it('is a function', function () { diff --git a/polyfills/Math/fround/polyfill.js b/polyfills/Math/fround/polyfill.js index 28deaa685..0565d3bec 100644 --- a/polyfills/Math/fround/polyfill.js +++ b/polyfills/Math/fround/polyfill.js @@ -1,4 +1,4 @@ -/* global Float32Array */ +/* global Float32Array, CreateMethodProperty */ // 20.2.2.17 Math.fround ( x ) CreateMethodProperty(Math, 'fround', function (x) { // 1. If x is NaN, return NaN. diff --git a/tasks/node/buildsources.js b/tasks/node/buildsources.js index 1a07e9206..984915a6f 100644 --- a/tasks/node/buildsources.js +++ b/tasks/node/buildsources.js @@ -42,7 +42,7 @@ function flattenPolyfillDirectories(directory) { } function checkForCircularDependencies(polyfills) { - const graph = [] + const graph = []; for (const polyfill of polyfills) { for (const dependency of polyfill.dependencies) {