diff --git a/packages/uni-uts-v1/__tests__/__snapshots__/code.spec.ts.snap b/packages/uni-uts-v1/__tests__/__snapshots__/code.spec.ts.snap index 428bd41591aa223538e7b1a05ea32c993111a5c2..6abfb75937b1a9b9faa22898c2edb3f02c4006c9 100644 --- a/packages/uni-uts-v1/__tests__/__snapshots__/code.spec.ts.snap +++ b/packages/uni-uts-v1/__tests__/__snapshots__/code.spec.ts.snap @@ -16,7 +16,7 @@ export const TestUTSComponent = {} registerUTSInterface('RequestTaskOptions',Object.assign({ moduleName, moduleType, errMsg, package: pkg, class: initUTSClassName(name, 'RequestTaskByJsProxy', is_uni_modules) }, {"methods":{"startByJs":{"async":false,"params":[],"return":{}},"abortByJs":{"async":false,"params":[],"return":{"type":"interface","options":"RequestTaskOptions"}}},"props":[]} )) export const onMemoryWarning = /*#__PURE__*/ initUTSProxyFunction(false, { moduleName, moduleType, errMsg, main: true, package: pkg, class: cls, name: 'onMemoryWarningByJs', params: [{"name":"callback","type":"UTSCallback"}], return: ""}) export const offMemoryWarning = /*#__PURE__*/ initUTSProxyFunction(false, { moduleName, moduleType, errMsg, main: true, package: pkg, class: cls, name: 'offMemoryWarningByJs', params: [{"name":"callback","type":"UTSCallback","default":"UTSNull"}], return: ""}) -export default /*#__PURE__*/ initUTSProxyClass(Object.assign({ moduleName, moduleType, errMsg, package: pkg, class: initUTSClassName(name, 'UserByJs', is_uni_modules) }, {"constructor":{"params":[]},"methods":{},"staticMethods":{},"props":[],"staticProps":[]} )) +export default /*#__PURE__*/ initUTSProxyClass(Object.assign({ moduleName, moduleType, errMsg, package: pkg, class: initUTSClassName(name, 'UserByJs', is_uni_modules) }, {"constructor":{"params":[]},"methods":{"registerByJs":{"async":false,"params":[],"returnOptions":{}},"loginByJs":{"async":false,"params":[],"returnOptions":{}}},"staticMethods":{},"props":[],"staticProps":[]} )) export const a = 1 export const showToast1 = /*#__PURE__*/ initUTSProxyFunction(false, { moduleName, moduleType, errMsg, main: true, package: pkg, class: cls, name: 'showToast1ByJs', params: [{"name":"msg","type":"string"}], return: ""}) export const showToast2 = /*#__PURE__*/ initUTSProxyFunction(false, { moduleName, moduleType, errMsg, main: true, package: pkg, class: cls, name: 'showToast2ByJs', params: [{"name":"msg","type":"string"}], return: ""}) @@ -329,7 +329,7 @@ exports.TestUTSComponent = {} registerUTSInterface('RequestTaskOptions',Object.assign({ moduleName, moduleType, errMsg, package: pkg, class: initUTSClassName(name, 'RequestTaskByJsProxy', is_uni_modules) }, {"methods":{"startByJs":{"async":false,"params":[],"return":{}},"abortByJs":{"async":false,"params":[],"return":{"type":"interface","options":"RequestTaskOptions"}}},"props":[]} )) exports.onMemoryWarning = /*#__PURE__*/ initUTSProxyFunction(false, { moduleName, moduleType, errMsg, main: true, package: pkg, class: cls, name: 'onMemoryWarningByJs', params: [{"name":"callback","type":"UTSCallback"}], return: ""}) exports.offMemoryWarning = /*#__PURE__*/ initUTSProxyFunction(false, { moduleName, moduleType, errMsg, main: true, package: pkg, class: cls, name: 'offMemoryWarningByJs', params: [{"name":"callback","type":"UTSCallback","default":"UTSNull"}], return: ""}) -exports.default = initUTSProxyClass(Object.assign({ moduleName, moduleType, errMsg, package: pkg, class: initUTSClassName(name, 'UserByJs', is_uni_modules) }, {"constructor":{"params":[]},"methods":{},"staticMethods":{},"props":[],"staticProps":[]} )) +exports.default = initUTSProxyClass(Object.assign({ moduleName, moduleType, errMsg, package: pkg, class: initUTSClassName(name, 'UserByJs', is_uni_modules) }, {"constructor":{"params":[]},"methods":{"registerByJs":{"async":false,"params":[],"returnOptions":{}},"loginByJs":{"async":false,"params":[],"returnOptions":{}}},"staticMethods":{},"props":[],"staticProps":[]} )) const a = 1 exports.a = a exports.showToast1 = /*#__PURE__*/ initUTSProxyFunction(false, { moduleName, moduleType, errMsg, main: true, package: pkg, class: cls, name: 'showToast1ByJs', params: [{"name":"msg","type":"string"}], return: ""}) diff --git a/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-android/uts/utssdk/test-uts/manifest.json b/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-android/uts/utssdk/test-uts/manifest.json index 68a090159ecf23ddc639f48cd1b665d58719289a..5e07ef4bc55569be383d6fd5cc69f55c49563f45 100644 --- a/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-android/uts/utssdk/test-uts/manifest.json +++ b/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-android/uts/utssdk/test-uts/manifest.json @@ -4,15 +4,15 @@ "compilerVersion": "3.6.10" }, "files": { + "app-android/index.uts": { + "md5": "6958ca9d5c365d041dd72dc46091b839" + }, "common/test/test.uts": { "md5": "d41d8cd98f00b204e9800998ecf8427e" }, "common/utils.uts": { "md5": "d41d8cd98f00b204e9800998ecf8427e" }, - "index.uts": { - "md5": "719035aae84f58b2028f63d102c49887" - }, "interface.uts": { "md5": "4821d45870be9af550787418f7a4c34c" }, diff --git a/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-ios/uts/utssdk/test-uts/manifest.json b/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-ios/uts/utssdk/test-uts/manifest.json index 68a090159ecf23ddc639f48cd1b665d58719289a..8f93ec89b2cbc15d65e195feb13700c212bc631b 100644 --- a/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-ios/uts/utssdk/test-uts/manifest.json +++ b/packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-ios/uts/utssdk/test-uts/manifest.json @@ -4,15 +4,15 @@ "compilerVersion": "3.6.10" }, "files": { + "app-ios/index.uts": { + "md5": "3134e94f54ea061ea2e6440ceec9cebf" + }, "common/test/test.uts": { "md5": "d41d8cd98f00b204e9800998ecf8427e" }, "common/utils.uts": { "md5": "d41d8cd98f00b204e9800998ecf8427e" }, - "index.uts": { - "md5": "719035aae84f58b2028f63d102c49887" - }, "interface.uts": { "md5": "4821d45870be9af550787418f7a4c34c" }, diff --git a/packages/uni-uts-v1/__tests__/examples/uts/utssdk/test-uts/index.uts b/packages/uni-uts-v1/__tests__/examples/uts/utssdk/test-uts/app-android/index.uts similarity index 86% rename from packages/uni-uts-v1/__tests__/examples/uts/utssdk/test-uts/index.uts rename to packages/uni-uts-v1/__tests__/examples/uts/utssdk/test-uts/app-android/index.uts index 7eb6585ea0e05fd91462c85e3c16c21c549dfc29..cf8412e39334d9ae339630ef4f44ea857731a856 100644 --- a/packages/uni-uts-v1/__tests__/examples/uts/utssdk/test-uts/index.uts +++ b/packages/uni-uts-v1/__tests__/examples/uts/utssdk/test-uts/app-android/index.uts @@ -1,10 +1,12 @@ -import { ShowToast, RequestTask } from './interface.uts' +import { ShowToast, RequestTask } from '../interface.uts' export function onMemoryWarning(callback: (level: number) => void) {} export function offMemoryWarning( callback: ((level: number) => void) | null = null ) {} -export default class User {} +export default class User { + login() {} +} export const a = 1 export let b = 2, c = 3 diff --git a/packages/uni-uts-v1/__tests__/examples/uts/utssdk/test-uts/app-ios/index.uts b/packages/uni-uts-v1/__tests__/examples/uts/utssdk/test-uts/app-ios/index.uts new file mode 100644 index 0000000000000000000000000000000000000000..cf30e7a67a05557360c2e3fada0fbd02d15b3c2a --- /dev/null +++ b/packages/uni-uts-v1/__tests__/examples/uts/utssdk/test-uts/app-ios/index.uts @@ -0,0 +1,28 @@ +import { ShowToast, RequestTask } from '../interface.uts' + +export function onMemoryWarning(callback: (level: number) => void) {} +export function offMemoryWarning( + callback: ((level: number) => void) | null = null +) {} +export default class User { + register() {} +} +export const a = 1 +export let b = 2, + c = 3 +export const showToast1: ShowToast = (msg) => { + console.log(msg) +} +export const showToast2: ShowToast = function showToast(msg) { + console.log(msg) +} + +export function request(): RequestTask {} + +export type User1 = { + name: string +} +export function addUsers1(users: User1[]) {} +export function addUsers2(users: Array) {} +export function addUsers3(user: User1, name: string[]) {} +export function addUsers4(user: User1, name: Array) {} diff --git a/packages/uni-uts-v1/__tests__/manifest.spec.ts b/packages/uni-uts-v1/__tests__/manifest.spec.ts index a16b532f88107b61d388773a728ece0e352b7d4c..7f659dade181b7116552021aea900a64a31194fb 100644 --- a/packages/uni-uts-v1/__tests__/manifest.spec.ts +++ b/packages/uni-uts-v1/__tests__/manifest.spec.ts @@ -41,11 +41,11 @@ const pluginOptions = { describe('manifest', () => { test('resolve android files', async () => { expect(await resolvePluginFiles('app-android', pluginDir, false)).toEqual([ - 'index.uts', 'interface.uts', 'package.json', 'common/utils.uts', 'common/test/test.uts', + 'app-android/index.uts', ]) expect( await resolvePluginFiles('app-android', pluginModuleDir, true) @@ -60,11 +60,11 @@ describe('manifest', () => { }) test('resolve ios files', async () => { expect(await resolvePluginFiles('app-ios', pluginDir, false)).toEqual([ - 'index.uts', 'interface.uts', 'package.json', 'common/utils.uts', 'common/test/test.uts', + 'app-ios/index.uts', ]) expect(await resolvePluginFiles('app-ios', pluginModuleDir, true)).toEqual([ 'package.json', diff --git a/packages/uni-uts-v1/src/code.ts b/packages/uni-uts-v1/src/code.ts index f92bfffdad110154e4cd80ac4548d18c46b632ce..52e1546ed44b27954b516a62e8cc3e489082f9a2 100644 --- a/packages/uni-uts-v1/src/code.ts +++ b/packages/uni-uts-v1/src/code.ts @@ -549,27 +549,48 @@ async function parseModuleDecls(module: string, options: GenProxyCodeOptions) { return decls } +function mergeRecord(from: Record, to: Record) { + Object.keys(from).forEach((key) => { + if (!hasOwn(to, key)) { + to[key] = from[key] + } + }) +} +function mergeArray(from: any[], to: any[]) { + from.forEach((item) => { + if (!to.includes(item)) { + to.push(item) + } + }) +} + function mergeDecls(from: ProxyDecl[], to: ProxyDecl[]) { from.forEach((item) => { if (item.type === 'InterfaceDeclaration') { - if ( - !to.find( - (toItem) => - toItem.type === 'InterfaceDeclaration' && toItem.cls === item.cls - ) - ) { + const decl = to.find( + (toItem) => + toItem.type === 'InterfaceDeclaration' && toItem.cls === item.cls + ) as ProxyInterface | undefined + if (!decl) { to.push(item) + } else { + mergeRecord(item.options.methods, decl.options.methods) + mergeArray(item.options.props, decl.options.props) } } else if (item.type === 'Class') { - if ( - !to.find( - (toItem) => - toItem.type === 'Class' && - toItem.cls === item.cls && - toItem.isDefault === item.isDefault - ) - ) { + const decl = to.find( + (toItem) => + toItem.type === 'Class' && + toItem.cls === item.cls && + toItem.isDefault === item.isDefault + ) as ProxyClass | undefined + if (!decl) { to.push(item) + } else { + mergeRecord(item.options.methods, decl.options.methods) + mergeRecord(item.options.staticMethods, decl.options.staticMethods) + mergeArray(item.options.props, decl.options.props) + mergeArray(item.options.staticProps, decl.options.staticProps) } } else if (item.type === 'FunctionDeclaration') { if (