Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace lodash 'sortBy' usage with Array.prototype.sort #11810

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
e003efe
Replace lodash 'sortBy' usage with Array.prototype.sort
jayaddison Jul 9, 2020
13fce2a
Rectify comment
jayaddison Jul 9, 2020
5a38c24
Extract priority as local function
jayaddison Jul 9, 2020
67a41ad
Update packages/babel-core/src/transformation/block-hoist-plugin.js
jayaddison Jul 9, 2020
47cffbe
Flip Array.prototype.sort comparator argument order
jayaddison Jul 9, 2020
65099df
Switch from Array.prototype.every to Array.prototype.find
jayaddison Jul 9, 2020
5ce6cf8
Ensure stable element sort order for Node < 12 by retaining a map of …
jayaddison Jul 9, 2020
3031dbf
Fixup: null rather than false argument to Map constructor when sortin…
jayaddison Jul 9, 2020
e84d4bb
Update packages/babel-core/src/transformation/block-hoist-plugin.js
jayaddison Jul 9, 2020
a7e7a71
Nit: rephrase boolean fallback behaviour to ensure that sortIsUnstabl…
jayaddison Jul 9, 2020
8617a7f
Nit: remove empty newline
jayaddison Jul 9, 2020
8a2e5a8
Refactor stabilityMap construction and comparator logic
jayaddison Jul 9, 2020
a472de9
Ensure that the sort comparator always returns a value
jayaddison Jul 9, 2020
fdd2756
Pre-check whether NodeJS 'process' variable is defined
jayaddison Jul 21, 2020
9e2db88
Fixup: linting
jayaddison Jul 21, 2020
3ee06b3
Update logical condition: use map as a tie-breaker for non-NodeJS env…
jayaddison Jul 21, 2020
e50df93
Simplification: use Array.prototype.some instead of Array.prototype.f…
jayaddison Jul 21, 2020
8cc9106
Simplify expression: invoke Object.entries instead of using an equiva…
jayaddison Jul 21, 2020
55b6994
Revert "Simplify expression: invoke Object.entries instead of using a…
jayaddison Jul 21, 2020
d213549
Merge branch 'main' into dependencies/reduce-lodash-usage-sortby
jayaddison Nov 10, 2020
264d0f3
Add key, value types to Map constructor
jayaddison Nov 10, 2020
f681c41
Use semver to perform version parsing and comparison
jayaddison Nov 10, 2020
fad44b7
Fixup: misplaced trailing semi-colon
jayaddison Nov 10, 2020
3fdb680
Fixup: add missing semver import
jayaddison Nov 10, 2020
f5764fa
Read node version from 'process.versions.node'
jayaddison Nov 10, 2020
3ddc74a
Use static string version instead of attempting semver parsing (from …
jayaddison Nov 10, 2020
af82e70
babel-core: update to semver@^7.3.2
jayaddison Nov 10, 2020
1d7e4f3
Skip flow type-checking for stabilityMap arithmetic
jayaddison Nov 10, 2020
2465432
Add flow type declaration for semver.clean
jayaddison Nov 10, 2020
bef7e93
Revert "babel-core: update to semver@^7.3.2"
jayaddison Nov 10, 2020
d468331
Add value presence check for nodeVersion
jayaddison Nov 10, 2020
26a35d1
lint: apply fixes suggested by prettier
jayaddison Nov 10, 2020
4f5673b
lint: apply string quoting fix suggested by prettier
jayaddison Nov 10, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/third-party-libs.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ declare module "semver" {
declare module.exports: {
SemVer: SemVer;

clean(version: string): string | null;
coerce(version: string | SemVer): SemVer | null;
gt(v1: string, v2: string): boolean;
intersects(r1: string, r2: string): boolean;
Expand Down
42 changes: 23 additions & 19 deletions packages/babel-core/src/transformation/block-hoist-plugin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @flow

import sortBy from "lodash/sortBy";
import semver from "semver";

import loadConfig, { type Plugin } from "../config";

Expand All @@ -25,7 +24,8 @@ export default function loadBlockHoistPlugin(): Plugin {

const blockHoistPlugin = {
/**
* [Please add a description.]
* When hoisted blocks are present, sort node elements by priority order
* (highest priority first)
*
* Priority:
*
Expand All @@ -41,23 +41,27 @@ const blockHoistPlugin = {
visitor: {
Block: {
exit({ node }) {
let hasChange = false;
for (let i = 0; i < node.body.length; i++) {
const bodyNode = node.body[i];
if (bodyNode?._blockHoist != null) {
hasChange = true;
break;
if (!node.body.some(node => node?._blockHoist != null)) return;
// TODO: Babel 9 (?) - remove stabilityMap once Node >= 12 is guaranteed
// Array sorting is not stable in earlier Node releases
// See: https://v8.dev/blog/array-sort for details
const nodeVersion = semver.clean(process.versions.node);
const stabilityMap =
nodeVersion && semver.lt(nodeVersion, "12.0.0")
? new Map<any, number>(node.body.map((n, idx) => [n, idx]))
: null;
const priority = node => {
if (node?._blockHoist == null) return -1;
if (node?._blockHoist === true) return -2;
return -1 * node?._blockHoist;
};
node.body.sort((a, b) => {
const result = priority(a) - priority(b);
if (stabilityMap) {
// $FlowIgnore: stabilityMap contains numeric values for all nodes
return result || stabilityMap.get(a) - stabilityMap.get(b);
}
}
if (!hasChange) return;

node.body = sortBy(node.body, function (bodyNode) {
let priority = bodyNode?._blockHoist;
if (priority == null) priority = 1;
if (priority === true) priority = 2;

// Higher priorities should move toward the top.
return -1 * priority;
return result;
});
},
},
Expand Down