Convert <gr-diff-view> to typescript

The change converts the following files to typescript:

* elements/diff/gr-diff-view/gr-diff-view.ts

Change-Id: Id848d3090f4ea44757fdfed7bc4d8430070e05b1
This commit is contained in:
Ben Rohlfs
2020-10-04 20:23:01 +02:00
committed by Dmitrii Filippov
parent 375a02f211
commit 3f3d989196
10 changed files with 1258 additions and 794 deletions

View File

@@ -131,15 +131,20 @@ export class ChangeComments {
return this._robotComments;
}
findCommentById(commentId: UrlEncodedCommentId): Comment | undefined {
const findComment = (comments: {[path: string]: CommentBasics[]}) => {
findCommentById(commentId?: UrlEncodedCommentId): UIComment | undefined {
if (!commentId) return undefined;
const findComment = (comments: {[path: string]: UIComment[]}) => {
let comment;
for (const path of Object.keys(comments)) {
comment = comment || comments[path].find(c => c.id === commentId);
}
return comment;
};
return findComment(this._comments) || findComment(this._robotComments);
return (
findComment(this._comments) ||
findComment(this._robotComments) ||
findComment(this._drafts)
);
}
/**
@@ -580,14 +585,14 @@ export class ChangeComments {
export const _testOnly_findCommentById =
ChangeComments.prototype.findCommentById;
interface GrCommentApi {
export interface GrCommentApi {
$: {
restAPI: RestApiService & Element;
};
}
@customElement('gr-comment-api')
class GrCommentApi extends GestureEventListeners(
export class GrCommentApi extends GestureEventListeners(
LegacyElementMixin(PolymerElement)
) {
static get template() {

View File

@@ -34,7 +34,6 @@ import {
import {
Comment,
isDraft,
PatchSetFile,
sortComments,
UIComment,
} from '../../../utils/comment-util';
@@ -70,6 +69,7 @@ import {PolymerDeepPropertyChange} from '@polymer/polymer/interfaces';
import {FilesWebLinks} from '../gr-patch-range-select/gr-patch-range-select';
import {LineNumber} from '../gr-diff/gr-diff-line';
import {GrCommentThread} from '../../shared/gr-comment-thread/gr-comment-thread';
import {PatchSetFile} from '../../../types/types';
const MSG_EMPTY_BLAME = 'No blame information for this diff.';

File diff suppressed because it is too large Load Diff

View File

@@ -24,6 +24,7 @@ import {SPECIAL_PATCH_SET_NUM} from '../../../utils/patch-set-util.js';
import {Shortcut} from '../../../mixins/keyboard-shortcut-mixin/keyboard-shortcut-mixin.js';
import {_testOnly_findCommentById} from '../gr-comment-api/gr-comment-api.js';
import {appContext} from '../../../services/app-context.js';
import {GerritView} from '../../core/gr-navigation/gr-navigation.js';
const basicFixture = fixtureFromElement('gr-diff-view');
@@ -82,7 +83,7 @@ suite('gr-diff-view tests', () => {
};
}
setup(() => {
setup(async () => {
clock = sinon.useFakeTimers();
sinon.stub(appContext.flagsService, 'isEnabled').returns(true);
stub('gr-rest-api-interface', {
@@ -118,6 +119,14 @@ suite('gr-diff-view tests', () => {
},
});
element = basicFixture.instantiate();
element._changeNum = '42';
element._path = 'some/path.txt';
element._change = {};
element._diff = {content: []};
element._patchRange = {
patchNum: 77,
basePatchNum: 'PARENT',
};
sinon.stub(element.$.commentAPI, 'loadAll').returns(Promise.resolve({
_comments: {'/COMMIT_MSG': [{id: 'c1', line: 10, patch_set: 2,
__commentSide: 'left', path: '/COMMIT_MSG'}]},
@@ -127,7 +136,8 @@ suite('gr-diff-view tests', () => {
getCommentsBySideForPath: () => {},
findCommentById: _testOnly_findCommentById,
}));
return element._loadComments();
await element._loadComments();
await flush();
});
teardown(() => {
@@ -269,13 +279,22 @@ suite('gr-diff-view tests', () => {
assert.equal(element._isFileUnchanged(diff), true);
});
test('diff toast to go to latest is shown and not base', () => {
test('diff toast to go to latest is shown and not base', async () => {
sinon.stub(element.reporting, 'diffViewDisplayed');
sinon.stub(element, '_loadBlame');
sinon.stub(element.$.diffHost, 'reload').returns(Promise.resolve());
sinon.spy(element, '_paramsChanged');
sinon.stub(element, '_getChangeDetail').returns(Promise.resolve(
generateChange({revisionsCount: 11})));
element.$.restAPI.getDiffChangeDetail.restore();
sinon.stub(element.$.restAPI, 'getDiffChangeDetail')
.returns(
Promise.resolve(generateChange({revisionsCount: 11})));
element._patchRange = {
patchNum: '2',
basePatchNum: '1',
};
sinon.stub(element, '_isFileUnchanged').returns(false);
const toastStub =
sinon.stub(element, '_displayDiffBaseAgainstLeftToast');
element.params = {
view: GerritNav.View.DIFF,
changeNum: '42',
@@ -283,12 +302,8 @@ suite('gr-diff-view tests', () => {
commentId: 'c1',
commentLink: true,
};
element._change = generateChange({revisionsCount: 11});
const toastStub =
sinon.stub(element, '_displayDiffBaseAgainstLeftToast');
return element._paramsChanged.returnValues[0].then(() => {
assert.isTrue(toastStub.called);
});
await element._paramsChanged.returnValues[0];
assert.isTrue(toastStub.called);
});
test('toggle left diff with a hotkey', () => {
@@ -1251,7 +1266,7 @@ suite('gr-diff-view tests', () => {
});
test('_getLineOfInterest', () => {
assert.isNull(element._getLineOfInterest({}));
assert.isUndefined(element._getLineOfInterest({}));
element._focusLineNum = 12;
let result = element._getLineOfInterest({});
@@ -1334,10 +1349,20 @@ suite('gr-diff-view tests', () => {
});
suite('_initPatchRange', () => {
setup(async () => {
// const changeDetail = generateChange({revisionsCount: 5});
// sinon.stub(element.$.restAPI, 'getDiffChangeDetail')
// .returns(Promise.resolve(changeDetail));
element.params = {
view: GerritView.DIFF,
changeNum: '42',
patchNum: '3',
};
await flush();
});
test('empty', () => {
sinon.stub(element, '_getCommentsForPath');
sinon.stub(element, '_getPaths').returns(new Map());
element.params = {};
element._initPatchRange();
assert.equal(Object.keys(element._commentMap).length, 0);
});
@@ -1354,7 +1379,6 @@ suite('gr-diff-view tests', () => {
basePatchNum: '3',
patchNum: '5',
};
element.params = {};
element._initPatchRange();
assert.deepEqual(Object.keys(element._commentMap),
['path/to/file/one.cpp', 'path-to/file/two.py']);
@@ -1523,6 +1547,9 @@ suite('gr-diff-view tests', () => {
.then(reviewed => assert.isFalse(reviewed)));
promises.push(element._getReviewedStatus(false, null, null, 'path')
.then(reviewed => assert.isFalse(reviewed)));
promises.push(element._getReviewedStatus(false, 3, 5, 'path')
.then(reviewed => assert.isTrue(reviewed)));
return Promise.all(promises);
@@ -1613,6 +1640,7 @@ suite('gr-diff-view tests', () => {
patchNum: 1,
basePatchNum: 'PARENT',
};
element._change = generateChange({revisionsCount: 1});
flush();
assert.isTrue(GerritNav.navigateToDiff.notCalled);
@@ -1754,6 +1782,7 @@ suite('gr-diff-view tests', () => {
getReviewedFiles() { return Promise.resolve([]); },
});
element = basicFixture.instantiate();
element._changeNum = '42';
return element._loadComments();
});

View File

@@ -20,7 +20,14 @@ import {
GroupDetailView,
RepoDetailView,
} from './core/gr-navigation/gr-navigation';
import {DashboardId, GroupId, RepoName} from '../types/common';
import {
DashboardId,
GroupId,
NumericChangeId,
PatchSetNum,
RepoName,
UrlEncodedCommentId,
} from '../types/common';
export interface AppElement extends HTMLElement {
params: AppElementParams | GenerateUrlParameters;
@@ -86,6 +93,19 @@ export interface AppElementAgreementParam {
view: GerritView.AGREEMENTS;
}
export interface AppElementDiffViewParam {
view: GerritView.DIFF;
changeNum: NumericChangeId;
project?: RepoName;
commentId?: UrlEncodedCommentId;
path?: string;
patchNum?: PatchSetNum;
basePatchNum?: PatchSetNum;
lineNum: number;
leftSide?: boolean;
commentLink?: boolean;
}
export interface AppElementJustRegisteredParams {
// We use params.view === ... as a type guard.
// The view?: never tells to the compiler that
@@ -106,4 +126,5 @@ export type AppElementParams =
| AppElementSearchParam
| AppElementSettingsParam
| AppElementAgreementParam
| AppElementJustRegisteredParams;
| AppElementJustRegisteredParams
| AppElementDiffViewParam;

View File

@@ -544,6 +544,7 @@ export interface CustomKeyboardEvent extends CustomEvent, EventApi {
readonly metaKey: boolean;
readonly shiftKey: boolean;
readonly keyCode: number;
readonly repeat: boolean;
}
function getKeyboardEvent(e: CustomKeyboardEvent): CustomKeyboardEvent {
const event = dom(e.detail ? e.detail.keyboardEvent : e);
@@ -1091,6 +1092,8 @@ export interface KeyboardShortcutMixinInterface {
getKeyboardEvent(e: CustomKeyboardEvent): CustomKeyboardEvent;
addKeyboardShortcutDirectoryListener(listener: ShortcutListener): void;
removeKeyboardShortcutDirectoryListener(listener: ShortcutListener): void;
// TODO(TS): Remove underscore. Apparently not a private method.
_throttleWrap(eventListener: EventListener): EventListener;
}
export function _testOnly_getShortcutManagerInstance() {

View File

@@ -818,6 +818,11 @@ export interface RestApiService {
topic: string | null
): Promise<string>;
getChangeFiles(
changeNum: NumericChangeId,
patchRange: PatchRange
): Promise<FileNameToFileInfoMap | undefined>;
getChangeOrEditFiles(
changeNum: NumericChangeId,
patchRange: PatchRange
@@ -844,4 +849,6 @@ export interface RestApiService {
): Promise<Response | undefined>;
getTopMenus(errFn?: ErrorCallback): Promise<TopMenuEntryInfo[] | undefined>;
setInProjectLookup(changeNum: NumericChangeId, project: RepoName): void;
}

View File

@@ -1158,6 +1158,7 @@ export interface UserConfigInfo {
* https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#comment-info
*/
export interface CommentInfo {
// TODO(TS): Make this required.
patch_set?: PatchSetNum;
id: UrlEncodedCommentId;
path?: string;
@@ -1441,7 +1442,7 @@ export interface CommentLinks {
/**
* The ConfigInfo entity contains information about the effective
* projectconfiguration.
* project configuration.
* https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#config-info
*/
export interface ConfigInfo {
@@ -1468,7 +1469,8 @@ export interface ConfigInfo {
}
/**
* The ProjectAccessInfo entity contains information about the access rights for a project
* The ProjectAccessInfo entity contains information about the access rights for
* a project.
* https://gerrit-review.googlesource.com/Documentation/rest-api-access.html#project-access-info
*/
export interface ProjectAccessInfo {

View File

@@ -14,12 +14,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Side} from '../constants/constants';
import {DiffViewMode, Side} from '../constants/constants';
import {IronA11yAnnouncer} from '@polymer/iron-a11y-announcer/iron-a11y-announcer';
import {GrDiffLine} from '../elements/diff/gr-diff/gr-diff-line';
import {FlattenedNodesObserver} from '@polymer/polymer/lib/utils/flattened-nodes-observer';
import {PaperInputElement} from '@polymer/paper-input/paper-input';
import {CommitId} from './common';
import {CommitId, NumericChangeId, PatchRange, PatchSetNum} from './common';
export function notUndefined<T>(x: T | undefined): x is T {
return x !== undefined;
@@ -165,3 +165,51 @@ export interface DiffLayer {
addListener?(listener: DiffLayerListener): void;
removeListener?(listener: DiffLayerListener): void;
}
export interface ChangeViewState {
changeNum: NumericChangeId | null;
patchRange: PatchRange | null;
selectedFileIndex: number;
showReplyDialog: boolean;
showDownloadDialog: boolean;
diffMode: DiffViewMode | null;
numFilesShown: number | null;
scrollTop: number;
}
export interface ChangeListViewState {
query: string | null;
offset: number;
selectedChangeIndex: number;
}
export interface DashboardViewState {
selectedChangeIndex: number;
}
export interface ViewState {
changeView: ChangeViewState;
changeListView: ChangeListViewState;
dashboardView: DashboardViewState;
}
export interface PatchSetFile {
path: string;
basePath?: string;
patchNum?: PatchSetNum;
}
export interface PatchNumOnly {
patchNum: PatchSetNum;
}
export function isPatchSetFile(
x: PatchSetFile | PatchNumOnly
): x is PatchSetFile {
return !!(x as PatchSetFile).path;
}
export interface FileRange {
basePath?: string;
path: string;
}

View File

@@ -90,7 +90,7 @@ export function computeDisplayPath(path: string) {
return path;
}
export function isMagicPath(path: string) {
export function isMagicPath(path?: string) {
return (
!!path &&
(path === SpecialFilePath.COMMIT_MESSAGE ||