Skip to content

Commit

Permalink
fix(compiler-sfc): properly parse d.ts files when resolving types
Browse files Browse the repository at this point in the history
close #8285
  • Loading branch information
yyx990803 committed May 12, 2023
1 parent 8dc8cf8 commit aa1e77d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,10 @@ describe('resolveType', () => {
test('relative ts', () => {
const files = {
'/foo.ts': 'export type P = { foo: number }',
'/bar.d.ts': 'type X = { bar: string }; export { X as Y }'
'/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(
`
Expand Down
5 changes: 3 additions & 2 deletions packages/compiler-sfc/src/script/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ export class ScriptCompileContext {

export function resolveParserPlugins(
lang: string,
userPlugins?: ParserPlugin[]
userPlugins?: ParserPlugin[],
dts = false
) {
const plugins: ParserPlugin[] = []
if (lang === 'jsx' || lang === 'tsx') {
Expand All @@ -156,7 +157,7 @@ export function resolveParserPlugins(
userPlugins = userPlugins.filter(p => p !== 'jsx')
}
if (lang === 'ts' || lang === 'tsx') {
plugins.push('typescript')
plugins.push(['typescript', { dts }])
if (!plugins.includes('decorators')) {
plugins.push('decorators-legacy')
}
Expand Down
6 changes: 5 additions & 1 deletion packages/compiler-sfc/src/script/resolveType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,11 @@ function parseFile(
const ext = extname(filename)
if (ext === '.ts' || ext === '.tsx') {
return babelParse(content, {
plugins: resolveParserPlugins(ext.slice(1), parserPlugins),
plugins: resolveParserPlugins(
ext.slice(1),
parserPlugins,
filename.endsWith('.d.ts')
),
sourceType: 'module'
}).program.body
} else if (ext === '.vue') {
Expand Down

0 comments on commit aa1e77d

Please sign in to comment.