Skip to content

Commit 3902d81

Browse files
committed
Add missing code
1 parent e1a20d4 commit 3902d81

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/__tests__/code.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,6 +1498,18 @@ describe('registerCodegen with usage output', () => {
14981498
expect(usageCode).toContain('<PrimaryButton')
14991499
expect(usageCode).toContain('variant="primary"')
15001500
expect(usageCode).toContain('size="lg"')
1501+
1502+
// Should also include the component definition
1503+
const definitionResult = result.find(
1504+
(r: unknown) =>
1505+
typeof r === 'object' &&
1506+
r !== null &&
1507+
'title' in r &&
1508+
(r as { title: string }).title === 'PrimaryButton',
1509+
)
1510+
expect(definitionResult).toBeDefined()
1511+
const definitionCode = (definitionResult as { code: string }).code
1512+
expect(definitionCode).toContain('export function PrimaryButton')
15011513
})
15021514

15031515
it('should generate usage for positioned INSTANCE node (absolute)', async () => {

src/code-impl.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,13 +422,49 @@ export function registerCodegen(ctx: typeof figma) {
422422
...responsiveComponentsCodes,
423423
]
424424

425+
// For INSTANCE nodes, include the referenced component definition(s)
426+
// alongside Usage so developers see both how to use AND the implementation.
427+
const componentDefinitionResults: {
428+
title: string
429+
language: 'TYPESCRIPT' | 'BASH'
430+
code: string
431+
}[] = []
432+
if (node.type === 'INSTANCE' && componentsCodes.length > 0) {
433+
const importStatement = generateImportStatements(componentsCodes)
434+
const combinedCode = componentsCodes
435+
.map(([, code]) => code)
436+
.join('\n\n')
437+
const label =
438+
componentsCodes.length === 1
439+
? componentsCodes[0][0]
440+
: `${node.name} - Components`
441+
componentDefinitionResults.push(
442+
{
443+
title: label,
444+
language: 'TYPESCRIPT',
445+
code: importStatement + combinedCode,
446+
},
447+
{
448+
title: `${label} - CLI (Bash)`,
449+
language: 'BASH',
450+
code: generateBashCLI(componentsCodes),
451+
},
452+
{
453+
title: `${label} - CLI (PowerShell)`,
454+
language: 'BASH',
455+
code: generatePowerShellCLI(componentsCodes),
456+
},
457+
)
458+
}
459+
425460
// For COMPONENT nodes, show both the single-variant code AND Usage.
426461
// For COMPONENT_SET and INSTANCE, show only Usage.
427462
// For all other types, show the main code.
428463
const showMainCode = !isComponentType || node.type === 'COMPONENT'
429464

430465
return [
431466
...usageResults,
467+
...componentDefinitionResults,
432468
...(showMainCode
433469
? [
434470
{

0 commit comments

Comments
 (0)