提交 5d68c339 编写于 作者: fxy060608's avatar fxy060608

wip(uts): compiler

上级 3c11f5a1
import fs from 'fs'
import path from 'path'
import { camelize, capitalize } from '@vue/shared'
import { camelize, capitalize, isArray } from '@vue/shared'
import type {
ArrowFunctionExpression,
......@@ -252,16 +252,21 @@ async function parseInterfaceTypes(
const ast: Module = await parse(fs.readFileSync(interfaceFilename, 'utf8'), {
noColor: !isColorSupported(),
})
const types: Record<string, Param[]> = {}
const types: Record<string, Param[] | string> = {}
ast.body.filter((node) => {
if (
node.type === 'ExportDeclaration' &&
node.declaration.type === 'TsTypeAliasDeclaration' &&
node.declaration.typeAnnotation.type === 'TsFunctionType'
node.declaration.type === 'TsTypeAliasDeclaration'
) {
const params = createParams(node.declaration.typeAnnotation.params)
if (params.length) {
types[node.declaration.id.value] = params
switch (node.declaration.typeAnnotation.type) {
case 'TsFunctionType':
const params = createParams(node.declaration.typeAnnotation.params)
if (params.length) {
types[node.declaration.id.value] = params
}
break
case 'TsTypeLiteral':
types[node.declaration.id.value] = node.declaration.id.value
}
}
})
......@@ -373,7 +378,11 @@ async function parseCode(code: string, namespace: string, types: Types) {
// eslint-disable-next-line no-restricted-globals
const { parse } = require('@dcloudio/uts')
const ast = await parse(code, { noColor: !isColorSupported() })
return parseAst(ast, createResolveTypeReferenceName(namespace, ast), types)
return parseAst(
ast,
createResolveTypeReferenceName(namespace, ast, types),
types
)
}
type ProxyDecl = ProxyFunctionDeclaration | ProxyClass | VariableDeclaration
......@@ -701,7 +710,10 @@ function genVariableDeclaration(
if (typeAnn && typeAnn.typeAnnotation.type === 'TsTypeReference') {
const { typeName } = typeAnn.typeAnnotation
if (typeName.type === 'Identifier') {
params = types[typeName.value]
const value = types[typeName.value]
if (isArray(value)) {
params = value
}
}
}
return genFunctionDeclaration(
......
import path, { basename, resolve } from 'path'
import fs from 'fs-extra'
import type { parse, bundle, UTSTarget } from '@dcloudio/uts'
import { camelize, capitalize, extend } from '@vue/shared'
import { camelize, capitalize, extend, isString } from '@vue/shared'
import glob from 'fast-glob'
import { Module, ModuleItem } from '../types/types'
import {
......@@ -184,10 +184,14 @@ function resolveTypeAliasDeclNames(items: ModuleItem[]) {
return names
}
export function createResolveTypeReferenceName(namespace: string, ast: Module) {
export function createResolveTypeReferenceName(
namespace: string,
ast: Module,
types: Record<string, unknown>
) {
const names = resolveTypeAliasDeclNames(ast.body)
return (name: string) => {
if (names.includes(name)) {
if (names.includes(name) || isString(types[name])) {
return namespace + capitalize(name)
}
return name
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册