1/*
2 * Copyright (c) 2021 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
16const fs = require('fs');
17const path = require('path');
18import ts from 'typescript';
19
20const COMPONENTS = 'components';
21const FORM_COMPONENTS = 'form_components';
22export const COMPONENT_MAP: any = {};
23export const FORM_MAP: any = {};
24
25export let COMMON_ATTRS: Set<string> = new Set([]);
26
27(function readComponents() {
28  const componentPath: Map<string, string> = new Map([
29    [`${COMPONENTS}`, `../${COMPONENTS}`],
30    [`${FORM_COMPONENTS}`, `../${FORM_COMPONENTS}`]
31  ]);
32  for (const [id, relPath] of componentPath.entries()) {
33    const componentsFile: string = path.join(__dirname, relPath);
34    const files: string[] = fs.readdirSync(componentsFile);
35    files.forEach(function(item) {
36      const fPath: string = path.join(componentsFile, item);
37      const json: any = require(fPath);
38      const stat: any = fs.statSync(fPath);
39      if (stat.isFile()) {
40        if (json.name) {
41          const compName: string = json.name;
42          delete json.name;
43          if (id === COMPONENTS) {
44            COMPONENT_MAP[compName] = json;
45          } else if (id === FORM_COMPONENTS) {
46            FORM_MAP[compName] = json;
47          }
48        } else {
49          if (id === COMPONENTS) {
50            COMMON_ATTRS = new Set(json.attrs);
51          }
52        }
53      }
54    });
55  }
56})();
57
58const TRANSITION_COMMON_ATTRS: Set<string> = new Set([
59  'slide', 'translate', 'scale', 'opacity'
60]);
61export const GESTURE_ATTRS: Set<string> = new Set([
62  'gesture', 'parallelGesture', 'priorityGesture'
63]);
64
65export const ID_ATTRS: Map<string, Map<string, string | number>> = new Map();
66
67export const forbiddenUseStateType: Set<string> = new Set(['Scroller', 'SwiperScroller',
68  'VideoController', 'WebController', 'CustomDialogController', 'SwiperController',
69  'TabsController', 'CalendarController', 'AbilityController', 'XComponentController',
70  'CanvasRenderingContext2D', 'CanvasGradient', 'ImageBitmap', 'ImageData', 'Path2D',
71  'RenderingContextSettings', 'OffscreenCanvasRenderingContext2D', 'PatternLockController',
72  'TextAreaController', 'TextInputController', 'TextTimerController', 'SearchController', 'RichEditorController'
73]);
74
75const FOREACH_ATTRIBUTE = ['onMove'];
76export const INNER_COMPONENT_NAMES: Set<string> = new Set();
77export const NO_DEBUG_LINE_COMPONENT: Set<string> = new Set();
78export const BUILDIN_CONTAINER_COMPONENT: Set<string> = new Set();
79export const BUILDIN_STYLE_NAMES: Set<string> = new Set([
80  ...COMMON_ATTRS, ...GESTURE_ATTRS, ...TRANSITION_COMMON_ATTRS, ...FOREACH_ATTRIBUTE
81]);
82export const AUTOMIC_COMPONENT: Set<string> = new Set();
83export const SINGLE_CHILD_COMPONENT: Set<string> = new Set();
84export const SPECIFIC_CHILD_COMPONENT: Map<string, Set<string>> = new Map();
85export const SPECIFIC_PARENT_COMPONENT: Map<string, Set<string>> = new Map();
86export const GESTURE_TYPE_NAMES: Set<string> = new Set([
87  'TapGesture', 'LongPressGesture', 'PanGesture', 'PinchGesture', 'RotationGesture', 'GestureGroup',
88  'SwipeGesture'
89]);
90export const CUSTOM_BUILDER_METHOD: Set<string> = new Set();
91export const INNER_CUSTOM_LOCALBUILDER_METHOD: Set<string> = new Set();
92export const INNER_STYLE_FUNCTION: Map<string, ts.Block> = new Map();
93export const GLOBAL_STYLE_FUNCTION: Map<string, ts.Block> = new Map();
94
95export interface ExtendParamterInterfance {
96  attribute: string,
97  parameterCount: number
98}
99export const EXTEND_ATTRIBUTE: Map<string, Set<string>> = new Map();
100export const STYLES_ATTRIBUTE: Set<string> = new Set();
101
102export const INTERFACE_NODE_SET: Set<ts.InterfaceDeclaration> = new Set();
103
104export const INNER_CUSTOM_BUILDER_METHOD: Set<string> = new Set();
105export const GLOBAL_CUSTOM_BUILDER_METHOD: Set<string> = new Set();
106
107export const JS_BIND_COMPONENTS: Set<string> = new Set([
108  'ForEach', 'LazyForEach', ...GESTURE_TYPE_NAMES, 'Gesture',
109  'PanGestureOption', 'CustomDialogController', 'Storage', 'Scroller', 'SwiperController',
110  'TabsController', 'CalendarController', 'AbilityController', 'VideoController', 'WebController',
111  'XComponentController', 'CanvasRenderingContext2D', 'CanvasGradient', 'ImageBitmap', 'ImageData',
112  'Path2D', 'RenderingContextSettings', 'OffscreenCanvasRenderingContext2D', 'DatePickerDialog',
113  'TextPickerDialog', 'AlertDialog', 'ContextMenu', 'ActionSheet', 'PatternLockController',
114  'TimePickerDialog', 'CalendarPickerDialog'
115]);
116
117export const NEEDPOP_COMPONENT: Set<string> = new Set(['Blank', 'Search']);
118
119export const CUSTOM_BUILDER_PROPERTIES: Set<string> = new Set(['background', 'bindPopup', 'bindMenu', 'bindContextMenu', 'title',
120  'menus', 'toolBar', 'tabBar', 'onDragStart', 'onItemDragStart', 'swipeAction', 'bindContentCover', 'bindSheet',
121  'navDestination', 'overlay', 'toolbarConfiguration', 'customKeyboard', 'bindSelectionMenu', 'description',
122  'showUnit', 'customKeyboard', 'create', 'addBuilderSpan', 'dragPreview', 'accessibilityVirtualNode']);
123export const CUSTOM_BUILDER_PROPERTIES_WITHOUTKEY: Set<string> = new Set(['showUnit', 'create']);
124export const CUSTOM_BUILDER_CONSTRUCTORS: Set<string> = new Set(['MenuItem', 'MenuItemGroup', 'Refresh', 'WaterFlow', 'Radio', 'Checkbox']);
125
126(function initComponent() {
127  Object.keys(COMPONENT_MAP).forEach((componentName) => {
128    INNER_COMPONENT_NAMES.add(componentName);
129    JS_BIND_COMPONENTS.add(componentName);
130    if (!COMPONENT_MAP[componentName].atomic) {
131      BUILDIN_CONTAINER_COMPONENT.add(componentName);
132    } else {
133      AUTOMIC_COMPONENT.add(componentName);
134    }
135    if (COMPONENT_MAP[componentName].single) {
136      SINGLE_CHILD_COMPONENT.add(componentName);
137    }
138    if (COMPONENT_MAP[componentName].children) {
139      SPECIFIC_CHILD_COMPONENT.set(componentName,
140        new Set([...COMPONENT_MAP[componentName].children]));
141    }
142    if (COMPONENT_MAP[componentName].attrs && COMPONENT_MAP[componentName].attrs.length) {
143      COMPONENT_MAP[componentName].attrs.forEach((item) => {
144        BUILDIN_STYLE_NAMES.add(item);
145      });
146    }
147    if (COMPONENT_MAP[componentName].noDebugLine) {
148      NO_DEBUG_LINE_COMPONENT.add(componentName);
149    }
150    if (COMPONENT_MAP[componentName].parents) {
151      SPECIFIC_PARENT_COMPONENT.set(componentName,
152        new Set([...COMPONENT_MAP[componentName].parents]));
153    }
154  });
155})();
156
157export function resetComponentMap(): void {
158  ID_ATTRS.clear();
159  EXTEND_ATTRIBUTE.clear();
160  STYLES_ATTRIBUTE.clear();
161}
162