Skip to content

Commit 9db441a

Browse files
Get most of the way to fixing type definitions based on #303 (comment). Doesn't compile yet.
1 parent ddb99ec commit 9db441a

File tree

10 files changed

+292
-123
lines changed

10 files changed

+292
-123
lines changed

Diff for: src/diff/array.ts

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Diff from './base';
2-
import {CallbackOption, DiffCallback, ChangeObject, DiffArraysOptions, AbortableDiffOptions} from '../types';
2+
import {ChangeObject, DiffArraysOptionsNonabortable, CallbackOptionNonabortable, DiffArraysOptionsAbortable, DiffCallbackNonabortable, CallbackOptionAbortable} from '../types';
33

44
class ArrayDiff extends Diff<any, Array<any>> {
55
protected tokenize(value: Array<any>) {
@@ -20,18 +20,28 @@ export const arrayDiff = new ArrayDiff();
2020
export function diffArrays(
2121
oldArr: any[],
2222
newArr: any[],
23-
options: (DiffArraysOptions & CallbackOption<any[]>) | DiffCallback<any[]>
23+
options: DiffCallbackNonabortable<any[]>
2424
): undefined;
2525
export function diffArrays(
2626
oldArr: any[],
2727
newArr: any[],
28-
options: DiffArraysOptions & AbortableDiffOptions
29-
): ChangeObject<any[]>[] | undefined;
28+
options: DiffArraysOptionsAbortable & CallbackOptionAbortable<any[]>
29+
): undefined
3030
export function diffArrays(
3131
oldArr: any[],
3232
newArr: any[],
33-
options?: DiffArraysOptions
34-
): ChangeObject<any[]>[];
33+
options: DiffArraysOptionsNonabortable & CallbackOptionNonabortable<any[]>
34+
): undefined
35+
export function diffArrays(
36+
oldArr: any[],
37+
newArr: any[],
38+
options: DiffArraysOptionsAbortable
39+
): ChangeObject<any[]>[] | undefined
40+
export function diffArrays(
41+
oldArr: any[],
42+
newArr: any[],
43+
options?: DiffArraysOptionsNonabortable
44+
): ChangeObject<any[]>[]
3545
export function diffArrays(
3646
oldArr: any[],
3747
newArr: any[],

Diff for: src/diff/base.ts

+32-23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {ChangeObject, AllDiffOptions, DiffOptionsWithCallback, DiffCallback, AbortableDiffOptions, AbortableDiffCallback} from '../types';
1+
import {ChangeObject, AllDiffOptions, AbortableDiffOptions, DiffCallbackNonabortable, CallbackOptionAbortable, CallbackOptionNonabortable, DiffCallbackAbortable, TimeoutOption, MaxEditLengthOption} from '../types';
22

33
/**
44
* Like a ChangeObject, but with no value and an extra `previousComponent` property.
@@ -21,33 +21,42 @@ interface Path {
2121
lastComponent?: DraftChangeObject
2222
}
2323

24-
// TODO: Types are wrong for case with a callback
25-
2624
export default class Diff<
2725
TokenT,
2826
ValueT extends Iterable<TokenT> = Iterable<TokenT>
2927
> {
3028
diff(
31-
oldString: ValueT,
32-
newString: ValueT,
33-
options: DiffCallback<TokenT> | DiffOptionsWithCallback<TokenT>
29+
oldStr: ValueT,
30+
newStr: ValueT,
31+
options: DiffCallbackNonabortable<ValueT>
32+
): undefined;
33+
diff(
34+
oldStr: ValueT,
35+
newStr: ValueT,
36+
options: AllDiffOptions & AbortableDiffOptions & CallbackOptionAbortable<ValueT>
3437
): undefined
3538
diff(
36-
oldString: ValueT,
37-
newString: ValueT,
38-
options: AllDiffOptions<TokenT> & AbortableDiffOptions
39+
oldStr: ValueT,
40+
newStr: ValueT,
41+
options: AllDiffOptions & CallbackOptionNonabortable<ValueT>
42+
): undefined
43+
diff(
44+
oldStr: ValueT,
45+
newStr: ValueT,
46+
options: AllDiffOptions & AbortableDiffOptions
3947
): ChangeObject<ValueT>[] | undefined
4048
diff(
41-
oldString: ValueT,
42-
newString: ValueT,
43-
options?: AllDiffOptions<TokenT>
49+
oldStr: ValueT,
50+
newStr: ValueT,
51+
options?: AllDiffOptions
4452
): ChangeObject<ValueT>[]
4553
diff(
4654
oldString: ValueT,
4755
newString: ValueT,
48-
options: DiffCallback<TokenT> | DiffOptionsWithCallback<TokenT> | AllDiffOptions<TokenT> = {}
56+
// Type below is not accurate/complete - see above for full possibilities - but it compiles
57+
options: DiffCallbackNonabortable<ValueT> | AllDiffOptions & Partial<CallbackOptionNonabortable<ValueT>> = {}
4958
): ChangeObject<ValueT>[] | undefined {
50-
let callback: AbortableDiffCallback<TokenT> | undefined;
59+
let callback: DiffCallbackAbortable<ValueT> | DiffCallbackNonabortable<ValueT> | undefined;
5160
if (typeof options === 'function') {
5261
callback = options;
5362
options = {};
@@ -67,8 +76,8 @@ export default class Diff<
6776
private diffWithOptionsObj(
6877
oldTokens: TokenT[],
6978
newTokens: TokenT[],
70-
options: AllDiffOptions<TokenT> | DiffOptionsWithCallback<TokenT>,
71-
callback: AbortableDiffCallback<TokenT> | undefined
79+
options: AllDiffOptions & Partial<TimeoutOption> & Partial<MaxEditLengthOption>,
80+
callback: DiffCallbackAbortable<ValueT> | DiffCallbackNonabortable<ValueT> | undefined
7281
): ChangeObject<ValueT>[] | undefined {
7382
const done = (value) => {
7483
value = this.postProcess(value, options);
@@ -182,7 +191,7 @@ export default class Diff<
182191
(function exec() {
183192
setTimeout(function() {
184193
if (editLength > maxEditLength || Date.now() > abortAfterTimestamp) {
185-
return callback();
194+
return callback(undefined);
186195
}
187196

188197
if (!execEditLength()) {
@@ -205,7 +214,7 @@ export default class Diff<
205214
added: boolean,
206215
removed: boolean,
207216
oldPosInc: number,
208-
options: AllDiffOptions<TokenT>
217+
options: AllDiffOptions
209218
): Path {
210219
const last = path.lastComponent;
211220
if (last && !options.oneChangePerToken && last.added === added && last.removed === removed) {
@@ -226,7 +235,7 @@ export default class Diff<
226235
newTokens: TokenT[],
227236
oldTokens: TokenT[],
228237
diagonalPath: number,
229-
options: AllDiffOptions<TokenT>
238+
options: AllDiffOptions
230239
): number {
231240
const newLen = newTokens.length,
232241
oldLen = oldTokens.length;
@@ -251,7 +260,7 @@ export default class Diff<
251260
return newPos;
252261
}
253262

254-
protected equals(left: TokenT, right: TokenT, options: AllDiffOptions<TokenT>): boolean {
263+
protected equals(left: TokenT, right: TokenT, options: AllDiffOptions): boolean {
255264
if (options.comparator) {
256265
return options.comparator(left, right);
257266
} else {
@@ -271,12 +280,12 @@ export default class Diff<
271280
}
272281

273282
// eslint-disable-next-line @typescript-eslint/no-unused-vars
274-
protected castInput(value: ValueT, options: AllDiffOptions<TokenT>): ValueT {
283+
protected castInput(value: ValueT, options: AllDiffOptions): ValueT {
275284
return value;
276285
}
277286

278287
// eslint-disable-next-line @typescript-eslint/no-unused-vars
279-
protected tokenize(value: ValueT, options: AllDiffOptions<TokenT>): TokenT[] {
288+
protected tokenize(value: ValueT, options: AllDiffOptions): TokenT[] {
280289
return Array.from(value);
281290
}
282291

@@ -291,7 +300,7 @@ export default class Diff<
291300
protected postProcess(
292301
changeObjects: ChangeObject<ValueT>[],
293302
// eslint-disable-next-line @typescript-eslint/no-unused-vars
294-
options: AllDiffOptions<TokenT>
303+
options: AllDiffOptions
295304
): ChangeObject<ValueT>[] {
296305
return changeObjects;
297306
}

Diff for: src/diff/character.ts

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Diff from './base';
2-
import {CallbackOption, DiffCallback, ChangeObject, DiffCharsOptions, AbortableDiffOptions} from '../types';
2+
import { ChangeObject, CallbackOptionAbortable, CallbackOptionNonabortable, DiffCallbackNonabortable, DiffCharsOptionsAbortable, DiffCharsOptionsNonabortable} from '../types';
33

44
class CharacterDiff extends Diff<string, string> {}
55

@@ -8,18 +8,28 @@ export const characterDiff = new CharacterDiff();
88
export function diffChars(
99
oldStr: string,
1010
newStr: string,
11-
options: (DiffCharsOptions & CallbackOption<string>) | DiffCallback<string>
11+
options: DiffCallbackNonabortable<string>
12+
): undefined;
13+
export function diffChars(
14+
oldStr: string,
15+
newStr: string,
16+
options: DiffCharsOptionsAbortable & CallbackOptionAbortable<string>
17+
): undefined
18+
export function diffChars(
19+
oldStr: string,
20+
newStr: string,
21+
options: DiffCharsOptionsNonabortable & CallbackOptionNonabortable<string>
1222
): undefined
1323
export function diffChars(
1424
oldStr: string,
1525
newStr: string,
16-
options: DiffCharsOptions & AbortableDiffOptions
17-
): ChangeObject<string>[] | undefined;
26+
options: DiffCharsOptionsAbortable
27+
): ChangeObject<string>[] | undefined
1828
export function diffChars(
1929
oldStr: string,
2030
newStr: string,
21-
options?: DiffCharsOptions
22-
): ChangeObject<string>[];
31+
options?: DiffCharsOptionsNonabortable
32+
): ChangeObject<string>[]
2333
export function diffChars(
2434
oldStr: string,
2535
newStr: string,

Diff for: src/diff/css.ts

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Diff from './base';
2-
import { AbortableDiffOptions, CallbackOption, ChangeObject, DiffCallback, DiffCssOptions } from '../types';
2+
import { ChangeObject, CallbackOptionAbortable, CallbackOptionNonabortable, DiffCallbackNonabortable, DiffCssOptionsAbortable, DiffCssOptionsNonabortable} from '../types';
33

44
class CssDiff extends Diff<string, string> {
55
protected tokenize(value: string) {
@@ -12,10 +12,28 @@ export const cssDiff = new CssDiff();
1212
export function diffCss(
1313
oldStr: string,
1414
newStr: string,
15-
options: (DiffCssOptions & CallbackOption<string>) | DiffCallback<string>
15+
options: DiffCallbackNonabortable<string>
16+
): undefined;
17+
export function diffCss(
18+
oldStr: string,
19+
newStr: string,
20+
options: DiffCssOptionsAbortable & CallbackOptionAbortable<string>
21+
): undefined
22+
export function diffCss(
23+
oldStr: string,
24+
newStr: string,
25+
options: DiffCssOptionsNonabortable & CallbackOptionNonabortable<string>
1626
): undefined
17-
export function diffCss(oldStr: string, newStr: string, options: DiffCssOptions & AbortableDiffOptions): ChangeObject<string>[] | undefined;
18-
export function diffCss(oldStr: string, newStr: string, options?: DiffCssOptions): ChangeObject<string>[];
27+
export function diffCss(
28+
oldStr: string,
29+
newStr: string,
30+
options: DiffCssOptionsAbortable
31+
): ChangeObject<string>[] | undefined
32+
export function diffCss(
33+
oldStr: string,
34+
newStr: string,
35+
options?: DiffCssOptionsNonabortable
36+
): ChangeObject<string>[]
1937
export function diffCss(oldStr: string, newStr: string, options?): undefined | ChangeObject<string>[] {
2038
return cssDiff.diff(oldStr, newStr, options);
2139
}

Diff for: src/diff/json.ts

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Diff from './base';
2-
import { AbortableDiffOptions, CallbackOption, ChangeObject, DiffCallback, DiffJsonOptions } from '../types';
2+
import { ChangeObject, CallbackOptionAbortable, CallbackOptionNonabortable, DiffCallbackNonabortable, DiffJsonOptionsAbortable, DiffJsonOptionsNonabortable} from '../types';
33
import {lineDiff} from './line';
44

55
class JsonDiff extends Diff<string, string> {
@@ -11,13 +11,13 @@ class JsonDiff extends Diff<string, string> {
1111

1212
protected tokenize = lineDiff.tokenize;
1313

14-
protected castInput(value: string, options: DiffJsonOptions) {
14+
protected castInput(value: string, options: DiffJsonOptionsNonabortable | DiffJsonOptionsAbortable) {
1515
const {undefinedReplacement, stringifyReplacer = (k, v) => typeof v === 'undefined' ? undefinedReplacement : v} = options;
1616

1717
return typeof value === 'string' ? value : JSON.stringify(canonicalize(value, null, null, stringifyReplacer), stringifyReplacer, ' ');
1818
}
1919

20-
protected equals(left: string, right: string, options: DiffJsonOptions) {
20+
protected equals(left: string, right: string, options: DiffJsonOptionsNonabortable | DiffJsonOptionsAbortable) {
2121
return super.equals(left.replace(/,([\r\n])/g, '$1'), right.replace(/,([\r\n])/g, '$1'), options);
2222
}
2323
}
@@ -28,10 +28,28 @@ const jsonDiff = new JsonDiff();
2828
export function diffJson(
2929
oldStr: string,
3030
newStr: string,
31-
options: (DiffJsonOptions & CallbackOption<string>) | DiffCallback<string>
31+
options: DiffCallbackNonabortable<string>
32+
): undefined;
33+
export function diffJson(
34+
oldStr: string,
35+
newStr: string,
36+
options: DiffJsonOptionsAbortable & CallbackOptionAbortable<string>
37+
): undefined
38+
export function diffJson(
39+
oldStr: string,
40+
newStr: string,
41+
options: DiffJsonOptionsNonabortable & CallbackOptionNonabortable<string>
3242
): undefined
33-
export function diffJson(oldStr: string, newStr: string, options: DiffJsonOptions & AbortableDiffOptions): ChangeObject<string>[] | undefined;
34-
export function diffJson(oldStr: string, newStr: string, options?: DiffJsonOptions): ChangeObject<string>[];
43+
export function diffJson(
44+
oldStr: string,
45+
newStr: string,
46+
options: DiffJsonOptionsAbortable
47+
): ChangeObject<string>[] | undefined
48+
export function diffJson(
49+
oldStr: string,
50+
newStr: string,
51+
options?: DiffJsonOptionsNonabortable
52+
): ChangeObject<string>[]
3553
export function diffJson(oldStr: string, newStr: string, options?): undefined | ChangeObject<string>[] {
3654
return jsonDiff.diff(oldStr, newStr, options);
3755
}

Diff for: src/diff/line.ts

+45-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import Diff from './base';
2-
import { AbortableDiffOptions, CallbackOption, ChangeObject, DiffCallback, DiffLinesOptions } from '../types';
2+
import { ChangeObject, CallbackOptionAbortable, CallbackOptionNonabortable, DiffCallbackNonabortable, DiffLinesOptionsAbortable, DiffLinesOptionsNonabortable} from '../types';
33
import {generateOptions} from '../util/params';
44

55

66
class LineDiff extends Diff<string, string> {
77
// public so it can be copied by jsonDiff
8-
public tokenize(value: string, options: DiffLinesOptions) {
8+
public tokenize(value: string, options: DiffLinesOptionsAbortable | DiffLinesOptionsNonabortable) {
99
if(options.stripTrailingCr) {
1010
// remove one \r before \n to match GNU diff's --strip-trailing-cr behavior
1111
value = value.replace(/\r\n/g, '\n');
@@ -33,7 +33,7 @@ class LineDiff extends Diff<string, string> {
3333
return retLines;
3434
}
3535

36-
protected equals(left: string, right: string, options: DiffLinesOptions) {
36+
protected equals(left: string, right: string, options: DiffLinesOptionsAbortable | DiffLinesOptionsNonabortable) {
3737
// If we're ignoring whitespace, we need to normalise lines by stripping
3838
// whitespace before checking equality. (This has an annoying interaction
3939
// with newlineIsToken that requires special handling: if newlines get their
@@ -65,10 +65,28 @@ export const lineDiff = new LineDiff();
6565
export function diffLines(
6666
oldStr: string,
6767
newStr: string,
68-
options: (DiffLinesOptions & CallbackOption<string>) | DiffCallback<string>
68+
options: DiffCallbackNonabortable<string>
69+
): undefined;
70+
export function diffLines(
71+
oldStr: string,
72+
newStr: string,
73+
options: DiffLinesOptionsAbortable & CallbackOptionAbortable<string>
74+
): undefined
75+
export function diffLines(
76+
oldStr: string,
77+
newStr: string,
78+
options: DiffLinesOptionsNonabortable & CallbackOptionNonabortable<string>
6979
): undefined
70-
export function diffLines(oldStr: string, newStr: string, options: DiffLinesOptions & AbortableDiffOptions): ChangeObject<string>[] | undefined;
71-
export function diffLines(oldStr: string, newStr: string, options?: DiffLinesOptions): ChangeObject<string>[];
80+
export function diffLines(
81+
oldStr: string,
82+
newStr: string,
83+
options: DiffLinesOptionsAbortable
84+
): ChangeObject<string>[] | undefined
85+
export function diffLines(
86+
oldStr: string,
87+
newStr: string,
88+
options?: DiffLinesOptionsNonabortable
89+
): ChangeObject<string>[]
7290
export function diffLines(oldStr: string, newStr: string, options?): undefined | ChangeObject<string>[] {
7391
return lineDiff.diff(oldStr, newStr, options);
7492
}
@@ -82,10 +100,28 @@ export function diffLines(oldStr: string, newStr: string, options?): undefined |
82100
export function diffTrimmedLines(
83101
oldStr: string,
84102
newStr: string,
85-
options: (DiffLinesOptions & CallbackOption<string>) | DiffCallback<string>
103+
options: DiffCallbackNonabortable<string>
104+
): undefined;
105+
export function diffTrimmedLines(
106+
oldStr: string,
107+
newStr: string,
108+
options: DiffLinesOptionsAbortable & CallbackOptionAbortable<string>
109+
): undefined
110+
export function diffTrimmedLines(
111+
oldStr: string,
112+
newStr: string,
113+
options: DiffLinesOptionsNonabortable & CallbackOptionNonabortable<string>
86114
): undefined
87-
export function diffTrimmedLines(oldStr: string, newStr: string, options: DiffLinesOptions & AbortableDiffOptions): ChangeObject<string>[] | undefined;
88-
export function diffTrimmedLines(oldStr: string, newStr: string, options?: DiffLinesOptions): ChangeObject<string>[];
115+
export function diffTrimmedLines(
116+
oldStr: string,
117+
newStr: string,
118+
options: DiffLinesOptionsAbortable
119+
): ChangeObject<string>[] | undefined
120+
export function diffTrimmedLines(
121+
oldStr: string,
122+
newStr: string,
123+
options?: DiffLinesOptionsNonabortable
124+
): ChangeObject<string>[]
89125
export function diffTrimmedLines(oldStr: string, newStr: string, options?): undefined | ChangeObject<string>[] {
90126
options = generateOptions(options, {ignoreWhitespace: true});
91127
return lineDiff.diff(oldStr, newStr, options);

0 commit comments

Comments
 (0)