Skip to content

Commit

Permalink
[WIP] feat: option to not escape strings in diff messages (#5661)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciej-ka authored and SimenB committed Sep 17, 2018
1 parent 5c8ec2f commit 67e4c82
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## master

### Features

- `[pretty-format]` Option to not escape strings in diff messages ([#5661](https://github.com/facebook/jest/pull/5661))

### Fixes

- `[jest-haste-map]` [**BREAKING**] Replaced internal data structures to improve performance ([#6960](https://github.com/facebook/jest/pull/6960))
Expand Down
2 changes: 2 additions & 0 deletions packages/pretty-format/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ console.log(prettyFormat(onClick, options));
| :------------------ | :-------- | :--------- | :------------------------------------------------------ |
| `callToJSON` | `boolean` | `true` | call `toJSON` method (if it exists) on objects |
| `escapeRegex` | `boolean` | `false` | escape special characters in regular expressions |
| `escapeString` | `boolean` | `true` | escape special characters in strings |
| `highlight` | `boolean` | `false` | highlight syntax with colors in terminal (some plugins) |
| `indent` | `number` | `2` | spaces in each level of indentation |
| `maxDepth` | `number` | `Infinity` | levels to print in arrays, objects, elements, and so on |
Expand Down Expand Up @@ -215,6 +216,7 @@ Write `serialize` to return a string, given the arguments:
| `callToJSON` | `boolean` | call `toJSON` method (if it exists) on objects |
| `colors` | `Object` | escape codes for colors to highlight syntax |
| `escapeRegex` | `boolean` | escape special characters in regular expressions |
| `escapeString` | `boolean` | escape special characters in strings |
| `indent` | `string` | spaces in each level of indentation |
| `maxDepth` | `number` | levels to print in arrays, objects, elements, and so on |
| `min` | `boolean` | minimize added space: no indentation nor line breaks |
Expand Down
5 changes: 5 additions & 0 deletions packages/pretty-format/src/__tests__/pretty_format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ describe('prettyFormat()', () => {
expect(prettyFormat(val)).toEqual('"\\"\'\\\\"');
});

it("doesn't escape string with {excapeString: false}", () => {
const val = '"\'\\n';
expect(prettyFormat(val, {escapeString: false})).toEqual('""\'\\n"');
});

it('prints a string with escapes', () => {
expect(prettyFormat('"-"')).toEqual('"\\"-\\""');
expect(prettyFormat('\\ \\\\')).toEqual('"\\\\ \\\\\\\\"');
Expand Down
15 changes: 14 additions & 1 deletion packages/pretty-format/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ function printBasicValue(
val: any,
printFunctionName: boolean,
escapeRegex: boolean,
escapeString: boolean,
): StringOrNull {
if (val === true || val === false) {
return '' + val;
Expand All @@ -119,7 +120,10 @@ function printBasicValue(
return printNumber(val);
}
if (typeOf === 'string') {
return '"' + val.replace(/"|\\/g, '\\$&') + '"';
if (escapeString) {
return '"' + val.replace(/"|\\/g, '\\$&') + '"';
}
return '"' + val + '"';
}
if (typeOf === 'function') {
return printFunction(val, printFunctionName);
Expand Down Expand Up @@ -322,6 +326,7 @@ function printer(
val,
config.printFunctionName,
config.escapeRegex,
config.escapeString,
);
if (basicResult !== null) {
return basicResult;
Expand Down Expand Up @@ -350,6 +355,7 @@ const DEFAULT_THEME_KEYS = Object.keys(DEFAULT_THEME);
const DEFAULT_OPTIONS: Options = {
callToJSON: true,
escapeRegex: false,
escapeString: true,
highlight: false,
indent: 2,
maxDepth: Infinity,
Expand Down Expand Up @@ -424,6 +430,11 @@ const getEscapeRegex = (options?: OptionsReceived) =>
? options.escapeRegex
: DEFAULT_OPTIONS.escapeRegex;

const getEscapeString = (options?: OptionsReceived) =>
options && options.escapeString !== undefined
? options.escapeString
: DEFAULT_OPTIONS.escapeString;

const getConfig = (options?: OptionsReceived): Config => ({
callToJSON:
options && options.callToJSON !== undefined
Expand All @@ -434,6 +445,7 @@ const getConfig = (options?: OptionsReceived): Config => ({
? getColorsHighlight(options)
: getColorsEmpty(),
escapeRegex: getEscapeRegex(options),
escapeString: getEscapeString(options),
indent:
options && options.min
? ''
Expand Down Expand Up @@ -475,6 +487,7 @@ function prettyFormat(val: any, options?: OptionsReceived): string {
val,
getPrintFunctionName(options),
getEscapeRegex(options),
getEscapeString(options),
);
if (basicResult !== null) {
return basicResult;
Expand Down
3 changes: 3 additions & 0 deletions types/PrettyFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type ThemeReceived = {|
export type Options = {|
callToJSON: boolean,
escapeRegex: boolean,
escapeString: boolean,
highlight: boolean,
indent: number,
maxDepth: number,
Expand All @@ -50,6 +51,7 @@ export type Options = {|
export type OptionsReceived = {|
callToJSON?: boolean,
escapeRegex?: boolean,
escapeString?: boolean,
highlight?: boolean,
indent?: number,
maxDepth?: number,
Expand All @@ -63,6 +65,7 @@ export type Config = {|
callToJSON: boolean,
colors: Colors,
escapeRegex: boolean,
escapeString: boolean,
indent: string,
maxDepth: number,
min: boolean,
Expand Down

0 comments on commit 67e4c82

Please sign in to comment.