Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate jest-diff to TypeScript #7824

Merged
merged 5 commits into from Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@
- `[diff-sequences]`: Migrate to Typescript ([#7820](https://github.com/facebook/jest/pull/7820))
- `[jest-get-type]`: Migrate to TypeScript ([#7818](https://github.com/facebook/jest/pull/7818))
- `[jest-regex-util]`: Migrate to TypeScript ([#7822](https://github.com/facebook/jest/pull/7822))
- `[jest-diff]`: Migrate to TypeScript ([#7824](https://github.com/facebook/jest/pull/7824))

### Performance

Expand Down
2 changes: 1 addition & 1 deletion packages/diff-sequences/src/index.ts
Expand Up @@ -65,7 +65,7 @@ type FoundSubsequence = (
) => void;

// Either original functions or wrapped to swap indexes if graph is transposed.
type Callbacks = {
export type Callbacks = {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

easier than inspecting the arguments it takes

foundSubsequence: FoundSubsequence;
isCommon: IsCommon;
};
Expand Down
5 changes: 5 additions & 0 deletions packages/jest-diff/package.json
Expand Up @@ -8,12 +8,17 @@
},
"license": "MIT",
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"chalk": "^2.0.1",
"diff-sequences": "^24.0.0",
"jest-get-type": "^24.0.0",
"pretty-format": "^24.0.0"
},
"devDependencies": {
"@types/strip-ansi": "^3.0.0",
"strip-ansi": "^5.0.0"
},
"engines": {
"node": ">= 6"
},
Expand Down
Expand Up @@ -3,16 +3,17 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

const stripAnsi = require('strip-ansi');
const diff = require('../');
import stripAnsi from 'strip-ansi';

import diff from '../';
import {DiffOptions} from '../types';

const NO_DIFF_MESSAGE = 'Compared values have no visual difference.';

const stripped = (a, b, options) => stripAnsi(diff(a, b, options));
const stripped = (a: unknown, b: unknown, options?: DiffOptions) =>
stripAnsi(diff(a, b, options) || '');

const unexpanded = {expand: false};
const expanded = {expand: true};
Expand Down
Expand Up @@ -3,8 +3,6 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import chalk from 'chalk';
Expand Down
Expand Up @@ -3,28 +3,19 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import chalk from 'chalk';
import type {Chalk} from 'chalk';
import diff from 'diff-sequences';
import {NO_DIFF_MESSAGE} from './constants.js';
import diff, {Callbacks} from 'diff-sequences';
import {NO_DIFF_MESSAGE} from './constants';
import {DiffOptions} from './types';

const DIFF_CONTEXT_DEFAULT = 5;

export type DiffOptions = {|
aAnnotation?: string,
bAnnotation?: string,
expand?: boolean,
contextLines?: number,
|};

type Original = {|
a: string,
b: string,
|};
type Original = {
a: string;
b: string;
};

const fgPatchMark = chalk.yellow;
const fgDelete = chalk.green;
Expand All @@ -35,24 +26,27 @@ const bgCommon = chalk.bgYellow; // edge spaces in common line (even indentation
const bgInverse = chalk.inverse; // edge spaces in any other lines

// ONLY trailing if expected value is snapshot or multiline string.
const highlightTrailingSpaces = (line: string, bgColor: Chalk): string =>
const highlightTrailingSpaces = (line: string, bgColor: typeof chalk): string =>
SimenB marked this conversation as resolved.
Show resolved Hide resolved
line.replace(/\s+$/, bgColor('$&'));

// BOTH leading AND trailing if expected value is data structure.
const highlightLeadingTrailingSpaces = (line: string, bgColor: Chalk): string =>
const highlightLeadingTrailingSpaces = (
line: string,
bgColor: typeof chalk,
): string =>
// If line consists of ALL spaces: highlight all of them.
highlightTrailingSpaces(line, bgColor).replace(
// If line has an ODD length of leading spaces: highlight only the LAST.
/^(\s\s)*(\s)(?=[^\s])/,
'$1' + bgColor('$2'),
);

type Highlight = (line: string, bgColor: Chalk) => string;
type Highlight = (line: string, bgColor: typeof chalk) => string;

const getHighlightSpaces = (bothEdges: boolean): Highlight =>
bothEdges ? highlightLeadingTrailingSpaces : highlightTrailingSpaces;

const getAnnotation = (options: ?DiffOptions): string =>
const getAnnotation = (options?: DiffOptions): string =>
fgDelete('- ' + ((options && options.aAnnotation) || 'Expected')) +
'\n' +
fgInsert('+ ' + ((options && options.bAnnotation) || 'Received')) +
Expand Down Expand Up @@ -134,17 +128,22 @@ const diffExpand = (
aLinesIn: Array<string>,
bLinesIn: Array<string>,
): string => {
const isCommon = (aIndex, bIndex) => aLinesUn[aIndex] === bLinesUn[bIndex];
const isCommon: Callbacks['isCommon'] = (aIndex, bIndex) =>
aLinesUn[aIndex] === bLinesUn[bIndex];

const array = [];
const array: string[] = [];
const put = (line: string) => {
array.push(line);
};

let aStart = 0;
let bStart = 0;

const foundSubsequence = (nCommon, aCommon, bCommon) => {
const foundSubsequence: Callbacks['foundSubsequence'] = (
nCommon,
aCommon,
bCommon,
) => {
formatDelete(aStart, aCommon, aLinesUn, aLinesIn, put);
formatInsert(bStart, bCommon, bLinesUn, bLinesIn, put);
formatCommon(nCommon, aCommon, bCommon, aLinesIn, bLinesUn, bLinesIn, put);
Expand Down Expand Up @@ -175,7 +174,7 @@ const createPatchMark = (
`@@ -${aStart + 1},${aEnd - aStart} +${bStart + 1},${bEnd - bStart} @@`,
);

const getContextLines = (options: ?DiffOptions): number =>
const getContextLines = (options?: DiffOptions): number =>
options &&
typeof options.contextLines === 'number' &&
options.contextLines >= 0
Expand All @@ -193,7 +192,8 @@ const diffNoExpand = (
bLinesIn: Array<string>,
nContextLines: number,
): string => {
const isCommon = (aIndex, bIndex) => aLinesUn[aIndex] === bLinesUn[bIndex];
const isCommon: Callbacks['isCommon'] = (aIndex, bIndex) =>
aLinesUn[aIndex] === bLinesUn[bIndex];

let iPatchMark = 0; // index of placeholder line for patch mark
const array = [''];
Expand All @@ -215,7 +215,11 @@ const diffNoExpand = (

// Given the number of items and starting indexes of each common subsequence,
// format any preceding change lines, and then common context lines.
const foundSubsequence = (nCommon, aStartCommon, bStartCommon) => {
const foundSubsequence: Callbacks['foundSubsequence'] = (
nCommon,
aStartCommon,
bStartCommon,
) => {
const aEndCommon = aStartCommon + nCommon;
const bEndCommon = bStartCommon + nCommon;
isAtEnd = aEndCommon === aLength && bEndCommon === bLength;
Expand Down Expand Up @@ -294,7 +298,7 @@ const diffNoExpand = (
export default (
a: string,
b: string,
options: ?DiffOptions,
options?: DiffOptions,
original?: Original,
): string => {
if (a === b) {
Expand Down
Expand Up @@ -3,17 +3,14 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import type {DiffOptions} from './diffStrings';

import prettyFormat from 'pretty-format';
import chalk from 'chalk';
import getType from 'jest-get-type';
import diffStrings from './diffStrings';
import {NO_DIFF_MESSAGE, SIMILAR_MESSAGE} from './constants';
import {DiffOptions} from './types';

const {
AsymmetricMatcher,
Expand Down Expand Up @@ -45,7 +42,7 @@ const FALLBACK_FORMAT_OPTIONS_0 = {...FALLBACK_FORMAT_OPTIONS, indent: 0};

// Generate a string that will highlight the difference between two values
// with green and red. (similar to how github does code diffing)
function diff(a: any, b: any, options: ?DiffOptions): ?string {
function diff(a: any, b: any, options?: DiffOptions): string | null {
if (Object.is(a, b)) {
return NO_DIFF_MESSAGE;
}
Expand Down Expand Up @@ -98,7 +95,7 @@ function diff(a: any, b: any, options: ?DiffOptions): ?string {
function comparePrimitive(
a: number | boolean,
b: number | boolean,
options: ?DiffOptions,
options?: DiffOptions,
) {
return diffStrings(
prettyFormat(a, FORMAT_OPTIONS),
Expand All @@ -107,15 +104,15 @@ function comparePrimitive(
);
}

function sortMap(map) {
function sortMap(map: Map<unknown, unknown>) {
return new Map(Array.from(map.entries()).sort());
}

function sortSet(set) {
function sortSet(set: Set<unknown>) {
return new Set(Array.from(set.values()).sort());
}

function compareObjects(a: Object, b: Object, options: ?DiffOptions) {
function compareObjects(a: Object, b: Object, options?: DiffOptions) {
let diffMessage;
let hasThrown = false;

Expand Down Expand Up @@ -153,4 +150,4 @@ function compareObjects(a: Object, b: Object, options: ?DiffOptions) {
return diffMessage;
}

module.exports = diff;
export = diff;
6 changes: 6 additions & 0 deletions packages/jest-diff/src/types.ts
@@ -0,0 +1,6 @@
export type DiffOptions = {
aAnnotation?: string;
bAnnotation?: string;
expand?: boolean;
contextLines?: number;
};
12 changes: 12 additions & 0 deletions packages/jest-diff/tsconfig.json
@@ -0,0 +1,12 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build"
},
"references": [
{"path": "../diff-sequences"},
{"path": "../jest-get-type"},
{"path": "../pretty-format"}
]
}
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -1782,6 +1782,11 @@
resolved "https://registry.yarnpkg.com/@types/string-length/-/string-length-2.0.0.tgz#358ce3ff2e8c2310270ee192ddd6b79b64fed7b2"
integrity sha512-xFwWZpIWcLsrcVEybzxmxQM/26Snj1gqxmVrelC3We3Nub7q70RCtqTBVQ7nL+315fAcw4BGSFpxvMkhLApKmQ==

"@types/strip-ansi@^3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@types/strip-ansi/-/strip-ansi-3.0.0.tgz#9b63d453a6b54aa849182207711a08be8eea48ae"
integrity sha1-m2PUU6a1SqhJGCIHcRoIvo7qSK4=

"@types/strip-bom@3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@types/strip-bom/-/strip-bom-3.0.0.tgz#14a8ec3956c2e81edb7520790aecf21c290aebd2"
Expand Down