• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 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 arkts from '@koalaui/libarkts';
17import { getClassPropertyType, PresetDecorators, getAnnotationUsage } from '../utils';
18import { UISyntaxRule, UISyntaxRuleContext } from './ui-syntax-rule';
19
20const CUSTOM_DIALOG_CONTROLLER: string = 'CustomDialogController';
21
22function missingController(
23  node: arkts.StructDeclaration,
24  context: UISyntaxRuleContext
25): void {
26  // Check for the @CustomDialog decorator
27  const hasCustomDialogDecorator = getAnnotationUsage(node, PresetDecorators.CUSTOM_DIALOG);
28  const structName = node.definition.ident;
29  if (!structName) {
30    return;
31  }
32  // Check if there is an attribute of type CustomDialogController in the class
33  let hasControllerProperty = false;
34  node.definition.body.forEach((property) => {
35    if (arkts.isClassProperty(property)) {
36      const propertyType = getClassPropertyType(property);
37      if (propertyType === CUSTOM_DIALOG_CONTROLLER) {
38        hasControllerProperty = true;
39      }
40    }
41  });
42  if (!hasControllerProperty && hasCustomDialogDecorator) {
43    context.report({
44      node: structName,
45      message: rule.messages.missingController,
46    });
47  }
48}
49
50const rule: UISyntaxRule = {
51  name: 'custom-dialog-missing-controller',
52  messages: {
53    missingController: `The @CustomDialog decorated custom component must contain a property of the CustomDialogController type.`,
54  },
55  setup(context) {
56    return {
57      parsed: (node): void => {
58        if (!arkts.isStructDeclaration(node)) {
59          return;
60        }
61        missingController(node, context);
62      },
63    };
64  },
65};
66
67export default rule;
68
69