Skip to content

Commit 2b66714

Browse files
committed
fix(plugin): some usage not support
1 parent cf8fa93 commit 2b66714

File tree

15 files changed

+827
-262
lines changed

15 files changed

+827
-262
lines changed

.vscode/settings.json

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
11
{
2-
"typescript.tsdk": "node_modules/typescript/lib"
2+
"typescript.tsdk": "node_modules/typescript/lib",
3+
// Disable the default formatter
4+
"prettier.enable": false,
5+
"editor.formatOnSave": false,
6+
7+
// Auto fix
8+
"editor.codeActionsOnSave": {
9+
"source.fixAll.eslint": "explicit"
10+
},
11+
12+
// Silent the stylistic rules in you IDE, but still auto fix them
13+
"eslint.rules.customizations": [
14+
{ "rule": "@stylistic", "severity": "off" },
15+
{ "rule": "*-indent", "severity": "off" },
16+
{ "rule": "*-spacing", "severity": "off" },
17+
{ "rule": "*-spaces", "severity": "off" },
18+
{ "rule": "*-order", "severity": "off" },
19+
{ "rule": "*-dangle", "severity": "off" },
20+
{ "rule": "*-newline", "severity": "off" },
21+
{ "rule": "*quotes", "severity": "off" },
22+
{ "rule": "*semi", "severity": "off" }
23+
],
24+
25+
"eslint.validate": [
26+
"javascript",
27+
"javascriptreact",
28+
"typescript",
29+
"typescriptreact",
30+
"vue",
31+
"html",
32+
"markdown",
33+
"json",
34+
"jsonc",
35+
"yaml"
36+
]
337
}

packages/plugins/typescript-syntax/__tests__/basic.test.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from 'vitest'
22
import { transformStyledSyntax } from '../src/ts-transformer'
3-
import { normalizeString } from './normalize'
3+
import { extractPropsFromCode } from './normalize'
44

55
describe('基本语法转换', () => {
66
it('应该转换简单的 styled.tag<Props> 为 styled("tag", { primary: { type: Boolean, required: false } })', () => {
@@ -19,7 +19,11 @@ describe('基本语法转换', () => {
1919
const result = transformStyledSyntax(code, 'test.tsx')
2020

2121
expect(result).not.toBeNull()
22-
expect(result?.code).toContain(`styled('button', { primary: { type: Boolean, required: false } })`)
22+
23+
const props = extractPropsFromCode(result?.props?.[0], 'Button')
24+
expect(props).toHaveProperty('primary')
25+
expect(props.primary.type).toBe('Boolean')
26+
expect(props.primary.required).toBe(false)
2327
})
2428

2529
it('应该转换行内泛型定义', () => {
@@ -35,8 +39,14 @@ describe('基本语法转换', () => {
3539
const result = transformStyledSyntax(code, 'test.tsx')
3640

3741
expect(result).not.toBeNull()
38-
/* console.log(result?.code) */
39-
expect(normalizeString(result?.code)).toContain(normalizeString(`styled('a', { active: { type: Boolean, required: true }, disabled: { type: Boolean, required: false } })`))
42+
43+
const props = extractPropsFromCode(result?.props?.[0], 'Link')
44+
expect(props).toHaveProperty('active')
45+
expect(props).toHaveProperty('disabled')
46+
expect(props.active.type).toBe('Boolean')
47+
expect(props.active.required).toBe(true)
48+
expect(props.disabled.type).toBe('Boolean')
49+
expect(props.disabled.required).toBe(false)
4050
})
4151

4252
it('不应该转换没有泛型的样式组件', () => {
@@ -93,7 +103,17 @@ describe('基本语法转换', () => {
93103
const result = transformStyledSyntax(code, 'test.tsx')
94104

95105
expect(result).not.toBeNull()
96-
expect(result?.code).toContain(`styled('button', { primary: { type: Boolean, required: false } })`)
97-
expect(result?.code).toContain(`styled('span', { size: { type: Number, required: false } })`)
106+
107+
// 检查第一个组件
108+
const buttonProps = extractPropsFromCode(result?.props?.[0], 'Button')
109+
expect(buttonProps).toHaveProperty('primary')
110+
expect(buttonProps.primary.type).toBe('Boolean')
111+
expect(buttonProps.primary.required).toBe(false)
112+
113+
// 检查第二个组件
114+
const iconProps = extractPropsFromCode(result?.props?.[1], 'Icon')
115+
expect(iconProps).toHaveProperty('size')
116+
expect(iconProps.size.type).toBe('Number')
117+
expect(iconProps.size.required).toBe(false)
98118
})
99119
})

packages/plugins/typescript-syntax/__tests__/complex.test.ts

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from 'vitest'
22
import { transformStyledSyntax } from '../src/ts-transformer'
3-
import { normalizeString } from './normalize'
3+
import { extractPropsFromCode } from './normalize'
44

55
describe('复杂泛型语法转换', () => {
66
it('应该转换嵌套泛型', () => {
@@ -24,7 +24,15 @@ describe('复杂泛型语法转换', () => {
2424
const result = transformStyledSyntax(code, 'test.tsx')
2525

2626
expect(result).not.toBeNull()
27-
expect(normalizeString(result?.code)).toContain(normalizeString(`styled('input', { value: { type: String, required: false }, onChange: { type: Function, required: false }, placeholder: { type: String, required: false } })`))
27+
28+
const props = extractPropsFromCode(result?.props?.[0], 'Input')
29+
expect(props).toHaveProperty('value')
30+
expect(props).toHaveProperty('onChange')
31+
expect(props).toHaveProperty('placeholder')
32+
expect(props.value.type).toBe('String')
33+
expect(props.value.required).toBe(false)
34+
expect(props.onChange.type).toBe('Function')
35+
expect(props.placeholder.type).toBe('String')
2836
})
2937

3038
it('应该转换多行泛型定义', () => {
@@ -44,11 +52,14 @@ describe('复杂泛型语法转换', () => {
4452

4553
expect(result).not.toBeNull()
4654

47-
expect(normalizeString(result?.code)).toContain(normalizeString(`styled('button', {
48-
primary: { type: Boolean, required: false },
49-
size: { type: String, required: false },
50-
disabled: { type: Boolean, required: false }
51-
})`))
55+
const props = extractPropsFromCode(result?.props?.[0], 'Button')
56+
expect(props).toHaveProperty('primary')
57+
expect(props).toHaveProperty('size')
58+
expect(props).toHaveProperty('disabled')
59+
expect(props.primary.type).toBe('Boolean')
60+
expect(props.primary.required).toBe(false)
61+
expect(props.size.type).toBe('String')
62+
expect(props.disabled.type).toBe('Boolean')
5263
})
5364

5465
it('应该转换带条件类型的复杂泛型', () => {
@@ -74,16 +85,12 @@ describe('复杂泛型语法转换', () => {
7485
const result = transformStyledSyntax(code, 'test.tsx')
7586

7687
expect(result).not.toBeNull()
77-
expect(normalizeString(result?.code)).toContain(normalizeString(`styled('button', {
78-
variant: {
79-
type: String,
80-
required: false
81-
},
82-
style: {
83-
type: String,
84-
required: false
85-
}
86-
})`))
88+
89+
const props = extractPropsFromCode(result?.props?.[0], 'Button')
90+
expect(props).toHaveProperty('variant')
91+
expect(props).toHaveProperty('style')
92+
expect(props.variant.required).toBe(false)
93+
expect(props.style.required).toBe(false)
8794
})
8895

8996
it('应该转换组合多个泛型和接口的类型', () => {
@@ -113,25 +120,14 @@ describe('复杂泛型语法转换', () => {
113120
const result = transformStyledSyntax(code, 'test.tsx')
114121

115122
expect(result).not.toBeNull()
116-
expect(normalizeString(result?.code)).toContain(normalizeString(`styled('button',
117-
{
118-
fullWidth: {
119-
type: Boolean,
120-
required: false
121-
},
122-
outlined: {
123-
type: Boolean,
124-
required: false
125-
},
126-
theme: {
127-
type: String,
128-
required: false
129-
},
130-
size: {
131-
type: String,
132-
required: false
133-
}
134-
}
135-
)`))
123+
124+
const props = extractPropsFromCode(result?.props?.[0], 'ComplexButton')
125+
expect(props).toHaveProperty('theme')
126+
expect(props).toHaveProperty('size')
127+
expect(props).toHaveProperty('fullWidth')
128+
expect(props).toHaveProperty('outlined')
129+
expect(props.theme.type).toBe('String')
130+
expect(props.fullWidth.type).toBe('Boolean')
131+
expect(props.fullWidth.required).toBe(false)
136132
})
137133
})

0 commit comments

Comments
 (0)