Skip to content

Commit 33b02c7

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

14 files changed

Lines changed: 59 additions & 33 deletions

File tree

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

Lines changed: 7 additions & 1 deletion
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

Lines changed: 4 additions & 6 deletions
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

Lines changed: 7 additions & 3 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 7 additions & 3 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 5 additions & 2 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 4 additions & 6 deletions
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')

0 commit comments

Comments
 (0)