From ffe4301fe201da03deb2a00a464b0fb20668db12 Mon Sep 17 00:00:00 2001 From: Benedikt Meurer Date: Sun, 29 Oct 2017 02:16:48 +0200 Subject: [PATCH] Fix property lookup on booleans in needsWhitespace. (#6584) The code ```js linesInfo && linesInfo[type] ``` performs a lot of dynamic lookups on the `Boolean.prototype`, as the *ToBoolean* operation let's `true` pass for `linesInfo` (which might itself be concerning that this can be a boolean). Instead of the coercion, the code should properly check for valid objects via `typeof` and strict equality with `null` comparison. This is a non-breaking performance fix. --- packages/babel-generator/src/node/index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/babel-generator/src/node/index.js b/packages/babel-generator/src/node/index.js index 7292f83a51..394e63b5cb 100644 --- a/packages/babel-generator/src/node/index.js +++ b/packages/babel-generator/src/node/index.js @@ -75,7 +75,11 @@ export function needsWhitespace(node, parent, type) { } } - return (linesInfo && linesInfo[type]) || 0; + if (typeof linesInfo === "object" && linesInfo !== null) { + return linesInfo[type] || 0; + } + + return 0; } export function needsWhitespaceBefore(node, parent) {