Commit e11c97e9 authored by KangMin An's avatar KangMin An
Browse files

Delete: Untracked Files-nodemodules & package.json

parent 9d3019a3
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.SIMILAR_MESSAGE = exports.NO_DIFF_MESSAGE = void 0;
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const NO_DIFF_MESSAGE = 'Compared values have no visual difference.';
exports.NO_DIFF_MESSAGE = NO_DIFF_MESSAGE;
const SIMILAR_MESSAGE =
'Compared values serialize to the same structure.\n' +
'Printing internal object structure without calling `toJSON` instead.';
exports.SIMILAR_MESSAGE = SIMILAR_MESSAGE;
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { Diff } from './cleanupSemantic';
import type { DiffOptions } from './types';
export declare const diffLinesUnified: (aLines: Array<string>, bLines: Array<string>, options?: DiffOptions | undefined) => string;
export declare const diffLinesUnified2: (aLinesDisplay: Array<string>, bLinesDisplay: Array<string>, aLinesCompare: Array<string>, bLinesCompare: Array<string>, options?: DiffOptions | undefined) => string;
export declare const diffLinesRaw: (aLines: Array<string>, bLines: Array<string>) => Array<Diff>;
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.diffLinesRaw = exports.diffLinesUnified2 = exports.diffLinesUnified = void 0;
var _diffSequences = _interopRequireDefault(require('diff-sequences'));
var _cleanupSemantic = require('./cleanupSemantic');
var _normalizeDiffOptions = require('./normalizeDiffOptions');
var _printDiffs = require('./printDiffs');
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {default: obj};
}
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const isEmptyString = lines => lines.length === 1 && lines[0].length === 0; // Compare two arrays of strings line-by-line. Format as comparison lines.
const diffLinesUnified = (aLines, bLines, options) =>
(0, _printDiffs.printDiffLines)(
diffLinesRaw(
isEmptyString(aLines) ? [] : aLines,
isEmptyString(bLines) ? [] : bLines
),
(0, _normalizeDiffOptions.normalizeDiffOptions)(options)
); // Given two pairs of arrays of strings:
// Compare the pair of comparison arrays line-by-line.
// Format the corresponding lines in the pair of displayable arrays.
exports.diffLinesUnified = diffLinesUnified;
const diffLinesUnified2 = (
aLinesDisplay,
bLinesDisplay,
aLinesCompare,
bLinesCompare,
options
) => {
if (isEmptyString(aLinesDisplay) && isEmptyString(aLinesCompare)) {
aLinesDisplay = [];
aLinesCompare = [];
}
if (isEmptyString(bLinesDisplay) && isEmptyString(bLinesCompare)) {
bLinesDisplay = [];
bLinesCompare = [];
}
if (
aLinesDisplay.length !== aLinesCompare.length ||
bLinesDisplay.length !== bLinesCompare.length
) {
// Fall back to diff of display lines.
return diffLinesUnified(aLinesDisplay, bLinesDisplay, options);
}
const diffs = diffLinesRaw(aLinesCompare, bLinesCompare); // Replace comparison lines with displayable lines.
let aIndex = 0;
let bIndex = 0;
diffs.forEach(diff => {
switch (diff[0]) {
case _cleanupSemantic.DIFF_DELETE:
diff[1] = aLinesDisplay[aIndex];
aIndex += 1;
break;
case _cleanupSemantic.DIFF_INSERT:
diff[1] = bLinesDisplay[bIndex];
bIndex += 1;
break;
default:
diff[1] = bLinesDisplay[bIndex];
aIndex += 1;
bIndex += 1;
}
});
return (0, _printDiffs.printDiffLines)(
diffs,
(0, _normalizeDiffOptions.normalizeDiffOptions)(options)
);
}; // Compare two arrays of strings line-by-line.
exports.diffLinesUnified2 = diffLinesUnified2;
const diffLinesRaw = (aLines, bLines) => {
const aLength = aLines.length;
const bLength = bLines.length;
const isCommon = (aIndex, bIndex) => aLines[aIndex] === bLines[bIndex];
const diffs = [];
let aIndex = 0;
let bIndex = 0;
const foundSubsequence = (nCommon, aCommon, bCommon) => {
for (; aIndex !== aCommon; aIndex += 1) {
diffs.push(
new _cleanupSemantic.Diff(_cleanupSemantic.DIFF_DELETE, aLines[aIndex])
);
}
for (; bIndex !== bCommon; bIndex += 1) {
diffs.push(
new _cleanupSemantic.Diff(_cleanupSemantic.DIFF_INSERT, bLines[bIndex])
);
}
for (; nCommon !== 0; nCommon -= 1, aIndex += 1, bIndex += 1) {
diffs.push(
new _cleanupSemantic.Diff(_cleanupSemantic.DIFF_EQUAL, bLines[bIndex])
);
}
};
(0, _diffSequences.default)(aLength, bLength, isCommon, foundSubsequence); // After the last common subsequence, push remaining change items.
for (; aIndex !== aLength; aIndex += 1) {
diffs.push(
new _cleanupSemantic.Diff(_cleanupSemantic.DIFF_DELETE, aLines[aIndex])
);
}
for (; bIndex !== bLength; bIndex += 1) {
diffs.push(
new _cleanupSemantic.Diff(_cleanupSemantic.DIFF_INSERT, bLines[bIndex])
);
}
return diffs;
};
exports.diffLinesRaw = diffLinesRaw;
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { Diff } from './cleanupSemantic';
declare const diffStrings: (a: string, b: string) => Array<Diff>;
export default diffStrings;
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.default = void 0;
var _diffSequences = _interopRequireDefault(require('diff-sequences'));
var _cleanupSemantic = require('./cleanupSemantic');
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {default: obj};
}
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const diffStrings = (a, b) => {
const isCommon = (aIndex, bIndex) => a[aIndex] === b[bIndex];
let aIndex = 0;
let bIndex = 0;
const diffs = [];
const foundSubsequence = (nCommon, aCommon, bCommon) => {
if (aIndex !== aCommon) {
diffs.push(
new _cleanupSemantic.Diff(
_cleanupSemantic.DIFF_DELETE,
a.slice(aIndex, aCommon)
)
);
}
if (bIndex !== bCommon) {
diffs.push(
new _cleanupSemantic.Diff(
_cleanupSemantic.DIFF_INSERT,
b.slice(bIndex, bCommon)
)
);
}
aIndex = aCommon + nCommon; // number of characters compared in a
bIndex = bCommon + nCommon; // number of characters compared in b
diffs.push(
new _cleanupSemantic.Diff(
_cleanupSemantic.DIFF_EQUAL,
b.slice(bCommon, bIndex)
)
);
};
(0, _diffSequences.default)(a.length, b.length, isCommon, foundSubsequence); // After the last common subsequence, push remaining change items.
if (aIndex !== a.length) {
diffs.push(
new _cleanupSemantic.Diff(_cleanupSemantic.DIFF_DELETE, a.slice(aIndex))
);
}
if (bIndex !== b.length) {
diffs.push(
new _cleanupSemantic.Diff(_cleanupSemantic.DIFF_INSERT, b.slice(bIndex))
);
}
return diffs;
};
var _default = diffStrings;
exports.default = _default;
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { Diff } from './cleanupSemantic';
import type { DiffOptionsColor } from './types';
declare const getAlignedDiffs: (diffs: Array<Diff>, changeColor: DiffOptionsColor) => Array<Diff>;
export default getAlignedDiffs;
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.default = void 0;
var _cleanupSemantic = require('./cleanupSemantic');
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
// Given change op and array of diffs, return concatenated string:
// * include common strings
// * include change strings which have argument op with changeColor
// * exclude change strings which have opposite op
const concatenateRelevantDiffs = (op, diffs, changeColor) =>
diffs.reduce(
(reduced, diff) =>
reduced +
(diff[0] === _cleanupSemantic.DIFF_EQUAL
? diff[1]
: diff[0] === op && diff[1].length !== 0 // empty if change is newline
? changeColor(diff[1])
: ''),
''
); // Encapsulate change lines until either a common newline or the end.
class ChangeBuffer {
// incomplete line
// complete lines
constructor(op, changeColor) {
_defineProperty(this, 'op', void 0);
_defineProperty(this, 'line', void 0);
_defineProperty(this, 'lines', void 0);
_defineProperty(this, 'changeColor', void 0);
this.op = op;
this.line = [];
this.lines = [];
this.changeColor = changeColor;
}
pushSubstring(substring) {
this.pushDiff(new _cleanupSemantic.Diff(this.op, substring));
}
pushLine() {
// Assume call only if line has at least one diff,
// therefore an empty line must have a diff which has an empty string.
// If line has multiple diffs, then assume it has a common diff,
// therefore change diffs have change color;
// otherwise then it has line color only.
this.lines.push(
this.line.length !== 1
? new _cleanupSemantic.Diff(
this.op,
concatenateRelevantDiffs(this.op, this.line, this.changeColor)
)
: this.line[0][0] === this.op
? this.line[0] // can use instance
: new _cleanupSemantic.Diff(this.op, this.line[0][1]) // was common diff
);
this.line.length = 0;
}
isLineEmpty() {
return this.line.length === 0;
} // Minor input to buffer.
pushDiff(diff) {
this.line.push(diff);
} // Main input to buffer.
align(diff) {
const string = diff[1];
if (string.includes('\n')) {
const substrings = string.split('\n');
const iLast = substrings.length - 1;
substrings.forEach((substring, i) => {
if (i < iLast) {
// The first substring completes the current change line.
// A middle substring is a change line.
this.pushSubstring(substring);
this.pushLine();
} else if (substring.length !== 0) {
// The last substring starts a change line, if it is not empty.
// Important: This non-empty condition also automatically omits
// the newline appended to the end of expected and received strings.
this.pushSubstring(substring);
}
});
} else {
// Append non-multiline string to current change line.
this.pushDiff(diff);
}
} // Output from buffer.
moveLinesTo(lines) {
if (!this.isLineEmpty()) {
this.pushLine();
}
lines.push(...this.lines);
this.lines.length = 0;
}
} // Encapsulate common and change lines.
class CommonBuffer {
constructor(deleteBuffer, insertBuffer) {
_defineProperty(this, 'deleteBuffer', void 0);
_defineProperty(this, 'insertBuffer', void 0);
_defineProperty(this, 'lines', void 0);
this.deleteBuffer = deleteBuffer;
this.insertBuffer = insertBuffer;
this.lines = [];
}
pushDiffCommonLine(diff) {
this.lines.push(diff);
}
pushDiffChangeLines(diff) {
const isDiffEmpty = diff[1].length === 0; // An empty diff string is redundant, unless a change line is empty.
if (!isDiffEmpty || this.deleteBuffer.isLineEmpty()) {
this.deleteBuffer.pushDiff(diff);
}
if (!isDiffEmpty || this.insertBuffer.isLineEmpty()) {
this.insertBuffer.pushDiff(diff);
}
}
flushChangeLines() {
this.deleteBuffer.moveLinesTo(this.lines);
this.insertBuffer.moveLinesTo(this.lines);
} // Input to buffer.
align(diff) {
const op = diff[0];
const string = diff[1];
if (string.includes('\n')) {
const substrings = string.split('\n');
const iLast = substrings.length - 1;
substrings.forEach((substring, i) => {
if (i === 0) {
const subdiff = new _cleanupSemantic.Diff(op, substring);
if (
this.deleteBuffer.isLineEmpty() &&
this.insertBuffer.isLineEmpty()
) {
// If both current change lines are empty,
// then the first substring is a common line.
this.flushChangeLines();
this.pushDiffCommonLine(subdiff);
} else {
// If either current change line is non-empty,
// then the first substring completes the change lines.
this.pushDiffChangeLines(subdiff);
this.flushChangeLines();
}
} else if (i < iLast) {
// A middle substring is a common line.
this.pushDiffCommonLine(new _cleanupSemantic.Diff(op, substring));
} else if (substring.length !== 0) {
// The last substring starts a change line, if it is not empty.
// Important: This non-empty condition also automatically omits
// the newline appended to the end of expected and received strings.
this.pushDiffChangeLines(new _cleanupSemantic.Diff(op, substring));
}
});
} else {
// Append non-multiline string to current change lines.
// Important: It cannot be at the end following empty change lines,
// because newline appended to the end of expected and received strings.
this.pushDiffChangeLines(diff);
}
} // Output from buffer.
getLines() {
this.flushChangeLines();
return this.lines;
}
} // Given diffs from expected and received strings,
// return new array of diffs split or joined into lines.
//
// To correctly align a change line at the end, the algorithm:
// * assumes that a newline was appended to the strings
// * omits the last newline from the output array
//
// Assume the function is not called:
// * if either expected or received is empty string
// * if neither expected nor received is multiline string
const getAlignedDiffs = (diffs, changeColor) => {
const deleteBuffer = new ChangeBuffer(
_cleanupSemantic.DIFF_DELETE,
changeColor
);
const insertBuffer = new ChangeBuffer(
_cleanupSemantic.DIFF_INSERT,
changeColor
);
const commonBuffer = new CommonBuffer(deleteBuffer, insertBuffer);
diffs.forEach(diff => {
switch (diff[0]) {
case _cleanupSemantic.DIFF_DELETE:
deleteBuffer.align(diff);
break;
case _cleanupSemantic.DIFF_INSERT:
insertBuffer.align(diff);
break;
default:
commonBuffer.align(diff);
}
});
return commonBuffer.getLines();
};
var _default = getAlignedDiffs;
exports.default = _default;
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff } from './cleanupSemantic';
import { diffLinesRaw, diffLinesUnified, diffLinesUnified2 } from './diffLines';
import { diffStringsRaw, diffStringsUnified } from './printDiffs';
import type { DiffOptions } from './types';
export type { DiffOptions, DiffOptionsColor } from './types';
export { diffLinesRaw, diffLinesUnified, diffLinesUnified2 };
export { diffStringsRaw, diffStringsUnified };
export { DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff };
declare function diff(a: any, b: any, options?: DiffOptions): string | null;
export default diff;
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
Object.defineProperty(exports, 'DIFF_DELETE', {
enumerable: true,
get: function () {
return _cleanupSemantic.DIFF_DELETE;
}
});
Object.defineProperty(exports, 'DIFF_EQUAL', {
enumerable: true,
get: function () {
return _cleanupSemantic.DIFF_EQUAL;
}
});
Object.defineProperty(exports, 'DIFF_INSERT', {
enumerable: true,
get: function () {
return _cleanupSemantic.DIFF_INSERT;
}
});
Object.defineProperty(exports, 'Diff', {
enumerable: true,
get: function () {
return _cleanupSemantic.Diff;
}
});
Object.defineProperty(exports, 'diffLinesRaw', {
enumerable: true,
get: function () {
return _diffLines.diffLinesRaw;
}
});
Object.defineProperty(exports, 'diffLinesUnified', {
enumerable: true,
get: function () {
return _diffLines.diffLinesUnified;
}
});
Object.defineProperty(exports, 'diffLinesUnified2', {
enumerable: true,
get: function () {
return _diffLines.diffLinesUnified2;
}
});
Object.defineProperty(exports, 'diffStringsRaw', {
enumerable: true,
get: function () {
return _printDiffs.diffStringsRaw;
}
});
Object.defineProperty(exports, 'diffStringsUnified', {
enumerable: true,
get: function () {
return _printDiffs.diffStringsUnified;
}
});
exports.default = void 0;
var _chalk = _interopRequireDefault(require('chalk'));
var _jestGetType = _interopRequireDefault(require('jest-get-type'));
var _prettyFormat = _interopRequireDefault(require('pretty-format'));
var _cleanupSemantic = require('./cleanupSemantic');
var _constants = require('./constants');
var _diffLines = require('./diffLines');
var _normalizeDiffOptions = require('./normalizeDiffOptions');
var _printDiffs = require('./printDiffs');
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {default: obj};
}
var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
const getCommonMessage = (message, options) => {
const {commonColor} = (0, _normalizeDiffOptions.normalizeDiffOptions)(
options
);
return commonColor(message);
};
const {
AsymmetricMatcher,
DOMCollection,
DOMElement,
Immutable,
ReactElement,
ReactTestComponent
} = _prettyFormat.default.plugins;
const PLUGINS = [
ReactTestComponent,
ReactElement,
DOMElement,
DOMCollection,
Immutable,
AsymmetricMatcher
];
const FORMAT_OPTIONS = {
plugins: PLUGINS
};
const FORMAT_OPTIONS_0 = {...FORMAT_OPTIONS, indent: 0};
const FALLBACK_FORMAT_OPTIONS = {
callToJSON: false,
maxDepth: 10,
plugins: PLUGINS
};
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)
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
function diff(a, b, options) {
if (Object.is(a, b)) {
return getCommonMessage(_constants.NO_DIFF_MESSAGE, options);
}
const aType = (0, _jestGetType.default)(a);
let expectedType = aType;
let omitDifference = false;
if (aType === 'object' && typeof a.asymmetricMatch === 'function') {
if (a.$$typeof !== Symbol.for('jest.asymmetricMatcher')) {
// Do not know expected type of user-defined asymmetric matcher.
return null;
}
if (typeof a.getExpectedType !== 'function') {
// For example, expect.anything() matches either null or undefined
return null;
}
expectedType = a.getExpectedType(); // Primitive types boolean and number omit difference below.
// For example, omit difference for expect.stringMatching(regexp)
omitDifference = expectedType === 'string';
}
if (expectedType !== (0, _jestGetType.default)(b)) {
return (
' Comparing two different types of values.' +
` Expected ${_chalk.default.green(expectedType)} but ` +
`received ${_chalk.default.red((0, _jestGetType.default)(b))}.`
);
}
if (omitDifference) {
return null;
}
switch (aType) {
case 'string':
return (0, _diffLines.diffLinesUnified)(
a.split('\n'),
b.split('\n'),
options
);
case 'boolean':
case 'number':
return comparePrimitive(a, b, options);
case 'map':
return compareObjects(sortMap(a), sortMap(b), options);
case 'set':
return compareObjects(sortSet(a), sortSet(b), options);
default:
return compareObjects(a, b, options);
}
}
function comparePrimitive(a, b, options) {
const aFormat = (0, _prettyFormat.default)(a, FORMAT_OPTIONS);
const bFormat = (0, _prettyFormat.default)(b, FORMAT_OPTIONS);
return aFormat === bFormat
? getCommonMessage(_constants.NO_DIFF_MESSAGE, options)
: (0, _diffLines.diffLinesUnified)(
aFormat.split('\n'),
bFormat.split('\n'),
options
);
}
function sortMap(map) {
return new Map(Array.from(map.entries()).sort());
}
function sortSet(set) {
return new Set(Array.from(set.values()).sort());
}
function compareObjects(a, b, options) {
let difference;
let hasThrown = false;
const noDiffMessage = getCommonMessage(_constants.NO_DIFF_MESSAGE, options);
try {
const aCompare = (0, _prettyFormat.default)(a, FORMAT_OPTIONS_0);
const bCompare = (0, _prettyFormat.default)(b, FORMAT_OPTIONS_0);
if (aCompare === bCompare) {
difference = noDiffMessage;
} else {
const aDisplay = (0, _prettyFormat.default)(a, FORMAT_OPTIONS);
const bDisplay = (0, _prettyFormat.default)(b, FORMAT_OPTIONS);
difference = (0, _diffLines.diffLinesUnified2)(
aDisplay.split('\n'),
bDisplay.split('\n'),
aCompare.split('\n'),
bCompare.split('\n'),
options
);
}
} catch {
hasThrown = true;
} // If the comparison yields no results, compare again but this time
// without calling `toJSON`. It's also possible that toJSON might throw.
if (difference === undefined || difference === noDiffMessage) {
const aCompare = (0, _prettyFormat.default)(a, FALLBACK_FORMAT_OPTIONS_0);
const bCompare = (0, _prettyFormat.default)(b, FALLBACK_FORMAT_OPTIONS_0);
if (aCompare === bCompare) {
difference = noDiffMessage;
} else {
const aDisplay = (0, _prettyFormat.default)(a, FALLBACK_FORMAT_OPTIONS);
const bDisplay = (0, _prettyFormat.default)(b, FALLBACK_FORMAT_OPTIONS);
difference = (0, _diffLines.diffLinesUnified2)(
aDisplay.split('\n'),
bDisplay.split('\n'),
aCompare.split('\n'),
bCompare.split('\n'),
options
);
}
if (difference !== noDiffMessage && !hasThrown) {
difference =
getCommonMessage(_constants.SIMILAR_MESSAGE, options) +
'\n\n' +
difference;
}
}
return difference;
}
var _default = diff;
exports.default = _default;
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { Diff } from './cleanupSemantic';
import type { DiffOptionsNormalized } from './types';
export declare const joinAlignedDiffsNoExpand: (diffs: Array<Diff>, options: DiffOptionsNormalized) => string;
export declare const joinAlignedDiffsExpand: (diffs: Array<Diff>, options: DiffOptionsNormalized) => string;
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.joinAlignedDiffsExpand = exports.joinAlignedDiffsNoExpand = void 0;
var _cleanupSemantic = require('./cleanupSemantic');
var _printDiffs = require('./printDiffs');
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// jest --no-expand
//
// Given array of aligned strings with inverse highlight formatting,
// return joined lines with diff formatting (and patch marks, if needed).
const joinAlignedDiffsNoExpand = (diffs, options) => {
const iLength = diffs.length;
const nContextLines = options.contextLines;
const nContextLines2 = nContextLines + nContextLines; // First pass: count output lines and see if it has patches.
let jLength = iLength;
let hasExcessAtStartOrEnd = false;
let nExcessesBetweenChanges = 0;
let i = 0;
while (i !== iLength) {
const iStart = i;
while (i !== iLength && diffs[i][0] === _cleanupSemantic.DIFF_EQUAL) {
i += 1;
}
if (iStart !== i) {
if (iStart === 0) {
// at start
if (i > nContextLines) {
jLength -= i - nContextLines; // subtract excess common lines
hasExcessAtStartOrEnd = true;
}
} else if (i === iLength) {
// at end
const n = i - iStart;
if (n > nContextLines) {
jLength -= n - nContextLines; // subtract excess common lines
hasExcessAtStartOrEnd = true;
}
} else {
// between changes
const n = i - iStart;
if (n > nContextLines2) {
jLength -= n - nContextLines2; // subtract excess common lines
nExcessesBetweenChanges += 1;
}
}
}
while (i !== iLength && diffs[i][0] !== _cleanupSemantic.DIFF_EQUAL) {
i += 1;
}
}
const hasPatch = nExcessesBetweenChanges !== 0 || hasExcessAtStartOrEnd;
if (nExcessesBetweenChanges !== 0) {
jLength += nExcessesBetweenChanges + 1; // add patch lines
} else if (hasExcessAtStartOrEnd) {
jLength += 1; // add patch line
}
const jLast = jLength - 1;
const lines = [];
let jPatchMark = 0; // index of placeholder line for current patch mark
if (hasPatch) {
lines.push(''); // placeholder line for first patch mark
} // Indexes of expected or received lines in current patch:
let aStart = 0;
let bStart = 0;
let aEnd = 0;
let bEnd = 0;
const pushCommonLine = line => {
const j = lines.length;
lines.push(
(0, _printDiffs.printCommonLine)(line, j === 0 || j === jLast, options)
);
aEnd += 1;
bEnd += 1;
};
const pushDeleteLine = line => {
const j = lines.length;
lines.push(
(0, _printDiffs.printDeleteLine)(line, j === 0 || j === jLast, options)
);
aEnd += 1;
};
const pushInsertLine = line => {
const j = lines.length;
lines.push(
(0, _printDiffs.printInsertLine)(line, j === 0 || j === jLast, options)
);
bEnd += 1;
}; // Second pass: push lines with diff formatting (and patch marks, if needed).
i = 0;
while (i !== iLength) {
let iStart = i;
while (i !== iLength && diffs[i][0] === _cleanupSemantic.DIFF_EQUAL) {
i += 1;
}
if (iStart !== i) {
if (iStart === 0) {
// at beginning
if (i > nContextLines) {
iStart = i - nContextLines;
aStart = iStart;
bStart = iStart;
aEnd = aStart;
bEnd = bStart;
}
for (let iCommon = iStart; iCommon !== i; iCommon += 1) {
pushCommonLine(diffs[iCommon][1]);
}
} else if (i === iLength) {
// at end
const iEnd = i - iStart > nContextLines ? iStart + nContextLines : i;
for (let iCommon = iStart; iCommon !== iEnd; iCommon += 1) {
pushCommonLine(diffs[iCommon][1]);
}
} else {
// between changes
const nCommon = i - iStart;
if (nCommon > nContextLines2) {
const iEnd = iStart + nContextLines;
for (let iCommon = iStart; iCommon !== iEnd; iCommon += 1) {
pushCommonLine(diffs[iCommon][1]);
}
lines[jPatchMark] = (0, _printDiffs.createPatchMark)(
aStart,
aEnd,
bStart,
bEnd,
options
);
jPatchMark = lines.length;
lines.push(''); // placeholder line for next patch mark
const nOmit = nCommon - nContextLines2;
aStart = aEnd + nOmit;
bStart = bEnd + nOmit;
aEnd = aStart;
bEnd = bStart;
for (let iCommon = i - nContextLines; iCommon !== i; iCommon += 1) {
pushCommonLine(diffs[iCommon][1]);
}
} else {
for (let iCommon = iStart; iCommon !== i; iCommon += 1) {
pushCommonLine(diffs[iCommon][1]);
}
}
}
}
while (i !== iLength && diffs[i][0] === _cleanupSemantic.DIFF_DELETE) {
pushDeleteLine(diffs[i][1]);
i += 1;
}
while (i !== iLength && diffs[i][0] === _cleanupSemantic.DIFF_INSERT) {
pushInsertLine(diffs[i][1]);
i += 1;
}
}
if (hasPatch) {
lines[jPatchMark] = (0, _printDiffs.createPatchMark)(
aStart,
aEnd,
bStart,
bEnd,
options
);
}
return lines.join('\n');
}; // jest --expand
//
// Given array of aligned strings with inverse highlight formatting,
// return joined lines with diff formatting.
exports.joinAlignedDiffsNoExpand = joinAlignedDiffsNoExpand;
const joinAlignedDiffsExpand = (diffs, options) =>
diffs
.map((diff, i, diffs) => {
const line = diff[1];
const isFirstOrLast = i === 0 || i === diffs.length - 1;
switch (diff[0]) {
case _cleanupSemantic.DIFF_DELETE:
return (0, _printDiffs.printDeleteLine)(line, isFirstOrLast, options);
case _cleanupSemantic.DIFF_INSERT:
return (0, _printDiffs.printInsertLine)(line, isFirstOrLast, options);
default:
return (0, _printDiffs.printCommonLine)(line, isFirstOrLast, options);
}
})
.join('\n');
exports.joinAlignedDiffsExpand = joinAlignedDiffsExpand;
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import type { DiffOptions, DiffOptionsNormalized } from './types';
export declare const noColor: (string: string) => string;
export declare const normalizeDiffOptions: (options?: DiffOptions) => DiffOptionsNormalized;
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.normalizeDiffOptions = exports.noColor = void 0;
var _chalk = _interopRequireDefault(require('chalk'));
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {default: obj};
}
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const noColor = string => string;
exports.noColor = noColor;
const DIFF_CONTEXT_DEFAULT = 5;
const OPTIONS_DEFAULT = {
aAnnotation: 'Expected',
aColor: _chalk.default.green,
aIndicator: '-',
bAnnotation: 'Received',
bColor: _chalk.default.red,
bIndicator: '+',
changeColor: _chalk.default.inverse,
changeLineTrailingSpaceColor: noColor,
commonColor: _chalk.default.dim,
commonIndicator: ' ',
commonLineTrailingSpaceColor: noColor,
contextLines: DIFF_CONTEXT_DEFAULT,
emptyFirstOrLastLinePlaceholder: '',
expand: true,
includeChangeCounts: false,
omitAnnotationLines: false,
patchColor: _chalk.default.yellow
};
const getContextLines = contextLines =>
typeof contextLines === 'number' &&
Number.isSafeInteger(contextLines) &&
contextLines >= 0
? contextLines
: DIFF_CONTEXT_DEFAULT; // Pure function returns options with all properties.
const normalizeDiffOptions = (options = {}) => ({
...OPTIONS_DEFAULT,
...options,
contextLines: getContextLines(options.contextLines)
});
exports.normalizeDiffOptions = normalizeDiffOptions;
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { Diff } from './cleanupSemantic';
import type { DiffOptions, DiffOptionsNormalized } from './types';
export declare const printDeleteLine: (line: string, isFirstOrLast: boolean, { aColor, aIndicator, changeLineTrailingSpaceColor, emptyFirstOrLastLinePlaceholder, }: DiffOptionsNormalized) => string;
export declare const printInsertLine: (line: string, isFirstOrLast: boolean, { bColor, bIndicator, changeLineTrailingSpaceColor, emptyFirstOrLastLinePlaceholder, }: DiffOptionsNormalized) => string;
export declare const printCommonLine: (line: string, isFirstOrLast: boolean, { commonColor, commonIndicator, commonLineTrailingSpaceColor, emptyFirstOrLastLinePlaceholder, }: DiffOptionsNormalized) => string;
export declare const hasCommonDiff: (diffs: Array<Diff>, isMultiline: boolean) => boolean;
export declare type ChangeCounts = {
a: number;
b: number;
};
export declare const countChanges: (diffs: Array<Diff>) => ChangeCounts;
export declare const printAnnotation: ({ aAnnotation, aColor, aIndicator, bAnnotation, bColor, bIndicator, includeChangeCounts, omitAnnotationLines, }: DiffOptionsNormalized, changeCounts: ChangeCounts) => string;
export declare const printDiffLines: (diffs: Array<Diff>, options: DiffOptionsNormalized) => string;
export declare const createPatchMark: (aStart: number, aEnd: number, bStart: number, bEnd: number, { patchColor }: DiffOptionsNormalized) => string;
export declare const diffStringsUnified: (a: string, b: string, options?: DiffOptions | undefined) => string;
export declare const diffStringsRaw: (a: string, b: string, cleanup: boolean) => Array<Diff>;
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.diffStringsRaw = exports.diffStringsUnified = exports.createPatchMark = exports.printDiffLines = exports.printAnnotation = exports.countChanges = exports.hasCommonDiff = exports.printCommonLine = exports.printInsertLine = exports.printDeleteLine = void 0;
var _cleanupSemantic = require('./cleanupSemantic');
var _diffLines = require('./diffLines');
var _diffStrings = _interopRequireDefault(require('./diffStrings'));
var _getAlignedDiffs = _interopRequireDefault(require('./getAlignedDiffs'));
var _joinAlignedDiffs = require('./joinAlignedDiffs');
var _normalizeDiffOptions = require('./normalizeDiffOptions');
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {default: obj};
}
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const formatTrailingSpaces = (line, trailingSpaceFormatter) =>
line.replace(/\s+$/, match => trailingSpaceFormatter(match));
const printDiffLine = (
line,
isFirstOrLast,
color,
indicator,
trailingSpaceFormatter,
emptyFirstOrLastLinePlaceholder
) =>
line.length !== 0
? color(
indicator + ' ' + formatTrailingSpaces(line, trailingSpaceFormatter)
)
: indicator !== ' '
? color(indicator)
: isFirstOrLast && emptyFirstOrLastLinePlaceholder.length !== 0
? color(indicator + ' ' + emptyFirstOrLastLinePlaceholder)
: '';
const printDeleteLine = (
line,
isFirstOrLast,
{
aColor,
aIndicator,
changeLineTrailingSpaceColor,
emptyFirstOrLastLinePlaceholder
}
) =>
printDiffLine(
line,
isFirstOrLast,
aColor,
aIndicator,
changeLineTrailingSpaceColor,
emptyFirstOrLastLinePlaceholder
);
exports.printDeleteLine = printDeleteLine;
const printInsertLine = (
line,
isFirstOrLast,
{
bColor,
bIndicator,
changeLineTrailingSpaceColor,
emptyFirstOrLastLinePlaceholder
}
) =>
printDiffLine(
line,
isFirstOrLast,
bColor,
bIndicator,
changeLineTrailingSpaceColor,
emptyFirstOrLastLinePlaceholder
);
exports.printInsertLine = printInsertLine;
const printCommonLine = (
line,
isFirstOrLast,
{
commonColor,
commonIndicator,
commonLineTrailingSpaceColor,
emptyFirstOrLastLinePlaceholder
}
) =>
printDiffLine(
line,
isFirstOrLast,
commonColor,
commonIndicator,
commonLineTrailingSpaceColor,
emptyFirstOrLastLinePlaceholder
);
exports.printCommonLine = printCommonLine;
const hasCommonDiff = (diffs, isMultiline) => {
if (isMultiline) {
// Important: Ignore common newline that was appended to multiline strings!
const iLast = diffs.length - 1;
return diffs.some(
(diff, i) =>
diff[0] === _cleanupSemantic.DIFF_EQUAL &&
(i !== iLast || diff[1] !== '\n')
);
}
return diffs.some(diff => diff[0] === _cleanupSemantic.DIFF_EQUAL);
};
exports.hasCommonDiff = hasCommonDiff;
const countChanges = diffs => {
let a = 0;
let b = 0;
diffs.forEach(diff => {
switch (diff[0]) {
case _cleanupSemantic.DIFF_DELETE:
a += 1;
break;
case _cleanupSemantic.DIFF_INSERT:
b += 1;
break;
}
});
return {
a,
b
};
};
exports.countChanges = countChanges;
const printAnnotation = (
{
aAnnotation,
aColor,
aIndicator,
bAnnotation,
bColor,
bIndicator,
includeChangeCounts,
omitAnnotationLines
},
changeCounts
) => {
if (omitAnnotationLines) {
return '';
}
let aRest = '';
let bRest = '';
if (includeChangeCounts) {
const aCount = String(changeCounts.a);
const bCount = String(changeCounts.b); // Padding right aligns the ends of the annotations.
const baAnnotationLengthDiff = bAnnotation.length - aAnnotation.length;
const aAnnotationPadding = ' '.repeat(Math.max(0, baAnnotationLengthDiff));
const bAnnotationPadding = ' '.repeat(Math.max(0, -baAnnotationLengthDiff)); // Padding left aligns the ends of the counts.
const baCountLengthDiff = bCount.length - aCount.length;
const aCountPadding = ' '.repeat(Math.max(0, baCountLengthDiff));
const bCountPadding = ' '.repeat(Math.max(0, -baCountLengthDiff));
aRest =
aAnnotationPadding + ' ' + aIndicator + ' ' + aCountPadding + aCount;
bRest =
bAnnotationPadding + ' ' + bIndicator + ' ' + bCountPadding + bCount;
}
return (
aColor(aIndicator + ' ' + aAnnotation + aRest) +
'\n' +
bColor(bIndicator + ' ' + bAnnotation + bRest) +
'\n\n'
);
};
exports.printAnnotation = printAnnotation;
const printDiffLines = (diffs, options) =>
printAnnotation(options, countChanges(diffs)) +
(options.expand
? (0, _joinAlignedDiffs.joinAlignedDiffsExpand)(diffs, options)
: (0, _joinAlignedDiffs.joinAlignedDiffsNoExpand)(diffs, options)); // In GNU diff format, indexes are one-based instead of zero-based.
exports.printDiffLines = printDiffLines;
const createPatchMark = (aStart, aEnd, bStart, bEnd, {patchColor}) =>
patchColor(
`@@ -${aStart + 1},${aEnd - aStart} +${bStart + 1},${bEnd - bStart} @@`
); // Compare two strings character-by-character.
// Format as comparison lines in which changed substrings have inverse colors.
exports.createPatchMark = createPatchMark;
const diffStringsUnified = (a, b, options) => {
if (a !== b && a.length !== 0 && b.length !== 0) {
const isMultiline = a.includes('\n') || b.includes('\n'); // getAlignedDiffs assumes that a newline was appended to the strings.
const diffs = diffStringsRaw(
isMultiline ? a + '\n' : a,
isMultiline ? b + '\n' : b,
true // cleanupSemantic
);
if (hasCommonDiff(diffs, isMultiline)) {
const optionsNormalized = (0, _normalizeDiffOptions.normalizeDiffOptions)(
options
);
const lines = (0, _getAlignedDiffs.default)(
diffs,
optionsNormalized.changeColor
);
return printDiffLines(lines, optionsNormalized);
}
} // Fall back to line-by-line diff.
return (0, _diffLines.diffLinesUnified)(
a.split('\n'),
b.split('\n'),
options
);
}; // Compare two strings character-by-character.
// Optionally clean up small common substrings, also known as chaff.
exports.diffStringsUnified = diffStringsUnified;
const diffStringsRaw = (a, b, cleanup) => {
const diffs = (0, _diffStrings.default)(a, b);
if (cleanup) {
(0, _cleanupSemantic.cleanupSemantic)(diffs); // impure function
}
return diffs;
};
exports.diffStringsRaw = diffStringsRaw;
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
export declare type DiffOptionsColor = (arg: string) => string;
export declare type DiffOptions = {
aAnnotation?: string;
aColor?: DiffOptionsColor;
aIndicator?: string;
bAnnotation?: string;
bColor?: DiffOptionsColor;
bIndicator?: string;
changeColor?: DiffOptionsColor;
changeLineTrailingSpaceColor?: DiffOptionsColor;
commonColor?: DiffOptionsColor;
commonIndicator?: string;
commonLineTrailingSpaceColor?: DiffOptionsColor;
contextLines?: number;
emptyFirstOrLastLinePlaceholder?: string;
expand?: boolean;
includeChangeCounts?: boolean;
omitAnnotationLines?: boolean;
patchColor?: DiffOptionsColor;
};
export declare type DiffOptionsNormalized = {
aAnnotation: string;
aColor: DiffOptionsColor;
aIndicator: string;
bAnnotation: string;
bColor: DiffOptionsColor;
bIndicator: string;
changeColor: DiffOptionsColor;
changeLineTrailingSpaceColor: DiffOptionsColor;
commonColor: DiffOptionsColor;
commonIndicator: string;
commonLineTrailingSpaceColor: DiffOptionsColor;
contextLines: number;
emptyFirstOrLastLinePlaceholder: string;
expand: boolean;
includeChangeCounts: boolean;
omitAnnotationLines: boolean;
patchColor: DiffOptionsColor;
};
{
"name": "jest-diff",
"version": "26.6.2",
"repository": {
"type": "git",
"url": "https://github.com/facebook/jest.git",
"directory": "packages/jest-diff"
},
"license": "MIT",
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"chalk": "^4.0.0",
"diff-sequences": "^26.6.2",
"jest-get-type": "^26.3.0",
"pretty-format": "^26.6.2"
},
"devDependencies": {
"@jest/test-utils": "^26.6.2",
"strip-ansi": "^6.0.0"
},
"engines": {
"node": ">= 10.14.2"
},
"publishConfig": {
"access": "public"
},
"gitHead": "4c46930615602cbf983fb7e8e82884c282a624d5"
}
MIT License
Copyright (c) Facebook, Inc. and its affiliates.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
declare type ValueType = 'array' | 'bigint' | 'boolean' | 'function' | 'null' | 'number' | 'object' | 'regexp' | 'map' | 'set' | 'date' | 'string' | 'symbol' | 'undefined';
declare function getType(value: unknown): ValueType;
declare namespace getType {
var isPrimitive: (value: unknown) => boolean;
}
export = getType;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment