• 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, UISyntaxRuleContext } from './ui-syntax-rule';
19
20function checkLocalBuilder(node: arkts.ClassDeclaration, context: UISyntaxRuleContext): void {
21  node.definition?.body.forEach(body => {
22    if (!arkts.isMethodDefinition(body)) {
23      return;
24    }
25    const localBuilder = body.scriptFunction?.annotations?.find(
26      annotation => annotation.expr &&
27        annotation.expr.dumpSrc() === PresetDecorators.LOCAL_BUILDER);
28    if (!localBuilder) {
29      return;
30    }
31    context.report({
32      node: localBuilder,
33      message: rule.messages.invalidUsage,
34      fix: (localBuilder) => {
35        const startPosition = arkts.getStartPosition(localBuilder);
36        const endPosition = arkts.getEndPosition(localBuilder);
37        return {
38          range: [startPosition, endPosition],
39          code: '',
40        };
41      },
42    });
43  });
44}
45
46const rule: UISyntaxRule = {
47  name: 'local-builder-check',
48  messages: {
49    invalidUsage: `The '@LocalBuilder' decorator can only be used in 'struct'.`,
50  },
51  setup(context) {
52    return {
53      parsed: (node): void => {
54        if (!arkts.isClassDeclaration(node)) {
55          return;
56        }
57        checkLocalBuilder(node, context);
58      },
59    };
60  },
61};
62
63export default rule;
64