|
| 1 | +import * as ts from "typescript"; |
| 2 | +import type { RulePlugin } from "../../core/types"; |
| 3 | +import { getLineNumber, unwrapExpression, walk } from "../../facts/ts-helpers"; |
| 4 | +import { delta } from "../../rule-delta"; |
| 5 | + |
| 6 | +const MAX_LOGICAL_LINES = 5000; |
| 7 | + |
| 8 | +type MatchKind = |
| 9 | + | "stringified-unknown-error" |
| 10 | + | "returned-stringified-unknown-error" |
| 11 | + | "property-stringified-unknown-error"; |
| 12 | + |
| 13 | +type StringifiedUnknownErrorMatch = { |
| 14 | + line: number; |
| 15 | + kind: MatchKind; |
| 16 | +}; |
| 17 | + |
| 18 | +function isErrorIdentifier(expression: ts.Expression, allowedNames: Set<string>): boolean { |
| 19 | + const unwrapped = unwrapExpression(expression); |
| 20 | + return ts.isIdentifier(unwrapped) && allowedNames.has(unwrapped.text); |
| 21 | +} |
| 22 | + |
| 23 | +function isStringCallOfError(expression: ts.Expression, allowedNames: Set<string>): boolean { |
| 24 | + const unwrapped = unwrapExpression(expression); |
| 25 | + return ( |
| 26 | + ts.isCallExpression(unwrapped) && |
| 27 | + ts.isIdentifier(unwrapped.expression) && |
| 28 | + unwrapped.expression.text === "String" && |
| 29 | + unwrapped.arguments.length === 1 && |
| 30 | + isErrorIdentifier(unwrapped.arguments[0]!, allowedNames) |
| 31 | + ); |
| 32 | +} |
| 33 | + |
| 34 | +function isErrorMessageAccess(expression: ts.Expression, allowedNames: Set<string>): boolean { |
| 35 | + const unwrapped = unwrapExpression(expression); |
| 36 | + return ( |
| 37 | + ts.isPropertyAccessExpression(unwrapped) && |
| 38 | + unwrapped.name.text === "message" && |
| 39 | + isErrorIdentifier(unwrapped.expression, allowedNames) |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +function isErrorInstanceofCheck(expression: ts.Expression, allowedNames: Set<string>): boolean { |
| 44 | + const unwrapped = unwrapExpression(expression); |
| 45 | + return ( |
| 46 | + ts.isBinaryExpression(unwrapped) && |
| 47 | + unwrapped.operatorToken.kind === ts.SyntaxKind.InstanceOfKeyword && |
| 48 | + isErrorIdentifier(unwrapped.left, allowedNames) && |
| 49 | + ts.isIdentifier(unwrapExpression(unwrapped.right)) && |
| 50 | + unwrapExpression(unwrapped.right).text === "Error" |
| 51 | + ); |
| 52 | +} |
| 53 | + |
| 54 | +function summarizeConditional( |
| 55 | + node: ts.ConditionalExpression, |
| 56 | + sourceFile: ts.SourceFile, |
| 57 | +): StringifiedUnknownErrorMatch | null { |
| 58 | + const allowedNames = new Set<string>(); |
| 59 | + |
| 60 | + const condition = unwrapExpression(node.condition); |
| 61 | + if ( |
| 62 | + ts.isBinaryExpression(condition) && |
| 63 | + condition.operatorToken.kind === ts.SyntaxKind.BarBarToken |
| 64 | + ) { |
| 65 | + for (const side of [condition.left, condition.right]) { |
| 66 | + const unwrapped = unwrapExpression(side); |
| 67 | + if ( |
| 68 | + ts.isBinaryExpression(unwrapped) && |
| 69 | + unwrapped.operatorToken.kind === ts.SyntaxKind.InstanceOfKeyword |
| 70 | + ) { |
| 71 | + const left = unwrapExpression(unwrapped.left); |
| 72 | + if (ts.isIdentifier(left)) { |
| 73 | + allowedNames.add(left.text); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } else if (isErrorInstanceofCheck(condition, new Set(["error", "err", "e", "cause"]))) { |
| 78 | + const left = unwrapExpression((condition as ts.BinaryExpression).left) as ts.Identifier; |
| 79 | + allowedNames.add(left.text); |
| 80 | + } |
| 81 | + |
| 82 | + if (allowedNames.size === 0) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + const whenTrue = unwrapExpression(node.whenTrue); |
| 87 | + const whenFalse = unwrapExpression(node.whenFalse); |
| 88 | + const isEitherDirection = |
| 89 | + (isErrorMessageAccess(whenTrue, allowedNames) && |
| 90 | + isStringCallOfError(whenFalse, allowedNames)) || |
| 91 | + (isErrorMessageAccess(whenFalse, allowedNames) && isStringCallOfError(whenTrue, allowedNames)); |
| 92 | + |
| 93 | + if (!isEitherDirection) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + let kind: MatchKind = "stringified-unknown-error"; |
| 98 | + if (ts.isReturnStatement(node.parent)) { |
| 99 | + kind = "returned-stringified-unknown-error"; |
| 100 | + } else if (ts.isPropertyAssignment(node.parent)) { |
| 101 | + kind = "property-stringified-unknown-error"; |
| 102 | + } |
| 103 | + |
| 104 | + return { |
| 105 | + line: getLineNumber(sourceFile, node.getStart(sourceFile)), |
| 106 | + kind, |
| 107 | + }; |
| 108 | +} |
| 109 | + |
| 110 | +function findStringifiedUnknownErrors(sourceFile: ts.SourceFile): StringifiedUnknownErrorMatch[] { |
| 111 | + const matches: StringifiedUnknownErrorMatch[] = []; |
| 112 | + |
| 113 | + walk(sourceFile, (node) => { |
| 114 | + if (!ts.isConditionalExpression(node)) { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + const match = summarizeConditional(node, sourceFile); |
| 119 | + if (match) { |
| 120 | + matches.push(match); |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + return matches; |
| 125 | +} |
| 126 | + |
| 127 | +export const stringifiedUnknownErrorsRule: RulePlugin = { |
| 128 | + id: "defensive.stringified-unknown-errors", |
| 129 | + family: "defensive", |
| 130 | + severity: "strong", |
| 131 | + scope: "file", |
| 132 | + requires: ["file.ast"], |
| 133 | + delta: delta.byLocations(), |
| 134 | + supports(context) { |
| 135 | + return context.scope === "file" && Boolean(context.file); |
| 136 | + }, |
| 137 | + evaluate(context) { |
| 138 | + if (context.file!.logicalLineCount > MAX_LOGICAL_LINES) { |
| 139 | + return []; |
| 140 | + } |
| 141 | + |
| 142 | + const sourceFile = context.runtime.store.getFileFact<ts.SourceFile>( |
| 143 | + context.file!.path, |
| 144 | + "file.ast", |
| 145 | + ); |
| 146 | + if (!sourceFile) { |
| 147 | + return []; |
| 148 | + } |
| 149 | + |
| 150 | + const matches = findStringifiedUnknownErrors(sourceFile); |
| 151 | + if (matches.length === 0) { |
| 152 | + return []; |
| 153 | + } |
| 154 | + |
| 155 | + return [ |
| 156 | + { |
| 157 | + ruleId: "defensive.stringified-unknown-errors", |
| 158 | + family: "defensive", |
| 159 | + severity: "strong", |
| 160 | + scope: "file", |
| 161 | + path: context.file!.path, |
| 162 | + message: `Found ${matches.length} unknown-error normalization${matches.length === 1 ? "" : "s"} that collapse caught values into generic strings`, |
| 163 | + evidence: matches.map((match) => `line ${match.line}: ${match.kind}`), |
| 164 | + score: Math.min(8, matches.length * 2), |
| 165 | + locations: matches.map((match) => ({ path: context.file!.path, line: match.line })), |
| 166 | + }, |
| 167 | + ]; |
| 168 | + }, |
| 169 | +}; |
0 commit comments