warnings.js 1.48 KB
Newer Older
Dan Abramov's avatar
Dan Abramov committed
1
2
3
4
5
6
7
8
9
/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

10
/* @flow */
11
12
13
import type { ReactFrame } from '../effects/proxyConsole';

function stripInlineStacktrace(message: string): string {
14
15
16
17
  return message
    .split('\n')
    .filter(line => !line.match(/^\s*in/))
    .join('\n'); // "  in Foo"
18
19
20
21
22
23
24
25
26
27
}

function massage(
  warning: string,
  frames: ReactFrame[]
): { message: string, stack: string } {
  let message = stripInlineStacktrace(warning);

  // Reassemble the stack with full filenames provided by React
  let stack = '';
28
29
  let lastFilename;
  let lastLineNumber;
30
31
32
33
34
  for (let index = 0; index < frames.length; ++index) {
    const { fileName, lineNumber } = frames[index];
    if (fileName == null || lineNumber == null) {
      continue;
    }
35
36
37
38
39
40
41
42
43
44
45
46
47

    // TODO: instead, collapse them in the UI
    if (
      fileName === lastFilename &&
      typeof lineNumber === 'number' &&
      typeof lastLineNumber === 'number' &&
      Math.abs(lineNumber - lastLineNumber) < 3
    ) {
      continue;
    }
    lastFilename = fileName;
    lastLineNumber = lineNumber;

48
49
50
    let { name } = frames[index];
    name = name || '(anonymous function)';
    stack += `in ${name} (at ${fileName}:${lineNumber})\n`;
51
52
53
54
55
56
  }

  return { message, stack };
}

export { massage };