Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@

import type {StackFrame} from '../../../Core/NativeExceptionsManager';

const {parseLogBoxException, parseLogBoxLog} = require('../parseLogBoxLog');
const {
parseLogBoxException,
parseLogBoxLog,
withoutANSIColorStyles,
} = require('../parseLogBoxLog');

describe('parseLogBoxLog', () => {
it('parses strings', () => {
Expand Down Expand Up @@ -1656,3 +1660,36 @@ Please follow the instructions at: fburl.com/rn-remote-assets`,
});
});
});

describe('withoutANSIColorStyles', () => {
it('works with non-strings', () => {
expect(withoutANSIColorStyles(null)).toEqual(null);
expect(withoutANSIColorStyles(undefined)).toEqual(undefined);
expect(withoutANSIColorStyles({})).toEqual({});
expect(withoutANSIColorStyles(1)).toEqual(1);
});

it('works with empty string', () => {
expect(withoutANSIColorStyles('')).toEqual('');
});

it("doesn't modify string that don't have ANSI escape sequences", () => {
expect(
withoutANSIColorStyles('Warning: this is the React warning %s'),
).toEqual('Warning: this is the React warning %s');
});

it('filters out ANSI escape sequences and preserves console substitutions', () => {
expect(
withoutANSIColorStyles(
'\x1b[2;38;2;124;124;124mWarning: this is the React warning %s\x1b[0m',
),
).toEqual('Warning: this is the React warning %s');
});

it('filters out ANSI escape sequences for string with only console substitutions', () => {
expect(
withoutANSIColorStyles('\x1b[2;38;2;124;124;124m%s %s\x1b[0m'),
).toEqual('%s %s');
});
});
14 changes: 13 additions & 1 deletion packages/react-native/Libraries/LogBox/Data/parseLogBoxLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,13 +435,25 @@ export function parseLogBoxException(
};
}

export function withoutANSIColorStyles(message: mixed): mixed {
if (typeof message !== 'string') {
return message;
}

return message.replace(
// eslint-disable-next-line no-control-regex
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
'',
);
}

export function parseLogBoxLog(args: $ReadOnlyArray<mixed>): {|
componentStack: ComponentStack,
componentStackType: ComponentStackType,
category: Category,
message: Message,
|} {
const message = args[0];
const message = withoutANSIColorStyles(args[0]);
let argsWithoutComponentStack: Array<mixed> = [];
let componentStack: ComponentStack = [];
let componentStackType = 'legacy';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5460,6 +5460,7 @@ declare export function parseComponentStack(message: string): {
declare export function parseLogBoxException(
error: ExtendedExceptionData
): LogBoxLogData;
declare export function withoutANSIColorStyles(message: mixed): mixed;
declare export function parseLogBoxLog(args: $ReadOnlyArray<mixed>): {|
componentStack: ComponentStack,
componentStackType: ComponentStackType,
Expand Down