• 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 { getAnnotationUsage, PresetDecorators } from '../utils';
18import { UISyntaxRule } from './ui-syntax-rule';
19
20const rule: UISyntaxRule = {
21  name: 'componentV2-mix-check',
22  messages: {
23    conflictWithComponentV2: `The struct '{{structName}}' can not be decorated with '@ComponentV2' and '@Component', '@Reusable', '@CustomDialog' at the same time.`,
24  },
25  setup(context) {
26    return {
27      parsed: (node): void => {
28        if (!arkts.isStructDeclaration(node)) {
29          return;
30        }
31        const structName = node.definition.ident?.name ?? '';
32        const structNameNode = node.definition.ident;
33        if (!structNameNode) {
34          return;
35        }
36        // Check if the struct has the '@ComponentV2' annotation
37        const hasComponentV2 = getAnnotationUsage(node, PresetDecorators.COMPONENT_V2);
38        if (!hasComponentV2) {
39          return;
40        }
41        // Check for the presence of conflicting decorators: '@Component', '@Reusable', '@CustomDialog'
42        const hasComponent = getAnnotationUsage(node, PresetDecorators.COMPONENT_V1);
43        const hasReusable = getAnnotationUsage(node, PresetDecorators.REUSABLE_V1);
44        const hasCustomDialog = getAnnotationUsage(node, PresetDecorators.CUSTOM_DIALOG);
45        if (hasComponent || hasReusable || hasCustomDialog) {
46          context.report({
47            node: structNameNode,
48            message: rule.messages.conflictWithComponentV2,
49            data: { structName },
50          });
51        }
52      },
53    };
54  },
55};
56
57export default rule;
58