コード例 #1
0
    /**
     * Verifies that the actual <code>{@link Rule}</code> partially matches a given input.
     *
     * @param prefixToBeMatched the prefix that must be fully matched
     * @param remainingInput the remainder of the input, which is not to be matched
     * @return this assertion object.
     */
    public ParserAssert matchesPrefix(String prefixToBeMatched, String remainingInput) {
      isNotNull();
      try {
        JavaScriptTree tree = (JavaScriptTree) actual.parse(prefixToBeMatched + remainingInput);
        SyntaxToken lastToken = tree.getLastToken();

        if (prefixToBeMatched.length() != lastToken.column() + lastToken.text().length()) {
          throw new RecognitionException(
              0,
              "Rule '"
                  + getRuleName()
                  + "' should match:\n"
                  + prefixToBeMatched
                  + "\nwhen followed by:\n"
                  + remainingInput);
        }
      } catch (RecognitionException e) {
        throw new RecognitionException(
            0,
            e.getMessage()
                + "\n"
                + "Rule '"
                + getRuleName()
                + "' should match:\n"
                + prefixToBeMatched
                + "\nwhen followed by:\n"
                + remainingInput);
      }
      return this;
    }
  @Override
  public void visitNode(Tree tree) {
    Tree parent = parent(tree);
    while (parent.is(Kind.BLOCK)) {
      parent = parent(parent);
    }

    if (!parent.is(LOOP_KINDS)) {
      return;
    }

    IterationStatementTree loopTree = (IterationStatementTree) parent;
    if (tree.is(Kind.CONTINUE_STATEMENT)
        || (!parent.is(Kind.FOR_IN_STATEMENT) && !canExecuteMoreThanOnce(loopTree))) {
      SyntaxToken keyword = ((JavaScriptTree) tree).getFirstToken();
      addIssue(
          keyword,
          String.format("Remove this \"%s\" statement or make it conditional", keyword.text()));
    }
  }