Skip to content

Commit acaf99c

Browse files
authored
fix: diff performance (#48)
1 parent 96c2582 commit acaf99c

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"chalk": "^4.1.0",
3535
"cosmiconfig": "^7.0.1",
3636
"debug": "^4.3.2",
37-
"diff": "^5.0.0",
37+
"diff-match-patch": "^1.0.5",
3838
"fast-glob": "^3.2.7",
3939
"jsonc-eslint-parser": "^1.4.1",
4040
"pathe": "^0.2.0",
@@ -50,7 +50,7 @@
5050
"@secretlint/secretlint-rule-preset-recommend": "^3.3.0",
5151
"@types/chai": "^4.2.22",
5252
"@types/debug": "^4.1.7",
53-
"@types/diff": "^5.0.1",
53+
"@types/diff-match-patch": "^1.0.32",
5454
"@types/eslint": "^7.2.6",
5555
"@types/eslint-visitor-keys": "^1.0.0",
5656
"@types/glob": "^7.1.3",

pnpm-lock.yaml

Lines changed: 14 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import createDebug from 'debug'
22
import fg from 'fast-glob'
3-
import { diffChars as diff } from 'diff'
3+
import diff from 'diff-match-patch'
44
import { parseJSON } from 'jsonc-eslint-parser'
55
import { parseYAML } from 'yaml-eslint-parser'
66
import { readFileSync } from 'fs'
@@ -145,9 +145,16 @@ export function escape(s: string): string {
145145
return s.replace(/[<>"&]/g, escapeChar)
146146
}
147147

148+
const df = new diff.diff_match_patch()
149+
148150
export function hasDiff(newContent: string, oldContent: string): boolean {
149-
const contents = diff(oldContent, newContent)
150-
return !!contents.find(content => content.added || content.removed)
151+
const diffs = df.diff_main(oldContent, newContent, true)
152+
if (diffs.length === 0) {
153+
return false
154+
}
155+
return !!diffs.find(
156+
d => d[0] === diff.DIFF_DELETE || d[0] === diff.DIFF_INSERT
157+
)
151158
}
152159

153160
export function buildSFCBlockTag(meta: {

0 commit comments

Comments
 (0)