|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.gradle.internal.checkstyle; |
| 10 | + |
| 11 | +import com.puppycrawl.tools.checkstyle.StatelessCheck; |
| 12 | +import com.puppycrawl.tools.checkstyle.api.AbstractCheck; |
| 13 | +import com.puppycrawl.tools.checkstyle.api.DetailAST; |
| 14 | +import com.puppycrawl.tools.checkstyle.api.TokenTypes; |
| 15 | + |
| 16 | +/** |
| 17 | + * This rule checks for switch statements that use a {@code between(x, y)} to generate some kind |
| 18 | + * of random behaviour in tests and which have a case that falls outside the random range. |
| 19 | + * <p> |
| 20 | + * Consider this example: |
| 21 | + * <pre>{@code |
| 22 | + * switch (between(0, 3)) { |
| 23 | + * case 0 -> name += randomAlphaOfLength(5); |
| 24 | + * case 1 -> requiredSize += between(1, 100); |
| 25 | + * case 2 -> minDocCount += between(1, 100); |
| 26 | + * case 3 -> subsetSize += between(1, 100); |
| 27 | + * case 4 -> supersetSize += between(1, 100); |
| 28 | + * } |
| 29 | + * }</pre> |
| 30 | + * <p>The "4" case is an error, because it can never execute. |
| 31 | + */ |
| 32 | +@StatelessCheck |
| 33 | +public class SwitchBetweenCheck extends AbstractCheck { |
| 34 | + |
| 35 | + public static final String SWITCH_RANDOM_INT_MSG_KEY = "forbidden.switch.randomInt"; |
| 36 | + public static final String SWITCH_BETWEEN_MSG_KEY = "forbidden.switch.between"; |
| 37 | + |
| 38 | + @Override |
| 39 | + public int[] getDefaultTokens() { |
| 40 | + return getRequiredTokens(); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public int[] getAcceptableTokens() { |
| 45 | + return getRequiredTokens(); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public int[] getRequiredTokens() { |
| 50 | + return new int[] { TokenTypes.LITERAL_SWITCH }; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void visitToken(DetailAST ast) { |
| 55 | + checkSwitchBetween(ast); |
| 56 | + } |
| 57 | + |
| 58 | + private void checkSwitchBetween(DetailAST ast) { |
| 59 | + // First dig out the switch expression |
| 60 | + final DetailAST switchExprAst = ast.findFirstToken(TokenTypes.EXPR); |
| 61 | + if (switchExprAst == null) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + // Check if it's a method call |
| 66 | + final DetailAST methodCallAst = switchExprAst.getFirstChild(); |
| 67 | + if (methodCallAst.getType() != TokenTypes.METHOD_CALL) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + // And check if the method call is a `between` or `randomIntBetween` call |
| 72 | + final DetailAST methodIdentAst = methodCallAst.findFirstToken(TokenTypes.IDENT); |
| 73 | + if (methodIdentAst == null) { |
| 74 | + return; |
| 75 | + } |
| 76 | + final String switchMethodName = methodIdentAst.getText(); |
| 77 | + switch (switchMethodName) { |
| 78 | + case "between": |
| 79 | + case "randomIntBetween": |
| 80 | + case "randomInt": |
| 81 | + // these are ok |
| 82 | + break; |
| 83 | + default: |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + // The method name is good, so dig out the arguments to the method. We only handle simple, |
| 88 | + // integer literal arguments |
| 89 | + final DetailAST argListAst = methodCallAst.findFirstToken(TokenTypes.ELIST); |
| 90 | + int min; |
| 91 | + int max; |
| 92 | + if (switchMethodName.equals("randomInt")) { |
| 93 | + if (argListAst.getChildCount() != 1) { // 1 arg |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + try { |
| 98 | + // Get first or last child, which is an EXPR, then get the argument itself |
| 99 | + final String maxStr = argListAst.getLastChild().getFirstChild().getText(); |
| 100 | + min = 0; |
| 101 | + max = Integer.parseInt(maxStr); |
| 102 | + } catch (NumberFormatException e) { |
| 103 | + return; |
| 104 | + } |
| 105 | + } else { |
| 106 | + if (argListAst.getChildCount() != 3) { // 2 args + COMMA |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + try { |
| 111 | + // Get first or last child, which is an EXPR, then get the argument itself |
| 112 | + final String minStr = argListAst.getFirstChild().getFirstChild().getText(); |
| 113 | + final String maxStr = argListAst.getLastChild().getFirstChild().getText(); |
| 114 | + min = Integer.parseInt(minStr); |
| 115 | + max = Integer.parseInt(maxStr); |
| 116 | + } catch (NumberFormatException e) { |
| 117 | + return; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Now check all the cases of the switch and look for values outside the possible range. |
| 122 | + // We ignore anything that doesn't parse as an integer, so it's possible we could miss |
| 123 | + // some cases. |
| 124 | + for (DetailAST caseAst = ast.getFirstChild(); caseAst != null; caseAst = caseAst.getNextSibling()) { |
| 125 | + if (caseAst.getType() != TokenTypes.CASE_GROUP && caseAst.getType() != TokenTypes.SWITCH_RULE) { |
| 126 | + continue; |
| 127 | + } |
| 128 | + |
| 129 | + final DetailAST literalCaseAst = caseAst.getFirstChild(); |
| 130 | + if (literalCaseAst.getType() == TokenTypes.LITERAL_DEFAULT) { |
| 131 | + continue; |
| 132 | + } |
| 133 | + |
| 134 | + final DetailAST exprAst = literalCaseAst.getFirstChild(); |
| 135 | + if (exprAst.getType() != TokenTypes.EXPR) { |
| 136 | + continue; |
| 137 | + } |
| 138 | + |
| 139 | + try { |
| 140 | + int value = Integer.parseInt(exprAst.getFirstChild().getText()); |
| 141 | + if (value < min || value > max) { |
| 142 | + if (switchMethodName.equals("randomInt")) { |
| 143 | + log(caseAst, SWITCH_RANDOM_INT_MSG_KEY, value, switchMethodName, max); |
| 144 | + } else { |
| 145 | + log(caseAst, SWITCH_BETWEEN_MSG_KEY, value, switchMethodName, min, max); |
| 146 | + } |
| 147 | + } |
| 148 | + } catch (NumberFormatException e) { |
| 149 | + // Ignore |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments