console.log to victory!

July 13, 2018 - Keiran O'Leary

A colleague of mine was investigating a peculiar bug within a separate company’s codebase. They needed to observe the bug in staging, and got to the point where console.log was the best/only approach.

However, the deployment pipeline for this repo had a linting step which threw…

Calls to console.log are not allowed (no-console.log)

Pretty fair call… just not ideal when spelunking, and without permissions to update the pipeline’s tslint config what to do?


Initial approach

The above situation sparked a wonderful office collaborative challenge of “how can we outsmart/confuse the linter”!

“..how about simply accessing log via a string on console?”

console["log"]("YOU'RE NOT MY SUPERVISOR!!!")

This was however met with…

Object access via string is disallowed (no-string-literals)

Caught again!


Finally

“What about trying to assign a variable to console?”

const braveheart = console;
braveheart.log("FREEEEEDOM!!!")```

Success!

A subsequent solution included simple destructuring…

const { log: forrest } = console;
forrest("Run forrest run!!");

Ultimately it was a few minutes of fun in enjyoing how versatile javascript is. While tslint is incredibly clever, (and it is) there’s always a way to outsmart it!