• 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 '../rules/ui-syntax-rule';
18import { getContainerComponents } from '../utils';
19
20export type UISyntaxRuleProcessor = {
21  parsed(node: arkts.AstNode): void;
22};
23
24export function createUISyntaxRuleProcessor(
25  rules: UISyntaxRule[],
26): UISyntaxRuleProcessor {
27  const containerComponents = getContainerComponents('../../components/');
28  const context: UISyntaxRuleContext = {
29    report(options) {
30      const position = arkts.getStartPosition(options.node);
31      let message: string;
32      if (!options.data) {
33        message = options.message;
34      } else {
35        message = Object.entries(options.data).reduce(
36          (message, [placehoderName, placehoderValue]) => {
37            return message.replace(`{{${placehoderName}}}`, placehoderValue);
38          },
39          options.message,
40        );
41      }
42      // todo
43      if (options.fix) {
44        const suggestion = options.fix(options.node);
45        console.log(`error: ${message}`);
46        console.log(`range: (${suggestion.range[0].index()}, ${suggestion.range[0].line()}) - (${suggestion.range[1].index()}, ${suggestion.range[1].line()})`,
47          `code: ${suggestion.code}`);
48      } else {
49        console.log(`syntax-error: ${message}  (${position.index()},${position.line()})`);
50      }
51    },
52    containerComponents: containerComponents,
53  };
54
55  const instances = rules.map((rule) => rule.setup(context));
56
57  return {
58    parsed(node): void {
59      for (const instance of instances) {
60        instance.parsed?.(node);
61      }
62    },
63  };
64}
65