/* * Copyright (c) 2023-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import * as fs from 'node:fs'; import * as path from 'node:path'; export function getConfiguredRuleTags( arkTSRulesMap: Map, configuredRulesMap: Map ): Set { const mergedRulesMap: string[] = Array.from(configuredRulesMap.values()).flat(); const configuredRuleTags = new Set(); const mergedRulesSet = new Set(mergedRulesMap); arkTSRulesMap.forEach((key, value) => { if (mergedRulesSet.has(key)) { configuredRuleTags.add(value); } }); return configuredRuleTags; } export function getRulesFromConfig(configRulePath: string): Map { try { const normalizedPath = path.normalize(configRulePath); const data = fs.readFileSync(normalizedPath, 'utf-8'); const jsonData = JSON.parse(data); const dataMap = new Map(); for (const [key, value] of Object.entries(jsonData)) { dataMap.set(key, value); } return convertToStringArrayMap(dataMap); } catch (error) { if (error instanceof SyntaxError) { throw new Error(`JSON parsing failed: ${error.message}`); } return new Map(); } } function convertToStringArrayMap(inputMap: Map): Map { const resultMap: Map = new Map(); for (const [key, value] of inputMap) { if (isStringArray(value)) { resultMap.set(key, value); } } return resultMap; } function isStringArray(value: any): value is string[] { return ( Array.isArray(value) && value.every((item) => { return typeof item === 'string'; }) ); }