mirror of https://github.com/vuejs/core.git
fix(compiler-sfc): properly parse d.ts files when resolving types
close #8285
This commit is contained in:
parent
8dc8cf852b
commit
aa1e77d532
|
|
@ -458,7 +458,10 @@ describe('resolveType', () => {
|
||||||
test('relative ts', () => {
|
test('relative ts', () => {
|
||||||
const files = {
|
const files = {
|
||||||
'/foo.ts': 'export type P = { foo: number }',
|
'/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(
|
const { props, deps } = resolve(
|
||||||
`
|
`
|
||||||
|
|
|
||||||
|
|
@ -145,7 +145,8 @@ export class ScriptCompileContext {
|
||||||
|
|
||||||
export function resolveParserPlugins(
|
export function resolveParserPlugins(
|
||||||
lang: string,
|
lang: string,
|
||||||
userPlugins?: ParserPlugin[]
|
userPlugins?: ParserPlugin[],
|
||||||
|
dts = false
|
||||||
) {
|
) {
|
||||||
const plugins: ParserPlugin[] = []
|
const plugins: ParserPlugin[] = []
|
||||||
if (lang === 'jsx' || lang === 'tsx') {
|
if (lang === 'jsx' || lang === 'tsx') {
|
||||||
|
|
@ -156,7 +157,7 @@ export function resolveParserPlugins(
|
||||||
userPlugins = userPlugins.filter(p => p !== 'jsx')
|
userPlugins = userPlugins.filter(p => p !== 'jsx')
|
||||||
}
|
}
|
||||||
if (lang === 'ts' || lang === 'tsx') {
|
if (lang === 'ts' || lang === 'tsx') {
|
||||||
plugins.push('typescript')
|
plugins.push(['typescript', { dts }])
|
||||||
if (!plugins.includes('decorators')) {
|
if (!plugins.includes('decorators')) {
|
||||||
plugins.push('decorators-legacy')
|
plugins.push('decorators-legacy')
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -933,7 +933,11 @@ function parseFile(
|
||||||
const ext = extname(filename)
|
const ext = extname(filename)
|
||||||
if (ext === '.ts' || ext === '.tsx') {
|
if (ext === '.ts' || ext === '.tsx') {
|
||||||
return babelParse(content, {
|
return babelParse(content, {
|
||||||
plugins: resolveParserPlugins(ext.slice(1), parserPlugins),
|
plugins: resolveParserPlugins(
|
||||||
|
ext.slice(1),
|
||||||
|
parserPlugins,
|
||||||
|
filename.endsWith('.d.ts')
|
||||||
|
),
|
||||||
sourceType: 'module'
|
sourceType: 'module'
|
||||||
}).program.body
|
}).program.body
|
||||||
} else if (ext === '.vue') {
|
} else if (ext === '.vue') {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue