All files / src/checker/checks/drop invalidDropOption.ts

100% Statements 12/12
100% Branches 4/4
100% Functions 2/2
100% Lines 12/12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47                      2x   2x 2x 2x     3x     2x   2x 2x 4x       1x                       1x       2x  
/**
 * This error is triggered when a DROP statement
 * has an invalid option following the 'DROP'.
 *
 * It would trigger for this:
 *   DROP RUBBISH thing;
 * It wouldn't trigger for this:
 *   DROP TABLE test;
 */
 
import { Query } from "../../../reader/query";
import { CheckerResult } from "../../checkerResult";
import { IChecker } from "../../interface";
import { Types } from "../../../lexer/tokens";
import { sprintf } from "sprintf-js";
import { Drop } from "../../../barrel/statements";
 
class InvalidDropOption implements IChecker {
  public message = "Option '%s' is not a valid option, must be one of '%s'";
 
  public check(query: Query): CheckerResult {
    const dropStatement = new Drop();
 
    for (const line of query.lines) {
      for (const token of line.tokens) {
        if (
          token[0] === Types.Option &&
          !dropStatement.options.includes(token[1])
        ) {
          return new CheckerResult(
            line.num,
            sprintf(
              this.message,
              token[1],
              JSON.stringify(dropStatement.options)
            )
          );
        }
      }
    }
 
    return new CheckerResult(0, "");
  }
}
 
export { InvalidDropOption };