• 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 { PresetDecorators } from '../utils';
18import { UISyntaxRule } from './ui-syntax-rule';
19
20const rule: UISyntaxRule = {
21  name: 'observed-observedV2-check',
22  messages: {
23    conflictingDecorators: `A class cannot be decorated by both '@Observed' and '@ObservedV2' at the same time.`,
24  },
25  setup(context) {
26    return {
27      parsed: (node): void => {
28        if (!arkts.isClassDeclaration(node)) {
29          return;
30        }
31        const hasObservedDecorator = node.definition?.annotations?.find(annotations => annotations.expr &&
32          annotations.expr.dumpSrc() === PresetDecorators.OBSERVED_V1);
33        const hasObservedV2Decorator = node.definition?.annotations?.find(annotations => annotations.expr &&
34          annotations.expr.dumpSrc() === PresetDecorators.OBSERVED_V2);
35        // If the current class is decorated by @Observed and @ObservedV2, an error is reported
36        if (hasObservedDecorator && hasObservedV2Decorator) {
37          context.report({
38            node: hasObservedDecorator,
39            message: rule.messages.conflictingDecorators,
40          });
41        }
42      },
43    };
44  },
45};
46
47export default rule;
48