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
7
* http://www.apache.org/licenses/LICENSE-2.0
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.
16
import { LinterError, LinterMessage } from "./linter";
18
import * as fs from "fs"
19
import { identName } from "./util";
21
export class LinterWhitelist {
22
suppressErrors = new Set<LinterError>()
23
suppressIdentifiers = new Map<string, LinterError[]>()
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)
36
if (json.suppressIdentifiers) {
37
let suppress = (json.suppressIdentifiers as Record<string, string[]>)
40
this.suppressIdentifiers.set(key,
42
.map(it => this.linterError(it)!)
43
.filter(it => it != undefined)
49
linterError(str: string | undefined): LinterError | undefined {
50
if (!str) return undefined
51
type Keys = keyof typeof LinterError;
52
return LinterError[str as Keys]
55
shallSuppress(error: LinterMessage): boolean {
56
if (this.suppressErrors.has(error.error)) return true
57
let ident = identName(error.node)
59
let suppressIdents = this.suppressIdentifiers.get(ident)
60
if (suppressIdents && (suppressIdents.indexOf(error.error) >= 0)) {