You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 line
12 KiB
1 line
12 KiB
{"version":3,"file":"bulk-suppressions-file.js","sourceRoot":"","sources":["../../src/eslint-bulk-suppressions/bulk-suppressions-file.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;;;;AAE3D,4CAAoB;AACpB,2CAAsD;AAmBtD,MAAM,oBAAoB,GAAY,OAAO,CAAC,GAAG,CAAC,mCAAuB,CAAC,KAAK,SAAS,CAAC;AACzF,MAAM,cAAc,GAAW,EAAE,GAAG,IAAI,CAAC;AACzC,MAAM,0BAA0B,GAAW,gCAAgC,CAAC;AAE5E,SAAS,qCAAqC,CAAC,CAAwB;IACrE,IAAI,CAAA,CAAC,aAAD,CAAC,uBAAD,CAAC,CAAE,IAAI,MAAK,QAAQ,EAAE,CAAC;QACzB,wDAAwD;QACxD,MAAM,CAAC,CAAC;IACV,CAAC;AACH,CAAC;AAMD,MAAM,4BAA4B,GAA+C,IAAI,GAAG,EAAE,CAAC;AAC3F,SAAgB,0CAA0C,CACxD,kBAA0B;IAE1B,MAAM,wBAAwB,GAC5B,4BAA4B,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC;IAEvD,IAAI,UAAmB,CAAC;IACxB,IAAI,kBAA2C,CAAC;IAChD,IAAI,wBAAwB,EAAE,CAAC;QAC7B,UAAU,GAAG,oBAAoB,IAAI,wBAAwB,CAAC,QAAQ,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,cAAc,CAAC;QACrG,kBAAkB,GAAG,wBAAwB,CAAC,kBAAkB,CAAC;IACnE,CAAC;SAAM,CAAC;QACN,UAAU,GAAG,IAAI,CAAC;IACpB,CAAC;IAED,IAAI,UAAU,EAAE,CAAC;QACf,MAAM,gBAAgB,GAAW,GAAG,kBAAkB,IAAI,0BAA0B,EAAE,CAAC;QACvF,IAAI,WAA+B,CAAC;QACpC,IAAI,CAAC;YACH,WAAW,GAAG,YAAE,CAAC,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AAC,WAAW,CAAC,cAAc,CAAC,YAAY,CAAC,EAAE,CAAC;gBAC9C,MAAM,IAAI,KAAK,CACb,YAAY,YAAY,8BAA8B,IAAI,CAAC,SAAS,CAAC,WAAW,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAC7F,CAAC;YACJ,CAAC;iBAAM,IAAI,OAAO,WAAW,CAAC,YAAY,CAAC,KAAK,QAAQ,EAAE,CAAC;gBACzD,MAAM,IAAI,KAAK,CACb,IAAI,YAAY,8CAA8C,IAAI,CAAC,SAAS,CAAC,WAAW,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CACrG,CAAC;YACJ,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport fs from 'fs';\nimport { VSCODE_PID_ENV_VAR_NAME } from './constants';\n\nexport interface ISuppression {\n file: string;\n scopeId: string;\n rule: string;\n}\n\nexport interface IBulkSuppressionsConfig {\n serializedSuppressions: Set<string>;\n jsonObject: IBulkSuppressionsJson;\n newSerializedSuppressions: Set<string>;\n newJsonObject: IBulkSuppressionsJson;\n}\n\nexport interface IBulkSuppressionsJson {\n suppressions: ISuppression[];\n}\n\nconst IS_RUNNING_IN_VSCODE: boolean = process.env[VSCODE_PID_ENV_VAR_NAME] !== undefined;\nconst TEN_SECONDS_MS: number = 10 * 1000;\nconst SUPPRESSIONS_JSON_FILENAME: string = '.eslint-bulk-suppressions.json';\n\nfunction throwIfAnythingOtherThanNotExistError(e: NodeJS.ErrnoException): void | never {\n if (e?.code !== 'ENOENT') {\n // Throw an error if any other error than file not found\n throw e;\n }\n}\n\ninterface ICachedBulkSuppressionsConfig {\n readTime: number;\n suppressionsConfig: IBulkSuppressionsConfig;\n}\nconst suppressionsJsonByFolderPath: Map<string, ICachedBulkSuppressionsConfig> = new Map();\nexport function getSuppressionsConfigForEslintrcFolderPath(\n eslintrcFolderPath: string\n): IBulkSuppressionsConfig {\n const cachedSuppressionsConfig: ICachedBulkSuppressionsConfig | undefined =\n suppressionsJsonByFolderPath.get(eslintrcFolderPath);\n\n let shouldLoad: boolean;\n let suppressionsConfig: IBulkSuppressionsConfig;\n if (cachedSuppressionsConfig) {\n shouldLoad = IS_RUNNING_IN_VSCODE && cachedSuppressionsConfig.readTime < Date.now() - TEN_SECONDS_MS;\n suppressionsConfig = cachedSuppressionsConfig.suppressionsConfig;\n } else {\n shouldLoad = true;\n }\n\n if (shouldLoad) {\n const suppressionsPath: string = `${eslintrcFolderPath}/${SUPPRESSIONS_JSON_FILENAME}`;\n let rawJsonFile: string | undefined;\n try {\n rawJsonFile = fs.readFileSync(suppressionsPath).toString();\n } catch (e) {\n throwIfAnythingOtherThanNotExistError(e);\n }\n\n if (!rawJsonFile) {\n suppressionsConfig = {\n serializedSuppressions: new Set(),\n jsonObject: { suppressions: [] },\n newSerializedSuppressions: new Set(),\n newJsonObject: { suppressions: [] }\n };\n } else {\n const jsonObject: IBulkSuppressionsJson = JSON.parse(rawJsonFile);\n validateSuppressionsJson(jsonObject);\n\n const serializedSuppressions: Set<string> = new Set();\n for (const suppression of jsonObject.suppressions) {\n serializedSuppressions.add(serializeSuppression(suppression));\n }\n\n suppressionsConfig = {\n serializedSuppressions,\n jsonObject,\n newSerializedSuppressions: new Set(),\n newJsonObject: { suppressions: [] }\n };\n }\n\n suppressionsJsonByFolderPath.set(eslintrcFolderPath, { readTime: Date.now(), suppressionsConfig });\n }\n\n return suppressionsConfig!;\n}\n\nexport function getAllBulkSuppressionsConfigsByEslintrcFolderPath(): [string, IBulkSuppressionsConfig][] {\n const result: [string, IBulkSuppressionsConfig][] = [];\n for (const [eslintrcFolderPath, { suppressionsConfig }] of suppressionsJsonByFolderPath) {\n result.push([eslintrcFolderPath, suppressionsConfig]);\n }\n\n return result;\n}\n\nexport function writeSuppressionsJsonToFile(\n eslintrcFolderPath: string,\n suppressionsConfig: IBulkSuppressionsConfig\n): void {\n suppressionsJsonByFolderPath.set(eslintrcFolderPath, { readTime: Date.now(), suppressionsConfig });\n const suppressionsPath: string = `${eslintrcFolderPath}/${SUPPRESSIONS_JSON_FILENAME}`;\n if (suppressionsConfig.jsonObject.suppressions.length === 0) {\n deleteFile(suppressionsPath);\n } else {\n suppressionsConfig.jsonObject.suppressions.sort(compareSuppressions);\n fs.writeFileSync(suppressionsPath, JSON.stringify(suppressionsConfig.jsonObject, undefined, 2));\n }\n}\n\nexport function deleteBulkSuppressionsFileInEslintrcFolder(eslintrcFolderPath: string): void {\n const suppressionsPath: string = `${eslintrcFolderPath}/${SUPPRESSIONS_JSON_FILENAME}`;\n deleteFile(suppressionsPath);\n}\n\nfunction deleteFile(filePath: string): void {\n try {\n fs.unlinkSync(filePath);\n } catch (e) {\n throwIfAnythingOtherThanNotExistError(e);\n }\n}\n\nexport function serializeSuppression({ file, scopeId, rule }: ISuppression): string {\n return `${file}|${scopeId}|${rule}`;\n}\n\nfunction compareSuppressions(a: ISuppression, b: ISuppression): -1 | 0 | 1 {\n if (a.file < b.file) {\n return -1;\n } else if (a.file > b.file) {\n return 1;\n } else if (a.scopeId < b.scopeId) {\n return -1;\n } else if (a.scopeId > b.scopeId) {\n return 1;\n } else if (a.rule < b.rule) {\n return -1;\n } else if (a.rule > b.rule) {\n return 1;\n } else {\n return 0;\n }\n}\n\nfunction validateSuppressionsJson(json: IBulkSuppressionsJson): json is IBulkSuppressionsJson {\n if (typeof json !== 'object') {\n throw new Error(`Invalid JSON object: ${JSON.stringify(json, null, 2)}`);\n }\n\n if (!json) {\n throw new Error('JSON object is null.');\n }\n\n const EXPECTED_ROOT_PROPERTY_NAMES: Set<keyof IBulkSuppressionsJson> = new Set(['suppressions']);\n\n for (const propertyName of Object.getOwnPropertyNames(json)) {\n if (!EXPECTED_ROOT_PROPERTY_NAMES.has(propertyName as keyof IBulkSuppressionsJson)) {\n throw new Error(`Unexpected property name: ${propertyName}`);\n }\n }\n\n const { suppressions } = json;\n if (!suppressions) {\n throw new Error('Missing \"suppressions\" property.');\n }\n\n if (!Array.isArray(suppressions)) {\n throw new Error('\"suppressions\" property is not an array.');\n }\n\n const EXPECTED_SUPPRESSION_PROPERTY_NAMES: Set<keyof ISuppression> = new Set(['file', 'scopeId', 'rule']);\n for (const suppression of suppressions) {\n if (typeof suppression !== 'object') {\n throw new Error(`Invalid suppression: ${JSON.stringify(suppression, null, 2)}`);\n }\n\n if (!suppression) {\n throw new Error(`Suppression is null: ${JSON.stringify(suppression, null, 2)}`);\n }\n\n for (const propertyName of Object.getOwnPropertyNames(suppression)) {\n if (!EXPECTED_SUPPRESSION_PROPERTY_NAMES.has(propertyName as keyof ISuppression)) {\n throw new Error(`Unexpected property name: ${propertyName}`);\n }\n }\n\n for (const propertyName of EXPECTED_SUPPRESSION_PROPERTY_NAMES) {\n if (!suppression.hasOwnProperty(propertyName)) {\n throw new Error(\n `Missing \"${propertyName}\" property in suppression: ${JSON.stringify(suppression, null, 2)}`\n );\n } else if (typeof suppression[propertyName] !== 'string') {\n throw new Error(\n `\"${propertyName}\" property in suppression is not a string: ${JSON.stringify(suppression, null, 2)}`\n );\n }\n }\n }\n\n return true;\n}\n"]} |