Skip to content

Commit

Permalink
[eslint] Prefer TypeScript interface over type aliases.
Browse files Browse the repository at this point in the history
We have a mix of `type` and `interface` usage throughout our codebase,
that is sometimes difficult to follow and reason about. We should follow
the suggestion from the TypeScript PM and use `interface` consistently
where possible. This leads to better type display in errors and makes
our codebase easier to read (b/c consistency).

This CL adds the `etc/prefer-interface` ESLint rule to accomplish this.

Fixed: 387237537
Change-Id: Idd6775094ba94b8397f626191788437f6b156dc6
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6135001
Commit-Queue: Nikolay Vitkov <[email protected]>
Reviewed-by: Nikolay Vitkov <[email protected]>
Commit-Queue: Benedikt Meurer <[email protected]>
  • Loading branch information
bmeurer authored and Devtools-frontend LUCI CQ committed Jan 2, 2025
1 parent 328b3a7 commit af6c633
Show file tree
Hide file tree
Showing 1,790 changed files with 54,393 additions and 1,695 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
parser: '@typescript-eslint/parser',

plugins: [
'etc',
'@typescript-eslint',
'mocha',
'rulesdir',
Expand Down Expand Up @@ -120,6 +121,9 @@ module.exports = {
}
],

// Forbids type aliases where interfaces can be used.
'etc/prefer-interface': 'error',

// Closure does not properly typecheck default exports
'import/no-default-export': 'error',

Expand Down
6 changes: 3 additions & 3 deletions front_end/core/common/Console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ export const enum Events {
MESSAGE_ADDED = 'messageAdded',
}

export type EventTypes = {
[Events.MESSAGE_ADDED]: Message,
};
export interface EventTypes {
[Events.MESSAGE_ADDED]: Message;
}

export const enum MessageLevel {
INFO = 'info',
Expand Down
14 changes: 7 additions & 7 deletions front_end/core/common/EventTarget.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ const enum Events {
UNION_EVENT = 'UnionEvent',
}

type TestEvents = {
[Events.VOID_EVENT]: void,
[Events.NUMBER_EVENT]: number,
[Events.KEY_VALUE_EVENT]: {key: string, value: number},
[Events.BOOLEAN_EVENT]: boolean,
[Events.UNION_EVENT]: string|null,
};
interface TestEvents {
[Events.VOID_EVENT]: void;
[Events.NUMBER_EVENT]: number;
[Events.KEY_VALUE_EVENT]: {key: string, value: number};
[Events.BOOLEAN_EVENT]: boolean;
[Events.UNION_EVENT]: string|null;
}

class TypedEventEmitter extends Common.ObjectWrapper.ObjectWrapper<TestEvents> {
testValidArgumentTypes() {
Expand Down
10 changes: 6 additions & 4 deletions front_end/core/common/EventTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ export function removeEventListeners(eventList: EventDescriptor[]): void {

// This type can be used as the type parameter for `EventTarget`/`ObjectWrapper`
// when the set of events is not known at compile time.
export type GenericEvents = {
export interface GenericEvents {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[eventName: string]: any,
};
[eventName: string]: any;
}

export type EventPayloadToRestParameters<Events, T extends keyof Events> = Events[T] extends void ? [] : [Events[T]];
export type EventListener<Events, T extends keyof Events> = (arg0: EventTargetEvent<Events[T], Events>) => void;
export interface EventListener<Events, T extends keyof Events> {
(arg0: EventTargetEvent<Events[T], Events>): void;
}

export interface EventTarget<Events> {
addEventListener<T extends keyof Events>(eventType: T, listener: EventListener<Events, T>, thisObject?: Object):
Expand Down
4 changes: 3 additions & 1 deletion front_end/core/common/Mutex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

type ReleaseFunction = () => void;
interface ReleaseFunction {
(): void;
}

/**
* Use Mutex class to coordinate local concurrent operations.
Expand Down
4 changes: 3 additions & 1 deletion front_end/core/common/Revealer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,6 @@ export const RevealerDestination = {
ANIMATIONS_PANEL: i18nLazyString(UIStrings.animationsPanel),
};

export type RevealerDestination = () => Platform.UIString.LocalizedString;
export interface RevealerDestination {
(): Platform.UIString.LocalizedString;
}
4 changes: 3 additions & 1 deletion front_end/core/common/Runnable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export interface Runnable {
run(): Promise<void>;
}

type LateInitializationLoader = () => Promise<Runnable>;
interface LateInitializationLoader {
(): Promise<Runnable>;
}
export interface LateInitializableRunnableSetting {
id: string;
loadRunnable: LateInitializationLoader;
Expand Down
4 changes: 3 additions & 1 deletion front_end/core/common/Throttler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

export type FinishCallback = (err: Error) => void;
export interface FinishCallback {
(err: Error): void;
}

export class Throttler {
readonly #timeout: number;
Expand Down
6 changes: 3 additions & 3 deletions front_end/core/host/AidaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,6 @@ export const enum Events {
AIDA_AVAILABILITY_CHANGED = 'aidaAvailabilityChanged',
}

export type EventTypes = {
[Events.AIDA_AVAILABILITY_CHANGED]: void,
};
export interface EventTypes {
[Events.AIDA_AVAILABILITY_CHANGED]: void;
}
60 changes: 30 additions & 30 deletions front_end/core/host/InspectorFrontendHostAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,36 +204,36 @@ export interface KeyDownEvent {
// `EventTypes` is not used at runtime.
// Please note that the "dispatch" side can't be type-checked as the dispatch is
// done dynamically.
export type EventTypes = {
[Events.AppendedToURL]: Platform.DevToolsPath.RawPathString|Platform.DevToolsPath.UrlString,
[Events.CanceledSaveURL]: Platform.DevToolsPath.UrlString,
[Events.ColorThemeChanged]: void,
[Events.ContextMenuCleared]: void,
[Events.ContextMenuItemSelected]: number,
[Events.DeviceCountUpdated]: number,
[Events.DevicesDiscoveryConfigChanged]: Adb.Config,
[Events.DevicesPortForwardingStatusChanged]: void,
[Events.DevicesUpdated]: void,
[Events.DispatchMessage]: string,
[Events.DispatchMessageChunk]: DispatchMessageChunkEvent,
[Events.EnterInspectElementMode]: void,
[Events.EyeDropperPickedColor]: EyeDropperPickedColorEvent,
[Events.FileSystemsLoaded]: DevToolsFileSystem[],
[Events.FileSystemRemoved]: Platform.DevToolsPath.RawPathString,
[Events.FileSystemAdded]: FileSystemAddedEvent,
[Events.FileSystemFilesChangedAddedRemoved]: FilesChangedEvent,
[Events.IndexingTotalWorkCalculated]: IndexingTotalWorkCalculatedEvent,
[Events.IndexingWorked]: IndexingWorkedEvent,
[Events.IndexingDone]: IndexingEvent,
[Events.KeyEventUnhandled]: KeyEventUnhandledEvent,
[Events.ReloadInspectedPage]: boolean,
[Events.RevealSourceLine]: RevealSourceLineEvent,
[Events.SavedURL]: SavedURLEvent,
[Events.SearchCompleted]: SearchCompletedEvent,
[Events.SetInspectedTabId]: string,
[Events.SetUseSoftMenu]: boolean,
[Events.ShowPanel]: string,
};
export interface EventTypes {
[Events.AppendedToURL]: Platform.DevToolsPath.RawPathString|Platform.DevToolsPath.UrlString;
[Events.CanceledSaveURL]: Platform.DevToolsPath.UrlString;
[Events.ColorThemeChanged]: void;
[Events.ContextMenuCleared]: void;
[Events.ContextMenuItemSelected]: number;
[Events.DeviceCountUpdated]: number;
[Events.DevicesDiscoveryConfigChanged]: Adb.Config;
[Events.DevicesPortForwardingStatusChanged]: void;
[Events.DevicesUpdated]: void;
[Events.DispatchMessage]: string;
[Events.DispatchMessageChunk]: DispatchMessageChunkEvent;
[Events.EnterInspectElementMode]: void;
[Events.EyeDropperPickedColor]: EyeDropperPickedColorEvent;
[Events.FileSystemsLoaded]: DevToolsFileSystem[];
[Events.FileSystemRemoved]: Platform.DevToolsPath.RawPathString;
[Events.FileSystemAdded]: FileSystemAddedEvent;
[Events.FileSystemFilesChangedAddedRemoved]: FilesChangedEvent;
[Events.IndexingTotalWorkCalculated]: IndexingTotalWorkCalculatedEvent;
[Events.IndexingWorked]: IndexingWorkedEvent;
[Events.IndexingDone]: IndexingEvent;
[Events.KeyEventUnhandled]: KeyEventUnhandledEvent;
[Events.ReloadInspectedPage]: boolean;
[Events.RevealSourceLine]: RevealSourceLineEvent;
[Events.SavedURL]: SavedURLEvent;
[Events.SearchCompleted]: SearchCompletedEvent;
[Events.SetInspectedTabId]: string;
[Events.SetUseSoftMenu]: boolean;
[Events.ShowPanel]: string;
}

export interface InspectorFrontendHostAPI {
addFileSystem(type?: string): void;
Expand Down
6 changes: 3 additions & 3 deletions front_end/core/i18n/i18nTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

export type Values = {
[key: string]: string|boolean|number,
};
export interface Values {
[key: string]: string|boolean|number;
}

export interface SerializedMessage {
string: string;
Expand Down
4 changes: 3 additions & 1 deletion front_end/core/platform/ArrayUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ export const removeElement = <T>(array: T[], element: T, firstOnly?: boolean): b
return true;
};

type NumberComparator = (a: number, b: number) => number;
interface NumberComparator {
(a: number, b: number): number;
}

function swap(array: number[], i1: number, i2: number): void {
const temp = array[i1];
Expand Down
16 changes: 8 additions & 8 deletions front_end/core/platform/ServerTiming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ const defaultWarningMessages: ServerTimingParsingWarningMessage = {
},
};

export type ServerTimingParsingWarningMessage = {
deprecratedSyntax: () => string,
duplicateParameter: (parameter: string) => string,
noValueFoundForParameter: (parameter: string) => string,
unrecognizedParameter: (parameter: string) => string,
extraneousTrailingCharacters: () => string,
unableToParseValue: (parameter: string, value: string) => string,
};
export interface ServerTimingParsingWarningMessage {
deprecratedSyntax: () => string;
duplicateParameter: (parameter: string) => string;
noValueFoundForParameter: (parameter: string) => string;
unrecognizedParameter: (parameter: string) => string;
extraneousTrailingCharacters: () => string;
unableToParseValue: (parameter: string, value: string) => string;
}

export class ServerTiming {
metric: string;
Expand Down
32 changes: 18 additions & 14 deletions front_end/core/protocol_client/InspectorBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ export const DevToolsStubErrorCode = -32015;
const GenericErrorCode = -32000;
const ConnectionClosedErrorCode = -32001;

type MessageParams = {
interface MessageParams {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[x: string]: any,
};
[x: string]: any;
}

type ProtocolDomainName = ProtocolProxyApi.ProtocolDomainName;

Expand All @@ -52,15 +52,15 @@ export interface MessageError {
data?: string|null;
}

export type Message = {
sessionId?: string,
url?: Platform.DevToolsPath.UrlString,
id?: number,
error?: MessageError|null,
result?: Object|null,
method?: QualifiedName,
params?: MessageParams|null,
};
export interface Message {
sessionId?: string;
url?: Platform.DevToolsPath.UrlString;
id?: number;
error?: MessageError|null;
result?: Object|null;
method?: QualifiedName;
params?: MessageParams|null;
}

interface EventMessage extends Message {
method: QualifiedName;
Expand Down Expand Up @@ -91,7 +91,9 @@ interface CommandParameter {
description: string;
}

type Callback = (error: MessageError|null, arg1: Object|null) => void;
interface Callback {
(error: MessageError|null, arg1: Object|null): void;
}

interface CallbackWithDebugInfo {
callback: Callback;
Expand Down Expand Up @@ -206,7 +208,9 @@ export class Connection {
}
}

type SendRawMessageCallback = (...args: unknown[]) => void;
interface SendRawMessageCallback {
(...args: unknown[]): void;
}

export const test = {
/**
Expand Down
4 changes: 3 additions & 1 deletion front_end/core/root/Runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,9 @@ export type HostConfig = Platform.TypeScriptUtilities.RecursivePartial<{
* When defining conditions make sure that objects used by the function have
* been instantiated.
*/
export type Condition = (config?: HostConfig) => boolean;
export interface Condition {
(config?: HostConfig): boolean;
}

export const conditions = {
canDock: () => Boolean(Runtime.queryParam('can_dock')),
Expand Down
6 changes: 3 additions & 3 deletions front_end/core/sdk/AccessibilityModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ export const enum Events {
TREE_UPDATED = 'TreeUpdated',
}

export type EventTypes = {
[Events.TREE_UPDATED]: {root?: AccessibilityNode},
};
export interface EventTypes {
[Events.TREE_UPDATED]: {root?: AccessibilityNode};
}

export class AccessibilityModel extends SDKModel<EventTypes> implements ProtocolProxyApi.AccessibilityDispatcher {
agent: ProtocolProxyApi.AccessibilityApi;
Expand Down
19 changes: 11 additions & 8 deletions front_end/core/sdk/AnimationModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ const REPORT_SCROLL_POSITION_BINDING_NAME = '__devtools_report_scroll_position__

const getScrollListenerNameInPage = (id: number): string => `__devtools_scroll_listener_${id}__`;

type ScrollListener = (param: {scrollLeft: number, scrollTop: number}) => void;
type BindingListener =
(ev: Common.EventTarget.EventTargetEvent<Protocol.Runtime.BindingCalledEvent, RuntimeModelEventTypes>) => void;
interface ScrollListener {
(param: {scrollLeft: number, scrollTop: number}): void;
}
interface BindingListener {
(ev: Common.EventTarget.EventTargetEvent<Protocol.Runtime.BindingCalledEvent, RuntimeModelEventTypes>): void;
}

async function resolveToObjectInWorld(domNode: DOMNode, worldName: string): Promise<RemoteObject|null> {
const resourceTreeModel = domNode.domModel().target().model(ResourceTreeModel) as ResourceTreeModel;
Expand Down Expand Up @@ -478,11 +481,11 @@ export enum Events {
/* eslint-enable @typescript-eslint/naming-convention */
}

export type EventTypes = {
[Events.AnimationGroupStarted]: AnimationGroup,
[Events.AnimationGroupUpdated]: AnimationGroup,
[Events.ModelReset]: void,
};
export interface EventTypes {
[Events.AnimationGroupStarted]: AnimationGroup;
[Events.AnimationGroupUpdated]: AnimationGroup;
[Events.ModelReset]: void;
}

export class AnimationImpl {
readonly #animationModel: AnimationModel;
Expand Down
6 changes: 3 additions & 3 deletions front_end/core/sdk/AutofillModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,6 @@ export interface AddressFormFilledEvent {
event: Protocol.Autofill.AddressFormFilledEvent;
}

export type EventTypes = {
[Events.ADDRESS_FORM_FILLED]: AddressFormFilledEvent,
};
export interface EventTypes {
[Events.ADDRESS_FORM_FILLED]: AddressFormFilledEvent;
}
8 changes: 4 additions & 4 deletions front_end/core/sdk/CPUProfilerModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ export const enum Events {
CONSOLE_PROFILE_FINISHED = 'ConsoleProfileFinished',
}

export type EventTypes = {
[Events.CONSOLE_PROFILE_STARTED]: EventData,
[Events.CONSOLE_PROFILE_FINISHED]: ProfileFinishedData,
};
export interface EventTypes {
[Events.CONSOLE_PROFILE_STARTED]: EventData;
[Events.CONSOLE_PROFILE_FINISHED]: ProfileFinishedData;
}

SDKModel.register(CPUProfilerModel, {capabilities: Capability.JS, autostart: true});

Expand Down
8 changes: 4 additions & 4 deletions front_end/core/sdk/CPUThrottlingManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ export const enum Events {
HARDWARE_CONCURRENCY_CHANGED = 'HardwareConcurrencyChanged',
}

export type EventTypes = {
[Events.RATE_CHANGED]: number,
[Events.HARDWARE_CONCURRENCY_CHANGED]: number,
};
export interface EventTypes {
[Events.RATE_CHANGED]: number;
[Events.HARDWARE_CONCURRENCY_CHANGED]: number;
}

export function throttlingManager(): CPUThrottlingManager {
return CPUThrottlingManager.instance();
Expand Down
Loading

0 comments on commit af6c633

Please sign in to comment.