mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
Adds support for TypeScript 4.7. Changes include: * Bumping the TS version as well as some Bazel dependencies to include https://github.com/bazelbuild/rules_nodejs/pull/3420. * Adding a backwards-compatibility layer for calls to `updateTypeParameterDeclaration`. * Making `LView` generic in order to make it easier to type the context based on the usage. Currently the context can be 4 different types which coupled with stricter type checking would required a lot of extra casting all over `core`. * Fixing a bunch of miscellaneous type errors. * Removing assertions of `ReferenceEntry.isDefinition` in a few of the language service tests. The field isn't returned by TS anymore and we weren't using it for anything. * Resolving in error in the language service that was caused by TS attempting to parse HTML files when we try to open them. Previous TS was silently setting them as `ScriptKind.Unknown` and ignoring the errors, but now it throws. I've worked around it by setting them as `ScriptKind.JSX`. PR Close #45749
92 lines
3.2 KiB
TypeScript
92 lines
3.2 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright Google LLC All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
* found in the LICENSE file at https://angular.io/license
|
|
*/
|
|
|
|
// tslint:disable-next-line:deprecation
|
|
import {DefaultIterableDiffer} from '@angular/core';
|
|
|
|
export interface MovedRecord {
|
|
currentIndex: number;
|
|
previousIndex: number;
|
|
}
|
|
|
|
export const diff = <T>(differ: DefaultIterableDiffer<T>, a: T[], b: T[]):
|
|
{newItems: T[]; removedItems: T[]; movedItems: T[];} => {
|
|
differ.diff(a);
|
|
differ.diff(b);
|
|
|
|
const alreadySet: boolean[] = [];
|
|
const movedItems: T[] = [];
|
|
|
|
// We first have to set the moved items to their correct positions.
|
|
// Keep in mind that the track by function may not guarantee
|
|
// that we haven't changed any of the items' props.
|
|
differ.forEachMovedItem(record => {
|
|
if (record.currentIndex === null) {
|
|
return;
|
|
}
|
|
if (record.previousIndex === null) {
|
|
return;
|
|
}
|
|
// We want to preserve the reference so that a default
|
|
// track by function used by the CDK, for instance, can
|
|
// recognize that this item's identity hasn't changed.
|
|
// At the same time, since we don't have the guarantee
|
|
// that we haven't already set the previousIndex while
|
|
// iterating, we need to check that. If we have, we assign
|
|
// this array item to a new object. We don't want to risk
|
|
// changing the properties of an object we'll use in the future.
|
|
if (!alreadySet[record.previousIndex]) {
|
|
a[record.currentIndex] = a[record.previousIndex];
|
|
} else {
|
|
a[record.currentIndex] = {} as unknown as T;
|
|
}
|
|
Object.keys(b[record.currentIndex] as unknown as {}).forEach(prop => {
|
|
// TypeScript's type inference didn't follow the check from above.
|
|
if (record.currentIndex === null) {
|
|
return;
|
|
}
|
|
(a[record.currentIndex] as any)[prop] = (b[record.currentIndex] as any)[prop];
|
|
});
|
|
if (!alreadySet[record.previousIndex]) {
|
|
// tslint:disable-next-line: no-non-null-assertion
|
|
a[record.previousIndex] = null!;
|
|
}
|
|
alreadySet[record.currentIndex] = true;
|
|
movedItems.push(a[record.currentIndex]);
|
|
});
|
|
|
|
// Now we can set the new items and remove the deleted ones.
|
|
const newItems: T[] = [];
|
|
const removedItems: T[] = [];
|
|
differ.forEachAddedItem(record => {
|
|
if (record.currentIndex !== null && record.previousIndex === null) {
|
|
a[record.currentIndex] = record.item;
|
|
alreadySet[record.currentIndex] = true;
|
|
newItems.push(record.item);
|
|
}
|
|
});
|
|
|
|
differ.forEachRemovedItem(record => {
|
|
if (record.previousIndex === null) {
|
|
return;
|
|
}
|
|
if (record.currentIndex === null && !alreadySet[record.previousIndex]) {
|
|
// tslint:disable-next-line: no-non-null-assertion
|
|
a[record.previousIndex] = null!;
|
|
}
|
|
removedItems.push(record.item);
|
|
});
|
|
|
|
for (let i = a.length - 1; i >= 0; i--) {
|
|
if (a[i] === null) {
|
|
a.splice(i, 1);
|
|
}
|
|
}
|
|
|
|
return {newItems, removedItems, movedItems};
|
|
};
|