提交 0c43e2b6 编写于 作者: fxy060608's avatar fxy060608

wip(uts): compiler

上级 1fd97588
import path from 'path'
import { parseUTSSwiftPluginStacktrace } from '../src/stacktrace'
import {
parseUTSSwiftPluginStacktrace,
parseUTSSyntaxError,
} from '../src/stacktrace'
const stacktrace = `/uts-development-ios/dependences/buildFramework/template/xcode_ust_template/unimoduleTestUTS1/src/index.swift:3:12: error: cannot convert return expression of type 'Int' to return type 'String'
/uts-development-ios/dependences/buildFramework/template/xcode_ust_template/unimoduleTestUTS1/src/index.swift:6:12: error: cannot convert return expression of type 'Int' to return type 'String'
`
......@@ -21,4 +24,24 @@ describe('uts:stacktrace', () => {
`uni_modules/test-uts1/utssdk/app-ios/index.uts:5:10`
)
})
test('parseUTSSyntaxError', () => {
const msg = parseUTSSyntaxError(
`Error:
x UTSCallback 已过时,详情查看 https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#%E5%B8%B8%E8%A7%81%E6%8A%A5%E9%94%99.
,-[uni_modules/uts-alert/utssdk/app-android/index.uts:29:1]
29 | inputET:EditText
30 | callback:UTSCallback
: ^^^^^^^^^^^
31 |
Error:
x UTSCallback 已过时,详情查看 https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#%E5%B8%B8%E8%A7%81%E6%8A%A5%E9%94%99.
,-[uni_modules/uts-alert/utssdk/app-android/index.uts:29:1]
29 | inputET:EditText
30 | callback:UTSCallback
: ^^^^^^^^^^^
31 |`,
``
)
expect(msg.match(/at\s/g)?.length).toBe(2)
})
})
......@@ -26,8 +26,10 @@ import {
createResolveTypeReferenceName,
ERR_MSG_PLACEHOLDER,
isColorSupported,
relative,
} from './utils'
import { normalizePath } from './shared'
import { parseUTSSyntaxError } from './stacktrace'
export const enum FORMATS {
ES = 'es',
......@@ -289,42 +291,49 @@ async function parseInterfaceTypes(
// 懒加载 uts 编译器
// eslint-disable-next-line no-restricted-globals
const { parse } = require('@dcloudio/uts')
const ast: Module = await parse(fs.readFileSync(interfaceFilename, 'utf8'), {
noColor: !isColorSupported(),
})
let ast: Module | null = null
try {
ast = await parse(fs.readFileSync(interfaceFilename, 'utf8'), {
filename: relative(interfaceFilename, process.env.UNI_INPUT_DIR),
noColor: !isColorSupported(),
})
} catch (e) {
console.error(parseUTSSyntaxError(e, process.env.UNI_INPUT_DIR))
}
const classTypes: string[] = []
const fnTypes: Record<string, Param[]> = {}
const exportNamed: string[] = []
ast.body.filter((node) => {
if (node.type === 'ExportNamedDeclaration') {
node.specifiers.forEach((s) => {
if (s.type === 'ExportSpecifier') {
if (s.exported) {
if (s.exported.type === 'Identifier') {
exportNamed.push(s.exported.value)
if (ast) {
ast.body.filter((node) => {
if (node.type === 'ExportNamedDeclaration') {
node.specifiers.forEach((s) => {
if (s.type === 'ExportSpecifier') {
if (s.exported) {
if (s.exported.type === 'Identifier') {
exportNamed.push(s.exported.value)
}
} else {
exportNamed.push(s.orig.value)
}
} else {
exportNamed.push(s.orig.value)
}
}
})
}
})
})
}
})
ast.body.filter((node) => {
if (
node.type === 'ExportDeclaration' &&
node.declaration.type === 'TsTypeAliasDeclaration'
) {
parseTypes(node.declaration, classTypes, fnTypes)
} else if (node.type === 'TsTypeAliasDeclaration') {
if (exportNamed.includes(node.id.value)) {
parseTypes(node, classTypes, fnTypes)
ast.body.filter((node) => {
if (
node.type === 'ExportDeclaration' &&
node.declaration.type === 'TsTypeAliasDeclaration'
) {
parseTypes(node.declaration, classTypes, fnTypes)
} else if (node.type === 'TsTypeAliasDeclaration') {
if (exportNamed.includes(node.id.value)) {
parseTypes(node, classTypes, fnTypes)
}
}
}
})
})
}
return {
class: classTypes,
fn: fnTypes,
......@@ -440,27 +449,41 @@ function mergeDecls(from: ProxyDecl[], to: ProxyDecl[]) {
async function parseFile(
filename: string | undefined | false,
options: GenProxyCodeOptions
) {
): Promise<ProxyDecl[]> {
if (filename) {
return parseCode(
fs.readFileSync(filename, 'utf8'),
options.namespace,
options.types!
options.types!,
filename
)
}
return []
}
async function parseCode(code: string, namespace: string, types: Types) {
async function parseCode(
code: string,
namespace: string,
types: Types,
filename: string
): Promise<ProxyDecl[]> {
// 懒加载 uts 编译器
// 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.class),
types
)
try {
const ast = await parse(code, {
filename: relative(filename, process.env.UNI_INPUT_DIR),
noColor: !isColorSupported(),
})
return parseAst(
ast,
createResolveTypeReferenceName(namespace, ast, types.class),
types
)
} catch (e: any) {
console.error(parseUTSSyntaxError(e, process.env.UNI_INPUT_DIR))
}
return []
}
type ProxyDecl = ProxyFunctionDeclaration | ProxyClass | VariableDeclaration
......@@ -492,10 +515,8 @@ function parseAst(
{ body }: Module,
resolveTypeReferenceName: ResolveTypeReferenceName,
types: Types
) {
const decls: Array<
ProxyFunctionDeclaration | ProxyClass | VariableDeclaration
> = []
): ProxyDecl[] {
const decls: ProxyDecl[] = []
body.forEach((item) => {
if (item.type === 'ExportDeclaration') {
......
......@@ -23,6 +23,7 @@ import {
isColorSupported,
} from './utils'
import { Module } from '../types/types'
import { parseUTSSyntaxError } from './stacktrace'
interface KotlinCompilerServer extends CompilerServer {
getKotlincHome(): string
......@@ -78,6 +79,10 @@ export async function runKotlinProd(
if (!res) {
return
}
if (res.error) {
console.error(parseUTSSyntaxError(res.error, inputDir))
return
}
genUTSPlatformResource(filename, {
inputDir,
outputDir,
......@@ -112,7 +117,10 @@ export async function runKotlinDev(
if (!result) {
return
}
if (result.error) {
console.error(parseUTSSyntaxError(result.error, inputDir))
return
}
result.type = 'kotlin'
result.changed = []
......
import { originalPositionFor } from './sourceMap'
import { relative } from './utils'
const splitRE = /\r?\n/
const uniModulesSwiftUTSRe =
......@@ -131,3 +132,17 @@ export function generateCodeFrame(
}
return res.join('\n')
}
export function parseUTSSyntaxError(error: any, inputDir: string): string {
let msg = String(error).replace(/\t/g, ' ')
let res: RegExpExecArray | null = null
const syntaxErrorRe = /(,-\[(.*):(\d+):(\d+)\])/g
while ((res = syntaxErrorRe.exec(msg))) {
const [row, filename, line, column] = res.slice(1)
msg = msg.replace(
row,
`at ${relative(filename, inputDir)}:${parseInt(line) + 1}:${column}`
)
}
return msg
}
......@@ -16,6 +16,7 @@ import {
} from './utils'
import { parseJson } from './shared'
import { UTSResult } from '@dcloudio/uts'
import { parseUTSSyntaxError } from './stacktrace'
function parseSwiftPackage(filename: string) {
const res = resolvePackage(filename)
......@@ -51,6 +52,10 @@ export async function runSwiftProd(
if (!res) {
return
}
if (res.error) {
console.error(parseUTSSyntaxError(res.error, inputDir))
return
}
genUTSPlatformResource(filename, {
inputDir,
outputDir,
......@@ -107,7 +112,10 @@ export async function runSwiftDev(
if (!result) {
return
}
if (result.error) {
console.error(parseUTSSyntaxError(result.error, inputDir))
return
}
result.type = 'swift'
const swiftFile = resolveUTSPlatformFile(filename, {
......
......@@ -407,3 +407,10 @@ export function isColorSupported() {
}
return true
}
export function relative(filename: string, inputDir: string) {
if (path.isAbsolute(filename)) {
return normalizePath(path.relative(inputDir, filename))
}
return filename
}
......@@ -71,6 +71,9 @@ export function bundleKotlin(options: UTSBundleOptions): Promise<UTSResult> {
return bindings
.bundleKotlin(toBuffer(bundleOptions))
.then((res: string) => JSON.parse(res))
.catch((error: Error) => {
return { error }
})
}
export function toSwift(options: UTSOptions): Promise<UTSResult> {
......@@ -81,6 +84,9 @@ export function toSwift(options: UTSOptions): Promise<UTSResult> {
return bindings
.toSwift(toBuffer(swiftOptions))
.then((res: string) => JSON.parse(res))
.catch((error: Error) => {
return { error }
})
}
export function bundleSwift(options: UTSBundleOptions): Promise<UTSResult> {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册