Skip to content

Commit 0c85ccd

Browse files
committed
Fix scala-js/scala-js#1990: Do not duplicate % for 1-arg console.log on Node.js >= 2.1.0.
As of io.js v2.1.0 (which later merged back into Node.js v4), console.log() does not deduplicate % signs anymore if only one argument is given to console.log(). This commit detects the version of Node.js, and will only duplicate % signs on old versions, or if there is more than one argument given to log(). This precisely restores the previous behavior of our patching of console.log() on recent versions.
1 parent e215281 commit 0c85ccd

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

js-envs/src/main/scala/org/scalajs/jsenv/nodejs/NodeJSEnv.scala

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,24 @@ class NodeJSEnv private (
281281
"""
282282
// Hack console log to duplicate double % signs
283283
(function() {
284+
function startsWithAnyOf(s, prefixes) {
285+
for (var i = 0; i < prefixes.length; i++) {
286+
// ES5 does not have .startsWith() on strings
287+
if (s.substring(0, prefixes[i].length) === prefixes[i])
288+
return true;
289+
}
290+
return false;
291+
}
292+
var nodeWillDeduplicateEvenForOneArgument = startsWithAnyOf(
293+
process.version, ["v0.", "v1.", "v2.0."]);
284294
var oldLog = console.log;
285295
var newLog = function() {
286296
var args = arguments;
287297
if (args.length >= 1 && args[0] !== void 0 && args[0] !== null) {
288-
args[0] = args[0].toString().replace(/%/g, "%%");
298+
var argStr = args[0].toString();
299+
if (args.length > 1 || nodeWillDeduplicateEvenForOneArgument)
300+
argStr = argStr.replace(/%/g, "%%");
301+
args[0] = argStr;
289302
}
290303
oldLog.apply(console, args);
291304
};

0 commit comments

Comments
 (0)