From c3ee8cb5d13c262d19b55c0692a9b634f4eb42cb Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 8 Apr 2022 20:45:23 +0800 Subject: [PATCH 1/8] Lint everything --- .prettierignore | 8 ++++++++ package.json | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/.prettierignore b/.prettierignore index 38dc19d98aa6..1c150f07c17e 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,3 +1,8 @@ +.idea +.DS_STORE +.eslintcache +*.swp +*~ api-extractor.json coverage @@ -9,3 +14,6 @@ coverage /website/backers.json /website/build /website/versions.json + +**/.yarn +**/.pnp.* diff --git a/package.json b/package.json index 5125d1a77316..d30215a1d125 100644 --- a/package.json +++ b/package.json @@ -100,8 +100,8 @@ "jest-jasmine-ci": "yarn jest-jasmine --color --config jest.config.ci.js", "jest-coverage": "yarn jest --coverage", "lint": "eslint . --cache --ext js,jsx,cjs,mjs,ts,tsx,md", - "lint:prettier": "prettier '**/*.{json,md,yml,yaml}' 'website/**/*.{css,js}' --write", - "lint:prettier:ci": "prettier '**/*.{json,md,yml,yaml}' 'website/**/*.{css,js}' --check", + "lint:prettier": "prettier . --write", + "lint:prettier:ci": "prettier . --check", "remove-examples": "node ./scripts/remove-examples.mjs", "test-types": "yarn jest --config jest.config.tsd.js", "test-ci-partial": "yarn test-ci-partial:parallel -i", From 8d7791115617c4e132dc8e39e6965258c743e912 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 8 Apr 2022 20:53:43 +0800 Subject: [PATCH 2/8] Run Prettier --- e2e/coverage-handlebars/greet.hbs | 12 +- examples/angular/.babelrc.js | 2 +- examples/react-native/.watchmanconfig | 2 +- packages/jest-diff/src/cleanupSemantic.ts | 231 +++++++++++++--------- 4 files changed, 143 insertions(+), 104 deletions(-) diff --git a/e2e/coverage-handlebars/greet.hbs b/e2e/coverage-handlebars/greet.hbs index 9dd3847305ae..35ae8749e348 100644 --- a/e2e/coverage-handlebars/greet.hbs +++ b/e2e/coverage-handlebars/greet.hbs @@ -5,9 +5,9 @@ * LICENSE file in the root directory of this source tree. }}

Good -{{#if am}} - morning -{{else}} - evening -{{/if}} -{{name}}!

+ {{#if am}} + morning + {{else}} + evening + {{/if}} + {{name}}!

\ No newline at end of file diff --git a/examples/angular/.babelrc.js b/examples/angular/.babelrc.js index 7a1d6c383636..118538499498 100644 --- a/examples/angular/.babelrc.js +++ b/examples/angular/.babelrc.js @@ -19,5 +19,5 @@ module.exports = { }, ], '@babel/preset-typescript', - ] + ], }; diff --git a/examples/react-native/.watchmanconfig b/examples/react-native/.watchmanconfig index 9e26dfeeb6e6..0967ef424bce 100644 --- a/examples/react-native/.watchmanconfig +++ b/examples/react-native/.watchmanconfig @@ -1 +1 @@ -{} \ No newline at end of file +{} diff --git a/packages/jest-diff/src/cleanupSemantic.ts b/packages/jest-diff/src/cleanupSemantic.ts index 0df74e0bc895..13a5edfe3bba 100644 --- a/packages/jest-diff/src/cleanupSemantic.ts +++ b/packages/jest-diff/src/cleanupSemantic.ts @@ -58,7 +58,6 @@ class Diff { } } - /** * Determine the common prefix of two strings. * @param {string} text1 First string. @@ -66,7 +65,7 @@ class Diff { * @return {number} The number of characters common to the start of each * string. */ -var diff_commonPrefix = function(text1: string, text2: string): number { +var diff_commonPrefix = function (text1: string, text2: string): number { // Quick check for common null cases. if (!text1 || !text2 || text1.charAt(0) != text2.charAt(0)) { return 0; @@ -78,8 +77,10 @@ var diff_commonPrefix = function(text1: string, text2: string): number { var pointermid = pointermax; var pointerstart = 0; while (pointermin < pointermid) { - if (text1.substring(pointerstart, pointermid) == - text2.substring(pointerstart, pointermid)) { + if ( + text1.substring(pointerstart, pointermid) == + text2.substring(pointerstart, pointermid) + ) { pointermin = pointermid; pointerstart = pointermin; } else { @@ -90,17 +91,19 @@ var diff_commonPrefix = function(text1: string, text2: string): number { return pointermid; }; - /** * Determine the common suffix of two strings. * @param {string} text1 First string. * @param {string} text2 Second string. * @return {number} The number of characters common to the end of each string. */ -var diff_commonSuffix = function(text1: string, text2: string): number { +var diff_commonSuffix = function (text1: string, text2: string): number { // Quick check for common null cases. - if (!text1 || !text2 || - text1.charAt(text1.length - 1) != text2.charAt(text2.length - 1)) { + if ( + !text1 || + !text2 || + text1.charAt(text1.length - 1) != text2.charAt(text2.length - 1) + ) { return 0; } // Binary search. @@ -110,8 +113,10 @@ var diff_commonSuffix = function(text1: string, text2: string): number { var pointermid = pointermax; var pointerend = 0; while (pointermin < pointermid) { - if (text1.substring(text1.length - pointermid, text1.length - pointerend) == - text2.substring(text2.length - pointermid, text2.length - pointerend)) { + if ( + text1.substring(text1.length - pointermid, text1.length - pointerend) == + text2.substring(text2.length - pointermid, text2.length - pointerend) + ) { pointermin = pointermid; pointerend = pointermin; } else { @@ -122,7 +127,6 @@ var diff_commonSuffix = function(text1: string, text2: string): number { return pointermid; }; - /** * Determine if the suffix of one string is the prefix of another. * @param {string} text1 First string. @@ -131,7 +135,7 @@ var diff_commonSuffix = function(text1: string, text2: string): number { * string and the start of the second string. * @private */ -var diff_commonOverlap_ = function(text1: string, text2: string): number { +var diff_commonOverlap_ = function (text1: string, text2: string): number { // Cache the text lengths to prevent multiple calls. var text1_length = text1.length; var text2_length = text2.length; @@ -163,27 +167,28 @@ var diff_commonOverlap_ = function(text1: string, text2: string): number { return best; } length += found; - if (found == 0 || text1.substring(text_length - length) == - text2.substring(0, length)) { + if ( + found == 0 || + text1.substring(text_length - length) == text2.substring(0, length) + ) { best = length; length++; } } }; - /** * Reduce the number of edits by eliminating semantically trivial equalities. * @param {!Array.} diffs Array of diff tuples. */ - var diff_cleanupSemantic = function(diffs: Array) { +var diff_cleanupSemantic = function (diffs: Array) { var changes = false; - var equalities = []; // Stack of indices where equalities are found. - var equalitiesLength = 0; // Keeping our own length var is faster in JS. + var equalities = []; // Stack of indices where equalities are found. + var equalitiesLength = 0; // Keeping our own length var is faster in JS. /** @type {?string} */ var lastEquality = null; // Always equal to diffs[equalities[equalitiesLength - 1]][1] - var pointer = 0; // Index of current position. + var pointer = 0; // Index of current position. // Number of characters that changed prior to the equality. var length_insertions1 = 0; var length_deletions1 = 0; @@ -191,14 +196,16 @@ var diff_commonOverlap_ = function(text1: string, text2: string): number { var length_insertions2 = 0; var length_deletions2 = 0; while (pointer < diffs.length) { - if (diffs[pointer][0] == DIFF_EQUAL) { // Equality found. + if (diffs[pointer][0] == DIFF_EQUAL) { + // Equality found. equalities[equalitiesLength++] = pointer; length_insertions1 = length_insertions2; length_deletions1 = length_deletions2; length_insertions2 = 0; length_deletions2 = 0; lastEquality = diffs[pointer][1]; - } else { // An insertion or deletion. + } else { + // An insertion or deletion. if (diffs[pointer][0] == DIFF_INSERT) { length_insertions2 += diffs[pointer][1].length; } else { @@ -206,13 +213,18 @@ var diff_commonOverlap_ = function(text1: string, text2: string): number { } // Eliminate an equality that is smaller or equal to the edits on both // sides of it. - if (lastEquality && (lastEquality.length <= - Math.max(length_insertions1, length_deletions1)) && - (lastEquality.length <= Math.max(length_insertions2, - length_deletions2))) { + if ( + lastEquality && + lastEquality.length <= + Math.max(length_insertions1, length_deletions1) && + lastEquality.length <= Math.max(length_insertions2, length_deletions2) + ) { // Duplicate record. - diffs.splice(equalities[equalitiesLength - 1], 0, - new Diff(DIFF_DELETE, lastEquality)); + diffs.splice( + equalities[equalitiesLength - 1], + 0, + new Diff(DIFF_DELETE, lastEquality), + ); // Change second copy to insert. diffs[equalities[equalitiesLength - 1] + 1][0] = DIFF_INSERT; // Throw away the equality we just deleted. @@ -220,7 +232,7 @@ var diff_commonOverlap_ = function(text1: string, text2: string): number { // Throw away the previous equality (it needs to be reevaluated). equalitiesLength--; pointer = equalitiesLength > 0 ? equalities[equalitiesLength - 1] : -1; - length_insertions1 = 0; // Reset the counters. + length_insertions1 = 0; // Reset the counters. length_deletions1 = 0; length_insertions2 = 0; length_deletions2 = 0; @@ -245,36 +257,51 @@ var diff_commonOverlap_ = function(text1: string, text2: string): number { // Only extract an overlap if it is as big as the edit ahead or behind it. pointer = 1; while (pointer < diffs.length) { - if (diffs[pointer - 1][0] == DIFF_DELETE && - diffs[pointer][0] == DIFF_INSERT) { + if ( + diffs[pointer - 1][0] == DIFF_DELETE && + diffs[pointer][0] == DIFF_INSERT + ) { var deletion = diffs[pointer - 1][1]; var insertion = diffs[pointer][1]; var overlap_length1 = diff_commonOverlap_(deletion, insertion); var overlap_length2 = diff_commonOverlap_(insertion, deletion); if (overlap_length1 >= overlap_length2) { - if (overlap_length1 >= deletion.length / 2 || - overlap_length1 >= insertion.length / 2) { + if ( + overlap_length1 >= deletion.length / 2 || + overlap_length1 >= insertion.length / 2 + ) { // Overlap found. Insert an equality and trim the surrounding edits. - diffs.splice(pointer, 0, new Diff(DIFF_EQUAL, - insertion.substring(0, overlap_length1))); - diffs[pointer - 1][1] = - deletion.substring(0, deletion.length - overlap_length1); + diffs.splice( + pointer, + 0, + new Diff(DIFF_EQUAL, insertion.substring(0, overlap_length1)), + ); + diffs[pointer - 1][1] = deletion.substring( + 0, + deletion.length - overlap_length1, + ); diffs[pointer + 1][1] = insertion.substring(overlap_length1); pointer++; } } else { - if (overlap_length2 >= deletion.length / 2 || - overlap_length2 >= insertion.length / 2) { + if ( + overlap_length2 >= deletion.length / 2 || + overlap_length2 >= insertion.length / 2 + ) { // Reverse overlap found. // Insert an equality and swap and trim the surrounding edits. - diffs.splice(pointer, 0, new Diff(DIFF_EQUAL, - deletion.substring(0, overlap_length2))); + diffs.splice( + pointer, + 0, + new Diff(DIFF_EQUAL, deletion.substring(0, overlap_length2)), + ); diffs[pointer - 1][0] = DIFF_INSERT; - diffs[pointer - 1][1] = - insertion.substring(0, insertion.length - overlap_length2); + diffs[pointer - 1][1] = insertion.substring( + 0, + insertion.length - overlap_length2, + ); diffs[pointer + 1][0] = DIFF_DELETE; - diffs[pointer + 1][1] = - deletion.substring(overlap_length2); + diffs[pointer + 1][1] = deletion.substring(overlap_length2); pointer++; } } @@ -284,14 +311,13 @@ var diff_commonOverlap_ = function(text1: string, text2: string): number { } }; - /** * Look for single edits surrounded on both sides by equalities * which can be shifted sideways to align the edit to a word boundary. * e.g: The cat came. -> The cat came. * @param {!Array.} diffs Array of diff tuples. */ -var diff_cleanupSemanticLossless = function(diffs: Array) { +var diff_cleanupSemanticLossless = function (diffs: Array) { /** * Given two strings, compute a score representing whether the internal * boundary falls on logical boundaries. @@ -317,18 +343,12 @@ var diff_cleanupSemanticLossless = function(diffs: Array) { var char2 = two.charAt(0); var nonAlphaNumeric1 = char1.match(nonAlphaNumericRegex_); var nonAlphaNumeric2 = char2.match(nonAlphaNumericRegex_); - var whitespace1 = nonAlphaNumeric1 && - char1.match(whitespaceRegex_); - var whitespace2 = nonAlphaNumeric2 && - char2.match(whitespaceRegex_); - var lineBreak1 = whitespace1 && - char1.match(linebreakRegex_); - var lineBreak2 = whitespace2 && - char2.match(linebreakRegex_); - var blankLine1 = lineBreak1 && - one.match(blanklineEndRegex_); - var blankLine2 = lineBreak2 && - two.match(blanklineStartRegex_); + var whitespace1 = nonAlphaNumeric1 && char1.match(whitespaceRegex_); + var whitespace2 = nonAlphaNumeric2 && char2.match(whitespaceRegex_); + var lineBreak1 = whitespace1 && char1.match(linebreakRegex_); + var lineBreak2 = whitespace2 && char2.match(linebreakRegex_); + var blankLine1 = lineBreak1 && one.match(blanklineEndRegex_); + var blankLine2 = lineBreak2 && two.match(blanklineStartRegex_); if (blankLine1 || blankLine2) { // Five points for blank lines. @@ -352,8 +372,10 @@ var diff_cleanupSemanticLossless = function(diffs: Array) { var pointer = 1; // Intentionally ignore the first and last element (don't need checking). while (pointer < diffs.length - 1) { - if (diffs[pointer - 1][0] == DIFF_EQUAL && - diffs[pointer + 1][0] == DIFF_EQUAL) { + if ( + diffs[pointer - 1][0] == DIFF_EQUAL && + diffs[pointer + 1][0] == DIFF_EQUAL + ) { // This is a single edit surrounded by equalities. var equality1 = diffs[pointer - 1][1]; var edit = diffs[pointer][1]; @@ -372,14 +394,16 @@ var diff_cleanupSemanticLossless = function(diffs: Array) { var bestEquality1 = equality1; var bestEdit = edit; var bestEquality2 = equality2; - var bestScore = diff_cleanupSemanticScore_(equality1, edit) + - diff_cleanupSemanticScore_(edit, equality2); + var bestScore = + diff_cleanupSemanticScore_(equality1, edit) + + diff_cleanupSemanticScore_(edit, equality2); while (edit.charAt(0) === equality2.charAt(0)) { equality1 += edit.charAt(0); edit = edit.substring(1) + equality2.charAt(0); equality2 = equality2.substring(1); - var score = diff_cleanupSemanticScore_(equality1, edit) + - diff_cleanupSemanticScore_(edit, equality2); + var score = + diff_cleanupSemanticScore_(equality1, edit) + + diff_cleanupSemanticScore_(edit, equality2); // The >= encourages trailing rather than leading whitespace on edits. if (score >= bestScore) { bestScore = score; @@ -410,7 +434,6 @@ var diff_cleanupSemanticLossless = function(diffs: Array) { } }; - // Define some regex patterns for matching boundaries. var nonAlphaNumericRegex_ = /[^a-zA-Z0-9]/; var whitespaceRegex_ = /\s/; @@ -418,13 +441,12 @@ var linebreakRegex_ = /[\r\n]/; var blanklineEndRegex_ = /\n\r?\n$/; var blanklineStartRegex_ = /^\r?\n\r?\n/; - /** * Reorder and merge like edit sections. Merge equalities. * Any edit section can move as long as it doesn't cross an equality. * @param {!Array.} diffs Array of diff tuples. */ -var diff_cleanupMerge = function(diffs: Array) { +var diff_cleanupMerge = function (diffs: Array) { // Add a dummy entry at the end. diffs.push(new Diff(DIFF_EQUAL, '')); var pointer = 0; @@ -452,14 +474,19 @@ var diff_cleanupMerge = function(diffs: Array) { // Factor out any common prefixies. commonlength = diff_commonPrefix(text_insert, text_delete); if (commonlength !== 0) { - if ((pointer - count_delete - count_insert) > 0 && - diffs[pointer - count_delete - count_insert - 1][0] == - DIFF_EQUAL) { + if ( + pointer - count_delete - count_insert > 0 && + diffs[pointer - count_delete - count_insert - 1][0] == + DIFF_EQUAL + ) { diffs[pointer - count_delete - count_insert - 1][1] += - text_insert.substring(0, commonlength); + text_insert.substring(0, commonlength); } else { - diffs.splice(0, 0, new Diff(DIFF_EQUAL, - text_insert.substring(0, commonlength))); + diffs.splice( + 0, + 0, + new Diff(DIFF_EQUAL, text_insert.substring(0, commonlength)), + ); pointer++; } text_insert = text_insert.substring(commonlength); @@ -468,25 +495,28 @@ var diff_cleanupMerge = function(diffs: Array) { // Factor out any common suffixies. commonlength = diff_commonSuffix(text_insert, text_delete); if (commonlength !== 0) { - diffs[pointer][1] = text_insert.substring(text_insert.length - - commonlength) + diffs[pointer][1]; - text_insert = text_insert.substring(0, text_insert.length - - commonlength); - text_delete = text_delete.substring(0, text_delete.length - - commonlength); + diffs[pointer][1] = + text_insert.substring(text_insert.length - commonlength) + + diffs[pointer][1]; + text_insert = text_insert.substring( + 0, + text_insert.length - commonlength, + ); + text_delete = text_delete.substring( + 0, + text_delete.length - commonlength, + ); } } // Delete the offending records and add the merged ones. pointer -= count_delete + count_insert; diffs.splice(pointer, count_delete + count_insert); if (text_delete.length) { - diffs.splice(pointer, 0, - new Diff(DIFF_DELETE, text_delete)); + diffs.splice(pointer, 0, new Diff(DIFF_DELETE, text_delete)); pointer++; } if (text_insert.length) { - diffs.splice(pointer, 0, - new Diff(DIFF_INSERT, text_insert)); + diffs.splice(pointer, 0, new Diff(DIFF_INSERT, text_insert)); pointer++; } pointer++; @@ -505,7 +535,7 @@ var diff_cleanupMerge = function(diffs: Array) { } } if (diffs[diffs.length - 1][1] === '') { - diffs.pop(); // Remove the dummy entry at the end. + diffs.pop(); // Remove the dummy entry at the end. } // Second pass: look for single edits surrounded on both sides by equalities @@ -515,25 +545,35 @@ var diff_cleanupMerge = function(diffs: Array) { pointer = 1; // Intentionally ignore the first and last element (don't need checking). while (pointer < diffs.length - 1) { - if (diffs[pointer - 1][0] == DIFF_EQUAL && - diffs[pointer + 1][0] == DIFF_EQUAL) { + if ( + diffs[pointer - 1][0] == DIFF_EQUAL && + diffs[pointer + 1][0] == DIFF_EQUAL + ) { // This is a single edit surrounded by equalities. - if (diffs[pointer][1].substring(diffs[pointer][1].length - - diffs[pointer - 1][1].length) == diffs[pointer - 1][1]) { + if ( + diffs[pointer][1].substring( + diffs[pointer][1].length - diffs[pointer - 1][1].length, + ) == diffs[pointer - 1][1] + ) { // Shift the edit over the previous equality. - diffs[pointer][1] = diffs[pointer - 1][1] + - diffs[pointer][1].substring(0, diffs[pointer][1].length - - diffs[pointer - 1][1].length); + diffs[pointer][1] = + diffs[pointer - 1][1] + + diffs[pointer][1].substring( + 0, + diffs[pointer][1].length - diffs[pointer - 1][1].length, + ); diffs[pointer + 1][1] = diffs[pointer - 1][1] + diffs[pointer + 1][1]; diffs.splice(pointer - 1, 1); changes = true; - } else if (diffs[pointer][1].substring(0, diffs[pointer + 1][1].length) == - diffs[pointer + 1][1]) { + } else if ( + diffs[pointer][1].substring(0, diffs[pointer + 1][1].length) == + diffs[pointer + 1][1] + ) { // Shift the edit over the next equality. diffs[pointer - 1][1] += diffs[pointer + 1][1]; diffs[pointer][1] = - diffs[pointer][1].substring(diffs[pointer + 1][1].length) + - diffs[pointer + 1][1]; + diffs[pointer][1].substring(diffs[pointer + 1][1].length) + + diffs[pointer + 1][1]; diffs.splice(pointer + 1, 1); changes = true; } @@ -546,7 +586,6 @@ var diff_cleanupMerge = function(diffs: Array) { } }; - export { Diff, DIFF_EQUAL, From 635f9c9dbe2369167bd894cac738a05a9c5239e3 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 8 Apr 2022 21:55:18 +0800 Subject: [PATCH 3/8] Fix --- .eslintignore | 4 +- .prettierignore | 6 + e2e/coverage-handlebars/greet.hbs | 12 +- package.json | 4 +- packages/jest-diff/src/cleanupSemantic.ts | 231 +++++++++------------- 5 files changed, 113 insertions(+), 144 deletions(-) diff --git a/.eslintignore b/.eslintignore index 48fa766a507d..ea7899381bc6 100644 --- a/.eslintignore +++ b/.eslintignore @@ -4,7 +4,6 @@ bin/ flow-typed/** packages/*/build/** packages/*/dist/** -packages/jest-diff/src/cleanupSemantic.ts website/.docusaurus website/blog website/build @@ -12,3 +11,6 @@ website/node_modules website/i18n/*.js website/static !.eslintrc.js + +# Third-party script +packages/jest-diff/src/cleanupSemantic.ts diff --git a/.prettierignore b/.prettierignore index 1c150f07c17e..01bd581587fa 100644 --- a/.prettierignore +++ b/.prettierignore @@ -10,6 +10,12 @@ coverage /packages/jest-config/src/__tests__/jest-preset.json /packages/pretty-format/perf/world.geo.json +# Breaks tests +/e2e/coverage-handlebars/greet.hbs + +# Third-party script +packages/jest-diff/src/cleanupSemantic.ts + /website/.docusaurus /website/backers.json /website/build diff --git a/e2e/coverage-handlebars/greet.hbs b/e2e/coverage-handlebars/greet.hbs index 35ae8749e348..9dd3847305ae 100644 --- a/e2e/coverage-handlebars/greet.hbs +++ b/e2e/coverage-handlebars/greet.hbs @@ -5,9 +5,9 @@ * LICENSE file in the root directory of this source tree. }}

Good - {{#if am}} - morning - {{else}} - evening - {{/if}} - {{name}}!

\ No newline at end of file +{{#if am}} + morning +{{else}} + evening +{{/if}} +{{name}}!

diff --git a/package.json b/package.json index d30215a1d125..330a25dd7a3b 100644 --- a/package.json +++ b/package.json @@ -100,8 +100,8 @@ "jest-jasmine-ci": "yarn jest-jasmine --color --config jest.config.ci.js", "jest-coverage": "yarn jest --coverage", "lint": "eslint . --cache --ext js,jsx,cjs,mjs,ts,tsx,md", - "lint:prettier": "prettier . --write", - "lint:prettier:ci": "prettier . --check", + "lint:prettier": "prettier . \"!**/*.{js,jsx,cjs,mjs,ts,tsx,md}\" --write", + "lint:prettier:ci": "prettier . \"!**/*.{js,jsx,cjs,mjs,ts,tsx,md}\" --check", "remove-examples": "node ./scripts/remove-examples.mjs", "test-types": "yarn jest --config jest.config.tsd.js", "test-ci-partial": "yarn test-ci-partial:parallel -i", diff --git a/packages/jest-diff/src/cleanupSemantic.ts b/packages/jest-diff/src/cleanupSemantic.ts index 13a5edfe3bba..0df74e0bc895 100644 --- a/packages/jest-diff/src/cleanupSemantic.ts +++ b/packages/jest-diff/src/cleanupSemantic.ts @@ -58,6 +58,7 @@ class Diff { } } + /** * Determine the common prefix of two strings. * @param {string} text1 First string. @@ -65,7 +66,7 @@ class Diff { * @return {number} The number of characters common to the start of each * string. */ -var diff_commonPrefix = function (text1: string, text2: string): number { +var diff_commonPrefix = function(text1: string, text2: string): number { // Quick check for common null cases. if (!text1 || !text2 || text1.charAt(0) != text2.charAt(0)) { return 0; @@ -77,10 +78,8 @@ var diff_commonPrefix = function (text1: string, text2: string): number { var pointermid = pointermax; var pointerstart = 0; while (pointermin < pointermid) { - if ( - text1.substring(pointerstart, pointermid) == - text2.substring(pointerstart, pointermid) - ) { + if (text1.substring(pointerstart, pointermid) == + text2.substring(pointerstart, pointermid)) { pointermin = pointermid; pointerstart = pointermin; } else { @@ -91,19 +90,17 @@ var diff_commonPrefix = function (text1: string, text2: string): number { return pointermid; }; + /** * Determine the common suffix of two strings. * @param {string} text1 First string. * @param {string} text2 Second string. * @return {number} The number of characters common to the end of each string. */ -var diff_commonSuffix = function (text1: string, text2: string): number { +var diff_commonSuffix = function(text1: string, text2: string): number { // Quick check for common null cases. - if ( - !text1 || - !text2 || - text1.charAt(text1.length - 1) != text2.charAt(text2.length - 1) - ) { + if (!text1 || !text2 || + text1.charAt(text1.length - 1) != text2.charAt(text2.length - 1)) { return 0; } // Binary search. @@ -113,10 +110,8 @@ var diff_commonSuffix = function (text1: string, text2: string): number { var pointermid = pointermax; var pointerend = 0; while (pointermin < pointermid) { - if ( - text1.substring(text1.length - pointermid, text1.length - pointerend) == - text2.substring(text2.length - pointermid, text2.length - pointerend) - ) { + if (text1.substring(text1.length - pointermid, text1.length - pointerend) == + text2.substring(text2.length - pointermid, text2.length - pointerend)) { pointermin = pointermid; pointerend = pointermin; } else { @@ -127,6 +122,7 @@ var diff_commonSuffix = function (text1: string, text2: string): number { return pointermid; }; + /** * Determine if the suffix of one string is the prefix of another. * @param {string} text1 First string. @@ -135,7 +131,7 @@ var diff_commonSuffix = function (text1: string, text2: string): number { * string and the start of the second string. * @private */ -var diff_commonOverlap_ = function (text1: string, text2: string): number { +var diff_commonOverlap_ = function(text1: string, text2: string): number { // Cache the text lengths to prevent multiple calls. var text1_length = text1.length; var text2_length = text2.length; @@ -167,28 +163,27 @@ var diff_commonOverlap_ = function (text1: string, text2: string): number { return best; } length += found; - if ( - found == 0 || - text1.substring(text_length - length) == text2.substring(0, length) - ) { + if (found == 0 || text1.substring(text_length - length) == + text2.substring(0, length)) { best = length; length++; } } }; + /** * Reduce the number of edits by eliminating semantically trivial equalities. * @param {!Array.} diffs Array of diff tuples. */ -var diff_cleanupSemantic = function (diffs: Array) { + var diff_cleanupSemantic = function(diffs: Array) { var changes = false; - var equalities = []; // Stack of indices where equalities are found. - var equalitiesLength = 0; // Keeping our own length var is faster in JS. + var equalities = []; // Stack of indices where equalities are found. + var equalitiesLength = 0; // Keeping our own length var is faster in JS. /** @type {?string} */ var lastEquality = null; // Always equal to diffs[equalities[equalitiesLength - 1]][1] - var pointer = 0; // Index of current position. + var pointer = 0; // Index of current position. // Number of characters that changed prior to the equality. var length_insertions1 = 0; var length_deletions1 = 0; @@ -196,16 +191,14 @@ var diff_cleanupSemantic = function (diffs: Array) { var length_insertions2 = 0; var length_deletions2 = 0; while (pointer < diffs.length) { - if (diffs[pointer][0] == DIFF_EQUAL) { - // Equality found. + if (diffs[pointer][0] == DIFF_EQUAL) { // Equality found. equalities[equalitiesLength++] = pointer; length_insertions1 = length_insertions2; length_deletions1 = length_deletions2; length_insertions2 = 0; length_deletions2 = 0; lastEquality = diffs[pointer][1]; - } else { - // An insertion or deletion. + } else { // An insertion or deletion. if (diffs[pointer][0] == DIFF_INSERT) { length_insertions2 += diffs[pointer][1].length; } else { @@ -213,18 +206,13 @@ var diff_cleanupSemantic = function (diffs: Array) { } // Eliminate an equality that is smaller or equal to the edits on both // sides of it. - if ( - lastEquality && - lastEquality.length <= - Math.max(length_insertions1, length_deletions1) && - lastEquality.length <= Math.max(length_insertions2, length_deletions2) - ) { + if (lastEquality && (lastEquality.length <= + Math.max(length_insertions1, length_deletions1)) && + (lastEquality.length <= Math.max(length_insertions2, + length_deletions2))) { // Duplicate record. - diffs.splice( - equalities[equalitiesLength - 1], - 0, - new Diff(DIFF_DELETE, lastEquality), - ); + diffs.splice(equalities[equalitiesLength - 1], 0, + new Diff(DIFF_DELETE, lastEquality)); // Change second copy to insert. diffs[equalities[equalitiesLength - 1] + 1][0] = DIFF_INSERT; // Throw away the equality we just deleted. @@ -232,7 +220,7 @@ var diff_cleanupSemantic = function (diffs: Array) { // Throw away the previous equality (it needs to be reevaluated). equalitiesLength--; pointer = equalitiesLength > 0 ? equalities[equalitiesLength - 1] : -1; - length_insertions1 = 0; // Reset the counters. + length_insertions1 = 0; // Reset the counters. length_deletions1 = 0; length_insertions2 = 0; length_deletions2 = 0; @@ -257,51 +245,36 @@ var diff_cleanupSemantic = function (diffs: Array) { // Only extract an overlap if it is as big as the edit ahead or behind it. pointer = 1; while (pointer < diffs.length) { - if ( - diffs[pointer - 1][0] == DIFF_DELETE && - diffs[pointer][0] == DIFF_INSERT - ) { + if (diffs[pointer - 1][0] == DIFF_DELETE && + diffs[pointer][0] == DIFF_INSERT) { var deletion = diffs[pointer - 1][1]; var insertion = diffs[pointer][1]; var overlap_length1 = diff_commonOverlap_(deletion, insertion); var overlap_length2 = diff_commonOverlap_(insertion, deletion); if (overlap_length1 >= overlap_length2) { - if ( - overlap_length1 >= deletion.length / 2 || - overlap_length1 >= insertion.length / 2 - ) { + if (overlap_length1 >= deletion.length / 2 || + overlap_length1 >= insertion.length / 2) { // Overlap found. Insert an equality and trim the surrounding edits. - diffs.splice( - pointer, - 0, - new Diff(DIFF_EQUAL, insertion.substring(0, overlap_length1)), - ); - diffs[pointer - 1][1] = deletion.substring( - 0, - deletion.length - overlap_length1, - ); + diffs.splice(pointer, 0, new Diff(DIFF_EQUAL, + insertion.substring(0, overlap_length1))); + diffs[pointer - 1][1] = + deletion.substring(0, deletion.length - overlap_length1); diffs[pointer + 1][1] = insertion.substring(overlap_length1); pointer++; } } else { - if ( - overlap_length2 >= deletion.length / 2 || - overlap_length2 >= insertion.length / 2 - ) { + if (overlap_length2 >= deletion.length / 2 || + overlap_length2 >= insertion.length / 2) { // Reverse overlap found. // Insert an equality and swap and trim the surrounding edits. - diffs.splice( - pointer, - 0, - new Diff(DIFF_EQUAL, deletion.substring(0, overlap_length2)), - ); + diffs.splice(pointer, 0, new Diff(DIFF_EQUAL, + deletion.substring(0, overlap_length2))); diffs[pointer - 1][0] = DIFF_INSERT; - diffs[pointer - 1][1] = insertion.substring( - 0, - insertion.length - overlap_length2, - ); + diffs[pointer - 1][1] = + insertion.substring(0, insertion.length - overlap_length2); diffs[pointer + 1][0] = DIFF_DELETE; - diffs[pointer + 1][1] = deletion.substring(overlap_length2); + diffs[pointer + 1][1] = + deletion.substring(overlap_length2); pointer++; } } @@ -311,13 +284,14 @@ var diff_cleanupSemantic = function (diffs: Array) { } }; + /** * Look for single edits surrounded on both sides by equalities * which can be shifted sideways to align the edit to a word boundary. * e.g: The cat came. -> The cat came. * @param {!Array.} diffs Array of diff tuples. */ -var diff_cleanupSemanticLossless = function (diffs: Array) { +var diff_cleanupSemanticLossless = function(diffs: Array) { /** * Given two strings, compute a score representing whether the internal * boundary falls on logical boundaries. @@ -343,12 +317,18 @@ var diff_cleanupSemanticLossless = function (diffs: Array) { var char2 = two.charAt(0); var nonAlphaNumeric1 = char1.match(nonAlphaNumericRegex_); var nonAlphaNumeric2 = char2.match(nonAlphaNumericRegex_); - var whitespace1 = nonAlphaNumeric1 && char1.match(whitespaceRegex_); - var whitespace2 = nonAlphaNumeric2 && char2.match(whitespaceRegex_); - var lineBreak1 = whitespace1 && char1.match(linebreakRegex_); - var lineBreak2 = whitespace2 && char2.match(linebreakRegex_); - var blankLine1 = lineBreak1 && one.match(blanklineEndRegex_); - var blankLine2 = lineBreak2 && two.match(blanklineStartRegex_); + var whitespace1 = nonAlphaNumeric1 && + char1.match(whitespaceRegex_); + var whitespace2 = nonAlphaNumeric2 && + char2.match(whitespaceRegex_); + var lineBreak1 = whitespace1 && + char1.match(linebreakRegex_); + var lineBreak2 = whitespace2 && + char2.match(linebreakRegex_); + var blankLine1 = lineBreak1 && + one.match(blanklineEndRegex_); + var blankLine2 = lineBreak2 && + two.match(blanklineStartRegex_); if (blankLine1 || blankLine2) { // Five points for blank lines. @@ -372,10 +352,8 @@ var diff_cleanupSemanticLossless = function (diffs: Array) { var pointer = 1; // Intentionally ignore the first and last element (don't need checking). while (pointer < diffs.length - 1) { - if ( - diffs[pointer - 1][0] == DIFF_EQUAL && - diffs[pointer + 1][0] == DIFF_EQUAL - ) { + if (diffs[pointer - 1][0] == DIFF_EQUAL && + diffs[pointer + 1][0] == DIFF_EQUAL) { // This is a single edit surrounded by equalities. var equality1 = diffs[pointer - 1][1]; var edit = diffs[pointer][1]; @@ -394,16 +372,14 @@ var diff_cleanupSemanticLossless = function (diffs: Array) { var bestEquality1 = equality1; var bestEdit = edit; var bestEquality2 = equality2; - var bestScore = - diff_cleanupSemanticScore_(equality1, edit) + - diff_cleanupSemanticScore_(edit, equality2); + var bestScore = diff_cleanupSemanticScore_(equality1, edit) + + diff_cleanupSemanticScore_(edit, equality2); while (edit.charAt(0) === equality2.charAt(0)) { equality1 += edit.charAt(0); edit = edit.substring(1) + equality2.charAt(0); equality2 = equality2.substring(1); - var score = - diff_cleanupSemanticScore_(equality1, edit) + - diff_cleanupSemanticScore_(edit, equality2); + var score = diff_cleanupSemanticScore_(equality1, edit) + + diff_cleanupSemanticScore_(edit, equality2); // The >= encourages trailing rather than leading whitespace on edits. if (score >= bestScore) { bestScore = score; @@ -434,6 +410,7 @@ var diff_cleanupSemanticLossless = function (diffs: Array) { } }; + // Define some regex patterns for matching boundaries. var nonAlphaNumericRegex_ = /[^a-zA-Z0-9]/; var whitespaceRegex_ = /\s/; @@ -441,12 +418,13 @@ var linebreakRegex_ = /[\r\n]/; var blanklineEndRegex_ = /\n\r?\n$/; var blanklineStartRegex_ = /^\r?\n\r?\n/; + /** * Reorder and merge like edit sections. Merge equalities. * Any edit section can move as long as it doesn't cross an equality. * @param {!Array.} diffs Array of diff tuples. */ -var diff_cleanupMerge = function (diffs: Array) { +var diff_cleanupMerge = function(diffs: Array) { // Add a dummy entry at the end. diffs.push(new Diff(DIFF_EQUAL, '')); var pointer = 0; @@ -474,19 +452,14 @@ var diff_cleanupMerge = function (diffs: Array) { // Factor out any common prefixies. commonlength = diff_commonPrefix(text_insert, text_delete); if (commonlength !== 0) { - if ( - pointer - count_delete - count_insert > 0 && - diffs[pointer - count_delete - count_insert - 1][0] == - DIFF_EQUAL - ) { + if ((pointer - count_delete - count_insert) > 0 && + diffs[pointer - count_delete - count_insert - 1][0] == + DIFF_EQUAL) { diffs[pointer - count_delete - count_insert - 1][1] += - text_insert.substring(0, commonlength); + text_insert.substring(0, commonlength); } else { - diffs.splice( - 0, - 0, - new Diff(DIFF_EQUAL, text_insert.substring(0, commonlength)), - ); + diffs.splice(0, 0, new Diff(DIFF_EQUAL, + text_insert.substring(0, commonlength))); pointer++; } text_insert = text_insert.substring(commonlength); @@ -495,28 +468,25 @@ var diff_cleanupMerge = function (diffs: Array) { // Factor out any common suffixies. commonlength = diff_commonSuffix(text_insert, text_delete); if (commonlength !== 0) { - diffs[pointer][1] = - text_insert.substring(text_insert.length - commonlength) + - diffs[pointer][1]; - text_insert = text_insert.substring( - 0, - text_insert.length - commonlength, - ); - text_delete = text_delete.substring( - 0, - text_delete.length - commonlength, - ); + diffs[pointer][1] = text_insert.substring(text_insert.length - + commonlength) + diffs[pointer][1]; + text_insert = text_insert.substring(0, text_insert.length - + commonlength); + text_delete = text_delete.substring(0, text_delete.length - + commonlength); } } // Delete the offending records and add the merged ones. pointer -= count_delete + count_insert; diffs.splice(pointer, count_delete + count_insert); if (text_delete.length) { - diffs.splice(pointer, 0, new Diff(DIFF_DELETE, text_delete)); + diffs.splice(pointer, 0, + new Diff(DIFF_DELETE, text_delete)); pointer++; } if (text_insert.length) { - diffs.splice(pointer, 0, new Diff(DIFF_INSERT, text_insert)); + diffs.splice(pointer, 0, + new Diff(DIFF_INSERT, text_insert)); pointer++; } pointer++; @@ -535,7 +505,7 @@ var diff_cleanupMerge = function (diffs: Array) { } } if (diffs[diffs.length - 1][1] === '') { - diffs.pop(); // Remove the dummy entry at the end. + diffs.pop(); // Remove the dummy entry at the end. } // Second pass: look for single edits surrounded on both sides by equalities @@ -545,35 +515,25 @@ var diff_cleanupMerge = function (diffs: Array) { pointer = 1; // Intentionally ignore the first and last element (don't need checking). while (pointer < diffs.length - 1) { - if ( - diffs[pointer - 1][0] == DIFF_EQUAL && - diffs[pointer + 1][0] == DIFF_EQUAL - ) { + if (diffs[pointer - 1][0] == DIFF_EQUAL && + diffs[pointer + 1][0] == DIFF_EQUAL) { // This is a single edit surrounded by equalities. - if ( - diffs[pointer][1].substring( - diffs[pointer][1].length - diffs[pointer - 1][1].length, - ) == diffs[pointer - 1][1] - ) { + if (diffs[pointer][1].substring(diffs[pointer][1].length - + diffs[pointer - 1][1].length) == diffs[pointer - 1][1]) { // Shift the edit over the previous equality. - diffs[pointer][1] = - diffs[pointer - 1][1] + - diffs[pointer][1].substring( - 0, - diffs[pointer][1].length - diffs[pointer - 1][1].length, - ); + diffs[pointer][1] = diffs[pointer - 1][1] + + diffs[pointer][1].substring(0, diffs[pointer][1].length - + diffs[pointer - 1][1].length); diffs[pointer + 1][1] = diffs[pointer - 1][1] + diffs[pointer + 1][1]; diffs.splice(pointer - 1, 1); changes = true; - } else if ( - diffs[pointer][1].substring(0, diffs[pointer + 1][1].length) == - diffs[pointer + 1][1] - ) { + } else if (diffs[pointer][1].substring(0, diffs[pointer + 1][1].length) == + diffs[pointer + 1][1]) { // Shift the edit over the next equality. diffs[pointer - 1][1] += diffs[pointer + 1][1]; diffs[pointer][1] = - diffs[pointer][1].substring(diffs[pointer + 1][1].length) + - diffs[pointer + 1][1]; + diffs[pointer][1].substring(diffs[pointer + 1][1].length) + + diffs[pointer + 1][1]; diffs.splice(pointer + 1, 1); changes = true; } @@ -586,6 +546,7 @@ var diff_cleanupMerge = function (diffs: Array) { } }; + export { Diff, DIFF_EQUAL, From 378789dfbd763b90ae8bfceaa3b5a77b03b9db71 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 8 Apr 2022 22:00:51 +0800 Subject: [PATCH 4/8] Don't ignore dot files on ESLint --- .eslintignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.eslintignore b/.eslintignore index ea7899381bc6..3a5333e9bce0 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,3 +1,4 @@ +!.* **/coverage/** **/node_modules/** bin/ @@ -10,7 +11,6 @@ website/build website/node_modules website/i18n/*.js website/static -!.eslintrc.js # Third-party script packages/jest-diff/src/cleanupSemantic.ts From 99389233cbe23ea681261ca57b9a45c37832cd04 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 8 Apr 2022 22:04:10 +0800 Subject: [PATCH 5/8] Update .eslintignore Co-authored-by: Tom Mrazauskas --- .eslintignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.eslintignore b/.eslintignore index 3a5333e9bce0..9edea61c8858 100644 --- a/.eslintignore +++ b/.eslintignore @@ -2,7 +2,6 @@ **/coverage/** **/node_modules/** bin/ -flow-typed/** packages/*/build/** packages/*/dist/** website/.docusaurus From e6ed1a284235238eb8c173c145000806a9a92c62 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 8 Apr 2022 22:08:55 +0800 Subject: [PATCH 6/8] Add `.md` back --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 330a25dd7a3b..314efb6d87cd 100644 --- a/package.json +++ b/package.json @@ -100,8 +100,8 @@ "jest-jasmine-ci": "yarn jest-jasmine --color --config jest.config.ci.js", "jest-coverage": "yarn jest --coverage", "lint": "eslint . --cache --ext js,jsx,cjs,mjs,ts,tsx,md", - "lint:prettier": "prettier . \"!**/*.{js,jsx,cjs,mjs,ts,tsx,md}\" --write", - "lint:prettier:ci": "prettier . \"!**/*.{js,jsx,cjs,mjs,ts,tsx,md}\" --check", + "lint:prettier": "prettier . \"!**/*.{js,jsx,cjs,mjs,ts,tsx}\" --write", + "lint:prettier:ci": "prettier . \"!**/*.{js,jsx,cjs,mjs,ts,tsx}\" --check", "remove-examples": "node ./scripts/remove-examples.mjs", "test-types": "yarn jest --config jest.config.tsd.js", "test-ci-partial": "yarn test-ci-partial:parallel -i", From 3037782d6a8db0cba876ef856dbdc9f1599dd901 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 8 Apr 2022 22:09:51 +0800 Subject: [PATCH 7/8] Ignore `dist` --- .prettierignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.prettierignore b/.prettierignore index 01bd581587fa..cf9b97f586fc 100644 --- a/.prettierignore +++ b/.prettierignore @@ -7,6 +7,7 @@ api-extractor.json coverage /packages/*/build +/packages/*/dist /packages/jest-config/src/__tests__/jest-preset.json /packages/pretty-format/perf/world.geo.json From e769fc25a4aa894c50450c09eadb107181faa7bb Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 8 Apr 2022 22:19:46 +0800 Subject: [PATCH 8/8] Fix ESLint script --- .eslintignore | 4 ++++ .eslintrc.cjs | 8 ++------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.eslintignore b/.eslintignore index 9edea61c8858..36c1940c60f0 100644 --- a/.eslintignore +++ b/.eslintignore @@ -13,3 +13,7 @@ website/static # Third-party script packages/jest-diff/src/cleanupSemantic.ts + +**/.yarn +**/.pnp.* + diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 8ee5a68dfe67..98ed42ee1aab 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -186,12 +186,6 @@ module.exports = { 'import/order': 'off', }, }, - { - files: 'packages/jest-types/**/*', - rules: { - 'import/no-extraneous-dependencies': 'off', - }, - }, { files: 'packages/**/*.ts', rules: { @@ -249,6 +243,8 @@ module.exports = { '**/__tests__/**', 'e2e/**', '**/pretty-format/perf/**', + 'packages/jest-types/**/*', + '.eslintplugin/**', ], rules: { 'import/no-extraneous-dependencies': 'off',