diff --git a/package.json b/package.json index 5c3f1b0..c89b5fd 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d0c7095..41ce23d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -10,7 +10,7 @@ specifiers: '@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 @@ -27,7 +27,7 @@ specifiers: chalk: ^4.1.0 cosmiconfig: ^7.0.1 debug: ^4.3.2 - diff: ^5.0.0 + diff-match-patch: ^1.0.5 eslint: ^8.0.0 eslint-config-prettier: ^8.3.0 eslint-plugin-prettier: ^4.0.0 @@ -60,7 +60,7 @@ dependencies: chalk: 4.1.2 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 @@ -76,7 +76,7 @@ devDependencies: '@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.28.2 '@types/eslint-visitor-keys': 1.0.0 '@types/glob': 7.2.0 @@ -1370,10 +1370,10 @@ packages: '@types/ms': 0.7.31 dev: true - /@types/diff/5.0.1: + /@types/diff-match-patch/1.0.32: resolution: { - integrity: sha512-XIpxU6Qdvp1ZE6Kr3yrkv1qgUab0fyf4mHYvW8N3Bx3PCsbN6or1q9/q72cv5jIFWolaGH08U9XyYoLLIykyKQ== + integrity: sha512-bPYT5ECFiblzsVzyURaNhljBH2Gh1t9LowgUwciMrNAhFewLkHT2H0Mto07Y4/3KCOGZHRQll3CTtQZ0X11D/A== } dev: true @@ -2906,12 +2906,20 @@ packages: } dev: true + /diff-match-patch/1.0.5: + resolution: + { + integrity: sha512-IayShXAgj/QMXgB0IWmKx+rOPuGMhqm5w6jvFxmVenXKIzRqTAAsbBPT3kWQeGANj3jGgvcvv4yK6SxqYmikgw== + } + dev: false + /diff/5.0.0: resolution: { integrity: sha512-/VTCrvm5Z0JGty/BWHljh+BAiw3IK+2j87NGMu8Nwc/f48WoDAC395uomO9ZD117ZOBaHmkX1oyLvkVM/aIT3w== } engines: { node: '>=0.3.1' } + dev: true /dir-glob/3.0.1: resolution: diff --git a/src/utils.ts b/src/utils.ts index 3cca8d4..acbd307 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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' @@ -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: {