]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): properly parse d.ts files when resolving types
authorEvan You <yyx990803@gmail.com>
Fri, 12 May 2023 10:08:29 +0000 (11:08 +0100)
committerEvan You <yyx990803@gmail.com>
Fri, 12 May 2023 10:08:29 +0000 (11:08 +0100)
close #8285

packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts
packages/compiler-sfc/src/script/context.ts
packages/compiler-sfc/src/script/resolveType.ts

index 4b36880bf1beef98a3e9c2bbda7d58a62ff9ebf1..240497310ee21fa68162212e73372efbddb0ccfc 100644 (file)
@@ -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(
         `
index d2c5dabd194a9b4b9b80aa914656af5d3412c58c..9a3d2eccd3c986cb239c08a9073bf1681d308379 100644 (file)
@@ -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') {
@@ -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')
     }
index 550b6c23767bbd87c39518b3e105b52442f4514b..6b43be582d590464ab69a0842b31fba0cf6f8986 100644 (file)
@@ -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') {