idlize
311 строк · 10.3 Кб
1/*
2* Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14*/
15
16import * as ts from 'ohos-typescript'
17import { id } from './ApiUtils'
18
19let knownPrefixes = ["@ohos", "@system"]
20
21export function isOhosImport(literal: string): boolean {
22for (let prefix of knownPrefixes) {
23if (literal.startsWith(prefix)) return true
24}
25return false
26}
27
28interface ArkUIVariance {
29koalaCommon: string
30koalaFramework: string
31koalaRuntime: string
32koalaArkuiCommon: string
33arkoala: string
34koalaAdaptor: string
35}
36
37const KoalaArkui: ArkUIVariance = {
38koalaCommon: "@koalaui/common",
39koalaFramework: "@koalaui/framework",
40koalaRuntime: "@koalaui/runtime",
41koalaArkuiCommon: "@koalaui/arkui-common",
42arkoala: "@koalaui/arkoala",
43koalaAdaptor: "@koalaui/arkui"
44}
45
46const ArkoalaArkui: ArkUIVariance = {
47koalaCommon: "@koalaui/common",
48koalaFramework: "@koalaui/framework",
49koalaRuntime: "@koalaui/runtime",
50koalaArkuiCommon: "@koalaui/arkui-common",
51arkoala: "@koalaui/arkoala",
52koalaAdaptor: "@koalaui/arkoala-arkui"
53}
54
55// Everything re-exported through arkoala-arkui
56const ArkoalaSdkArkui: ArkUIVariance = {
57koalaCommon: "@koalaui/arkoala-arkui",
58koalaFramework: "@koalaui/arkoala-arkui",
59koalaRuntime: "@koalaui/arkoala-arkui",
60koalaArkuiCommon: "@koalaui/arkoala-arkui",
61arkoala: "@koalaui/arkoala-arkui",
62koalaAdaptor: "@koalaui/arkoala-arkui",
63}
64
65
66export class Importer {
67// TODO: Every use of this property is a bad HACK!
68// Think hard how to eliminate it!
69public __isArkoalaImplementation: boolean = false
70private allImports: Map<string, Set<string>>
71private implementedPackages: Set<string>
72
73static koalaCommon = "@koalaui/common"
74static koalaFramework = "@koalaui/framework"
75static koalaRuntime = "@koalaui/runtime"
76static koalaArkuiCommon = "@koalaui/arkui-common"
77static arkoala = "@koalaui/arkoala"
78
79subproject: ArkUIVariance
80
81constructor(koalaAdaptor: string = "@koalaui/arkui", public moduleInfo?: (moduleName: string)=>any) {
82this.__isArkoalaImplementation = (koalaAdaptor == "@koalaui/arkoala-arkui")
83this.subproject = this.__isArkoalaImplementation ? ArkoalaSdkArkui : KoalaArkui
84
85this.implementedPackages = new Set(
86this.__isArkoalaImplementation ?
87[
88'ohos.arkui.testing',
89'ohos.matrix4',
90'ohos.router',
91'system.router'
92] :
93[
94'ohos.curves',
95'ohos.events.emitter',
96'ohos.matrix44',
97'ohos.router',
98'ohos.data.preferences',
99'ohos.hilog',
100'ohos.mediaquery',
101'ohos.UiTest',
102'ohos.display',
103'ohos/hypium',
104'ohos.net.http',
105'system.router'
106]
107)
108
109// TODO Add special handling for ForEach imports
110this.allImports = new Map([
111[
112this.subproject.koalaAdaptor, new Set(
113this.__isArkoalaImplementation ? [
114"ForEach",
115"LazyForEach",
116"IDataSource",
117"SwiperController",
118"Scroller",
119"VideoController",
120"TabsController",
121"SearchController",
122"PatternLockController",
123"AppStorage",
124"LocalStorage",
125"animateTo",
126"TransitionEffect",
127"TapGesture",
128"LongPressGesture",
129"PanGestureOptions",
130"PanGesture",
131"PinchGesture",
132"GestureGroup",
133"LinearGradient",
134"TextClockController",
135"TextTimerController",
136"RichEditorController",
137"TextAreaController",
138"CanvasRenderingContext2D",
139"OffscreenCanvasRenderingContext2D",
140"ImageBitmap",
141"RenderingContextSettings",
142"XComponentController",
143"Indicator",
144"TextInputOptions",
145"TextInputController",
146"WebController",
147"ESObject",
148] : [
149"PanGestureOptions",
150"TapGesture",
151"LongPressGesture",
152"PanGesture",
153"SwipeGesture",
154"PinchGesture",
155"RotationGesture",
156"GestureGroup",
157"DataChangeListener",
158"ForEach",
159"IDataSource",
160"Scroller",
161"CustomDialogController",
162"SwiperController",
163"RenderingContextSettings",
164"CanvasRenderingContext2D",
165"VideoController",
166"TabsController",
167"TextAreaController",
168// generated classes do not see common_ts_ets_api.d.ts
169// so, import next five classes from Storage.ts
170"AppStorage",
171"PersistentStorage",
172"Environment",
173"SubscribedAbstractProperty",
174"LocalStorage",
175
176"Observed",
177"$r",
178"$rawfile",
179"getContext",
180"getInspectorByKey",
181"vp2px",
182"px2vp",
183"fp2px",
184"px2fp",
185"lpx2px",
186"px2lpx",
187"animateTo",
188])
189]
190])
191}
192
193
194addImport(pkg: string, subpackage: string | undefined, name: string) {
195const from = subpackage ? `${pkg}/${subpackage}` : pkg
196if (this.allImports.has(from)) {
197this.allImports.get(from)?.add(name)
198} else {
199this.allImports.set(from, new Set([name]))
200}
201}
202
203addCommonImport(name: string) {
204this.addImport(this.subproject.koalaCommon, undefined, name)
205}
206addFrameworkImport(name: string) {
207this.addImport(this.subproject.koalaFramework, undefined, name)
208}
209addArkoalaImport(name: string) {
210this.addImport(this.subproject.arkoala, undefined, name)
211}
212addRuntimeImport(name: string) {
213this.addImport(this.subproject.koalaRuntime, undefined, name)
214}
215addAdaptorImport(name: string) {
216this.addImport(this.subproject.koalaAdaptor, undefined, name)
217}
218addArkuiCommonImport(name: string) {
219this.addImport(this.subproject.koalaArkuiCommon, undefined, name)
220}
221addOhosImport(pkg: string, name: string) {
222this.addImport(this.subproject.koalaAdaptor, pkg, name)
223}
224allImportsSorted(from: string): string[] {
225const nameSet: Set<string> | undefined = this.allImports.get(from)
226if (!nameSet) return []
227return Array.from(nameSet).sort()
228}
229
230withCommonImport(name: string): string {
231this.addCommonImport(name)
232return name
233}
234
235withAdaptorImport(name: string): string {
236this.addAdaptorImport(name)
237return name
238}
239
240withArkuiCommonImport(name: string): string {
241this.addArkuiCommonImport(name)
242return name
243}
244
245withOhosImport(pkg: string, name: string): string {
246this.addOhosImport(pkg, name)
247return name
248}
249
250withFrameworkImport(name: string): string {
251this.addFrameworkImport(name)
252return name
253}
254
255withArkoalaImport(name: string): string {
256this.addArkoalaImport(name)
257return name
258}
259
260withRuntimeImport(name: string): string {
261this.addRuntimeImport(name)
262return name
263}
264
265generateAllImports(array: string[], from: string): ts.ImportDeclaration {
266const namedImports = array.map(it => ts.factory.createImportSpecifier(
267false,
268undefined,
269id(it)
270))
271
272return ts.factory.createImportDeclaration(
273undefined,
274undefined,
275ts.factory.createImportClause(
276false,
277undefined,
278ts.factory.createNamedImports(
279namedImports
280)
281),
282ts.factory.createStringLiteral(from),
283)
284}
285
286generate(): ts.ImportDeclaration[] {
287const allImports = Array.from(this.allImports.keys()).map(from =>
288this.generateAllImports(this.allImportsSorted(from), from)
289)
290
291return allImports
292}
293
294translateOhosImport(node: ts.ImportDeclaration, oldPackage: string, oldDefaultName: string): ts.ImportDeclaration {
295if (!oldPackage.startsWith("@")) return node
296const stringName = oldPackage.substring(1)
297if (!this.implementedPackages.has(stringName)) return node
298
299return ts.factory.updateImportDeclaration(
300node,
301node.modifiers,
302(oldDefaultName.length == 0) ? node.importClause : ts.factory.createImportClause(
303false,
304id(oldDefaultName),
305undefined
306),
307ts.factory.createStringLiteral(`${this.subproject.koalaAdaptor}/${stringName}`),
308undefined
309)
310}
311}
312