Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/handlebars/helpers/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ export default function (instance) {
}
args[0] = level;

// Only add depth to args if it's explicitly provided
let depth;
if (options.hash.depth != null) {
depth = options.hash.depth;
} else if (options.data && options.data.depth != null) {
depth = options.data.depth;
Comment thread
btea marked this conversation as resolved.
Outdated
}
if (depth != null) {
args.splice(1, 0, depth);
Comment thread
btea marked this conversation as resolved.
Outdated
}
Comment thread
btea marked this conversation as resolved.
Outdated

instance.log(...args);
Comment thread
btea marked this conversation as resolved.
});
}
51 changes: 51 additions & 0 deletions lib/handlebars/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { indexOf } from './utils';
let logger = {
methodMap: ['debug', 'info', 'warn', 'error'],
level: 'info',
depth: 2,

// Maps a given level value to the `methodMap` indexes above.
lookupLevel: function (level) {
Expand All @@ -18,10 +19,30 @@ let logger = {
return level;
},

convertDepth: function (depth) {
depth = parseInt(depth, 10);
if (isNaN(depth) || depth < 0) {
depth = logger.depth;
}

return depth;
},

// Can be overridden in the host environment
log: function (level, ...message) {
level = logger.lookupLevel(level);

// Check if the first message argument is a numeric depth
let depth = logger.depth;
if (
message.length > 0 &&
typeof message[0] === 'number' &&
!isNaN(message[0])
) {
depth = logger.convertDepth(message[0]);
Comment thread
btea marked this conversation as resolved.
Outdated
message = message.slice(1);
}
Comment thread
btea marked this conversation as resolved.

Comment thread
btea marked this conversation as resolved.
if (
typeof console !== 'undefined' &&
logger.lookupLevel(logger.level) <= level
Expand All @@ -31,6 +52,36 @@ let logger = {
if (!console[method]) {
method = 'log';
}

const isNodeEnvironment =
typeof process !== 'undefined' &&
process.versions && // eslint-disable-line no-undef
process.versions.node; // eslint-disable-line no-undef
if (isNodeEnvironment && depth !== 2) {
let formatWithOptions;
if (typeof require === 'function') {
try {
formatWithOptions = require('util').formatWithOptions;
} catch (e) {
// util module not available, fallback to default behavior
}
}
if (typeof formatWithOptions === 'function') {
const supportsColor =
typeof process !== 'undefined' &&
process.stdout &&
Comment thread
btea marked this conversation as resolved.
Outdated
process.stdout.isTTY; // eslint-disable-line no-undef
const formatted = formatWithOptions(
{
depth: depth,
colors: supportsColor,
},
...message
);
message = [formatted];
}
}
Comment thread
btea marked this conversation as resolved.

console[method](...message); // eslint-disable-line no-console
}
},
Expand Down
65 changes: 65 additions & 0 deletions spec/builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,71 @@ describe('builtin helpers', function () {
expectTemplate('{{log}}').withInput({ blah: 'whee' }).toCompileTo('');
expect(called).to.be.true();
});

it('should handle hash depth parameter', function () {
var levelArg, depthArg, logArg;
handlebarsEnv.log = function (level, ...args) {
levelArg = level;
// Check if the first arg is a number (depth)
if (typeof args[0] === 'number') {
depthArg = args[0];
logArg = args[1];
} else {
logArg = args[0];
}
};

expectTemplate('{{log blah depth=5}}')
.withInput({ blah: 'whee' })
.toCompileTo('');
equals(1, levelArg);
equals(5, depthArg);
equals('whee', logArg);
});
Comment thread
btea marked this conversation as resolved.

it('should handle data depth parameter', function () {
var levelArg, depthArg, logArg;
handlebarsEnv.log = function (level, ...args) {
levelArg = level;
// Check if the first arg is a number (depth)
if (typeof args[0] === 'number') {
depthArg = args[0];
logArg = args[1];
} else {
logArg = args[0];
}
};

expectTemplate('{{log blah}}')
.withInput({ blah: 'whee' })
.withRuntimeOptions({ data: { depth: 3 } })
.withCompileOptions({ data: true })
.toCompileTo('');
equals(1, levelArg);
equals(3, depthArg);
equals('whee', logArg);
});

it('should handle both level and depth parameters', function () {
var levelArg, depthArg, logArg;
handlebarsEnv.log = function (level, ...args) {
levelArg = level;
// Check if the first arg is a number (depth)
if (typeof args[0] === 'number') {
depthArg = args[0];
logArg = args[1];
} else {
logArg = args[0];
}
};

expectTemplate('{{log blah level="error" depth=10}}')
.withInput({ blah: 'whee' })
.toCompileTo('');
equals('error', levelArg);
equals(10, depthArg);
equals('whee', logArg);
});
/* eslint-enable no-console */
});

Expand Down
Loading