|
| 1 | +/** |
| 2 | + * @name Consistent alert message |
| 3 | + * @description The alert message should be consistent across languages. |
| 4 | + * @kind problem |
| 5 | + * @problem.severity warning |
| 6 | + * @id ql/consistent-alert-message |
| 7 | + * @tags correctness |
| 8 | + * @precision very-high |
| 9 | + */ |
| 10 | + |
| 11 | +import ql |
| 12 | + |
| 13 | +/** |
| 14 | + * Gets a string representation of the entire message in `sel`. |
| 15 | + * Ignores everything that is not a string constant. |
| 16 | + */ |
| 17 | +string getMessage(Select sel) { |
| 18 | + result = |
| 19 | + strictconcat(String e, Location l | |
| 20 | + // is child of an expression in the select (in an odd-indexed position, that's where the message is) |
| 21 | + e.getParent*() = sel.getExpr(any(int i | i % 2 = 1)) and l = e.getFullLocation() |
| 22 | + | |
| 23 | + e.getValue(), " | " order by l.getStartLine(), l.getStartColumn() |
| 24 | + ).trim() |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Gets a language agnostic fingerprint for a Select. |
| 29 | + * The fingerPrint includes e.g. the query-id, the @kind of the query, and the number of expressions in the select. |
| 30 | + * |
| 31 | + * This fingerprint avoids false positives where two queries with the same ID behave differently (which is OK). |
| 32 | + */ |
| 33 | +string getSelectFingerPrint(Select sel) { |
| 34 | + exists(QueryDoc doc | doc = sel.getQueryDoc() | |
| 35 | + result = |
| 36 | + doc.getQueryId() // query ID (without lang) |
| 37 | + + "-" + doc.getQueryKind() // @kind |
| 38 | + + "-" + |
| 39 | + strictcount(String e | e.getParent*() = sel.getExpr(any(int i | i % 2 = 1))) // the number of string constants in the select |
| 40 | + + "-" + count(sel.getExpr(_)) // and the total number of expressions in the select |
| 41 | + ) |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Gets information about the select. |
| 46 | + * The query-id (without language), the language, the message from the select, and a language agnostic fingerprint. |
| 47 | + */ |
| 48 | +Select parseSelect(string id, string lang, string msg, string fingerPrint) { |
| 49 | + exists(QueryDoc doc | |
| 50 | + doc = result.getQueryDoc() and |
| 51 | + id = doc.getQueryId() and |
| 52 | + lang = doc.getQueryLanguage() and |
| 53 | + fingerPrint = getSelectFingerPrint(result) and |
| 54 | + msg = getMessage(result).toLowerCase() // case normalize, because some languages upper-case methods. |
| 55 | + ) and |
| 56 | + // excluding experimental |
| 57 | + not result.getLocation().getFile().getRelativePath().matches("%/experimental/%") and |
| 58 | + not lang = "ql" // excluding QL-for-QL |
| 59 | +} |
| 60 | + |
| 61 | +from Select sel, string id, string lang, string msg, string fingerPrint, string otherLangs |
| 62 | +where |
| 63 | + // for a select with a fingerprint |
| 64 | + sel = parseSelect(id, lang, msg, fingerPrint) and |
| 65 | + // there exists other languages with the same fingerprint, but other message |
| 66 | + otherLangs = |
| 67 | + strictconcat(string bad | |
| 68 | + bad != lang and |
| 69 | + exists(parseSelect(id, bad, any(string otherMsg | otherMsg != msg), fingerPrint)) |
| 70 | + | |
| 71 | + bad, ", " |
| 72 | + ) |
| 73 | +select sel, |
| 74 | + "The " + lang + "/" + id + " query does not have the same alert message as " + otherLangs + "." |
0 commit comments