• 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 ts from 'typescript';
17import * as path from 'path';
18
19export class ComponentFile {
20  public componentName: string;
21  public outFileName: string;
22
23  static snake2Camel(name: string, low: boolean = false): string {
24    if (!name.includes('_')) {
25      return name;
26    }
27    return name
28      .split('_')
29      .filter(word => word !== '')
30      .map((word, index) => {
31        if (index === 0 && low) {
32          return word.toLowerCase();
33        }
34        return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
35      })
36      .join('');
37  }
38
39  public appendAttribute(str: string) {
40    this.attributeSource.push(str);
41  }
42
43  public appendFunction(str: string) {
44    this.functionSource += str;
45  }
46
47  get concactSource() {
48    return [...this.attributeSource, this.functionSource].join('\n');
49  }
50
51  constructor(
52    public fileName: string,
53    public sourceFile: ts.SourceFile,
54    public attributeSource: string[] = [],
55    public functionSource: string = '',
56  ) {
57    const pureName = path.basename(this.fileName).replace(/\.d\.e?ts/g, "");
58    this.componentName = ComponentFile.snake2Camel(pureName);
59    this.outFileName = ComponentFile.snake2Camel(pureName, true).concat(".d.ets");
60  }
61}