• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 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 fs from 'fs';
17import * as path from 'path';
18import { parse } from 'json5';
19import {
20    createSourceFile,
21    forEachChild,
22    isComputedPropertyName,
23    isParameter,
24    isPropertyAccessExpression,
25    isStringLiteral,
26    isTypeParameterDeclaration,
27    ScriptTarget
28 } from 'typescript';
29
30export const visitTscLibNode = (astNode): void => {
31    if (!astNode || !astNode.name) {
32        return;
33    }
34    let nameToAdd: string;
35    if (isStringLiteral(astNode.name)) {
36        nameToAdd = astNode.name.text;
37    } else if (isComputedPropertyName(astNode.name)) {
38        nameToAdd = isPropertyAccessExpression(astNode.name.expression) ?
39            undefined : astNode.name.expression.getText();
40    } else {
41        nameToAdd = astNode.name.getText();
42    }
43
44    if (nameToAdd && !isParameter(astNode) && !isTypeParameterDeclaration(astNode)) {
45        resultSet.add(nameToAdd);
46    }
47
48    forEachChild(astNode, visitTscLibNode);
49};
50
51interface SystemApiWhitelist {
52    [key: string]: string[];
53}
54const runTest = (files: string[], expectedFilePath: string, directoryPath: string): void => {
55    let systemApiWhitelist: SystemApiWhitelist = {
56        es2015: [], es2016: [], es2017: [], es2018: [], es2019: [], es2020: [], es2021: []
57    };
58
59    if (fs.existsSync(expectedFilePath)) {
60        systemApiWhitelist = parse(fs.readFileSync(expectedFilePath, 'utf-8'));
61    } else {
62        console.error(`LanguageWhitelist file does not exist in the directory ${expectedFilePath}`);
63    }
64
65    for (const key of Object.keys(systemApiWhitelist)) {
66        systemApiWhitelist[key].forEach(element => expectSet.add(element));
67    }
68
69    files.forEach(fileName => {
70        const filePath = path.join(directoryPath, fileName);
71        if (fs.existsSync(filePath)) {
72            fileCount++;
73            const sourceFile = createSourceFile(fileName, fs.readFileSync(filePath).toString(), ScriptTarget.ES2015, true);
74            forEachChild(sourceFile, visitTscLibNode);
75        } else {
76            console.error(`File ${fileName} does not exist in the directory ${directoryPath}`);
77        }
78    });
79
80    const missingElements: string[] = [];
81    resultSet.forEach(element => {
82        if (!expectSet.has(element) && !skipElements.includes(element)) {
83            missingElements.push(element);
84        }
85    });
86
87    const result: string = (fileCount === scanFilesList.length) && (missingElements.length === 0) ?
88        'success!' : 'fail!';
89    console.log('----------------------------- System Api Test summary -----------------------------');
90    console.log('Run result:', result);
91    console.log('Scan file counts:', fileCount);
92    console.log('Number of missing system api:', missingElements.length);
93    console.log('Missing system api:', missingElements);
94};
95
96const scanFilesList: string[] = [
97    'lib.es2015.collection.d.ts',
98    'lib.es2015.core.d.ts',
99    'lib.es2015.d.ts',
100    'lib.es2015.generator.d.ts',
101    'lib.es2015.iterable.d.ts',
102    'lib.es2015.promise.d.ts',
103    'lib.es2015.proxy.d.ts',
104    'lib.es2015.reflect.d.ts',
105    'lib.es2015.symbol.d.ts',
106    'lib.es2015.symbol.wellknown.d.ts',
107    'lib.es2016.array.include.d.ts',
108    'lib.es2016.d.ts',
109    'lib.es2017.d.ts',
110    'lib.es2017.intl.d.ts',
111    'lib.es2017.object.d.ts',
112    'lib.es2017.sharedmemory.d.ts',
113    'lib.es2017.string.d.ts',
114    'lib.es2017.typedarrays.d.ts',
115    'lib.es2018.asyncgenerator.d.ts',
116    'lib.es2018.asynciterable.d.ts',
117    'lib.es2018.d.ts',
118    'lib.es2018.intl.d.ts',
119    'lib.es2018.promise.d.ts',
120    'lib.es2018.regexp.d.ts',
121    'lib.es2019.array.d.ts',
122    'lib.es2019.d.ts',
123    'lib.es2019.intl.d.ts',
124    'lib.es2019.object.d.ts',
125    'lib.es2019.string.d.ts',
126    'lib.es2019.symbol.d.ts',
127    'lib.es2020.bigint.d.ts',
128    'lib.es2020.date.d.ts',
129    'lib.es2020.d.ts',
130    'lib.es2020.intl.d.ts',
131    'lib.es2020.number.d.ts',
132    'lib.es2020.promise.d.ts',
133    'lib.es2020.sharedmemory.d.ts',
134    'lib.es2020.string.d.ts',
135    'lib.es2020.symbol.wellknown.d.ts',
136    'lib.es2021.d.ts',
137    'lib.es2021.intl.d.ts',
138    'lib.es2021.promise.d.ts',
139    'lib.es2021.string.d.ts',
140    'lib.es2021.weakref.d.ts',
141    'lib.es5.d.ts',
142];
143const skipElements: string[] = [
144    'MapConstructor',
145    'ReadonlyMap',
146    'WeakMapConstructor',
147    'SetConstructor',
148    'ReadonlySet',
149    'WeakSetConstructor',
150    'Generator',
151    'GeneratorFunction',
152    'GeneratorFunctionConstructor',
153    'IteratorYieldResult',
154    'IteratorReturnResult',
155    'IteratorResult',
156    'Iterable',
157    'IterableIterator',
158    'ProxyHandler',
159    'ProxyConstructor',
160    'proxy',
161    'revoke',
162    'literal',
163    'DateTimeFormatPartTypes',
164    'DateTimeFormatPart',
165    'SharedArrayBufferConstructor',
166    'recur',
167    'BigUint64ArrayConstructor',
168    'ImportCallOptions',
169    'ImportAssertions',
170    'Awaited',
171    'ESObject'
172];
173const expectedFilePath = path.join(__dirname, '../../../../arkcompiler/ets_frontend/arkguard/src/configs/preset/es_reserved_properties_optimized.json');
174const directoryPath = path.join(__dirname, '../../lib');
175let fileCount: number = 0;
176let resultSet: Set<string> = new Set<string>();
177let expectSet: Set<string> = new Set<string>();
178
179runTest(scanFilesList, expectedFilePath, directoryPath);