Files
react-native-devtools-frontend/front_end/third_party/diff/DiffWrapper.ts
T
Jack Franklin cf56966bc3 [ESLint] lint front_end/third_party
In crrev.com/c/3748106 we noticed that a CL was able to land that
imported a file without the `.js` extension.

This is because `.eslintignore` listed `front_end/third_party`, so that
folder wasn't linted at all. This is an issue because whilst most of the
code in that directory is not authored by us, the entry points usually
are, and we want to apply our lint rules there.

This CL removes the blanket ignore, instead preferring to ignore
`third_party/X/package/`, and a few other special cases, ensuring that
devtools-frontend authored files in `third_party/` do get linted.

The CL updates the `es_modules_import` rule to not apply the import
checks to files in `third_party` (as they cannot apply the same
conventions we use in DevTools), but it will still check for file
extensions on imports.

The remaining changes are small stylistic changes to align to our ESLint
rules.

Fixed: 1342530
Change-Id: I4ce438499b8c9c77c3fe661576a58bad59d9b061
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3749191
Auto-Submit: Jack Franklin <jacktfranklin@chromium.org>
Reviewed-by: Alex Rudenko <alexrudenko@chromium.org>
Commit-Queue: Alex Rudenko <alexrudenko@chromium.org>
2022-07-07 10:55:21 +00:00

106 lines
3.0 KiB
TypeScript

// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import * as Common from '../../core/common/common.js';
declare global {
/* eslint-disable @typescript-eslint/naming-convention */
class diff_match_patch {
diff_main(text1: string, text2: string): Array<{0: number, 1: string}>;
diff_cleanupSemantic(diff: Array<{0: number, 1: string}>): void;
}
/* eslint-enable @typescript-eslint/naming-convention */
}
export const DiffWrapper = {
charDiff: function(text1: string, text2: string, cleanup?: boolean): {0: number, 1: string}[] {
const differ = new diff_match_patch();
const diff = differ.diff_main(text1, text2);
if (cleanup) {
differ.diff_cleanupSemantic(diff);
}
return diff;
},
lineDiff: function(lines1: string[], lines2: string[]): DiffArray {
const idMap = new Common.CharacterIdMap.CharacterIdMap<string>();
const text1 = lines1.map(line => idMap.toChar(line)).join('');
const text2 = lines2.map(line => idMap.toChar(line)).join('');
const diff = DiffWrapper.charDiff(text1, text2);
const lineDiff = [];
for (let i = 0; i < diff.length; i++) {
const lines = [];
for (let j = 0; j < diff[i][1].length; j++) {
lines.push(idMap.fromChar(diff[i][1][j]) || '');
}
lineDiff.push({ 0: diff[i][0], 1: lines });
}
return lineDiff;
},
convertToEditDiff: function(diff: DiffArray): number[][] {
const normalized = [];
let added = 0;
let removed = 0;
for (let i = 0; i < diff.length; ++i) {
const token = diff[i];
if (token[0] === Operation.Equal) {
flush();
normalized.push([Operation.Equal, token[1].length]);
}
else if (token[0] === Operation.Delete) {
removed += token[1].length;
}
else {
added += token[1].length;
}
}
flush();
return normalized;
function flush(): void {
if (added && removed) {
const min = Math.min(added, removed);
normalized.push([Operation.Edit, min]);
added -= min;
removed -= min;
}
if (added || removed) {
const balance = added - removed;
const type = balance < 0 ? Operation.Delete : Operation.Insert;
normalized.push([type, Math.abs(balance)]);
added = 0;
removed = 0;
}
}
},
/**
* Scores character-sequence diffs, giving higher scores for longer sequences.
*/
characterScore: function(item: string, against: string): number {
let score = 0;
const diff = DiffWrapper.charDiff(item, against);
for (let i = 0; i < diff.length; ++i) {
if (diff[i][0] === Operation.Equal) {
score += diff[i][1].length * diff[i][1].length;
}
}
return score;
},
};
// TODO(crbug.com/1167717): Make this a const enum again
// eslint-disable-next-line rulesdir/const_enum
export enum Operation {
Equal = 0,
Insert = 1,
Delete = -1,
Edit = 2,
}
export type DiffArray = {0: Operation, 1: string[]}[];