Skip to content

Commit

Permalink
fix: 馃悰 Allow imported TS in JS SFCs
Browse files Browse the repository at this point in the history
Previously when importing TypeScript from a plain JavaScript SFC, the
validator value resolver assumed that imported modules would also be
JavaScript (or rather at best flow). This change updates the resolver to
check the extension of the file about to be parsed to use the TypeScript
plugin rather than the flow plugin when the file extension is `.ts` or
`.tsx`
  • Loading branch information
sarayourfriend committed Mar 9, 2022
1 parent 3860e4c commit ccc06ce
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/vue-docgen-api/src/utils/getPathFromExportedValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { ParseOptions } from '../parse'

const read = promisify(readFile)

const tsExtensions = /.tsx?$/

export default async function getPathOfExportedValue(
pathResolver: (path: string, originalDirNameOverride?: string) => string | null,
exportName: string,
Expand All @@ -28,10 +30,17 @@ export default async function getPathOfExportedValue(
if (!currentFilePath) {
return undefined
}

let filePlugins = plugins
// Fixes SFCs written in JS having their imported modules being assumed to also be JS
if (tsExtensions.test(currentFilePath)) {
filePlugins = filePlugins.map(plugin => plugin === 'flow' ? 'typescript' : plugin)
}

const source = await read(currentFilePath, {
encoding: 'utf-8'
})
const ast = cacher(() => parse(source, { parser: buildParser({ plugins }) }), source)
const ast = cacher(() => parse(source, { parser: buildParser({ plugins: filePlugins }) }), source)

visit(ast, {
visitExportNamedDeclaration(p) {
Expand Down

0 comments on commit ccc06ce

Please sign in to comment.