• 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 { UISyntaxRule, UISyntaxRuleContext } from './ui-syntax-rule';
18
19// A list of decorators that needs to be initialized locally
20const mustInitializeDecorators = ['State', 'StorageLink', 'StorageProp', 'LocalStorageLink', 'LocalStorageProp',
21  'Provide'];
22// Disables a list of decorators that are initialized locally
23const prohibitInitializeDecorators = ['Link', 'Consume', 'ObjectLink'];
24
25function reportVariablesInitializationError(context: UISyntaxRuleContext, node: arkts.ClassProperty): void {
26  // Check whether the value field exists and whether it has been initialized
27  const valueExists = !!node.value;
28  // Iterate through each decorator and verify the initialization rules
29  node.annotations.forEach(annotation => {
30    const decoratorName = annotation.expr?.dumpSrc() ?? '';
31    if (mustInitializeDecorators.includes(decoratorName)) {
32      // If it is a decorator that needs to be initialized
33      if (!valueExists) {
34        // If there is no initialization expression and there is no @Require, an error is reported
35        context.report({
36          node: annotation,
37          message: rule.messages.mustBeInitializedLocally,
38        });
39      }
40    } else if (prohibitInitializeDecorators.includes(decoratorName)) {
41      // If it is a decorator that prohibits initialization
42      if (valueExists) {
43        // If an initialization expression exists, an error is reported
44        context.report({
45          node: annotation,
46          message: rule.messages.prohibitLocalInitialization,
47        });
48      }
49    }
50  });
51}
52
53const rule: UISyntaxRule = {
54  name: 'struct-variable-initialization',
55  messages: {
56    mustBeInitializedLocally: `Variables decorated by '@State', '@StorageLink', '@StorageProp', '@LocalStorageLink', '@LocalStorageProp' and '@Provide' must be initialized locally.`,
57    prohibitLocalInitialization: `Variables decorated by '@Link', '@Consume', and '@ObjectLink' cannot be initialized locally.`
58  },
59  setup(context) {
60    return {
61      parsed: (node): void => {
62        // Check if the current node is a class attribute
63        if (!arkts.isClassProperty(node)) {
64          return;
65        }
66        reportVariablesInitializationError(context, node);
67      }
68    };
69  }
70};
71
72export default rule;