• 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 { getClassPropertyAnnotationNames, PresetDecorators } from '../utils';
18import { UISyntaxRule, UISyntaxRuleContext } from './ui-syntax-rule';
19
20const PROPERTY_ANNOTATION_NUM: number = 2;
21
22function validateWatchDecorator(node: arkts.StructDeclaration, context: UISyntaxRuleContext): void {
23  node.definition.body.forEach(member => {
24    if (!arkts.isClassProperty(member)) {
25      return;
26    }
27    const hasWatchDecorator = member.annotations?.find(annotation =>
28      annotation.expr &&
29      annotation.expr.dumpSrc() === PresetDecorators.WATCH
30    );
31    const propertyAnnotationNames = getClassPropertyAnnotationNames(member);
32    // Determine if there are any decorations other than @watch decorations
33    // rule1: The @Watch decorator must be used with other decorators
34    if (hasWatchDecorator && propertyAnnotationNames.length < PROPERTY_ANNOTATION_NUM) {
35      context.report({
36        node: hasWatchDecorator,
37        message: rule.messages.invalidWatch,
38      });
39    }
40  });
41}
42
43const rule: UISyntaxRule = {
44  name: 'watch-decorator-regular',
45  messages: {
46    invalidWatch: `The @Watch decorator must be used with other decorators.`,
47  },
48  setup(context) {
49    return {
50      parsed: (node): void => {
51        if (!arkts.isStructDeclaration(node)) {
52          return;
53        }
54        validateWatchDecorator(node, context);
55      },
56    };
57  },
58};
59
60export default rule;
61