Be more explicit about types in the conformancePatternRule.
This caused typing issues in TypeScript 3.5, due to increased scrutiny in unsound writes to indexed access types.
PiperOrigin-RevId: 253987179
diff --git a/internal/tsetse/rules/conformance_pattern_rule.ts b/internal/tsetse/rules/conformance_pattern_rule.ts
index add5286..707fffc 100644
--- a/internal/tsetse/rules/conformance_pattern_rule.ts
+++ b/internal/tsetse/rules/conformance_pattern_rule.ts
@@ -15,7 +15,7 @@
* This is templated, mostly to ensure the nodes that have been matched
* correspond to what the Fixer expects.
*/
-export class ConformancePatternRule<P extends PatternKind> implements
+export class ConformancePatternRule<P extends keyof MatchedNodeTypes> implements
AbstractRule {
readonly ruleName: string;
readonly code = ErrorCode.CONFORMANCE_PATTERN;
@@ -27,23 +27,28 @@
// that P is Config.kind.
// tslint:disable-next-line:no-any See above.
let engine: PatternEngine<any>;
+ // Formatter breaks the types, b/135552145
+ // clang-format off
switch (config.kind) {
case PatternKind.BANNED_PROPERTY_WRITE:
engine = new PropertyWriteEngine(
- config as Config<PatternKind.BANNED_PROPERTY_WRITE>, fixer);
+ config as Config<PatternKind.BANNED_PROPERTY_WRITE>,
+ fixer as Fixer<MatchedNodeTypes[PatternKind.BANNED_PROPERTY_WRITE]>);
break;
case PatternKind.BANNED_PROPERTY_NON_CONSTANT_WRITE:
engine = new PropertyNonConstantWriteEngine(
config as Config<PatternKind.BANNED_PROPERTY_NON_CONSTANT_WRITE>,
- fixer);
+ fixer as Fixer<MatchedNodeTypes[PatternKind.BANNED_PROPERTY_NON_CONSTANT_WRITE]>);
break;
case PatternKind.BANNED_NAME:
- engine =
- new NameEngine(config as Config<PatternKind.BANNED_NAME>, fixer);
+ engine = new NameEngine(
+ config as Config<PatternKind.BANNED_NAME>,
+ fixer as Fixer<MatchedNodeTypes[PatternKind.BANNED_NAME]>);
break;
default:
throw new Error('Config type not recognized, or not implemented yet.');
}
+ // clang-format on
this.ruleName = `conformance-pattern-${config.kind}`;
this.engine = engine as PatternEngine<P>;
}
diff --git a/internal/tsetse/util/pattern_config.ts b/internal/tsetse/util/pattern_config.ts
index 9ec75f7..e4dcc0d 100644
--- a/internal/tsetse/util/pattern_config.ts
+++ b/internal/tsetse/util/pattern_config.ts
@@ -80,13 +80,9 @@
MANUALLY_REVIEWED
}
-/** Maps the type of nodes that each `PatternType` produces. */
+/** Maps the type of nodes that each `PatternType` consumes. */
export interface MatchedNodeTypes {
- [PatternKind.BANNED_PROPERTY_WRITE]: ts.BinaryExpression&{
- left: ts.PropertyAccessExpression;
- };
- [PatternKind.BANNED_PROPERTY_NON_CONSTANT_WRITE]: ts.BinaryExpression&{
- left: ts.PropertyAccessExpression;
- };
+ [PatternKind.BANNED_PROPERTY_WRITE]: ts.BinaryExpression;
+ [PatternKind.BANNED_PROPERTY_NON_CONSTANT_WRITE]: ts.BinaryExpression;
[PatternKind.BANNED_NAME]: ts.Identifier;
}