#691·chalk

A null/undefined argument renders as literal text with two arguments, but as empty with three or more

Author: januththedevCreated Sep 27, 2026Updated Sep 27, 2026

A null/undefined argument renders as literal text with two arguments, but as empty with three or more

Description

createBuilder has a two-argument fast path that concatenates with +, and a fallback for three or more that uses Array#join:

javascript
if (arguments_.length === 2) {
    return applyStyle(builder, arguments_[0] + ' ' + arguments_[1]);
}

return applyStyle(builder, arguments_.join(' '));

+ uses ToString, which maps null to "null". Array#join maps null/undefined elements to the empty string. The two branches of the same function therefore disagree, and both disagree with published [email protected], which uses join at every arity.

Reproduction

With FORCE_COLOR=3:

red(null, "a")               "\u001b[31mnull a\u001b[39m"          <- bug
red("a", null)               "\u001b[31ma null\u001b[39m"          <- bug
red(undefined, "a")          "\u001b[31mundefined a\u001b[39m"     <- bug
red("a", undefined)          "\u001b[31ma undefined\u001b[39m"     <- bug
red(null, "a", "b")          "\u001b[31m a b\u001b[39m"            <- correct
red("a", null, "b")          "\u001b[31ma  b\u001b[39m"            <- correct
red(null)                    "\u001b[31mnull\u001b[39m"            <- correct (one-arg coercion, matches upstream)

The readme's "Multiple arguments will be separated by space" plus console.log substitution semantics imply empty slots, and three arguments already behave that way.

Expected

Every arity uses Array#join, so a skipped slot is empty regardless of how many arguments are passed. The one-argument path should keep its '' + arguments_[0] coercion, since that matches upstream and is documented in the comment as a hot path.

Fix

I have a tested change ready, but GitHub is currently refusing to let this account open a pull request against chalk/chalk (CreatePullRequest permission denied, and the fork is not linked into the repository network). So this is a request for a maintainer to open the PR, or to tell me how to proceed.

The change removes the redundant two-argument branch in source/index.js:

diff
-		if (arguments_.length === 2) {
-			return applyStyle(builder, arguments_[0] + ' ' + arguments_[1]);
-		}
-
+		// `Array#join` renders `null` and `undefined` as empty, unlike concatenation
+		// with `+`, which would stringify them.
 		return applyStyle(builder, arguments_.join(' '));

plus a regression test in test/chalk.js pinning the two-argument form to Array#join semantics.

Local verification: the new test fails before the change (actual 'null bar' vs expected ' bar') and passes after; npx ava test/chalk.js → 31 passed.

As a cross-check I diffed this build against published [email protected] over 60,537 cases (all styles, 3-deep chains, model styles, .visible positions, arities, levels 1-3). Before the fix: 138 divergences, 129 of which were this bug. After: 54, and every remaining one is the fork's intentional ansi256 level-1 downsample, which is documented in the readme and already asserted in the tests.

One methodological note in case anyone repeats that cross-check: import chalk from 'chalk' inside the chalk package resolves to the package itself through Node's trySelf, so the comparison silently compares the fork to itself unless the real dependency is imported by explicit path.