idlize

Форк
0
/
LinterWhitelist.ts 
66 строк · 2.4 Кб
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

16
import { LinterError, LinterMessage } from "./linter";
17

18
import * as fs from "fs"
19
import { identName } from "./util";
20

21
export class LinterWhitelist {
22
    suppressErrors = new Set<LinterError>()
23
    suppressIdentifiers = new Map<string, LinterError[]>()
24

25
    constructor(filename: string) {
26
        let content = fs.readFileSync(filename)?.toString()
27
        if (!content) throw new Error(`Cannot read whitelist file ${filename}`)
28
        let json = JSON.parse(content)
29
        if (!json) throw new Error(`Cannot parse whitelist file ${filename}`)
30
        if (json.suppressErrors) {
31
            (json.suppressErrors as string[]).forEach(it => {
32
                let error = this.linterError(it)
33
                if (error) this.suppressErrors.add(error)
34
            })
35
        }
36
        if (json.suppressIdentifiers) {
37
            let suppress = (json.suppressIdentifiers as Record<string, string[]>)
38
            Object.keys(suppress)
39
                .forEach(key => {
40
                    this.suppressIdentifiers.set(key,
41
                        suppress[key]
42
                            .map(it => this.linterError(it)!)
43
                            .filter(it => it != undefined)
44
                    )
45
                })
46
        }
47
    }
48

49
    linterError(str: string | undefined): LinterError | undefined {
50
        if (!str) return undefined
51
        type Keys = keyof typeof LinterError;
52
        return LinterError[str as Keys]
53
    }
54

55
    shallSuppress(error: LinterMessage): boolean {
56
        if (this.suppressErrors.has(error.error)) return true
57
        let ident = identName(error.node)
58
        if (ident) {
59
            let suppressIdents = this.suppressIdentifiers.get(ident)
60
            if (suppressIdents && (suppressIdents.indexOf(error.error) >= 0)) {
61
                return true
62
            }
63
        }
64
        return false
65
    }
66
}
67

68

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.