Skip to content

Commit

Permalink
fix: diff performance (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon authored Oct 27, 2021
1 parent 96c2582 commit acaf99c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"chalk": "^4.1.0",
"cosmiconfig": "^7.0.1",
"debug": "^4.3.2",
"diff": "^5.0.0",
"diff-match-patch": "^1.0.5",
"fast-glob": "^3.2.7",
"jsonc-eslint-parser": "^1.4.1",
"pathe": "^0.2.0",
Expand All @@ -50,7 +50,7 @@
"@secretlint/secretlint-rule-preset-recommend": "^3.3.0",
"@types/chai": "^4.2.22",
"@types/debug": "^4.1.7",
"@types/diff": "^5.0.1",
"@types/diff-match-patch": "^1.0.32",
"@types/eslint": "^7.2.6",
"@types/eslint-visitor-keys": "^1.0.0",
"@types/glob": "^7.1.3",
Expand Down
20 changes: 14 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import createDebug from 'debug'
import fg from 'fast-glob'
import { diffChars as diff } from 'diff'
import diff from 'diff-match-patch'
import { parseJSON } from 'jsonc-eslint-parser'
import { parseYAML } from 'yaml-eslint-parser'
import { readFileSync } from 'fs'
Expand Down Expand Up @@ -145,9 +145,16 @@ export function escape(s: string): string {
return s.replace(/[<>"&]/g, escapeChar)
}

const df = new diff.diff_match_patch()

export function hasDiff(newContent: string, oldContent: string): boolean {
const contents = diff(oldContent, newContent)
return !!contents.find(content => content.added || content.removed)
const diffs = df.diff_main(oldContent, newContent, true)
if (diffs.length === 0) {
return false
}
return !!diffs.find(
d => d[0] === diff.DIFF_DELETE || d[0] === diff.DIFF_INSERT
)
}

export function buildSFCBlockTag(meta: {
Expand Down

0 comments on commit acaf99c

Please sign in to comment.