util: highlight stack frames · nodejs/node@1940114 (original) (raw)

`@@ -86,6 +86,8 @@ const {

`

86

86

``

87

87

`const assert = require('internal/assert');

`

88

88

``

``

89

`+

const { NativeModule } = require('internal/bootstrap/loaders');

`

``

90

+

89

91

`let hexSlice;

`

90

92

``

91

93

`const inspectDefaultOptions = Object.seal({

`

`@@ -115,6 +117,9 @@ const strEscapeSequencesReplacerSingle = /[\x00-\x1f\x5c]/g;

`

115

117

`const keyStrRegExp = /^[a-zA-Z_][a-zA-Z_0-9]*$/;

`

116

118

`const numberRegExp = /^(0|[1-9][0-9]*)$/;

`

117

119

``

``

120

`+

const coreModuleRegExp = /^ at (?:[^/\(]+ (|)((?<![/\]).+).js:\d+:\d+)?$/;

`

``

121

`+

const nodeModulesRegExp = /[/\]node_modules/\(?=[/\])/g;

`

``

122

+

118

123

`const readableRegExps = {};

`

119

124

``

120

125

`const kMinLineLength = 16;

`

`@@ -253,7 +258,8 @@ inspect.styles = Object.assign(Object.create(null), {

`

253

258

`symbol: 'green',

`

254

259

`date: 'magenta',

`

255

260

`// "name": intentionally not styling

`

256

``

`-

regexp: 'red'

`

``

261

`+

regexp: 'red',

`

``

262

`+

module: 'underline'

`

257

263

`});

`

258

264

``

259

265

`function addQuotes(str, quotes) {

`

`@@ -838,10 +844,37 @@ function formatError(err, constructor, tag, ctx) {

`

838

844

`}

`

839

845

`}

`

840

846

`}

`

``

847

`+

// Ignore the error message if it's contained in the stack.

`

``

848

`+

let pos = err.message && stack.indexOf(err.message) || -1;

`

``

849

`+

if (pos !== -1)

`

``

850

`+

pos += err.message.length;

`

841

851

`// Wrap the error in brackets in case it has no stack trace.

`

842

``

`-

const stackStart = stack.indexOf('\n at');

`

``

852

`+

const stackStart = stack.indexOf('\n at', pos);

`

843

853

`if (stackStart === -1) {

`

844

854

`` stack = [${stack}];

``

``

855

`+

} else if (ctx.colors) {

`

``

856

`+

// Highlight userland code and node modules.

`

``

857

`+

let newStack = stack.slice(0, stackStart);

`

``

858

`+

const lines = stack.slice(stackStart + 1).split('\n');

`

``

859

`+

for (const line of lines) {

`

``

860

`+

const core = line.match(coreModuleRegExp);

`

``

861

`+

if (core !== null && NativeModule.exists(core[1])) {

`

``

862

`` +

newStack += \n${ctx.stylize(line, 'undefined')};

``

``

863

`+

} else {

`

``

864

`+

// This adds underscores to all node_modules to quickly identify them.

`

``

865

`+

let nodeModule;

`

``

866

`+

newStack += '\n';

`

``

867

`+

let pos = 0;

`

``

868

`+

while (nodeModule = nodeModulesRegExp.exec(line)) {

`

``

869

`+

// '/node_modules/'.length === 14

`

``

870

`+

newStack += line.slice(pos, nodeModule.index + 14);

`

``

871

`+

newStack += ctx.stylize(nodeModule[1], 'module');

`

``

872

`+

pos = nodeModule.index + nodeModule[0].length;

`

``

873

`+

}

`

``

874

`+

newStack += pos === 0 ? line : line.slice(pos);

`

``

875

`+

}

`

``

876

`+

}

`

``

877

`+

stack = newStack;

`

845

878

`}

`

846

879

`// The message and the stack have to be indented as well!

`

847

880

`if (ctx.indentationLvl !== 0) {

`