Tidy up the full frontend codebase and use optional chaining where possible (#620)

* Tidy up the full frontend codebase and use optional chaining where possible

* Code review changes
This commit is contained in:
Keavon Chambers
2022-04-30 02:52:53 -07:00
parent 92ee3bbad3
commit 07736a9fca
28 changed files with 640 additions and 622 deletions
+16 -16
View File
@@ -244,9 +244,9 @@ interface DataBuffer {
}
export function newDisplayDocumentLayerTreeStructure(input: { data_buffer: DataBuffer }, wasm: WasmInstance): DisplayDocumentLayerTreeStructure {
const { pointer, length } = input.data_buffer;
const pointerNum = Number(pointer);
const lengthNum = Number(length);
const pointerNum = Number(input.data_buffer.pointer);
const lengthNum = Number(input.data_buffer.length);
const wasmMemoryBuffer = wasm.wasm_memory().buffer;
// Decode the folder structure encoding
@@ -265,7 +265,7 @@ export function newDisplayDocumentLayerTreeStructure(input: { data_buffer: DataB
for (let i = 0; i < structureSectionLength; i += 1) {
const msbSigned = structureSectionMsbSigned.getBigUint64(i * 8, true);
const msbMask = BigInt(1) << BigInt(63);
const msbMask = BigInt(1) << BigInt(64 - 1);
// Set the MSB to 0 to clear the sign and then read the number as usual
const numberOfLayersAtThisDepth = msbSigned & ~msbMask;
@@ -485,7 +485,7 @@ export class UpdatePropertyPanelSectionsLayout extends JsMessage implements Widg
// Unpacking rust types to more usable type in the frontend
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function createWidgetLayout(widgetLayout: any[]): LayoutRow[] {
return widgetLayout.map((rowOrSection) => {
return widgetLayout.map((rowOrSection): LayoutRow => {
if (rowOrSection.Row) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const widgets = rowOrSection.Row.widgets.map((widgetHolder: any) => {
@@ -496,16 +496,16 @@ function createWidgetLayout(widgetLayout: any[]): LayoutRow[] {
return { widget_id, kind, props };
});
return {
name: rowOrSection.Row.name,
widgets,
};
const result: WidgetRow = { widgets };
return result;
}
if (rowOrSection.Section) {
return {
name: rowOrSection.Section.name,
layout: createWidgetLayout(rowOrSection.Section.layout),
};
const { name } = rowOrSection.Section;
const layout = createWidgetLayout(rowOrSection.Section.layout);
const result: WidgetSection = { name, layout };
return result;
}
throw new Error("Layout row type does not exist");
@@ -524,12 +524,12 @@ export class TriggerTextCopy extends JsMessage {
export class TriggerViewportResize extends JsMessage {}
// Any is used since the type of the object should be known from the rust side
// `any` is used since the type of the object should be known from the Rust side
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type JSMessageFactory = (data: any, wasm: WasmInstance, instance: RustEditorInstance) => JsMessage;
type MessageMaker = typeof JsMessage | JSMessageFactory;
export const messageConstructors: Record<string, MessageMaker> = {
export const messageMakers: Record<string, MessageMaker> = {
DisplayConfirmationToCloseAllDocuments,
DisplayConfirmationToCloseDocument,
DisplayDialogAboutGraphite,
@@ -568,4 +568,4 @@ export const messageConstructors: Record<string, MessageMaker> = {
UpdateToolOptionsLayout,
UpdateWorkingColors,
} as const;
export type JsMessageType = keyof typeof messageConstructors;
export type JsMessageType = keyof typeof messageMakers;