Skip to content

Commit 1b5696a

Browse files
committed
Fix indent issue
1 parent 9a78f23 commit 1b5696a

File tree

8 files changed

+81
-57
lines changed

8 files changed

+81
-57
lines changed

src/codegen/Codegen.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getComponentName } from '../utils'
22
import { getProps } from './props'
3-
import { getSelectorProps } from './props/selector'
3+
import { getSelectorProps, sanitizePropertyName } from './props/selector'
44
import { renderComponent, renderNode } from './render'
55
import { renderText } from './render/text'
66
import type { ComponentTree, NodeTree } from './types'
@@ -79,7 +79,7 @@ export class Codegen {
7979
for (const [compNode, compTree] of this.componentTrees) {
8080
if (!this.components.has(compNode)) {
8181
this.components.set(compNode, {
82-
code: Codegen.renderTree(compTree.tree, 2),
82+
code: Codegen.renderTree(compTree.tree, 0),
8383
variants: compTree.variants,
8484
})
8585
}
@@ -138,6 +138,17 @@ export class Codegen {
138138

139139
const componentName = getComponentName(mainComponent || node)
140140

141+
// Extract variant props from instance's componentProperties
142+
const variantProps: Record<string, string> = {}
143+
if (node.componentProperties) {
144+
for (const [key, prop] of Object.entries(node.componentProperties)) {
145+
if (prop.type === 'VARIANT') {
146+
const sanitizedKey = sanitizePropertyName(key)
147+
variantProps[sanitizedKey] = String(prop.value)
148+
}
149+
}
150+
}
151+
141152
// Check if needs position wrapper
142153
if (props.pos) {
143154
return {
@@ -158,7 +169,7 @@ export class Codegen {
158169
children: [
159170
{
160171
component: componentName,
161-
props: {},
172+
props: variantProps,
162173
children: [],
163174
nodeType: node.type,
164175
nodeName: node.name,
@@ -172,7 +183,7 @@ export class Codegen {
172183

173184
return {
174185
component: componentName,
175-
props: {},
186+
props: variantProps,
176187
children: [],
177188
nodeType: node.type,
178189
nodeName: node.name,

src/codegen/__tests__/__snapshots__/codegen.test.ts.snap

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2948,17 +2948,17 @@ exports[`render real world component real world $ 104`] = `
29482948
transition="0.3ms ease-out"
29492949
transitionProperty="background,box-shadow,opacity"
29502950
>
2951-
<Text
2952-
color="#FFF"
2953-
fontFamily="Pretendard"
2954-
fontSize="16px"
2955-
fontWeight="700"
2956-
letterSpacing="-0.04em"
2957-
lineHeight="1.4"
2958-
wordBreak="keep-all"
2959-
>
2960-
자세히 알아보기 →
2961-
</Text>
2951+
<Text
2952+
color="#FFF"
2953+
fontFamily="Pretendard"
2954+
fontSize="16px"
2955+
fontWeight="700"
2956+
letterSpacing="-0.04em"
2957+
lineHeight="1.4"
2958+
wordBreak="keep-all"
2959+
>
2960+
자세히 알아보기 →
2961+
</Text>
29622962
</Center>
29632963
)
29642964
}"
@@ -3005,23 +3005,23 @@ export function Button({ size, variant }: ButtonProps) {
30053005
transition="0.2ms ease-out"
30063006
transitionProperty="background"
30073007
>
3008-
<Text
3009-
color={{
3010-
primary: "#FFF",
3011-
white: "$primaryAccent"
3012-
}[variant]}
3013-
fontFamily="Noto Sans KR"
3014-
fontSize={{
3015-
Md: ["16px", null, null, null, "20px"],
3016-
Sm: ["14px", null, null, null, "15px"]
3017-
}[size]}
3018-
fontWeight="500"
3019-
letterSpacing="-0.04em"
3020-
lineHeight="normal"
3021-
wordBreak="keep-all"
3022-
>
3023-
참가 신청하기
3024-
</Text>
3008+
<Text
3009+
color={{
3010+
primary: "#FFF",
3011+
white: "$primaryAccent"
3012+
}[variant]}
3013+
fontFamily="Noto Sans KR"
3014+
fontSize={{
3015+
Md: ["16px", null, null, null, "20px"],
3016+
Sm: ["14px", null, null, null, "15px"]
3017+
}[size]}
3018+
fontWeight="500"
3019+
letterSpacing="-0.04em"
3020+
lineHeight="normal"
3021+
wordBreak="keep-all"
3022+
>
3023+
참가 신청하기
3024+
</Text>
30253025
</Center>
30263026
)
30273027
}"

src/codegen/__tests__/render.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('renderNode', () => {
2929
props: { p: '8px' },
3030
deps: 1,
3131
children: ['<Text />'] as string[],
32-
expected: ' <Box p="8px">\n <Text />\n </Box>',
32+
expected: ' <Box p="8px">\n <Text />\n </Box>',
3333
},
3434
])('$title', ({ component, props, deps, children, expected }) => {
3535
const result = renderNode(component, props, deps, children)
@@ -61,9 +61,9 @@ describe('renderComponent', () => {
6161
6262
export function Banner({ size }: BannerProps) {
6363
return (
64-
<Box>
65-
<Text />
66-
</Box>
64+
<Box>
65+
<Text />
66+
</Box>
6767
)
6868
}`,
6969
},

src/codegen/render/index.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { space } from '../../utils'
22
import { filterPropsWithComponent } from '../props'
33
import { isDefaultProp } from '../utils/is-default-prop'
4-
import { paddingLeftMultiline } from '../utils/padding-left-multiline'
4+
import {
5+
paddingLeftMultiline,
6+
wrapReturnStatement,
7+
} from '../utils/padding-left-multiline'
58
import { propsToString } from '../utils/props-to-str'
69

710
export function renderNode(
@@ -25,7 +28,7 @@ export function renderNode(
2528
}`,
2629
hasChildren
2730
? childrenCodes
28-
.map((child) => space(1) + child.split('\n').join(`\n${space(1)}`))
31+
.map((child) => paddingLeftMultiline(child, deps + 1))
2932
.join('\n')
3033
: '',
3134
tail,
@@ -55,14 +58,7 @@ ${Object.entries(filteredVariants)
5558
? `{ ${Object.keys(filteredVariants).join(', ')} }: ${component}Props`
5659
: ''
5760
return `${interfaceCode}export function ${component}(${propsParam}) {
58-
return ${
59-
code.includes('\n')
60-
? `(\n${code
61-
.split('\n')
62-
.map((line) => line)
63-
.join('\n')}\n${space(1)})`
64-
: code.trim().replace(/\s+/g, ' ')
65-
}
61+
return ${wrapReturnStatement(code, 1)}
6662
}`
6763
}
6864

src/codegen/responsive/ResponsiveCodegen.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ export class ResponsiveCodegen {
375375
// Generate merged responsive code
376376
const mergedCode = responsiveCodegen.generateMergedCode(
377377
treesByBreakpoint,
378-
2,
378+
0,
379379
)
380380

381381
results.push([
@@ -574,7 +574,7 @@ export class ResponsiveCodegen {
574574
const mergedCode = responsiveCodegen.generateMultiVariantMergedCode(
575575
sanitizedVariantKeys,
576576
responsivePropsByComposite,
577-
2,
577+
0,
578578
)
579579

580580
const result: Array<readonly [string, string]> = [
@@ -608,7 +608,7 @@ export class ResponsiveCodegen {
608608
}
609609

610610
// Render the tree to JSX
611-
const code = Codegen.renderTree(tree, 2)
611+
const code = Codegen.renderTree(tree, 0)
612612

613613
// No variant props needed since effect is handled via pseudo-selectors
614614
const result: Array<readonly [string, string]> = [
@@ -678,7 +678,7 @@ export class ResponsiveCodegen {
678678
const mergedCode = responsiveCodegen.generateVariantOnlyMergedCode(
679679
sanitizedPrimaryVariantKey,
680680
treesByVariant,
681-
2,
681+
0,
682682
)
683683

684684
const result: Array<readonly [string, string]> = [

src/codegen/responsive/__tests__/__snapshots__/ResponsiveCodegen.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
exports[`ResponsiveCodegen generateVariantOnlyMergedCode renders OR conditional for child existing in multiple but not all variants 1`] = `"render:Flex:depth=0:{}|{(status === "scroll" || status === "hover") && render:Box:depth=0:{"id":{"__variantProp":true,"variantKey":"status","values":{"scroll":"PartialChild","hover":"PartialChildHover"}}}|}"`;
44

5-
exports[`ResponsiveCodegen generateVariantResponsiveComponents handles effect + viewport + size + variant (4 dimensions) 1`] = `"component:Button:{"size":"'Md' | 'Sm'","variant":"'primary' | 'white'"}|render:Box:depth=2:{"id":"RootTablet","_hover":{"bg":{"__variantProp":true,"variantKey":"variant","values":{"primary":"#3D2B1F","white":"#F2F2F2"}}},"_active":{"bg":{"__variantProp":true,"variantKey":"variant","values":{"primary":"#30241A","white":"#E6E6E6"}}}}|render:Box:depth=0:{"id":"Shared"}|"`;
5+
exports[`ResponsiveCodegen generateVariantResponsiveComponents handles effect + viewport + size + variant (4 dimensions) 1`] = `"component:Button:{"size":"'Md' | 'Sm'","variant":"'primary' | 'white'"}|render:Box:depth=0:{"id":"RootTablet","_hover":{"bg":{"__variantProp":true,"variantKey":"variant","values":{"primary":"#3D2B1F","white":"#F2F2F2"}}},"_active":{"bg":{"__variantProp":true,"variantKey":"variant","values":{"primary":"#30241A","white":"#E6E6E6"}}}}|render:Box:depth=0:{"id":"Shared"}|"`;
Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
import { space } from '../../utils'
22

3-
export function paddingLeftMultiline(code: string, dep = 0) {
4-
if (dep === 0) return code
3+
/**
4+
* 코드 블록의 각 줄에 들여쓰기 추가
5+
*/
6+
export function paddingLeftMultiline(code: string, depth = 0) {
7+
if (depth === 0) return code
58
return code
69
.split('\n')
7-
.map((line) => space(dep) + line)
10+
.map((line) => space(depth) + line)
811
.join('\n')
912
}
13+
14+
/**
15+
* return 문으로 JSX 코드를 래핑
16+
* - 한 줄: `<Component />`
17+
* - 여러 줄: `(\n <Component>\n </Component>\n )`
18+
*/
19+
export function wrapReturnStatement(code: string, baseDepth = 1): string {
20+
const hasNewLine = code.includes('\n')
21+
if (!hasNewLine) {
22+
return code.trim().replace(/\s+/g, ' ')
23+
}
24+
// 멀티라인: ( ... )
25+
const indentedCode = paddingLeftMultiline(code, baseDepth + 1)
26+
return `(\n${indentedCode}\n${space(baseDepth)})`
27+
}

src/codegen/utils/wrap-component.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { paddingLeftMultiline } from './padding-left-multiline'
1+
import { wrapReturnStatement } from './padding-left-multiline'
22

33
interface WrapComponentOptions {
44
exportDefault?: boolean
@@ -10,9 +10,8 @@ export function wrapComponent(
1010
options: WrapComponentOptions = {},
1111
) {
1212
const { exportDefault = false } = options
13-
const hasNewLine = code.includes('\n')
1413
const exportKeyword = exportDefault ? 'export default' : 'export'
1514
return `${exportKeyword} function ${name}() {
16-
return ${hasNewLine ? `(\n${paddingLeftMultiline(code, 2)}\n )` : code}
15+
return ${wrapReturnStatement(code, 1)}
1716
}`
1817
}

0 commit comments

Comments
 (0)