Convert files to typescript
The change converts the following files to typescript: * elements/core/gr-router/gr-router.ts Change-Id: I827da7e6ed664b3d2188a849b863cf53c5844758
This commit is contained in:
@@ -66,7 +66,7 @@ export class GrCommitInfo extends GestureEventListeners(
|
||||
}
|
||||
|
||||
@computed('change', 'commitInfo', 'serverConfig')
|
||||
get _webLink(): string {
|
||||
get _webLink(): string | undefined {
|
||||
if (!this.change || !this.commitInfo || !this.serverConfig) {
|
||||
return '';
|
||||
}
|
||||
|
||||
@@ -30,6 +30,8 @@ import {
|
||||
Hashtag,
|
||||
UrlEncodedCommentId,
|
||||
CommentLinks,
|
||||
ParentPatchSetNum,
|
||||
ServerInfo,
|
||||
} from '../../../types/common';
|
||||
|
||||
// Navigation parameters object format:
|
||||
@@ -120,11 +122,6 @@ const uninitializedMapCommentLinks: MapCommentLinksCallback = () => {
|
||||
return {};
|
||||
};
|
||||
|
||||
// TODO(TS): PatchSetNum type express an API type, it is not good to add
|
||||
// PARENT into it. Find a better way to add PARENT patchset into our code
|
||||
type ParentPatchSetNum = 'PARENT';
|
||||
const PARENT_PATCHNUM: ParentPatchSetNum = 'PARENT';
|
||||
|
||||
const USER_PLACEHOLDER_PATTERN = /\${user}/g;
|
||||
|
||||
export interface DashboardSection {
|
||||
@@ -250,6 +247,10 @@ export interface GenerateUrlChangeViewParameters {
|
||||
edit?: boolean;
|
||||
host?: string;
|
||||
messageHash?: string;
|
||||
queryMap?: Map<string, string> | URLSearchParams;
|
||||
|
||||
// TODO(TS): querystring isn't set anywhere, try to remove
|
||||
querystring?: string;
|
||||
}
|
||||
|
||||
export interface GenerateUrlRepoViewParameters {
|
||||
@@ -263,6 +264,11 @@ export interface GenerateUrlDashboardViewParameters {
|
||||
user?: string;
|
||||
repo?: RepoName;
|
||||
dashboard?: DashboardId;
|
||||
|
||||
// TODO(TS): properties bellow aren't set anywhere, try to remove
|
||||
project?: RepoName;
|
||||
sections?: DashboardSection[];
|
||||
title?: string;
|
||||
}
|
||||
|
||||
export interface GenerateUrlGroupViewParameters {
|
||||
@@ -277,7 +283,7 @@ export interface GenerateUrlEditViewParameters {
|
||||
project: RepoName;
|
||||
path: string;
|
||||
patchNum: PatchSetNum;
|
||||
lineNum?: number;
|
||||
lineNum?: number | string;
|
||||
}
|
||||
|
||||
export interface GenerateUrlRootViewParameters {
|
||||
@@ -293,11 +299,13 @@ export interface GenerateUrlDiffViewParameters {
|
||||
changeNum: NumericChangeId | LegacyChangeId;
|
||||
project: RepoName;
|
||||
path?: string;
|
||||
patchNum?: PatchSetNum;
|
||||
basePatchNum?: PatchSetNum | ParentPatchSetNum;
|
||||
lineNum?: number;
|
||||
patchNum?: PatchSetNum | null;
|
||||
basePatchNum?: PatchSetNum | null;
|
||||
lineNum?: number | string;
|
||||
leftSide?: boolean;
|
||||
commentId?: UrlEncodedCommentId;
|
||||
// TODO(TS): remove - property is set but never used
|
||||
commentLink?: boolean;
|
||||
}
|
||||
|
||||
export type GenerateUrlParameters =
|
||||
@@ -311,27 +319,47 @@ export type GenerateUrlParameters =
|
||||
| GenerateUrlSettingsViewParameters
|
||||
| GenerateUrlDiffViewParameters;
|
||||
|
||||
export function isGenerateUrlChangeViewParameters(
|
||||
x: GenerateUrlParameters
|
||||
): x is GenerateUrlChangeViewParameters {
|
||||
return x.view === GerritView.CHANGE;
|
||||
}
|
||||
|
||||
export function isGenerateUrlEditViewParameters(
|
||||
x: GenerateUrlParameters
|
||||
): x is GenerateUrlEditViewParameters {
|
||||
return x.view === GerritView.EDIT;
|
||||
}
|
||||
|
||||
export function isGenerateUrlDiffViewParameters(
|
||||
x: GenerateUrlParameters
|
||||
): x is GenerateUrlDiffViewParameters {
|
||||
return x.view === GerritView.DIFF;
|
||||
}
|
||||
|
||||
export interface GenerateWebLinksOptions {
|
||||
weblinks?: GeneratedWebLink[];
|
||||
config?: ServerInfo;
|
||||
}
|
||||
|
||||
export interface GenerateWebLinksPatchsetParameters {
|
||||
type: WeblinkType.PATCHSET;
|
||||
repo: RepoName;
|
||||
commit?: CommitId;
|
||||
// TODO(TS): provide better typing
|
||||
options?: unknown;
|
||||
options?: GenerateWebLinksOptions;
|
||||
}
|
||||
export interface GenerateWebLinksFileParameters {
|
||||
type: WeblinkType.FILE;
|
||||
repo: RepoName;
|
||||
commit: CommitId;
|
||||
file: string;
|
||||
// TODO(TS): provide better typing
|
||||
options?: unknown;
|
||||
options?: GenerateWebLinksOptions;
|
||||
}
|
||||
export interface GenerateWebLinksChangeParameters {
|
||||
type: WeblinkType.CHANGE;
|
||||
repo: RepoName;
|
||||
commit: CommitId;
|
||||
// TODO(TS): provide better typing
|
||||
options?: unknown;
|
||||
options?: GenerateWebLinksOptions;
|
||||
}
|
||||
|
||||
export type GenerateWebLinksParameters =
|
||||
@@ -343,7 +371,7 @@ export type NavigateCallback = (target: string, redirect?: boolean) => void;
|
||||
export type GenerateUrlCallback = (params: GenerateUrlParameters) => string;
|
||||
export type GenerateWebLinksCallback = (
|
||||
params: GenerateWebLinksParameters
|
||||
) => WebLink[] | WebLink;
|
||||
) => GeneratedWebLink[] | GeneratedWebLink;
|
||||
|
||||
export type MapCommentLinksCallback = (patterns: CommentLinks) => CommentLinks;
|
||||
|
||||
@@ -353,6 +381,12 @@ export interface WebLink {
|
||||
url: string;
|
||||
}
|
||||
|
||||
export interface GeneratedWebLink {
|
||||
name?: string;
|
||||
label?: string;
|
||||
url?: string;
|
||||
}
|
||||
|
||||
export enum GerritView {
|
||||
ADMIN = 'admin',
|
||||
AGREEMENTS = 'agreements',
|
||||
@@ -407,10 +441,7 @@ export const GerritNav = {
|
||||
|
||||
mapCommentlinks: uninitializedMapCommentLinks,
|
||||
|
||||
_checkPatchRange(
|
||||
patchNum?: PatchSetNum,
|
||||
basePatchNum?: PatchSetNum | ParentPatchSetNum
|
||||
) {
|
||||
_checkPatchRange(patchNum?: PatchSetNum, basePatchNum?: PatchSetNum) {
|
||||
if (basePatchNum && !patchNum) {
|
||||
throw new Error('Cannot use base patch number without patch number.');
|
||||
}
|
||||
@@ -567,11 +598,11 @@ export const GerritNav = {
|
||||
getUrlForChange(
|
||||
change: ChangeInfo,
|
||||
patchNum?: PatchSetNum,
|
||||
basePatchNum?: PatchSetNum | ParentPatchSetNum,
|
||||
basePatchNum?: PatchSetNum,
|
||||
isEdit?: boolean,
|
||||
messageHash?: string
|
||||
) {
|
||||
if (basePatchNum === PARENT_PATCHNUM) {
|
||||
if (basePatchNum === ParentPatchSetNum) {
|
||||
basePatchNum = undefined;
|
||||
}
|
||||
|
||||
@@ -628,7 +659,7 @@ export const GerritNav = {
|
||||
change: ChangeInfo,
|
||||
filePath: string,
|
||||
patchNum?: PatchSetNum,
|
||||
basePatchNum?: PatchSetNum | ParentPatchSetNum,
|
||||
basePatchNum?: PatchSetNum,
|
||||
lineNum?: number
|
||||
) {
|
||||
return this.getUrlForDiffById(
|
||||
@@ -662,11 +693,11 @@ export const GerritNav = {
|
||||
project: RepoName,
|
||||
filePath: string,
|
||||
patchNum?: PatchSetNum,
|
||||
basePatchNum?: PatchSetNum | ParentPatchSetNum,
|
||||
basePatchNum?: PatchSetNum,
|
||||
lineNum?: number,
|
||||
leftSide?: boolean
|
||||
) {
|
||||
if (basePatchNum === PARENT_PATCHNUM) {
|
||||
if (basePatchNum === ParentPatchSetNum) {
|
||||
basePatchNum = undefined;
|
||||
}
|
||||
|
||||
@@ -727,7 +758,7 @@ export const GerritNav = {
|
||||
change: ChangeInfo,
|
||||
filePath: string,
|
||||
patchNum?: PatchSetNum,
|
||||
basePatchNum?: PatchSetNum | ParentPatchSetNum
|
||||
basePatchNum?: PatchSetNum
|
||||
) {
|
||||
this._navigate(
|
||||
this.getUrlForDiff(change, filePath, patchNum, basePatchNum)
|
||||
@@ -867,8 +898,8 @@ export const GerritNav = {
|
||||
repo: RepoName,
|
||||
commit: CommitId,
|
||||
file: string,
|
||||
options?: unknown
|
||||
): WebLink[] {
|
||||
options?: GenerateWebLinksOptions
|
||||
): GeneratedWebLink[] {
|
||||
const params: GenerateWebLinksFileParameters = {
|
||||
type: WeblinkType.FILE,
|
||||
repo,
|
||||
@@ -878,14 +909,14 @@ export const GerritNav = {
|
||||
if (options) {
|
||||
params.options = options;
|
||||
}
|
||||
return ([] as WebLink[]).concat(this._generateWeblinks(params));
|
||||
return ([] as GeneratedWebLink[]).concat(this._generateWeblinks(params));
|
||||
},
|
||||
|
||||
getPatchSetWeblink(
|
||||
repo: RepoName,
|
||||
commit?: CommitId,
|
||||
options?: unknown
|
||||
): WebLink {
|
||||
options?: GenerateWebLinksOptions
|
||||
): GeneratedWebLink {
|
||||
const params: GenerateWebLinksPatchsetParameters = {
|
||||
type: WeblinkType.PATCHSET,
|
||||
repo,
|
||||
@@ -899,7 +930,7 @@ export const GerritNav = {
|
||||
// TODO(TS): Unclear what to do with empty array.
|
||||
// Either write a comment why result can't be empty or change the return
|
||||
// type or add a check.
|
||||
return result.pop() as WebLink;
|
||||
return result.pop()!;
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
@@ -908,8 +939,8 @@ export const GerritNav = {
|
||||
getChangeWeblinks(
|
||||
repo: RepoName,
|
||||
commit: CommitId,
|
||||
options?: unknown
|
||||
): WebLink[] {
|
||||
options?: GenerateWebLinksOptions
|
||||
): GeneratedWebLink[] {
|
||||
const params: GenerateWebLinksChangeParameters = {
|
||||
type: WeblinkType.CHANGE,
|
||||
repo,
|
||||
@@ -918,7 +949,7 @@ export const GerritNav = {
|
||||
if (options) {
|
||||
params.options = options;
|
||||
}
|
||||
return ([] as WebLink[]).concat(this._generateWeblinks(params));
|
||||
return ([] as GeneratedWebLink[]).concat(this._generateWeblinks(params));
|
||||
},
|
||||
|
||||
getUserDashboard(
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -518,11 +518,12 @@ suite('gr-router tests', () => {
|
||||
|
||||
suite('param normalization', () => {
|
||||
let projectLookupStub;
|
||||
let generateUrlStub;
|
||||
|
||||
setup(() => {
|
||||
projectLookupStub = sinon
|
||||
.stub(element.$.restAPI, 'getFromProjectLookup');
|
||||
sinon.stub(element, '_generateUrl');
|
||||
generateUrlStub = sinon.stub(element, '_generateUrl');
|
||||
});
|
||||
|
||||
suite('_normalizeLegacyRouteParams', () => {
|
||||
@@ -541,9 +542,9 @@ suite('gr-router tests', () => {
|
||||
projectLookupStub.returns(Promise.resolve('foo/bar'));
|
||||
const params = {};
|
||||
return element._normalizeLegacyRouteParams(params).then(() => {
|
||||
assert.isFalse(generateUrlStub.calledOnce);
|
||||
assert.isFalse(projectLookupStub.called);
|
||||
assert.isFalse(rangeStub.called);
|
||||
assert.isNotOk(params.project);
|
||||
assert.isFalse(redirectStub.called);
|
||||
assert.isFalse(show404Stub.called);
|
||||
});
|
||||
@@ -552,10 +553,13 @@ suite('gr-router tests', () => {
|
||||
test('w/ changeNum', () => {
|
||||
projectLookupStub.returns(Promise.resolve('foo/bar'));
|
||||
const params = {changeNum: 1234};
|
||||
|
||||
return element._normalizeLegacyRouteParams(params).then(() => {
|
||||
assert.isTrue(generateUrlStub.calledOnce);
|
||||
const updatedParams = generateUrlStub.lastCall.args[0];
|
||||
assert.isTrue(projectLookupStub.called);
|
||||
assert.isTrue(rangeStub.called);
|
||||
assert.equal(params.project, 'foo/bar');
|
||||
assert.equal(updatedParams.project, 'foo/bar');
|
||||
assert.isTrue(redirectStub.calledOnce);
|
||||
assert.isFalse(show404Stub.called);
|
||||
});
|
||||
@@ -565,9 +569,9 @@ suite('gr-router tests', () => {
|
||||
projectLookupStub.returns(Promise.resolve(undefined));
|
||||
const params = {changeNum: 1234};
|
||||
return element._normalizeLegacyRouteParams(params).then(() => {
|
||||
assert.isFalse(generateUrlStub.calledOnce);
|
||||
assert.isTrue(projectLookupStub.called);
|
||||
assert.isFalse(rangeStub.called);
|
||||
assert.isUndefined(params.project);
|
||||
assert.isFalse(redirectStub.called);
|
||||
assert.isTrue(show404Stub.calledOnce);
|
||||
});
|
||||
|
||||
103
polygerrit-ui/app/elements/gr-app-types.ts
Normal file
103
polygerrit-ui/app/elements/gr-app-types.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright (C) 2020 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import {
|
||||
GenerateUrlParameters,
|
||||
GerritView,
|
||||
GroupDetailView,
|
||||
RepoDetailView,
|
||||
} from './core/gr-navigation/gr-navigation';
|
||||
import {GroupId, RepoName} from '../types/common';
|
||||
|
||||
export interface AppElement extends HTMLElement {
|
||||
params: AppElementParams | GenerateUrlParameters;
|
||||
}
|
||||
|
||||
// TODO(TS): Remove unify AppElementParams with GenerateUrlParameters
|
||||
// Seems we can use GenerateUrlParameters instead of AppElementParams,
|
||||
// but it require some refactoring
|
||||
export interface AppElementDashboardParams {
|
||||
view: GerritView.DASHBOARD;
|
||||
project?: RepoName;
|
||||
dashboard?: string;
|
||||
user?: string;
|
||||
sections?: Array<{name: string; query: string}>;
|
||||
title?: string;
|
||||
}
|
||||
|
||||
export interface AppElementGroupParams {
|
||||
view: GerritView.GROUP;
|
||||
detail?: GroupDetailView;
|
||||
groupId: GroupId;
|
||||
}
|
||||
|
||||
export interface AppElementAdminParams {
|
||||
view: GerritView.ADMIN;
|
||||
adminView: string;
|
||||
offset?: string | number;
|
||||
filter?: string | null;
|
||||
openCreateModal?: boolean;
|
||||
}
|
||||
|
||||
export interface AppElementRepoParams {
|
||||
view: GerritView.REPO;
|
||||
detail?: RepoDetailView;
|
||||
repo: RepoName;
|
||||
offset?: string | number;
|
||||
filter?: string | null;
|
||||
}
|
||||
|
||||
export interface AppElementDocSearchParams {
|
||||
view: GerritView.DOCUMENTATION_SEARCH;
|
||||
filter: string | null;
|
||||
}
|
||||
|
||||
export interface AppElementPluginScreenParams {
|
||||
view: GerritView.PLUGIN_SCREEN;
|
||||
plugin: string;
|
||||
screen: string;
|
||||
}
|
||||
|
||||
export interface AppElementSearchParam {
|
||||
view: GerritView.SEARCH;
|
||||
query: string;
|
||||
offset: string;
|
||||
}
|
||||
|
||||
export interface AppElementSettingsParam {
|
||||
view: GerritView.SETTINGS;
|
||||
emailToken?: string;
|
||||
}
|
||||
|
||||
export interface AppElementAgreementParam {
|
||||
view: GerritView.AGREEMENTS;
|
||||
}
|
||||
|
||||
export interface AppElementJustRegisteredParams {
|
||||
justRegistered: true;
|
||||
}
|
||||
|
||||
export type AppElementParams =
|
||||
| AppElementDashboardParams
|
||||
| AppElementGroupParams
|
||||
| AppElementAdminParams
|
||||
| AppElementRepoParams
|
||||
| AppElementDocSearchParams
|
||||
| AppElementPluginScreenParams
|
||||
| AppElementSearchParam
|
||||
| AppElementSettingsParam
|
||||
| AppElementAgreementParam
|
||||
| AppElementJustRegisteredParams;
|
||||
@@ -56,6 +56,8 @@ security.polymer_resin.install({
|
||||
class GrApp extends GestureEventListeners(
|
||||
LegacyElementMixin(
|
||||
PolymerElement)) {
|
||||
// When you are converting gr-app.js to ts, implement interface AppElement
|
||||
// from the gr-app-types.ts
|
||||
static get template() { return htmlTemplate; }
|
||||
|
||||
static get is() { return 'gr-app'; }
|
||||
|
||||
@@ -801,15 +801,17 @@ export interface EntriesInfo {
|
||||
/**
|
||||
* The GerritInfo entity contains information about Gerrit configuration from
|
||||
* the gerrit section.
|
||||
* https://gerrit-review.googlesource.com/Documentation/rest-api-config.html
|
||||
* https://gerrit-review.googlesource.com/Documentation/rest-api-config.html#gerrit-info
|
||||
*/
|
||||
export interface GerritInfo {
|
||||
all_projects_name: string;
|
||||
all_users_name: string;
|
||||
all_projects: string; // Doc contains incorrect name
|
||||
all_users: string; // Doc contains incorrect name
|
||||
doc_search: string;
|
||||
doc_url?: string;
|
||||
edit_gpg_keys: boolean;
|
||||
report_bug_url?: string;
|
||||
// The following property is missed in doc
|
||||
primary_weblink_name?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -20,9 +20,35 @@ import 'page/page';
|
||||
// page.js and replace "this" to "window". Otherwise, it can't assign global
|
||||
// property. We can't import page.mjs because typescript doesn't support mjs
|
||||
// extensions
|
||||
|
||||
export interface Page {
|
||||
(pattern: string | RegExp, ...pageCallback: PageCallback[]): void;
|
||||
(pageCallback: PageCallback): void;
|
||||
show(url: string): void;
|
||||
redirect(url: string): void;
|
||||
base(url: string): void;
|
||||
start(): void;
|
||||
exit(pattern: string | RegExp, ...pageCallback: PageCallback[]): void;
|
||||
}
|
||||
|
||||
// See https://visionmedia.github.io/page.js/ for details
|
||||
export interface PageContext {
|
||||
save(): void;
|
||||
handled: boolean;
|
||||
canonicalPath: string;
|
||||
path: string;
|
||||
querystring: string;
|
||||
pathname: string;
|
||||
state: unknown;
|
||||
title: string;
|
||||
hash: string;
|
||||
params: {[paramIndex: string]: string};
|
||||
}
|
||||
|
||||
export type PageNextCallback = () => void;
|
||||
|
||||
export type PageCallback = (
|
||||
context: PageContext,
|
||||
next: PageNextCallback
|
||||
) => void;
|
||||
|
||||
export const page = window['page'] as Page;
|
||||
|
||||
Reference in New Issue
Block a user