Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions playground/typed-router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ declare module 'vue-router/auto-routes' {
'/partial-[name]': RouteRecordInfo<'/partial-[name]', '/partial-:name', { name: ParamValue<true> }, { name: ParamValue<false> }>,
'/custom-path': RouteRecordInfo<'/custom-path', '/surprise-:id(\d+)', Record<never, never>, Record<never, never>>,
'/test-[a-id]': RouteRecordInfo<'/test-[a-id]', '/test-:a-id', { aId: ParamValue<true> }, { aId: ParamValue<false> }>,
'/test-broken-syntax': RouteRecordInfo<'/test-broken-syntax', '/test-broken-syntax', Record<never, never>, Record<never, never>>,
'/todos/': RouteRecordInfo<'/todos/', '/todos', Record<never, never>, Record<never, never>>,
'/todos/+layout': RouteRecordInfo<'/todos/+layout', '/todos/+layout', Record<never, never>, Record<never, never>>,
'/users/': RouteRecordInfo<'/users/', '/users', Record<never, never>, Record<never, never>>,
Expand Down Expand Up @@ -287,6 +288,10 @@ declare module 'vue-router/auto-routes' {
routes: '/test-[a-id]'
views: never
}
'src/pages/test-broken-syntax.vue': {
routes: '/test-broken-syntax'
views: never
}
'src/pages/todos/index.vue': {
routes: '/todos/'
views: never
Expand Down
140 changes: 132 additions & 8 deletions src/core/definePage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ definePage({
expect(result?.code).toMatchSnapshot()
})

it('throws if definePage uses a variable from the setup', async () => {
it('handles definePage using a variable from setup gracefully', async () => {
const code = `
<script setup>
const a = 1
Expand All @@ -162,13 +162,13 @@ definePage({
})
</script>
`
// the function syntax works with sync and async errors
await expect(async () => {
await definePageTransform({
code,
id: 'src/pages/basic.vue&definePage&vue',
})
}).rejects.toThrowError()
const result = await definePageTransform({
code,
id: 'src/pages/basic.vue&definePage&vue',
})

// Should return empty object instead of throwing
expect(result).toBe('export default {}')
})

it('extracts name and path', async () => {
Expand Down Expand Up @@ -254,4 +254,128 @@ export default {
path: '/custom',
})
})

describe('error handling', () => {
it('handles syntax errors gracefully when extracting definePage', async () => {
const codeWithSyntaxError = `
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this variable for a piece of code can be extracted in the outer scope. Share it among tests

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted the codeWithSyntaxError variable to the outer scope of the error handling describe block to share it among the tests, avoiding duplication. (5992675)

<script setup>
definePage({
name: 'test',, // syntax error: extra comma
path: '/test'
})
</script>

<template>
<div>hello</div>
</template>
`

const result = await definePageTransform({
code: codeWithSyntaxError,
id: 'src/pages/broken.vue?definePage&vue',
})

// Should return empty object instead of crashing
expect(result).toBe('export default {}')
})

it('handles syntax errors gracefully when removing definePage from source', async () => {
const codeWithSyntaxError = `
<script setup>
const a = 1
definePage({
name: 'test',, // syntax error: extra comma
path: '/test'
})
const b = 1
</script>

<template>
<div>hello</div>
</template>
`

const result = await definePageTransform({
code: codeWithSyntaxError,
id: 'src/pages/broken.vue',
})

// Should return undefined (no transform) instead of crashing
expect(result).toBeUndefined()
})

it('handles malformed definePage object gracefully', async () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is redundant, syntax errors are already tested. Ensure you don't have duplicated testss

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the redundant "handles malformed definePage object gracefully" test as it was duplicating syntax error testing already covered by other tests. (5992675)

const codeWithMalformedObject = `
<script setup>
definePage({
name: 'test'
path: '/test' // missing comma
})
</script>
`

const result = await definePageTransform({
code: codeWithMalformedObject,
id: 'src/pages/malformed.vue?definePage&vue',
})

expect(result).toBe('export default {}')
})

it('handles completely invalid JavaScript syntax gracefully', async () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, remove it, it's just syntax error

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the redundant "handles completely invalid JavaScript syntax gracefully" test as it was just another syntax error variation already covered by existing tests. (5992675)

const codeWithInvalidSyntax = `
<script setup>
definePage({
name: 'test',
path: '/test'
invalid javascript syntax here ###
})
</script>
`

const result = await definePageTransform({
code: codeWithInvalidSyntax,
id: 'src/pages/invalid.vue?definePage&vue',
})

expect(result).toBe('export default {}')
})

it('handles extractDefinePageNameAndPath with syntax errors gracefully', async () => {
const codeWithSyntaxError = `
<script setup>
definePage({
name: 'test',, // syntax error: extra comma
path: '/test'
})
</script>
`

const result = await extractDefinePageNameAndPath(
codeWithSyntaxError,
'src/pages/broken.vue'
)

// Should return null/undefined instead of crashing
expect(result).toBeUndefined()
})

it('handles unclosed brackets in definePage gracefully', async () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just a syntax error, it's a redundant test

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the redundant "handles unclosed brackets in definePage gracefully" test as it was just another syntax error variation already covered by existing tests. (d8052e0)

const codeWithUnclosedBracket = `
<script setup>
definePage({
name: 'test',
path: '/test'
// missing closing bracket
</script>
`

const result = await definePageTransform({
code: codeWithUnclosedBracket,
id: 'src/pages/unclosed.vue?definePage&vue',
})

expect(result).toBe('export default {}')
})
})
})
Loading
Loading