Skip to content

Commit

Permalink
fix(cli): missing props/emits when disable typescript (#105)
Browse files Browse the repository at this point in the history
* feat(cli): support remove type when non-typescript

* fix(cli): missing props/emits when doesn't enable typescript

* test: add test cases for defineProps/defineEmits

* fix: withDefaults case

* fix: cannot resolve external and packages types

* fix: missing assign variable
  • Loading branch information
Dunqing authored Nov 2, 2023
1 parent 67e6f1a commit a4774ff
Show file tree
Hide file tree
Showing 9 changed files with 995 additions and 1,239 deletions.
4 changes: 2 additions & 2 deletions apps/www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
"@types/node": "^20.8.7",
"@vitejs/plugin-vue": "^4.4.0",
"@vitejs/plugin-vue-jsx": "^3.0.2",
"@vue/compiler-core": "^3.3.6",
"@vue/compiler-dom": "^3.3.6",
"@vue/compiler-core": "^3.3.7",
"@vue/compiler-dom": "^3.3.7",
"autoprefixer": "^10.4.16",
"lodash.template": "^4.5.0",
"radix-vue": "^1.0.0",
Expand Down
5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@
"@commitlint/config-conventional"
]
},
"pnpm": {
"patchedDependencies": {
"detype@0.6.3": "patches/detype@0.6.3.patch"
}
},
"simple-git-hooks": {
"pre-commit": "pnpm lint-staged",
"commit-msg": "pnpm commitlint --edit ${1}"
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
"@babel/core": "^7.22.17",
"@babel/parser": "^7.22.16",
"@babel/plugin-transform-typescript": "^7.22.15",
"@vue/compiler-sfc": "^3.3.6",
"@vue/compiler-sfc": "^3.3.7",
"chalk": "5.3.0",
"commander": "^11.0.0",
"cosmiconfig": "^8.3.6",
"detype": "^0.6.3",
"detype": "npm:detypes@^0.7.6",
"diff": "^5.1.0",
"execa": "^8.0.1",
"fs-extra": "^11.1.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/utils/transformers/transform-sfc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export async function transformByDetype(content: string, filename: string) {
})
}

export const transformSFC: Transformer<string> = async ({ sourceFile, config }) => {
export const transformSFC: Transformer<string> = async ({ sourceFile, config, filename }) => {
const output = sourceFile?.getFullText()
if (config?.typescript)
return output

return await transformByDetype(output, 'app.vue')
return await transformByDetype(output, filename)
}
4 changes: 4 additions & 0 deletions packages/cli/test/utils/__fixtures__/props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Props {
a: string
b: number
}
50 changes: 50 additions & 0 deletions packages/cli/test/utils/__snapshots__/transform-sfc.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,53 @@ const array = [1, 2, 3];
<style scoped></style>
"
`;
exports[`transformSFC > defineEmits 1`] = `
"<script setup>
const emit = defineEmits([\\"foo\\"]);
</script>
"
`;
exports[`transformSFC > defineProps 1`] = `
"<script setup>
const props = defineProps({
foo: { type: String, required: true },
});
</script>
"
`;
exports[`transformSFC > defineProps with external props 1`] = `
"<script setup>
const props = defineProps({
foo: { type: String, required: false, default: \\"bar\\" },
a: { type: String, required: true },
b: { type: Number, required: true },
});
export {};
</script>
"
`;
exports[`transformSFC > defineProps with package props 1`] = `
"<script setup>
const props = defineProps({
foo: { type: String, required: false, default: \\"bar\\" },
for: { type: String, required: false },
asChild: { type: Boolean, required: false },
as: { type: [String, Object], required: false },
});
export {};
</script>
"
`;
exports[`transformSFC > defineProps with withDefaults 1`] = `
"<script setup>
const props = defineProps({
foo: { type: String, required: true, default: \\"bar\\" },
});
</script>
"
`;
69 changes: 69 additions & 0 deletions packages/cli/test/utils/transform-sfc.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { resolve } from 'node:path'
import { describe, expect, test } from 'vitest'
import { transform } from '../../src/utils/transformers'

Expand All @@ -22,4 +23,72 @@ describe('transformSFC', () => {
})
expect(result).toMatchSnapshot()
})

test('defineProps', async () => {
const result = await transform({
filename: 'app.vue',
raw: `<script lang="ts" setup>
const props = defineProps<{ foo: string }>()
</script>
`,
config: {},
})
expect(result).toMatchSnapshot()
})

test('defineProps with withDefaults', async () => {
const result = await transform({
filename: 'app.vue',
raw: `<script lang="ts" setup>
const props = withDefaults(defineProps<{ foo: string }>(), {
foo: 'bar'
})
</script>
`,
config: {},
})
expect(result).toMatchSnapshot()
})

test('defineProps with external props', async () => {
const result = await transform({
filename: resolve(__dirname, './test.vue'),
raw: `<script lang="ts" setup>
import { type Props } from './__fixtures__/props'
const props = withDefaults(defineProps<{ foo?: string } & Props>(), {
foo: 'bar'
})
</script>
`,
config: {},
})
expect(result).toMatchSnapshot()
})

test('defineProps with package props', async () => {
const result = await transform({
filename: resolve(__dirname, './test.vue'),
raw: `<script lang="ts" setup>
import { type LabelProps } from 'radix-vue'
const props = withDefaults(defineProps<{ foo?: string } & LabelProps>(), {
foo: 'bar'
})
</script>
`,
config: {},
})
expect(result).toMatchSnapshot()
})

test('defineEmits', async () => {
const result = await transform({
filename: 'app.vue',
raw: `<script lang="ts" setup>
const emit = defineEmits<{ foo: string }>()
</script>
`,
config: {},
})
expect(result).toMatchSnapshot()
})
})
16 changes: 0 additions & 16 deletions patches/detype@0.6.3.patch

This file was deleted.

Loading

0 comments on commit a4774ff

Please sign in to comment.