source: frontend/node_modules/@rushstack/eslint-patch/lib-esm/eslint-bulk-suppressions/bulk-suppressions-file.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 6.2 KB
Line 
1// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2// See LICENSE in the project root for license information.
3import fs from 'node:fs';
4import { VSCODE_PID_ENV_VAR_NAME } from './constants';
5const IS_RUNNING_IN_VSCODE = process.env[VSCODE_PID_ENV_VAR_NAME] !== undefined;
6const TEN_SECONDS_MS = 10 * 1000;
7const SUPPRESSIONS_JSON_FILENAME = '.eslint-bulk-suppressions.json';
8function throwIfAnythingOtherThanNotExistError(e) {
9 if ((e === null || e === void 0 ? void 0 : e.code) !== 'ENOENT') {
10 // Throw an error if any other error than file not found
11 throw e;
12 }
13}
14const suppressionsJsonByFolderPath = new Map();
15export function getSuppressionsConfigForEslintConfigFolderPath(eslintConfigFolderPath) {
16 const cachedSuppressionsConfig = suppressionsJsonByFolderPath.get(eslintConfigFolderPath);
17 let shouldLoad;
18 let suppressionsConfig;
19 if (cachedSuppressionsConfig) {
20 shouldLoad = IS_RUNNING_IN_VSCODE && cachedSuppressionsConfig.readTime < Date.now() - TEN_SECONDS_MS;
21 suppressionsConfig = cachedSuppressionsConfig.suppressionsConfig;
22 }
23 else {
24 shouldLoad = true;
25 }
26 if (shouldLoad) {
27 const suppressionsPath = `${eslintConfigFolderPath}/${SUPPRESSIONS_JSON_FILENAME}`;
28 let rawJsonFile;
29 try {
30 rawJsonFile = fs.readFileSync(suppressionsPath).toString();
31 }
32 catch (e) {
33 throwIfAnythingOtherThanNotExistError(e);
34 }
35 if (!rawJsonFile) {
36 suppressionsConfig = {
37 serializedSuppressions: new Set(),
38 jsonObject: { suppressions: [] },
39 newSerializedSuppressions: new Set(),
40 newJsonObject: { suppressions: [] }
41 };
42 }
43 else {
44 const jsonObject = JSON.parse(rawJsonFile);
45 validateSuppressionsJson(jsonObject);
46 const serializedSuppressions = new Set();
47 for (const suppression of jsonObject.suppressions) {
48 serializedSuppressions.add(serializeSuppression(suppression));
49 }
50 suppressionsConfig = {
51 serializedSuppressions,
52 jsonObject,
53 newSerializedSuppressions: new Set(),
54 newJsonObject: { suppressions: [] }
55 };
56 }
57 suppressionsJsonByFolderPath.set(eslintConfigFolderPath, { readTime: Date.now(), suppressionsConfig });
58 }
59 return suppressionsConfig;
60}
61export function getAllBulkSuppressionsConfigsByEslintConfigFolderPath() {
62 const result = [];
63 for (const [eslintConfigFolderPath, { suppressionsConfig }] of suppressionsJsonByFolderPath) {
64 result.push([eslintConfigFolderPath, suppressionsConfig]);
65 }
66 return result;
67}
68export function writeSuppressionsJsonToFile(eslintConfigFolderPath, suppressionsConfig) {
69 suppressionsJsonByFolderPath.set(eslintConfigFolderPath, { readTime: Date.now(), suppressionsConfig });
70 const suppressionsPath = `${eslintConfigFolderPath}/${SUPPRESSIONS_JSON_FILENAME}`;
71 if (suppressionsConfig.jsonObject.suppressions.length === 0) {
72 deleteFile(suppressionsPath);
73 }
74 else {
75 suppressionsConfig.jsonObject.suppressions.sort(compareSuppressions);
76 fs.writeFileSync(suppressionsPath, JSON.stringify(suppressionsConfig.jsonObject, undefined, 2));
77 }
78}
79export function deleteBulkSuppressionsFileInEslintConfigFolder(eslintConfigFolderPath) {
80 const suppressionsPath = `${eslintConfigFolderPath}/${SUPPRESSIONS_JSON_FILENAME}`;
81 deleteFile(suppressionsPath);
82}
83function deleteFile(filePath) {
84 try {
85 fs.unlinkSync(filePath);
86 }
87 catch (e) {
88 throwIfAnythingOtherThanNotExistError(e);
89 }
90}
91export function serializeSuppression({ file, scopeId, rule }) {
92 return `${file}|${scopeId}|${rule}`;
93}
94function compareSuppressions(a, b) {
95 if (a.file < b.file) {
96 return -1;
97 }
98 else if (a.file > b.file) {
99 return 1;
100 }
101 else if (a.scopeId < b.scopeId) {
102 return -1;
103 }
104 else if (a.scopeId > b.scopeId) {
105 return 1;
106 }
107 else if (a.rule < b.rule) {
108 return -1;
109 }
110 else if (a.rule > b.rule) {
111 return 1;
112 }
113 else {
114 return 0;
115 }
116}
117function validateSuppressionsJson(json) {
118 if (typeof json !== 'object') {
119 throw new Error(`Invalid JSON object: ${JSON.stringify(json, null, 2)}`);
120 }
121 if (!json) {
122 throw new Error('JSON object is null.');
123 }
124 const EXPECTED_ROOT_PROPERTY_NAMES = new Set(['suppressions']);
125 for (const propertyName of Object.getOwnPropertyNames(json)) {
126 if (!EXPECTED_ROOT_PROPERTY_NAMES.has(propertyName)) {
127 throw new Error(`Unexpected property name: ${propertyName}`);
128 }
129 }
130 const { suppressions } = json;
131 if (!suppressions) {
132 throw new Error('Missing "suppressions" property.');
133 }
134 if (!Array.isArray(suppressions)) {
135 throw new Error('"suppressions" property is not an array.');
136 }
137 const EXPECTED_SUPPRESSION_PROPERTY_NAMES = new Set(['file', 'scopeId', 'rule']);
138 for (const suppression of suppressions) {
139 if (typeof suppression !== 'object') {
140 throw new Error(`Invalid suppression: ${JSON.stringify(suppression, null, 2)}`);
141 }
142 if (!suppression) {
143 throw new Error(`Suppression is null: ${JSON.stringify(suppression, null, 2)}`);
144 }
145 for (const propertyName of Object.getOwnPropertyNames(suppression)) {
146 if (!EXPECTED_SUPPRESSION_PROPERTY_NAMES.has(propertyName)) {
147 throw new Error(`Unexpected property name: ${propertyName}`);
148 }
149 }
150 for (const propertyName of EXPECTED_SUPPRESSION_PROPERTY_NAMES) {
151 if (!suppression.hasOwnProperty(propertyName)) {
152 throw new Error(`Missing "${propertyName}" property in suppression: ${JSON.stringify(suppression, null, 2)}`);
153 }
154 else if (typeof suppression[propertyName] !== 'string') {
155 throw new Error(`"${propertyName}" property in suppression is not a string: ${JSON.stringify(suppression, null, 2)}`);
156 }
157 }
158 }
159 return true;
160}
161//# sourceMappingURL=bulk-suppressions-file.js.map
Note: See TracBrowser for help on using the repository browser.