Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly resolve types from relative paths on Windows #9446

Merged
merged 3 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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