Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
interpolate expression in logMessages
Browse files Browse the repository at this point in the history
  • Loading branch information
weinand committed Mar 22, 2018
1 parent 67daf47 commit 1233378
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/node/nodeDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class InternalSourceBreakpoint {
this.column = this.orgColumn = column;

if (logMessage) {
this.condition = `console.log('${logMessage}')`;
this.condition = logMessageToExpression(logMessage);
if (condition) {
this.condition = `(${condition}) && ${this.condition}`;
}
Expand Down Expand Up @@ -4205,6 +4205,28 @@ function isIndex(name: string | number) {
}
}

const LOGMESSAGE_VARIABLE_REGEXP = /{(.*?)}/g;

function logMessageToExpression(msg) {

msg = msg.replace('%', '%%');

let args: string[] = [];
let format = msg.replace(LOGMESSAGE_VARIABLE_REGEXP, (match, group) => {
const a = group.trim();
if (a) {
args.push(`(${a})`);
return '%s';
} else {
return '';
}
});

format = format.replace('\'', '\\\'');

return `console.log('${format}', ${args.join(', ')})`;
}

function random(low: number, high: number): number {
return Math.floor(Math.random() * (high - low) + low);
}
Expand Down

0 comments on commit 1233378

Please sign in to comment.