Skip to content

Commit

Permalink
Only do work for error reporting if there's an error reporter.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielRosenwasser committed Dec 7, 2023
1 parent cd2d86e commit af7f5b8
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20619,30 +20619,37 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (sourceValue !== targetValue) {
const sourceIsString = typeof sourceValue === "string";
const targetIsString = typeof targetValue === "string";
let errorFlags: RelationComparisonResult = errorReporter ? RelationComparisonResult.Reported : RelationComparisonResult.None;

if (sourceValue !== undefined && targetValue !== undefined) {
// If we have 2 *known* values that differ, we should report an error.
const escapedSource = sourceIsString ? `"${escapeString(sourceValue)}"` : sourceValue;
const escapedTarget = targetIsString ? `"${escapeString(targetValue)}"` : targetValue;
errorReporter?.(Diagnostics.Each_declaration_of_0_1_differs_in_its_value_where_2_was_expected_but_3_was_given, symbolName(targetSymbol), symbolName(targetProperty), escapedTarget, escapedSource);
errorFlags |= RelationComparisonResult.Failed;
// If we have 2 enums with *known* values that differ, they are incompatible.
if (!errorReporter) {
enumRelation.set(id, RelationComparisonResult.Failed);
}
else {
const escapedSource = sourceIsString ? `"${escapeString(sourceValue)}"` : sourceValue;
const escapedTarget = targetIsString ? `"${escapeString(targetValue)}"` : targetValue;
errorReporter(Diagnostics.Each_declaration_of_0_1_differs_in_its_value_where_2_was_expected_but_3_was_given, symbolName(targetSymbol), symbolName(targetProperty), escapedTarget, escapedSource);
enumRelation.set(id, RelationComparisonResult.Failed | RelationComparisonResult.Reported);
}
return false;
}
else if (sourceIsString || targetIsString) {
// At this point we know that at least one of the values is 'undefined'.
// This may mean that we have an opaque member from an ambient enum declaration,
// or that we were not able to calculate it (which is basically an error).
//
// Either way, we can assume that it's numeric.
// If we have a string, we report a mismatch in the types.
const knownStringValue = (sourceValue ?? targetValue) as string;
const escapedValue = `"${escapeString(knownStringValue)}"`;
errorReporter?.(Diagnostics.One_value_of_0_1_is_the_string_2_and_the_other_is_assumed_to_be_an_unknown_numeric_value, symbolName(targetSymbol), symbolName(targetProperty), escapedValue);
errorFlags |= RelationComparisonResult.Failed;
}

if (errorFlags & RelationComparisonResult.Failed) {
enumRelation.set(id, errorFlags);
// If the other is a string, we have a mismatch in types.
if (!errorReporter) {
enumRelation.set(id, RelationComparisonResult.Failed);
}
else {
const knownStringValue = sourceValue ?? targetValue;
Debug.assert(typeof knownStringValue === "string");
const escapedValue = `"${escapeString(knownStringValue)}"`;
errorReporter(Diagnostics.One_value_of_0_1_is_the_string_2_and_the_other_is_assumed_to_be_an_unknown_numeric_value, symbolName(targetSymbol), symbolName(targetProperty), escapedValue);
enumRelation.set(id, RelationComparisonResult.Failed | RelationComparisonResult.Reported);
}
return false;
}
}
Expand Down

0 comments on commit af7f5b8

Please sign in to comment.