Skip to content

Commit 14798cb

Browse files
committed
fix(prettier): remove dependency on diff-match-patch, fixing incorrect formatting in some cases
1 parent d959b6b commit 14798cb

File tree

3 files changed

+8
-29
lines changed

3 files changed

+8
-29
lines changed

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
},
3030
"dependencies": {
3131
"@softwareventures/array": "4.1.3",
32-
"diff-match-patch": "1.0.5",
3332
"execa": "5.1.1",
3433
"find-up": "5.0.0",
3534
"glob": "7.2.0",
@@ -47,7 +46,6 @@
4746
"@softwareventures/prettier-config": "2.0.0",
4847
"@softwareventures/semantic-release-config": "2.0.0",
4948
"@softwareventures/tsconfig": "5.1.0",
50-
"@types/diff-match-patch": "1.0.32",
5149
"@types/find-up": "2.1.1",
5250
"@types/glob": "7.2.0",
5351
"@types/jest": "27.0.3",

src/precise-formatters/prettier.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import * as fs from "fs";
2-
import {diff_match_patch as DiffMatchPatch} from "diff-match-patch";
32
import ignore from "ignore";
43
import {extname, join} from "path";
54
import {Options as PrettierOptions, getSupportInfo, format, resolveConfig, check} from "prettier";
65

76
import {PreciseFormatter} from "../precise-formatter";
87
import {CharacterRange} from "../utils";
98

10-
const dmp = new DiffMatchPatch();
11-
129
let PRETTIER_SUPPORTED_FILE_EXTENSIONS: string[] = [];
1310
getSupportInfo().languages.forEach(language => {
1411
PRETTIER_SUPPORTED_FILE_EXTENSIONS = [
@@ -67,20 +64,14 @@ export const preciseFormatterPrettier: PreciseFormatter<PrettierOptions> = {
6764
config: PrettierOptions | null,
6865
characterRanges: CharacterRange[]
6966
): string {
70-
let patches: any = [];
71-
characterRanges.forEach(characterRange => {
72-
const diffs = dmp.diff_main(
73-
fileContents,
74-
format(fileContents, {
75-
...config,
76-
rangeStart: characterRange.rangeStart,
77-
rangeEnd: characterRange.rangeEnd
78-
})
79-
);
80-
patches = [...patches, ...dmp.patch_make(fileContents, diffs)];
81-
});
82-
const [formattedContents] = dmp.patch_apply(patches, fileContents);
83-
return formattedContents;
67+
// Start from the last character range and work backwards so that
68+
// we don't have to update character ranges to account for the changes
69+
// we've already made.
70+
return characterRanges.reduceRight(
71+
(fileContents, {rangeStart, rangeEnd}) =>
72+
format(fileContents, {...config, rangeStart, rangeEnd}),
73+
fileContents
74+
);
8475
},
8576
/**
8677
* Generate a predicate function which will return true if the filename

yarn.lock

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -973,11 +973,6 @@
973973
dependencies:
974974
"@babel/types" "^7.3.0"
975975

976-
"@types/diff-match-patch@1.0.32":
977-
version "1.0.32"
978-
resolved "https://registry.yarnpkg.com/@types/diff-match-patch/-/diff-match-patch-1.0.32.tgz#d9c3b8c914aa8229485351db4865328337a3d09f"
979-
integrity sha512-bPYT5ECFiblzsVzyURaNhljBH2Gh1t9LowgUwciMrNAhFewLkHT2H0Mto07Y4/3KCOGZHRQll3CTtQZ0X11D/A==
980-
981976
"@types/find-up@2.1.1":
982977
version "2.1.1"
983978
resolved "https://registry.yarnpkg.com/@types/find-up/-/find-up-2.1.1.tgz#1cd2d240f1ad1f48d32346074724dc3107248a11"
@@ -2149,11 +2144,6 @@ dezalgo@^1.0.0:
21492144
asap "^2.0.0"
21502145
wrappy "1"
21512146

2152-
diff-match-patch@1.0.5:
2153-
version "1.0.5"
2154-
resolved "https://registry.yarnpkg.com/diff-match-patch/-/diff-match-patch-1.0.5.tgz#abb584d5f10cd1196dfc55aa03701592ae3f7b37"
2155-
integrity sha512-IayShXAgj/QMXgB0IWmKx+rOPuGMhqm5w6jvFxmVenXKIzRqTAAsbBPT3kWQeGANj3jGgvcvv4yK6SxqYmikgw==
2156-
21572147
diff-sequences@^27.4.0:
21582148
version "27.4.0"
21592149
resolved "https://registry.yarnpkg.com/diff-sequences/-/diff-sequences-27.4.0.tgz#d783920ad8d06ec718a060d00196dfef25b132a5"

0 commit comments

Comments
 (0)