diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-05-28 00:38:50 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-05-28 00:40:43 +0200 |
commit | 7fff4499fd915bcea3fa93b1aa8b35f4fe7a6027 (patch) | |
tree | 6de9a1aebd150a23b7f8c273ec657a5d0a18fe3e /node_modules/jest-diff | |
parent | 963b7a41feb29cc4be090a2446bdfe0c1f1bcd81 (diff) |
add linting (and some initial fixes)
Diffstat (limited to 'node_modules/jest-diff')
-rw-r--r-- | node_modules/jest-diff/.npmignore | 3 | ||||
-rw-r--r-- | node_modules/jest-diff/build/constants.js | 21 | ||||
-rw-r--r-- | node_modules/jest-diff/build/diffStrings.js | 145 | ||||
-rw-r--r-- | node_modules/jest-diff/build/index.js | 137 | ||||
-rw-r--r-- | node_modules/jest-diff/package.json | 16 |
5 files changed, 322 insertions, 0 deletions
diff --git a/node_modules/jest-diff/.npmignore b/node_modules/jest-diff/.npmignore new file mode 100644 index 000000000..85e48fe7b --- /dev/null +++ b/node_modules/jest-diff/.npmignore @@ -0,0 +1,3 @@ +**/__mocks__/** +**/__tests__/** +src diff --git a/node_modules/jest-diff/build/constants.js b/node_modules/jest-diff/build/constants.js new file mode 100644 index 000000000..cb6ae145f --- /dev/null +++ b/node_modules/jest-diff/build/constants.js @@ -0,0 +1,21 @@ +/** + * Copyright (c) 2014, 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. + * + * + */ + +'use strict'; + +const chalk = require('chalk'); + +exports.NO_DIFF_MESSAGE = +chalk.dim('Compared values have no visual difference.'); + +exports.SIMILAR_MESSAGE = +chalk.dim( +'Compared values serialize to the same structure.\n' + +'Printing internal object structure without calling `toJSON` instead.');
\ No newline at end of file diff --git a/node_modules/jest-diff/build/diffStrings.js b/node_modules/jest-diff/build/diffStrings.js new file mode 100644 index 000000000..6961ff801 --- /dev/null +++ b/node_modules/jest-diff/build/diffStrings.js @@ -0,0 +1,145 @@ +/** + * Copyright (c) 2014, 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. + * + * + */ + +'use strict'; + +const chalk = require('chalk'); +const diff = require('diff');var _require = + +require('./constants.js');const NO_DIFF_MESSAGE = _require.NO_DIFF_MESSAGE; +const DIFF_CONTEXT = 5; + + + + + + + + + + + + + + + + + +const getColor = (added, removed) => +added ? +chalk.red : +removed ? chalk.green : chalk.dim; + +const getBgColor = (added, removed) => +added ? +chalk.bgRed : +removed ? chalk.bgGreen : chalk.dim; + +const highlightTrailingWhitespace = (line, bgColor) => +line.replace(/\s+$/, bgColor('$&')); + +const getAnnotation = options => +chalk.green('- ' + (options && options.aAnnotation || 'Expected')) + '\n' + +chalk.red('+ ' + (options && options.bAnnotation || 'Received')) + '\n\n'; + +const diffLines = (a, b) => { + let isDifferent = false; + return { + diff: diff.diffLines(a, b).map(part => {const + added = part.added,removed = part.removed; + if (part.added || part.removed) { + isDifferent = true; + } + + const lines = part.value.split('\n'); + const color = getColor(added, removed); + const bgColor = getBgColor(added, removed); + + if (lines[lines.length - 1] === '') { + lines.pop(); + } + + return lines.map(line => { + const highlightedLine = highlightTrailingWhitespace(line, bgColor); + const mark = color(part.added ? '+' : part.removed ? '-' : ' '); + return mark + ' ' + color(highlightedLine) + '\n'; + }).join(''); + }).join('').trim(), + isDifferent }; + +}; + +// Only show patch marks ("@@ ... @@") if the diff is big. +// To determine this, we need to compare either the original string (a) to +// `hunk.oldLines` or a new string to `hunk.newLines`. +// If the `oldLinesCount` is greater than `hunk.oldLines` +// we can be sure that at least 1 line has been "hidden". +const shouldShowPatchMarks = +(hunk, oldLinesCount) => oldLinesCount > hunk.oldLines; + +const createPatchMark = hunk => { + const markOld = `-${hunk.oldStart},${hunk.oldLines}`; + const markNew = `+${hunk.newStart},${hunk.newLines}`; + return chalk.yellow(`@@ ${markOld} ${markNew} @@\n`); +}; + +const structuredPatch = (a, b) => { + const options = { context: DIFF_CONTEXT }; + let isDifferent = false; + // Make sure the strings end with a newline. + if (!a.endsWith('\n')) { + a += '\n'; + } + if (!b.endsWith('\n')) { + b += '\n'; + } + + const oldLinesCount = (a.match(/\n/g) || []).length; + + return { + diff: diff.structuredPatch('', '', a, b, '', '', options). + hunks.map(hunk => { + const lines = hunk.lines.map(line => { + const added = line[0] === '+'; + const removed = line[0] === '-'; + + const color = getColor(added, removed); + const bgColor = getBgColor(added, removed); + + const highlightedLine = highlightTrailingWhitespace(line, bgColor); + return color(highlightedLine) + '\n'; + }).join(''); + + isDifferent = true; + return shouldShowPatchMarks(hunk, oldLinesCount) ? + createPatchMark(hunk) + lines : + lines; + }).join('').trim(), + isDifferent }; + +}; + +function diffStrings(a, b, options) { + // `diff` uses the Myers LCS diff algorithm which runs in O(n+d^2) time + // (where "d" is the edit distance) and can get very slow for large edit + // distances. Mitigate the cost by switching to a lower-resolution diff + // whenever linebreaks are involved. + const result = options && options.expand === false ? + structuredPatch(a, b) : + diffLines(a, b); + + if (result.isDifferent) { + return getAnnotation(options) + result.diff; + } else { + return NO_DIFF_MESSAGE; + } +} + +module.exports = diffStrings;
\ No newline at end of file diff --git a/node_modules/jest-diff/build/index.js b/node_modules/jest-diff/build/index.js new file mode 100644 index 000000000..782207208 --- /dev/null +++ b/node_modules/jest-diff/build/index.js @@ -0,0 +1,137 @@ +/** + * Copyright (c) 2014, 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. + * + * + */ + +'use strict'; + + + +const ReactElementPlugin = require('pretty-format/build/plugins/ReactElement'); +const ReactTestComponentPlugin = require('pretty-format/build/plugins/ReactTestComponent'); +const AsymmetricMatcherPlugin = require('pretty-format/build/plugins/AsymmetricMatcher'); + +const chalk = require('chalk'); +const diffStrings = require('./diffStrings');var _require = +require('jest-matcher-utils');const getType = _require.getType; +const prettyFormat = require('pretty-format');var _require2 = + + + + +require('./constants');const NO_DIFF_MESSAGE = _require2.NO_DIFF_MESSAGE,SIMILAR_MESSAGE = _require2.SIMILAR_MESSAGE; + +const PLUGINS = [ +ReactTestComponentPlugin, +ReactElementPlugin, +AsymmetricMatcherPlugin]; + +const FORMAT_OPTIONS = { + plugins: PLUGINS }; + +const FALLBACK_FORMAT_OPTIONS = { + callToJSON: false, + maxDepth: 10, + plugins: PLUGINS }; + + +// 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, b, options) { + if (a === b) { + return NO_DIFF_MESSAGE; + } + + const aType = getType(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 !== getType(b)) { + return ( + ' Comparing two different types of values.' + + ` Expected ${chalk.green(expectedType)} but ` + + `received ${chalk.red(getType(b))}.`); + + } + + if (omitDifference) { + return null; + } + + switch (aType) { + case 'string': + const multiline = a.match(/[\r\n]/) !== -1 && b.indexOf('\n') !== -1; + if (multiline) { + return diffStrings(String(a), String(b), options); + } + return null; + case 'number': + case 'boolean': + return null; + 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 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 diffMessage; + let hasThrown = false; + + try { + diffMessage = diffStrings( + prettyFormat(a, FORMAT_OPTIONS), + prettyFormat(b, FORMAT_OPTIONS), + options); + + } catch (e) { + 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 (!diffMessage || diffMessage === NO_DIFF_MESSAGE) { + diffMessage = diffStrings( + prettyFormat(a, FALLBACK_FORMAT_OPTIONS), + prettyFormat(b, FALLBACK_FORMAT_OPTIONS), + options); + + if (diffMessage !== NO_DIFF_MESSAGE && !hasThrown) { + diffMessage = SIMILAR_MESSAGE + '\n\n' + diffMessage; + } + } + + return diffMessage; +} + +module.exports = diff;
\ No newline at end of file diff --git a/node_modules/jest-diff/package.json b/node_modules/jest-diff/package.json new file mode 100644 index 000000000..50d5c0e37 --- /dev/null +++ b/node_modules/jest-diff/package.json @@ -0,0 +1,16 @@ +{ + "name": "jest-diff", + "version": "19.0.0", + "repository": { + "type": "git", + "url": "https://github.com/facebook/jest.git" + }, + "license": "BSD-3-Clause", + "main": "build/index.js", + "dependencies": { + "chalk": "^1.1.3", + "diff": "^3.0.0", + "jest-matcher-utils": "^19.0.0", + "pretty-format": "^19.0.0" + } +} |