Skip to content

AI-native design editor. Open-source Figma alternative.

Fix lint warnings: non-null assertions, useless spreads, unused code, type safety

Danila Poyarkov6072ce57c161
diff --git a/packages/cli/src/headless.ts b/packages/cli/src/headless.ts
index ee9c17ac7..cb2071866 100644
--- a/packages/cli/src/headless.ts
+++ b/packages/cli/src/headless.ts
@@ -41,7 +41,8 @@ export async function loadFonts(graph: SceneGraph): Promise<void> {
}
function createRenderer(ckInstance: CanvasKit, width: number, height: number): SkiaRenderer {
- const surface = ckInstance.MakeSurface(width, height)!
+ const surface = ckInstance.MakeSurface(width, height)
+ if (!surface) throw new Error('Failed to create Skia surface')
const renderer = new SkiaRenderer(ckInstance, surface)
renderer.viewportWidth = width
renderer.viewportHeight = height
diff --git a/packages/core/src/fig-export.ts b/packages/core/src/fig-export.ts
index d9e8d4c60..30e0e3ce7 100644
--- a/packages/core/src/fig-export.ts
+++ b/packages/core/src/fig-export.ts
@@ -43,7 +43,11 @@ function variableValueToKiwi(
return { value: { boolValue: !!value }, dataType: 'BOOLEAN', resolvedDataType: 'BOOLEAN' }
}
if (type === 'STRING') {
- return { value: { textValue: String(value) }, dataType: 'STRING', resolvedDataType: 'STRING' }
+ return {
+ value: { textValue: typeof value === 'string' ? value : JSON.stringify(value) },
+ dataType: 'STRING',
+ resolvedDataType: 'STRING'
+ }
}
return { value: { floatValue: Number(value) }, dataType: 'FLOAT', resolvedDataType: 'FLOAT' }
}
diff --git a/packages/core/src/figma-api.ts b/packages/core/src/figma-api.ts
index 976ace15b..1ddd11509 100644
--- a/packages/core/src/figma-api.ts
+++ b/packages/core/src/figma-api.ts
@@ -1023,13 +1023,10 @@ export class FigmaAPI {
proxy: FigmaNodeProxy
): FigmaNodeProxy & { selection: FigmaNodeProxy[] } {
if (!this._pageProxies.has(proxy)) {
- const self = this
Object.defineProperty(proxy, 'selection', {
- get() {
- return self._selection
- },
- set(nodes: FigmaNodeProxy[]) {
- self._selection = nodes
+ get: () => this._selection,
+ set: (nodes: FigmaNodeProxy[]) => {
+ this._selection = nodes
},
enumerable: true,
configurable: true
@@ -1122,7 +1119,7 @@ export class FigmaAPI {
const raw = this.graph.getNode(node[INTERNAL_ID])
if (raw?.type !== 'GROUP') return
const parentId = raw.parentId ?? this._currentPageId
- for (const childId of [...raw.childIds]) {
+ for (const childId of Array.from(raw.childIds)) {
this.graph.reparentNode(childId, parentId)
}
this.graph.deleteNode(node[INTERNAL_ID])
@@ -1230,8 +1227,8 @@ export class FigmaAPI {
): FigmaNodeProxy {
if (nodeIds.length < 2) throw new Error('Need at least 2 nodes for boolean operation')
const nodes = nodeIds.map((id) => this.graph.getNode(id))
- if (nodes.some((n) => !n)) throw new Error('One or more nodes not found')
- const first = nodes[0]!
+ const first = nodes[0]
+ if (!first || nodes.some((n) => !n)) throw new Error('One or more nodes not found')
const parentId = first.parentId ?? this._currentPageId
const group = this.graph.createNode('GROUP', parentId, {
name: `Boolean ${operation.toLowerCase()}`,
diff --git a/packages/core/src/fonts.ts b/packages/core/src/fonts.ts
index 5304f1f0f..3eaade425 100644
--- a/packages/core/src/fonts.ts
+++ b/packages/core/src/fonts.ts
@@ -68,7 +68,7 @@ function normalizeFontFamily(family: string): string {
}
async function fetchGoogleFontFiles(family: string): Promise<Record<string, string> | null> {
- if (googleFontsCache.has(family)) return googleFontsCache.get(family)!
+ if (googleFontsCache.has(family)) return googleFontsCache.get(family) ?? null
if (googleFontsFailed.has(family)) return null
const url = `https://www.googleapis.com/webfonts/v1/webfonts?family=${encodeURIComponent(family)}&key=${GOOGLE_FONTS_API_KEY}`
diff --git a/packages/core/src/kiwi/codec.ts b/packages/core/src/kiwi/codec.ts
index 51abd95e3..7db68ba41 100644
--- a/packages/core/src/kiwi/codec.ts
+++ b/packages/core/src/kiwi/codec.ts
@@ -13,8 +13,6 @@ import { compileSchema, encodeBinarySchema } from './kiwi-schema'
import { isZstdCompressed, getKiwiMessageType } from './protocol'
import figmaSchema from './schema'
-import type { Schema } from './kiwi-schema'
-
interface CompiledSchema {
encodeMessage(message: unknown): Uint8Array
decodeMessage(data: Uint8Array): unknown
diff --git a/packages/core/src/kiwi/instance-overrides.ts b/packages/core/src/kiwi/instance-overrides.ts
index f206a96ae..770364607 100644
--- a/packages/core/src/kiwi/instance-overrides.ts
+++ b/packages/core/src/kiwi/instance-overrides.ts
@@ -86,7 +86,8 @@ export function populateAndApplyOverrides(
}
while (populateQueue.length > 0) {
- const nodeId = populateQueue.pop()!
+ const nodeId = populateQueue.pop()
+ if (!nodeId) continue
const node = graph.getNode(nodeId)
if (node?.type !== 'INSTANCE' || !node.componentId || node.childIds.length > 0) continue
const comp = graph.getNode(node.componentId)
@@ -247,7 +248,7 @@ export function populateAndApplyOverrides(
const node = graph.getNode(nodeId)
if (node?.type !== 'INSTANCE') return
- for (const childId of [...node.childIds]) graph.deleteNode(childId)
+ for (const childId of Array.from(node.childIds)) graph.deleteNode(childId)
graph.updateNode(nodeId, { componentId: compId })
const comp = graph.getNode(compId)
if (comp && comp.childIds.length > 0) {
diff --git a/packages/core/src/layout.ts b/packages/core/src/layout.ts
index 6c85cdbfa..c5e5f1a82 100644
--- a/packages/core/src/layout.ts
+++ b/packages/core/src/layout.ts
@@ -379,9 +379,8 @@ function estimateTextSize(node: SceneNode): { width: number; height: number } {
function configureTextLeaf(
yogaChild: YogaNode,
child: SceneNode,
- parent: SceneNode
+ _parent: SceneNode
): void {
- const isRow = parent.layoutMode === 'HORIZONTAL'
const autoResize = child.textAutoResize
if (child.layoutGrow > 0) {
@@ -398,7 +397,7 @@ function configureTextLeaf(
const cached = cache.get(cacheKey)
if (cached) return cached
- const measured = globalTextMeasurer!(child, maxW)
+ const measured = globalTextMeasurer?.(child, maxW)
const result = measured ?? { width: child.width, height: child.height }
cache.set(cacheKey, result)
return result
@@ -415,7 +414,7 @@ function configureTextLeaf(
const cached = cache.get(cacheKey)
if (cached) return cached
- const measured = globalTextMeasurer!(child, constraintW)
+ const measured = globalTextMeasurer?.(child, constraintW)
const result = { width: constraintW, height: measured?.height ?? child.height }
cache.set(cacheKey, result)
return result
diff --git a/packages/core/src/render/renderer.ts b/packages/core/src/render/renderer.ts
index f038e7fa1..c49b7b3df 100644
--- a/packages/core/src/render/renderer.ts
+++ b/packages/core/src/render/renderer.ts
@@ -228,10 +228,6 @@ function applyPaddingOverrides(props: Record<string, unknown>, o: Partial<SceneN
const PADDING_KEYS = ['p', 'padding', 'px', 'py', 'pt', 'pr', 'pb', 'pl'] as const
const AUTO_LAYOUT_TRIGGER_KEYS = [...PADDING_KEYS, 'justify', 'items'] as const
-function hasPaddingProps(props: Record<string, unknown>): boolean {
- return PADDING_KEYS.some((k) => props[k] !== undefined)
-}
-
function hasAutoLayoutTriggerProps(props: Record<string, unknown>): boolean {
return AUTO_LAYOUT_TRIGGER_KEYS.some((k) => props[k] !== undefined)
}
diff --git a/packages/core/src/renderer/renderer.ts b/packages/core/src/renderer/renderer.ts
index 748daac65..6d55b3e81 100644
--- a/packages/core/src/renderer/renderer.ts
+++ b/packages/core/src/renderer/renderer.ts
@@ -651,7 +651,7 @@ export class SkiaRenderer {
p.beginPhase('render:scene')
if (canUsePicture) {
p.beginPhase('render:drawPicture')
- canvas.drawPicture(this.scenePicture!)
+ if (this.scenePicture) canvas.drawPicture(this.scenePicture)
p.endPhase('render:drawPicture')
} else if (hasVolatileOverlays) {
this._nodeCount = 0
@@ -831,7 +831,8 @@ export class SkiaRenderer {
}
})
- const builder = ck.ParagraphBuilder.MakeFromFontProvider(paraStyle, this.fontProvider!)
+ if (!this.fontProvider) throw new Error('Font provider not initialized')
+ const builder = ck.ParagraphBuilder.MakeFromFontProvider(paraStyle, this.fontProvider)
if (node.styleRuns.length === 0) {
builder.addText(node.text)
diff --git a/packages/core/src/scene-graph.ts b/packages/core/src/scene-graph.ts
index 30289842b..638cbca98 100644
--- a/packages/core/src/scene-graph.ts
+++ b/packages/core/src/scene-graph.ts
@@ -584,7 +584,7 @@ export class SceneGraph {
removeCollection(id: string): void {
const collection = this.variableCollections.get(id)
if (collection) {
- for (const varId of [...collection.variableIds]) {
+ for (const varId of Array.from(collection.variableIds)) {
this.removeVariable(varId)
}
}
diff --git a/packages/core/src/style-runs.ts b/packages/core/src/style-runs.ts
index c204ec11d..303e13f0f 100644
--- a/packages/core/src/style-runs.ts
+++ b/packages/core/src/style-runs.ts
@@ -10,7 +10,7 @@ export function getStyleAt(runs: StyleRun[], index: number): CharacterStyleOverr
}
function expandRuns(runs: StyleRun[], textLength: number): (CharacterStyleOverride | null)[] {
- const chars: (CharacterStyleOverride | null)[] = new Array(textLength).fill(null)
+ const chars: (CharacterStyleOverride | null)[] = Array.from({ length: textLength }, (): CharacterStyleOverride | null => null)
for (const run of runs) {
for (let i = run.start; i < run.start + run.length && i < textLength; i++) {
chars[i] = { ...chars[i], ...run.style }
@@ -46,7 +46,7 @@ export function removeStyleFromRange(
for (let i = start; i < end && i < textLength; i++) {
if (chars[i]) {
- const copy = { ...chars[i]! }
+ const copy = { ...chars[i] }
for (const k of keys) delete copy[k]
chars[i] = Object.keys(copy).length > 0 ? copy : null
}
@@ -94,7 +94,8 @@ function compactRuns(chars: (CharacterStyleOverride | null)[]): StyleRun[] {
continue
}
const start = i
- const style = chars[i]!
+ const style = chars[i]
+ if (!style) { i++; continue }
while (i < chars.length && stylesEqual(chars[i], style)) i++
result.push({ start, length: i - start, style: { ...style } })
}
diff --git a/packages/core/src/tools/analyze.ts b/packages/core/src/tools/analyze.ts
index 60030feae..9d1579e09 100644
--- a/packages/core/src/tools/analyze.ts
+++ b/packages/core/src/tools/analyze.ts
@@ -545,13 +545,13 @@ export const diffShow = defineTool({
const modified: SceneNode = structuredClone(raw)
if (newProps.fill) {
modified.fills = [
- { type: 'SOLID', color: parseColor(String(newProps.fill)), opacity: 1, visible: true }
+ { type: 'SOLID', color: parseColor(newProps.fill as string), opacity: 1, visible: true }
]
}
if (newProps.stroke) {
modified.strokes = [
{
- color: parseColor(String(newProps.stroke)),
+ color: parseColor(newProps.stroke as string),
weight: modified.strokes[0]?.weight ?? 1,
opacity: 1,
visible: true,
@@ -575,7 +575,7 @@ export const diffShow = defineTool({
if (newProps.visible !== undefined) modified.visible = Boolean(newProps.visible)
if (newProps.locked !== undefined) modified.locked = Boolean(newProps.locked)
if (newProps.rotation !== undefined) modified.rotation = Number(newProps.rotation)
- if (newProps.text !== undefined) modified.text = String(newProps.text)
+ if (newProps.text !== undefined) modified.text = newProps.text as string
const newContent = serializeNodeProps(modified)
diff --git a/packages/core/src/tools/read.ts b/packages/core/src/tools/read.ts
index 7e38527a4..836fa65b7 100644
--- a/packages/core/src/tools/read.ts
+++ b/packages/core/src/tools/read.ts
@@ -194,14 +194,14 @@ export const listFonts = defineTool({
if (raw) {
const key = raw.fontFamily
if (!fonts.has(key)) fonts.set(key, new Set())
- fonts.get(key)!.add(raw.fontWeight)
+ fonts.get(key)?.add(raw.fontWeight)
}
}
return false
})
let result = [...fonts.entries()].map(([family, weights]) => ({
family,
- weights: [...weights].sort()
+ weights: [...weights].sort((a, b) => a - b)
}))
if (args.family) {
const q = args.family.toLowerCase()
diff --git a/packages/core/src/xpath.ts b/packages/core/src/xpath.ts
index c55c09e41..14fdd182b 100644
--- a/packages/core/src/xpath.ts
+++ b/packages/core/src/xpath.ts
@@ -104,6 +104,8 @@ function getAttrs(wrapped: XPathNode): XPathAttr[] {
if (attrName in node) {
const value = (node as unknown as Record<string, unknown>)[attrName]
if (value === undefined || value === null || typeof value === 'symbol') continue
+ const stringValue =
+ typeof value === 'object' ? JSON.stringify(value) : String(value as string | number | boolean)
attrs.push({
nodeType: NODE_TYPES.ATTRIBUTE_NODE,
nodeName: attrName,
@@ -111,7 +113,7 @@ function getAttrs(wrapped: XPathNode): XPathAttr[] {
localName: attrName,
namespaceURI: null,
prefix: null,
- value: String(value),
+ value: stringValue,
ownerElement: wrapped
})
}
@@ -150,7 +152,7 @@ function createDomFacade(graph: SceneGraph) {
if (attributeName in sceneNode) {
const value = (sceneNode as unknown as Record<string, unknown>)[attributeName]
if (value === undefined || value === null || typeof value === 'symbol') return null
- return String(value)
+ return typeof value === 'object' ? JSON.stringify(value) : String(value as string | number | boolean)
}
return null
},
diff --git a/src/ai/chat-debug.ts b/src/ai/chat-debug.ts
index de089a3d0..29eaff477 100644
--- a/src/ai/chat-debug.ts
+++ b/src/ai/chat-debug.ts
@@ -9,12 +9,12 @@ function formatToolPart(part: Record<string, unknown>): string {
return lines.join('\n')
}
- const name = String(part.type ?? 'unknown').replace(/^tool-/, '')
- const state = String(part.state ?? '?')
+ const name = (typeof part.type === 'string' ? part.type : 'unknown').replace(/^tool-/, '')
+ const state = typeof part.state === 'string' ? part.state : '?'
const lines = [` [tool] ${name} (${state})`]
if (part.input) lines.push(` input: ${JSON.stringify(part.input)}`)
if (part.output !== undefined) lines.push(` output: ${JSON.stringify(part.output)}`)
- if (part.errorText) lines.push(` error: ${String(part.errorText)}`)
+ if (part.errorText) lines.push(` error: ${part.errorText as string}`)
return lines.join('\n')
}
@@ -28,9 +28,11 @@ export function serializeChatLog(messages: UIMessage[]): string {
for (const part of msg.parts) {
const p = part as Record<string, unknown>
if (p.type === 'text') {
- parts.push(` ${p.text}`)
+ parts.push(` ${p.text as string}`)
} else if (p.type === 'reasoning') {
- parts.push(` [reasoning] ${String(p.text ?? p.content ?? '')}`)
+ parts.push(
+ ` [reasoning] ${typeof p.text === 'string' ? p.text : (typeof p.content === 'string' ? p.content : '')}`
+ )
} else if (
p.type === 'tool-invocation' ||
p.toolInvocation ||
@@ -38,7 +40,7 @@ export function serializeChatLog(messages: UIMessage[]): string {
) {
parts.push(formatToolPart(p))
} else {
- parts.push(` [${String(p.type ?? 'unknown')}] ${JSON.stringify(p)}`)
+ parts.push(` [${typeof p.type === 'string' ? p.type : 'unknown'}] ${JSON.stringify(p)}`)
}
}
diff --git a/src/components/FillPicker.vue b/src/components/FillPicker.vue
index 728e83155..6dff5ffc0 100644
--- a/src/components/FillPicker.vue
+++ b/src/components/FillPicker.vue
@@ -236,7 +236,10 @@ const imagePreviewUrl = useObjectUrl(imageBlob)
watch(
() => fill.imageHash,
(hash) => {
- if (!hash) { imageBlob.value = null; return }
+ if (!hash) {
+ imageBlob.value = null
+ return
+ }
const data = store.graph.images.get(hash)
imageBlob.value = data ? new Blob([data]) : null
},
@@ -245,7 +248,7 @@ watch(
const { open: pickImage, onChange: onFileChange } = useFileDialog({
accept: 'image/png,image/jpeg,image/webp',
- multiple: false,
+ multiple: false
})
onFileChange(async (files) => {
diff --git a/src/composables/use-collab.ts b/src/composables/use-collab.ts
index 6aae336bb..047cc6390 100644
--- a/src/composables/use-collab.ts
+++ b/src/composables/use-collab.ts
@@ -224,8 +224,9 @@ export function useCollab(store: EditorStore) {
const unbindDeleted = store.graph.emitter.on('node:deleted', (id) => {
if (!suppressGraphSync && ydoc && ynodes) {
suppressYjsEvents = true
+ const map = ynodes
ydoc.transact(() => {
- ynodes!.delete(id)
+ map.delete(id)
})
suppressYjsEvents = false
}
diff --git a/src/stores/editor.ts b/src/stores/editor.ts
index c46afc46c..556bf7d8b 100644
--- a/src/stores/editor.ts
+++ b/src/stores/editor.ts
@@ -988,7 +988,8 @@ export function createEditorStore() {
let pendingComponentSync: Set<string> | null = null
function flushComponentSync() {
- const ids = pendingComponentSync!
+ const ids = pendingComponentSync
+ if (!ids) return
pendingComponentSync = null
const componentIds = new Set<string>()
for (const id of ids) {