|
| 1 | +import { normalizePathCase, isFileSystemCaseSensitive } from 'pyright-internal/common/pathUtils'; |
| 2 | +import { PyrightFileSystem } from 'pyright-internal/pyrightFileSystem'; |
| 3 | +import { createFromRealFileSystem } from 'pyright-internal/common/realFileSystem'; |
| 4 | + |
| 5 | +export enum SeenCondition { |
| 6 | + AlwaysFalse = 'always-false', |
| 7 | + AlwaysTrue = 'always-true', |
| 8 | + Mixed = 'mixed' |
| 9 | +} |
| 10 | + |
| 11 | +export class AssertionError extends Error { |
| 12 | + constructor(message: string) { |
| 13 | + super(message); |
| 14 | + this.name = 'AssertionError'; |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +// Private global state - never export directly |
| 19 | +let _assertionFlags = { |
| 20 | + pathNormalizationChecks: false, |
| 21 | + otherChecks: false |
| 22 | +}; |
| 23 | +let _context = ''; |
| 24 | +const _sometimesResults = new Map<string, Map<string, SeenCondition>>(); |
| 25 | + |
| 26 | +export function setGlobalAssertionFlags(pathNormalizationChecks: boolean, otherChecks: boolean): void { |
| 27 | + _assertionFlags.pathNormalizationChecks = pathNormalizationChecks; |
| 28 | + _assertionFlags.otherChecks = otherChecks; |
| 29 | +} |
| 30 | + |
| 31 | +export function setGlobalContext(context: string): void { |
| 32 | + _context = context; |
| 33 | +} |
| 34 | + |
| 35 | +// Internal implementation functions |
| 36 | +function assertAlwaysImpl(enableFlag: boolean, check: () => boolean, message: () => string): void { |
| 37 | + if (!enableFlag) return; |
| 38 | + |
| 39 | + if (!check()) { |
| 40 | + throw new AssertionError(message()); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +function assertSometimesImpl(enableFlag: boolean, check: () => boolean, key: string): void { |
| 45 | + if (!enableFlag) return; |
| 46 | + |
| 47 | + const ctx = _context; |
| 48 | + if (ctx === '') { |
| 49 | + throw new AssertionError('Context must be set before calling assertSometimes'); |
| 50 | + } |
| 51 | + |
| 52 | + let ctxMap = _sometimesResults.get(key); |
| 53 | + if (!ctxMap) { |
| 54 | + ctxMap = new Map(); |
| 55 | + _sometimesResults.set(key, ctxMap); |
| 56 | + } |
| 57 | + |
| 58 | + const result = check() ? SeenCondition.AlwaysTrue : SeenCondition.AlwaysFalse; |
| 59 | + const prev = ctxMap.get(ctx); |
| 60 | + |
| 61 | + if (prev === undefined) { |
| 62 | + ctxMap.set(ctx, result); |
| 63 | + } else if (prev !== result) { |
| 64 | + ctxMap.set(ctx, SeenCondition.Mixed); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +const _fs = new PyrightFileSystem(createFromRealFileSystem()); |
| 69 | + |
| 70 | +export function assertAlways(check: () => boolean, message: () => string): void { |
| 71 | + assertAlwaysImpl(_assertionFlags.otherChecks, check, message); |
| 72 | +} |
| 73 | + |
| 74 | +export function assertSometimes(check: () => boolean, key: string): void { |
| 75 | + assertSometimesImpl(_assertionFlags.otherChecks, check, key); |
| 76 | +} |
| 77 | + |
| 78 | +export function assertNeverNormalized(path: string): void { |
| 79 | + const normalized = normalizePathCase(_fs, path); |
| 80 | + assertAlwaysImpl( |
| 81 | + _assertionFlags.pathNormalizationChecks, |
| 82 | + () => normalized !== path, |
| 83 | + () => `Path should not be normalized but was: ${path}` |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +export function assertAlwaysNormalized(path: string): void { |
| 88 | + const normalized = normalizePathCase(_fs, path); |
| 89 | + assertAlwaysImpl( |
| 90 | + _assertionFlags.pathNormalizationChecks, |
| 91 | + () => normalized === path, |
| 92 | + () => `Path should be normalized but was not: ${path} -> ${normalized}` |
| 93 | + ); |
| 94 | +} |
| 95 | + |
| 96 | +export function assertSometimesNormalized(path: string, key: string): void { |
| 97 | + const normalized = normalizePathCase(_fs, path); |
| 98 | + assertSometimesImpl( |
| 99 | + _assertionFlags.pathNormalizationChecks, |
| 100 | + () => normalized === path, |
| 101 | + key |
| 102 | + ); |
| 103 | +} |
| 104 | + |
| 105 | +// Monoidal combination logic |
| 106 | +function combine(a: SeenCondition, b: SeenCondition): SeenCondition { |
| 107 | + if (a === b) return a; |
| 108 | + if (a === SeenCondition.Mixed || b === SeenCondition.Mixed) { |
| 109 | + return SeenCondition.Mixed; |
| 110 | + } |
| 111 | + // AlwaysTrue + AlwaysFalse = Mixed |
| 112 | + return SeenCondition.Mixed; |
| 113 | +} |
| 114 | + |
| 115 | +export function checkSometimesAssertions(): Map<string, SeenCondition> { |
| 116 | + const summary = new Map<string, SeenCondition>(); |
| 117 | + |
| 118 | + for (const [key, ctxMap] of _sometimesResults) { |
| 119 | + let agg: SeenCondition | undefined; |
| 120 | + for (const state of ctxMap.values()) { |
| 121 | + agg = agg === undefined ? state : combine(agg, state); |
| 122 | + if (agg === SeenCondition.Mixed) break; |
| 123 | + } |
| 124 | + if (agg !== undefined) { |
| 125 | + summary.set(key, agg); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return summary; |
| 130 | +} |
0 commit comments