Skip to content

Commit 24ebda9

Browse files
committed
Improve typing
1 parent 88fe0bd commit 24ebda9

File tree

6 files changed

+39
-16
lines changed

6 files changed

+39
-16
lines changed

packages/backend/src/altNodes/iconDetection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ function checkChildrenRecursively(children: ReadonlyArray<SceneNode>): {
151151
* @param logDetails Set to true to print debug information to the console.
152152
* @returns True if the node is likely an icon, false otherwise.
153153
*/
154-
export function isLikelyIcon(node: SceneNode, logDetails = true): boolean {
154+
export function isLikelyIcon(node: SceneNode, logDetails = false): boolean {
155155
const info: string[] = [`Node: ${node.name} (${node.type}, ID: ${node.id})`];
156156
let result = false;
157157
let reason = "";

packages/backend/src/altNodes/jsonNodeConversion.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { variableToColorName } from "../tailwind/conversionTables";
44
import { HasGeometryTrait, Node, Paint } from "../api_types";
55
import { calculateRectangleFromBoundingBox } from "../common/commonPosition";
66
import { isLikelyIcon } from "./iconDetection";
7+
import { AltNode } from "../alt_api_types";
78

89
// Performance tracking counters
910
export let getNodeByIdAsyncTime = 0;
@@ -265,10 +266,10 @@ function adjustChildrenOrder(node: any) {
265266
* @returns Potentially modified jsonNode, array of nodes (for inlined groups), or null
266267
*/
267268
const processNodePair = async (
268-
jsonNode: Node,
269+
jsonNode: AltNode,
269270
figmaNode: SceneNode,
270271
settings: PluginSettings,
271-
parentNode?: Node,
272+
parentNode?: AltNode,
272273
parentCumulativeRotation: number = 0,
273274
): Promise<Node | Node[] | null> => {
274275
if (!jsonNode.id) return null;
@@ -303,7 +304,7 @@ const processNodePair = async (
303304
);
304305
}
305306

306-
if ("rotation" in jsonNode) {
307+
if ("rotation" in jsonNode && jsonNode.rotation) {
307308
jsonNode.rotation = -jsonNode.rotation * (180 / Math.PI);
308309
}
309310

@@ -442,6 +443,7 @@ const processNodePair = async (
442443
jsonNode.styledTextSegments = styledTextSegments;
443444
}
444445

446+
// Inline text style.
445447
Object.assign(jsonNode, jsonNode.style);
446448
if (!jsonNode.textAutoResize) {
447449
jsonNode.textAutoResize = "NONE";
@@ -492,11 +494,16 @@ const processNodePair = async (
492494
(jsonNode as any).canBeFlattened = false;
493495
}
494496

495-
if ("individualStrokeWeights" in jsonNode) {
496-
jsonNode.strokeTopWeight = jsonNode.individualStrokeWeights.top;
497-
jsonNode.strokeBottomWeight = jsonNode.individualStrokeWeights.bottom;
498-
jsonNode.strokeLeftWeight = jsonNode.individualStrokeWeights.left;
499-
jsonNode.strokeRightWeight = jsonNode.individualStrokeWeights.right;
497+
if (
498+
"individualStrokeWeights" in jsonNode &&
499+
jsonNode.individualStrokeWeights
500+
) {
501+
(jsonNode as any).strokeTopWeight = jsonNode.individualStrokeWeights.top;
502+
(jsonNode as any).strokeBottomWeight =
503+
jsonNode.individualStrokeWeights.bottom;
504+
(jsonNode as any).strokeLeftWeight = jsonNode.individualStrokeWeights.left;
505+
(jsonNode as any).strokeRightWeight =
506+
jsonNode.individualStrokeWeights.right;
500507
}
501508

502509
await getColorVariables(jsonNode, settings);
@@ -554,7 +561,7 @@ const processNodePair = async (
554561
// Get only visible JSON children
555562
const visibleJsonChildren = jsonNode.children.filter(
556563
(child) => child.visible !== false,
557-
);
564+
) as AltNode[];
558565

559566
// Create a map of figma children by ID for easier matching
560567
const figmaChildrenById = new Map();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { type Node } from "./api_types";
2+
3+
export type AltNode = Node & {
4+
styledTextSegments: Array<
5+
Pick<StyledTextSegment, any | "characters" | "start" | "end">
6+
>;
7+
cumulativeRotation: number;
8+
uniqueName: string;
9+
canBeFlattened: boolean;
10+
isRelative: boolean;
11+
width: number;
12+
height: number;
13+
x: number;
14+
y: number;
15+
};

packages/backend/src/flutter/builderImpl/flutterBlend.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AltNode } from "../../alt_api_types";
12
import {
23
generateWidgetCode,
34
numberToFixedString,
@@ -38,7 +39,7 @@ export const flutterVisibility = (node: SceneNode, child: string): string => {
3839
* https://api.flutter.dev/flutter/widgets/Transform-class.html
3940
* that's how you convert angles to clockwise radians: angle * -pi/180
4041
*/
41-
export const flutterRotation = (node: LayoutMixin, child: string): string => {
42+
export const flutterRotation = (node: AltNode, child: string): string => {
4243
if (
4344
node.rotation !== undefined &&
4445
child !== "" &&
@@ -58,7 +59,7 @@ export const flutterRotation = (node: LayoutMixin, child: string): string => {
5859
/**
5960
* Generates a rotation matrix string for Flutter transforms
6061
*/
61-
export const generateRotationMatrix = (node: LayoutMixin): string => {
62+
export const generateRotationMatrix = (node: AltNode): string => {
6263
const rotation = (node.rotation || 0) + (node.cumulativeRotation || 0);
6364

6465
if (Math.round(rotation) === 0) {

packages/backend/src/html/builderImpl/htmlBlend.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { roundToNearestHundreth } from "./../../common/numToAutoFixed";
2-
import { addWarning } from "../../common/commonConversionWarnings";
31
import { numberToFixedString } from "../../common/numToAutoFixed";
42
import { formatWithJSX } from "../../common/parseJSX";
3+
import { AltNode } from "../../alt_api_types";
54

65
/**
76
* https://tailwindcss.com/docs/opacity/
@@ -110,7 +109,7 @@ export const htmlVisibility = (
110109
* default is [-180, -90, -45, 0, 45, 90, 180], but '0' will be ignored:
111110
* if rotation was changed, let it be perceived. Therefore, 1 => 45
112111
*/
113-
export const htmlRotation = (node: LayoutMixin, isJsx: boolean): string[] => {
112+
export const htmlRotation = (node: AltNode, isJsx: boolean): string[] => {
114113
const rotation =
115114
-Math.round((node.rotation || 0) + (node.cumulativeRotation || 0)) || 0;
116115

packages/backend/src/swiftui/builderImpl/swiftuiBlend.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { SwiftUIModifier } from "types";
22
import { numberToFixedString } from "../../common/numToAutoFixed";
3+
import { AltNode } from "../../alt_api_types";
34

45
/**
56
* https://developer.apple.com/documentation/swiftui/view/opacity(_:)
@@ -29,7 +30,7 @@ export const swiftuiVisibility = (
2930
/**
3031
* https://developer.apple.com/documentation/swiftui/modifiedcontent/rotationeffect(_:anchor:)
3132
*/
32-
export const swiftuiRotation = (node: LayoutMixin): SwiftUIModifier | null => {
33+
export const swiftuiRotation = (node: AltNode): SwiftUIModifier | null => {
3334
const rotation = (node.rotation || 0) + (node.cumulativeRotation || 0);
3435
if (Math.round(rotation) !== 0) {
3536
return ["rotationEffect", `.degrees(${numberToFixedString(rotation)})`];

0 commit comments

Comments
 (0)