-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(twoslash-vue): improve position handling
- Loading branch information
Showing
23 changed files
with
1,089 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/// <reference types="vite/client" /> | ||
|
||
import { extname } from 'node:path' | ||
import process from 'node:process' | ||
import { expect, it } from 'vitest' | ||
import type { TwoslashReturn } from 'twoslash' | ||
import { createTwoslasher } from '../src/index' | ||
|
||
// To add a test, create a file in the fixtures folder and it will will run through | ||
// as though it was the codeblock. | ||
|
||
const fixtures = import.meta.glob('./fixtures/**/*.*', { as: 'raw' }) | ||
|
||
// A temporary list of regex to match with the path of the file to test | ||
const filters: RegExp[] = [ | ||
// /completions-files/, | ||
] | ||
|
||
if (process.env.CI && filters.length) | ||
throw new Error('Should not filters fixture tests in CI, did you forget to remove them?') | ||
|
||
const twoslasher = createTwoslasher() | ||
|
||
Object.entries(fixtures).forEach(([path, fixture]) => { | ||
path = path.replace(/\\/g, '/') | ||
const expectThrows = path.includes('/throws/') | ||
const inExt = extname(path).slice(1) | ||
const outExt = expectThrows ? '.txt' : '.json' | ||
const outPath = path.replace('/fixtures/', '/results/').replace(/\.[^/.]+$/, outExt) | ||
|
||
it.skipIf(filters.length && !filters.some(f => path.match(f)))( | ||
path, | ||
async () => { | ||
let result: TwoslashReturn = undefined! | ||
try { | ||
result = twoslasher( | ||
await fixture(), | ||
inExt, | ||
{ | ||
customTags: ['annotate'], | ||
}, | ||
) | ||
} | ||
catch (err: any) { | ||
if (expectThrows) { | ||
expect(err.message).toMatchFileSnapshot(outPath) | ||
return | ||
} | ||
else { | ||
throw err | ||
} | ||
} | ||
|
||
if (expectThrows) { | ||
throw new Error('Expected to throw') | ||
} | ||
|
||
else { | ||
expect(cleanFixture(result)) | ||
.toMatchFileSnapshot(outPath) | ||
} | ||
}, | ||
) | ||
}) | ||
|
||
function cleanFixture(result: TwoslashReturn) { | ||
return JSON.stringify({ | ||
code: result.code, | ||
nodes: result.nodes, | ||
flags: result.meta.flagNotations, | ||
// compilerOptions: ts.meta.compilerOptions | ||
}, null, 2).replaceAll(process.cwd(), '[home]') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<script setup lang="ts"> | ||
import { ref } from "vue" | ||
const count1 = ref(1) | ||
function count2() { | ||
return count1.value + 1 | ||
} | ||
</script> | ||
|
||
<template> | ||
<div> | ||
{{ count1 }} | ||
// ^| | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<script setup lang="ts"> | ||
import { ref, computed } from 'vue' | ||
// ^? | ||
const count = ref(0) | ||
const double = computed(() => count.value * 2) | ||
// ^? | ||
</script> | ||
|
||
<script> | ||
export default { | ||
name: 'HelloWorld', | ||
data() { | ||
return { | ||
msg: 'Hello!' | ||
} | ||
}, | ||
methods: { | ||
greet() { | ||
console.log(this.msg) | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<template> | ||
<button @click="count++">{{ msg }} Count is: {{ count }}</button> | ||
// ^? | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<script setup lang="ts"> | ||
import { ref, computed } from 'vue' | ||
// ^? | ||
const count = ref(0) | ||
const double = computed(() => count.value * 2) | ||
// ^? | ||
</script> | ||
|
||
<template> | ||
<button @click="count++">count is: {{ count }}</button> | ||
// ^? | ||
<p>Count is: {{ count }}</p> | ||
// ^? | ||
</template> |
Oops, something went wrong.