Skip to content

Commit 33b02c7

Browse files
authored
fix: genCodeUnitTest failed (#1291)
1 parent 6072920 commit 33b02c7

File tree

14 files changed

+59
-33
lines changed

14 files changed

+59
-33
lines changed

packages/vue-generator/src/generator/vue/sfc/generateAttribute.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ const transformObjValue = (renderKey, value, globalHooks, config, transformObjTy
469469
return result
470470
}
471471

472+
const normalKeyRegexp = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/
472473
export const transformObjType = (obj, globalHooks, config) => {
473474
if (!obj || typeof obj !== 'object') {
474475
return {
@@ -481,7 +482,12 @@ export const transformObjType = (obj, globalHooks, config) => {
481482
let shouldRenderKey = !Array.isArray(obj)
482483

483484
for (const [key, value] of Object.entries(obj)) {
484-
let renderKey = shouldRenderKey ? `${key}: ` : ''
485+
let renderKey = shouldRenderKey
486+
? // 如果 key 是合法的变量名,则直接使用 key: 的形式,否则需要添加引号,防止不合法的 key 导致语法错误
487+
normalKeyRegexp.test(key)
488+
? `${key}: `
489+
: `'${key}': `
490+
: ''
485491

486492
const { res, shouldBindToState: tmpShouldBindToState } = transformObjValue(
487493
renderKey,

packages/vue-generator/test/testcases/element-plus-case/element-plus-case.test.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ describe('generate element-plus material project correctly', () => {
2929
fs.mkdirSync(path.resolve(__dirname, `./result/appdemo01/${filePath}`), { recursive: true })
3030

3131
if (typeof fileContent === 'string') {
32-
fs.writeFileSync(
33-
path.resolve(__dirname, `./result/appdemo01/${filePath}/${fileName}`),
34-
// 这里需要将换行符替换成 CRLF 格式的
35-
fileContent.replace(/\r?\n/g, '\r\n')
36-
)
32+
fs.writeFileSync(path.resolve(__dirname, `./result/appdemo01/${filePath}/${fileName}`), fileContent)
3733
} else if (fileContent instanceof Blob) {
3834
const arrayBuffer = await fileContent.arrayBuffer()
3935
const buffer = Buffer.from(arrayBuffer)
@@ -46,7 +42,9 @@ describe('generate element-plus material project correctly', () => {
4642
compareContent: true,
4743
ignoreLineEnding: true,
4844
ignoreAllWhiteSpaces: true,
49-
ignoreEmptyLines: true
45+
ignoreEmptyLines: true,
46+
compareFileSync: dirCompare.fileCompareHandlers.lineBasedFileCompare.compareSync,
47+
compareFileAsync: dirCompare.fileCompareHandlers.lineBasedFileCompare.compareAsync
5048
}
5149

5250
const path1 = path.resolve(__dirname, './expected/appdemo01')

packages/vue-generator/test/testcases/element-plus-case/expected/appdemo01/src/http/axios.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default (config) => {
2020

2121
if (typeof MockAdapter.prototype.proxy === 'undefined') {
2222
MockAdapter.prototype.proxy = function ({ url, config = {}, proxy, response, handleData } = {}) {
23+
// eslint-disable-next-line @typescript-eslint/no-this-alias
2324
let stream = this
2425
const request = (proxy, any) => {
2526
return (setting) => {
@@ -28,8 +29,9 @@ export default (config) => {
2829
axios
2930
.get(any ? proxy + setting.url + '.json' : proxy, config)
3031
.then(({ data }) => {
31-
/* eslint-disable no-useless-call */
32-
typeof handleData === 'function' && (data = handleData.call(null, data, setting))
32+
if (typeof handleData === 'function') {
33+
data = handleData.call(null, data, setting)
34+
}
3335
resolve([200, data])
3436
})
3537
.catch((error) => {
@@ -127,7 +129,9 @@ export default (config) => {
127129
return mock
128130
},
129131
disableMock() {
130-
mock && mock.restore()
132+
if (mock) {
133+
mock.restore()
134+
}
131135
mock = undefined
132136
},
133137
isMock() {

packages/vue-generator/test/testcases/element-plus-case/expected/appdemo01/src/lowcodeConfig/dataSource.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ dataSources.list.forEach((config) => {
7272
}
7373

7474
const errorHandler = (error) => {
75-
config.errorHandler?.value && createFn(config.errorHandler.value)(error)
75+
if (config.errorHandler?.value) {
76+
createFn(config.errorHandler.value)(error)
77+
}
7678
dataSource.status = 'error'
7779
dataSource.error = error
7880
}

packages/vue-generator/test/testcases/element-plus-case/expected/appdemo01/vite.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ export default defineConfig({
1919
transformMixedEsModules: true
2020
},
2121
cssCodeSplit: false
22-
}
22+
},
23+
base: './'
2324
})

packages/vue-generator/test/testcases/generator/expected/appdemo01/src/http/axios.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default (config) => {
2020

2121
if (typeof MockAdapter.prototype.proxy === 'undefined') {
2222
MockAdapter.prototype.proxy = function ({ url, config = {}, proxy, response, handleData } = {}) {
23+
// eslint-disable-next-line @typescript-eslint/no-this-alias
2324
let stream = this
2425
const request = (proxy, any) => {
2526
return (setting) => {
@@ -28,8 +29,9 @@ export default (config) => {
2829
axios
2930
.get(any ? proxy + setting.url + '.json' : proxy, config)
3031
.then(({ data }) => {
31-
/* eslint-disable no-useless-call */
32-
typeof handleData === 'function' && (data = handleData.call(null, data, setting))
32+
if (typeof handleData === 'function') {
33+
data = handleData.call(null, data, setting)
34+
}
3335
resolve([200, data])
3436
})
3537
.catch((error) => {
@@ -127,7 +129,9 @@ export default (config) => {
127129
return mock
128130
},
129131
disableMock() {
130-
mock && mock.restore()
132+
if (mock) {
133+
mock.restore()
134+
}
131135
mock = undefined
132136
},
133137
isMock() {

packages/vue-generator/test/testcases/generator/expected/appdemo01/src/lowcodeConfig/dataSource.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ dataSources.list.forEach((config) => {
7272
}
7373

7474
const errorHandler = (error) => {
75-
config.errorHandler?.value && createFn(config.errorHandler.value)(error)
75+
if (config.errorHandler?.value) {
76+
createFn(config.errorHandler.value)(error)
77+
}
7678
dataSource.status = 'error'
7779
dataSource.error = error
7880
}

packages/vue-generator/test/testcases/generator/expected/appdemo01/src/views/createVm.vue

+5-2
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,10 @@
285285
>
286286
<tiny-form-item label="数据盘" style="border-radius: 0px">
287287
<div v-for="() in state.dataDisk" style="margin-top: 12px; display: flex">
288-
<tiny-icon-panel-mini style="margin-right: 10px; width: 16px; height: 16px"></tiny-icon-panel-mini>
288+
<tiny-icon-panel-mini
289+
style="margin-right: 10px; width: 16px; height: 16px"
290+
fill="currentColor"
291+
></tiny-icon-panel-mini>
289292
<tiny-select
290293
modelValue=""
291294
placeholder="请选择"
@@ -300,7 +303,7 @@
300303
<tiny-input placeholder="请输入" modelValue="" style="width: 120px"></tiny-input>
301304
</div>
302305
<div style="display: flex; margin-top: 12px; border-radius: 0px">
303-
<tiny-icon-plus style="width: 16px; height: 16px; margin-right: 10px"></tiny-icon-plus>
306+
<tiny-icon-plus style="width: 16px; height: 16px; margin-right: 10px" fill="currentColor"></tiny-icon-plus>
304307
<span style="font-size: 12px; border-radius: 0px; margin-right: 10px">增加一块数据盘</span>
305308
<span style="color: #8a8e99; font-size: 12px">您还可以挂载 21 块磁盘(云硬盘)</span>
306309
</div></tiny-form-item

packages/vue-generator/test/testcases/generator/expected/appdemo01/vite.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ export default defineConfig({
1919
transformMixedEsModules: true
2020
},
2121
cssCodeSplit: false
22-
}
22+
},
23+
base: './'
2324
})

packages/vue-generator/test/testcases/generator/generateApp.test.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ describe('generate whole application', () => {
2929
fs.mkdirSync(path.resolve(__dirname, `./result/appdemo01/${filePath}`), { recursive: true })
3030

3131
if (typeof fileContent === 'string') {
32-
fs.writeFileSync(
33-
path.resolve(__dirname, `./result/appdemo01/${filePath}/${fileName}`),
34-
// 这里需要将换行符替换成 CRLF 格式的
35-
fileContent.replace(/\r?\n/g, '\r\n')
36-
)
32+
fs.writeFileSync(path.resolve(__dirname, `./result/appdemo01/${filePath}/${fileName}`), fileContent)
3733
} else if (fileContent instanceof Blob) {
3834
const arrayBuffer = await fileContent.arrayBuffer()
3935
const buffer = Buffer.from(arrayBuffer)
@@ -46,7 +42,9 @@ describe('generate whole application', () => {
4642
compareContent: true,
4743
ignoreLineEnding: true,
4844
ignoreAllWhiteSpaces: true,
49-
ignoreEmptyLines: true
45+
ignoreEmptyLines: true,
46+
compareFileSync: dirCompare.fileCompareHandlers.lineBasedFileCompare.compareSync,
47+
compareFileAsync: dirCompare.fileCompareHandlers.lineBasedFileCompare.compareAsync
5048
}
5149

5250
const path1 = path.resolve(__dirname, './expected/appdemo01')
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { expect, test } from 'vitest'
1+
import { expect, test, describe } from 'vitest'
22
import { genSFCWithDefaultPlugin } from '@/generator/vue/sfc'
33
import schema from './schema.json'
44
import { formatCode } from '@/utils/formatCode'
55

6-
test('should validate tagName', async () => {
7-
const res = genSFCWithDefaultPlugin(schema, [])
8-
const formattedCode = formatCode(res, 'vue')
6+
describe('should generate state correctly', () => {
7+
test('should generate state accessor correctly', async () => {
8+
const res = genSFCWithDefaultPlugin(schema, [])
9+
const formattedCode = formatCode(res, 'vue')
910

10-
await expect(formattedCode).toMatchFileSnapshot('./expected/Accessor.vue')
11+
await expect(formattedCode).toMatchFileSnapshot('./expected/Accessor.vue')
12+
})
1113
})

packages/vue-generator/test/testcases/sfc/accessor/expected/Accessor.vue

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const { utils } = wrap(function () {
2020
return this
2121
})()
2222
const state = vue.reactive({
23+
'a.b': 'test',
2324
firstName: 'Opentiny',
2425
lastName: 'TinyEngine',
2526
nullValue: null,

packages/vue-generator/test/testcases/sfc/accessor/schema.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
}
1414
],
1515
"state": {
16+
"a.b": "test",
1617
"firstName": "Opentiny",
1718
"lastName": "TinyEngine",
1819
"nullValue": {

packages/vue-generator/test/testcases/sfc/case01/expected/FormTable.vue

+6-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
></tiny-grid>
6161
</div>
6262
<div :style="{ width: props.quotePopWidth }">循环渲染:</div>
63-
<tiny-icon-help-circle v-if="false"></tiny-icon-help-circle>
63+
<tiny-icon-help-circle v-if="false" fill="currentColor"></tiny-icon-help-circle>
6464

6565
<tiny-button
6666
v-for="(item, index) in state.buttons"
@@ -181,7 +181,7 @@ const state = vue.reactive({
181181
slots: {
182182
default: ({ row }, h) => (
183183
<div>
184-
<TinyIconEdit></TinyIconEdit>
184+
<TinyIconEdit fill="currentColor"></TinyIconEdit>
185185
{props.isEdit && (
186186
<CrmQuoteListGridStatus isEdit={props.isEdit} status={row.status}></CrmQuoteListGridStatus>
187187
)}
@@ -207,7 +207,10 @@ const state = vue.reactive({
207207
>
208208
<TinyInput value={row.giveamount}></TinyInput>
209209
{state.cityOptions.length && <span>{t('operation.hello')}</span>}
210-
<TinyIconHelpCircle style="margin-left: 6px; cursor: pointer;vertical-align: top;"></TinyIconHelpCircle>
210+
<TinyIconHelpCircle
211+
style="margin-left: 6px; cursor: pointer;vertical-align: top;"
212+
fill="currentColor"
213+
></TinyIconHelpCircle>
211214
</div>
212215
)
213216
}

0 commit comments

Comments
 (0)