diff --git a/release-notes.md b/release-notes.md index 49769dee..98281b4a 100644 --- a/release-notes.md +++ b/release-notes.md @@ -1,5 +1,9 @@ # Release Notes +## 8.0.1 + +- [#610](https://github.com/kpdecker/jsdiff/pull/610) **Fixes types for `diffJson` which were broken by 8.0.0**. The new bundled types in 8.0.0 only allowed `diffJson` to be passed string arguments, but it should've been possible to pass either strings or objects (and now is). Thanks to Josh Kelley for the fix. + ## 8.0.0 - [#580](https://github.com/kpdecker/jsdiff/pull/580) **Multiple tweaks to `diffSentences`**: diff --git a/src/diff/base.ts b/src/diff/base.ts index cfc3719d..ef2dd462 100644 --- a/src/diff/base.ts +++ b/src/diff/base.ts @@ -23,36 +23,37 @@ interface Path { export default class Diff< TokenT, - ValueT extends Iterable = Iterable + ValueT extends Iterable = Iterable, + InputValueT = ValueT > { diff( - oldStr: ValueT, - newStr: ValueT, + oldStr: InputValueT, + newStr: InputValueT, options: DiffCallbackNonabortable ): undefined; diff( - oldStr: ValueT, - newStr: ValueT, + oldStr: InputValueT, + newStr: InputValueT, options: AllDiffOptions & AbortableDiffOptions & CallbackOptionAbortable ): undefined diff( - oldStr: ValueT, - newStr: ValueT, + oldStr: InputValueT, + newStr: InputValueT, options: AllDiffOptions & CallbackOptionNonabortable ): undefined diff( - oldStr: ValueT, - newStr: ValueT, + oldStr: InputValueT, + newStr: InputValueT, options: AllDiffOptions & AbortableDiffOptions ): ChangeObject[] | undefined diff( - oldStr: ValueT, - newStr: ValueT, + oldStr: InputValueT, + newStr: InputValueT, options?: AllDiffOptions ): ChangeObject[] diff( - oldString: ValueT, - newString: ValueT, + oldStr: InputValueT, + newStr: InputValueT, // Type below is not accurate/complete - see above for full possibilities - but it compiles options: DiffCallbackNonabortable | AllDiffOptions & Partial> = {} ): ChangeObject[] | undefined { @@ -64,8 +65,8 @@ export default class Diff< callback = options.callback; } // Allow subclasses to massage the input prior to running - oldString = this.castInput(oldString, options); - newString = this.castInput(newString, options); + const oldString = this.castInput(oldStr, options); + const newString = this.castInput(newStr, options); const oldTokens = this.removeEmpty(this.tokenize(oldString, options)); const newTokens = this.removeEmpty(this.tokenize(newString, options)); @@ -282,8 +283,8 @@ export default class Diff< } // eslint-disable-next-line @typescript-eslint/no-unused-vars - castInput(value: ValueT, options: AllDiffOptions): ValueT { - return value; + castInput(value: InputValueT, options: AllDiffOptions): ValueT { + return value as unknown as ValueT; } // eslint-disable-next-line @typescript-eslint/no-unused-vars diff --git a/src/diff/json.ts b/src/diff/json.ts index be7e7c4c..898cd3f4 100644 --- a/src/diff/json.ts +++ b/src/diff/json.ts @@ -2,7 +2,7 @@ import Diff from './base.js'; import type { ChangeObject, CallbackOptionAbortable, CallbackOptionNonabortable, DiffCallbackNonabortable, DiffJsonOptionsAbortable, DiffJsonOptionsNonabortable} from '../types.js'; import { tokenize } from './line.js'; -class JsonDiff extends Diff { +class JsonDiff extends Diff { get useLongestToken() { // Discriminate between two lines of pretty-printed, serialized JSON where one of them has a // dangling comma and the other doesn't. Turns out including the dangling comma yields the nicest output: @@ -11,7 +11,7 @@ class JsonDiff extends Diff { tokenize = tokenize; - castInput(value: string, options: DiffJsonOptionsNonabortable | DiffJsonOptionsAbortable) { + castInput(value: string | object, options: DiffJsonOptionsNonabortable | DiffJsonOptionsAbortable) { const {undefinedReplacement, stringifyReplacer = (k, v) => typeof v === 'undefined' ? undefinedReplacement : v} = options; return typeof value === 'string' ? value : JSON.stringify(canonicalize(value, null, null, stringifyReplacer), null, ' '); @@ -31,31 +31,31 @@ export const jsonDiff = new JsonDiff(); * @returns a list of change objects. */ export function diffJson( - oldStr: string, - newStr: string, + oldStr: string | object, + newStr: string | object, options: DiffCallbackNonabortable ): undefined; export function diffJson( - oldStr: string, - newStr: string, + oldStr: string | object, + newStr: string | object, options: DiffJsonOptionsAbortable & CallbackOptionAbortable ): undefined export function diffJson( - oldStr: string, - newStr: string, + oldStr: string | object, + newStr: string | object, options: DiffJsonOptionsNonabortable & CallbackOptionNonabortable ): undefined export function diffJson( - oldStr: string, - newStr: string, + oldStr: string | object, + newStr: string | object, options: DiffJsonOptionsAbortable ): ChangeObject[] | undefined export function diffJson( - oldStr: string, - newStr: string, + oldStr: string | object, + newStr: string | object, options?: DiffJsonOptionsNonabortable ): ChangeObject[] -export function diffJson(oldStr: string, newStr: string, options?: any): undefined | ChangeObject[] { +export function diffJson(oldStr: string | object, newStr: string | object, options?: any): undefined | ChangeObject[] { return jsonDiff.diff(oldStr, newStr, options); }