Skip to content

Commit

Permalink
fix: correctly resolve types from relative paths on Windows (#9446)
Browse files Browse the repository at this point in the history
  • Loading branch information
haoqunjiang authored Oct 21, 2023
1 parent e9e2778 commit 089d36d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
37 changes: 33 additions & 4 deletions packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { normalize } from 'node:path'
import { Identifier } from '@babel/types'
import { SFCScriptCompileOptions, parse } from '../../src'
import { ScriptCompileContext } from '../../src/script/context'
Expand Down Expand Up @@ -478,6 +479,33 @@ describe('resolveType', () => {
expect(deps && [...deps]).toStrictEqual(Object.keys(files))
})

test.runIf(process.platform === 'win32')('relative ts on Windows', () => {
const files = {
'C:\\Test\\foo.ts': 'export type P = { foo: number }',
'C:\\Test\\bar.d.ts':
'type X = { bar: string }; export { X as Y };' +
// verify that we can parse syntax that is only valid in d.ts
'export const baz: boolean'
}
const { props, deps } = resolve(
`
import { P } from './foo'
import { Y as PP } from './bar'
defineProps<P & PP>()
`,
files,
{},
'C:\\Test\\Test.vue'
)
expect(props).toStrictEqual({
foo: ['Number'],
bar: ['String']
})
expect(deps && [...deps].map(normalize)).toStrictEqual(
Object.keys(files).map(normalize)
)
})

// #8244
test('utility type in external file', () => {
const files = {
Expand Down Expand Up @@ -898,19 +926,20 @@ describe('resolveType', () => {
function resolve(
code: string,
files: Record<string, string> = {},
options?: Partial<SFCScriptCompileOptions>
options?: Partial<SFCScriptCompileOptions>,
sourceFileName: string = '/Test.vue'
) {
const { descriptor } = parse(`<script setup lang="ts">\n${code}\n</script>`, {
filename: '/Test.vue'
filename: sourceFileName
})
const ctx = new ScriptCompileContext(descriptor, {
id: 'test',
fs: {
fileExists(file) {
return !!files[file]
return !!(files[file] ?? files[normalize(file)])
},
readFile(file) {
return files[file]
return files[file] ?? files[normalize(file)]
}
},
...options
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-sfc/src/script/resolveType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ function importSourceToScope(
if (!resolved) {
if (source.startsWith('.')) {
// relative import - fast path
const filename = joinPaths(scope.filename, '..', source)
const filename = joinPaths(dirname(scope.filename), source)
resolved = resolveExt(filename, fs)
} else {
// module or aliased import - use full TS resolution, only supported in Node
Expand Down

0 comments on commit 089d36d

Please sign in to comment.