Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions packages/mobile/src/styles/builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,36 @@ export class StyleBuilder<
definitions: K[],
conditionalDefinitions: (ConditionalK | null | undefined | boolean)[] = []
): UnionToIntersection<D[K]> & Partial<UnionToIntersection<D[ConditionalK]>> {
const styles: any[] = [];
const allDefinitions = [
...(definitions || []),
...(conditionalDefinitions || []),
].filter((definition) => typeof definition === "string");
Comment on lines +176 to +179

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure definitions are not null or undefined.

The code concatenates definitions and conditionalDefinitions and filters out non-string values. Ensure that definitions and conditionalDefinitions are not null or undefined before concatenation.

-    const allDefinitions = [
-      ...(definitions || []),
-      ...(conditionalDefinitions || []),
-    ].filter((definition) => typeof definition === "string");
+    const allDefinitions = [
+      ...(definitions ?? []),
+      ...(conditionalDefinitions ?? []),
+    ].filter((definition) => typeof definition === "string");
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const allDefinitions = [
...(definitions || []),
...(conditionalDefinitions || []),
].filter((definition) => typeof definition === "string");
const allDefinitions = [
...(definitions ?? []),
...(conditionalDefinitions ?? []),
].filter((definition) => typeof definition === "string");

const sortedDefinitions = allDefinitions.sort();
// Since definitions are sorted, the order of input doesn't matter now -> same styles will have the same keys
const keyStyle = sortedDefinitions.reduce(
(prevStyle, currStyle) => `${prevStyle}${currStyle}`,
""
);
let styles = this.cached.get(keyStyle);
if (styles) return styles;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure styles initialization.

Before merging styles, ensure that the styles variable is properly initialized.

-    let styles = this.cached.get(keyStyle);
-    if (styles) return styles;
+    let styles = this.cached.get(keyStyle) || {};
+    if (Object.keys(styles).length > 0) return styles;
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let styles = this.cached.get(keyStyle) || {};
if (Object.keys(styles).length > 0) return styles;

for (const definition of definitions) {
styles.push(this.get<D, K>(definition));
styles = {
...(styles || {}),
...(this.get<D, K>(definition) || {}),
};
}
for (const definition of conditionalDefinitions) {
if (definition && definition !== true) {
styles.push(this.get<D, K>(definition as unknown as K));
styles = {
...(styles || {}),
...(this.get<D, K>(definition as unknown as K) || {}),
};
}
}
return StyleSheet.flatten(styles);

this.cached.set(keyStyle, styles);
return styles;
}

get<
Expand Down