Java Code Examples for com.google.javascript.rhino.Node#addSuppression()

The following examples show how to use com.google.javascript.rhino.Node#addSuppression() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: 1_VarCheck.java    From SimFix with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.getType() != Token.NAME) {
    return;
  }

  String varName = n.getString();

  // Only a function can have an empty name.
  if (varName.isEmpty()) {
    Preconditions.checkState(NodeUtil.isFunction(parent));

    // A function declaration with an empty name passes Rhino,
    // but is supposed to be a syntax error according to the spec.
    if (!NodeUtil.isFunctionExpression(parent)) {
      t.report(n, INVALID_FUNCTION_DECL);
    }
    return;
  }

  // Check if this is a declaration for a var that has been declared
  // elsewhere. If so, mark it as a duplicate.
  if ((parent.getType() == Token.VAR ||
       NodeUtil.isFunctionDeclaration(parent)) &&
      varsToDeclareInExterns.contains(varName)) {
    createSynthesizedExternVar(varName);

    n.addSuppression("duplicate");
  }

  // Check that the var has been declared.
  Scope scope = t.getScope();
  Scope.Var var = scope.getVar(varName);
  if (var == null) {
    if (NodeUtil.isFunctionExpression(parent)) {
      // e.g. [ function foo() {} ], it's okay if "foo" isn't defined in the
      // current scope.
    } else {
      // The extern checks are stricter, don't report a second error.
      if (!strictExternCheck || !t.getInput().isExtern()) {
        t.report(n, UNDEFINED_VAR_ERROR, varName);
      }

      if (sanityCheck) {
        throw new IllegalStateException("Unexpected variable " + varName);
      } else {
        createSynthesizedExternVar(varName);
        scope.getGlobalScope().declare(varName, n,
            null, getSynthesizedExternsInput());
      }
    }
    return;
  }

  CompilerInput currInput = t.getInput();
  CompilerInput varInput = var.input;
  if (currInput == varInput || currInput == null || varInput == null) {
    // The variable was defined in the same file. This is fine.
    return;
  }

  // Check module dependencies.
  JSModule currModule = currInput.getModule();
  JSModule varModule = varInput.getModule();
  JSModuleGraph moduleGraph = compiler.getModuleGraph();
  if (varModule != currModule && varModule != null && currModule != null) {
    if (moduleGraph.dependsOn(currModule, varModule)) {
      // The module dependency was properly declared.
    } else {
      if (!sanityCheck && scope.isGlobal()) {
        if (moduleGraph.dependsOn(varModule, currModule)) {
          // The variable reference violates a declared module dependency.
          t.report(n, VIOLATED_MODULE_DEP_ERROR,
                   currModule.getName(), varModule.getName(), varName);
        } else {
          // The variable reference is between two modules that have no
          // dependency relationship. This should probably be considered an
          // error, but just issue a warning for now.
          t.report(n, MISSING_MODULE_DEP_ERROR,
                   currModule.getName(), varModule.getName(), varName);
        }
      } else {
        t.report(n, STRICT_MODULE_DEP_ERROR,
                 currModule.getName(), varModule.getName(), varName);
      }
    }
  }
}
 
Example 2
Source File: 1_VarCheck.java    From SimFix with GNU General Public License v2.0 4 votes vote down vote up
@Override
  public void visit(NodeTraversal t, Node n, Node parent) {
    if (n.getType() != Token.NAME) {
      return;
    }

    String varName = n.getString();

    // Only a function can have an empty name.
    if (varName.isEmpty()) {
      Preconditions.checkState(NodeUtil.isFunction(parent));

      // A function declaration with an empty name passes Rhino,
      // but is supposed to be a syntax error according to the spec.
      if (!NodeUtil.isFunctionExpression(parent)) {
        t.report(n, INVALID_FUNCTION_DECL);
      }
      return;
    }

    // Check if this is a declaration for a var that has been declared
    // elsewhere. If so, mark it as a duplicate.
// start of generated patch
if((parent.getType()==Token.VAR||NodeUtil.isFunctionDeclaration(parent))&&varsToDeclareInExterns.contains(varName)){
compiler.reportCodeChange();
createSynthesizedExternVar(varName);
n.addSuppression("duplicate");
}
// end of generated patch
/* start of original code
    if ((parent.getType() == Token.VAR ||
         NodeUtil.isFunctionDeclaration(parent)) &&
        varsToDeclareInExterns.contains(varName)) {
      createSynthesizedExternVar(varName);

      n.addSuppression("duplicate");
    }
 end of original code*/

    // Check that the var has been declared.
    Scope scope = t.getScope();
    Scope.Var var = scope.getVar(varName);
    if (var == null) {
      if (NodeUtil.isFunctionExpression(parent)) {
        // e.g. [ function foo() {} ], it's okay if "foo" isn't defined in the
        // current scope.
      } else {
        // The extern checks are stricter, don't report a second error.
        if (!strictExternCheck || !t.getInput().isExtern()) {
          t.report(n, UNDEFINED_VAR_ERROR, varName);
        }

        if (sanityCheck) {
          throw new IllegalStateException("Unexpected variable " + varName);
        } else {
          createSynthesizedExternVar(varName);
          scope.getGlobalScope().declare(varName, n,
              null, getSynthesizedExternsInput());
        }
      }
      return;
    }

    CompilerInput currInput = t.getInput();
    CompilerInput varInput = var.input;
    if (currInput == varInput || currInput == null || varInput == null) {
      // The variable was defined in the same file. This is fine.
      return;
    }

    // Check module dependencies.
    JSModule currModule = currInput.getModule();
    JSModule varModule = varInput.getModule();
    JSModuleGraph moduleGraph = compiler.getModuleGraph();
    if (varModule != currModule && varModule != null && currModule != null) {
      if (moduleGraph.dependsOn(currModule, varModule)) {
        // The module dependency was properly declared.
      } else {
        if (!sanityCheck && scope.isGlobal()) {
          if (moduleGraph.dependsOn(varModule, currModule)) {
            // The variable reference violates a declared module dependency.
            t.report(n, VIOLATED_MODULE_DEP_ERROR,
                     currModule.getName(), varModule.getName(), varName);
          } else {
            // The variable reference is between two modules that have no
            // dependency relationship. This should probably be considered an
            // error, but just issue a warning for now.
            t.report(n, MISSING_MODULE_DEP_ERROR,
                     currModule.getName(), varModule.getName(), varName);
          }
        } else {
          t.report(n, STRICT_MODULE_DEP_ERROR,
                   currModule.getName(), varModule.getName(), varName);
        }
      }
    }
  }
 
Example 3
Source File: Closure_79_VarCheck_t.java    From coming with MIT License 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.getType() != Token.NAME) {
    return;
  }

  String varName = n.getString();

  // Only a function can have an empty name.
  if (varName.isEmpty()) {
    Preconditions.checkState(NodeUtil.isFunction(parent));

    // A function declaration with an empty name passes Rhino,
    // but is supposed to be a syntax error according to the spec.
    if (!NodeUtil.isFunctionExpression(parent)) {
      t.report(n, INVALID_FUNCTION_DECL);
    }
    return;
  }

  // Check if this is a declaration for a var that has been declared
  // elsewhere. If so, mark it as a duplicate.
  if ((parent.getType() == Token.VAR ||
       NodeUtil.isFunctionDeclaration(parent)) &&
      varsToDeclareInExterns.contains(varName)) {
    createSynthesizedExternVar(varName);

    n.addSuppression("duplicate");
  }

  // Check that the var has been declared.
  Scope scope = t.getScope();
  Scope.Var var = scope.getVar(varName);
  if (var == null) {
    if (NodeUtil.isFunctionExpression(parent)) {
      // e.g. [ function foo() {} ], it's okay if "foo" isn't defined in the
      // current scope.
    } else {
      // The extern checks are stricter, don't report a second error.
      if (!strictExternCheck || !t.getInput().isExtern()) {
        t.report(n, UNDEFINED_VAR_ERROR, varName);
      }

      if (sanityCheck) {
        throw new IllegalStateException("Unexpected variable " + varName);
      } else {
        createSynthesizedExternVar(varName);
        scope.getGlobalScope().declare(varName, n,
            null, getSynthesizedExternsInput());
      }
    }
    return;
  }

  CompilerInput currInput = t.getInput();
  CompilerInput varInput = var.input;
  if (currInput == varInput || currInput == null || varInput == null) {
    // The variable was defined in the same file. This is fine.
    return;
  }

  // Check module dependencies.
  JSModule currModule = currInput.getModule();
  JSModule varModule = varInput.getModule();
  JSModuleGraph moduleGraph = compiler.getModuleGraph();
  if (varModule != currModule && varModule != null && currModule != null) {
    if (moduleGraph.dependsOn(currModule, varModule)) {
      // The module dependency was properly declared.
    } else {
      if (!sanityCheck && scope.isGlobal()) {
        if (moduleGraph.dependsOn(varModule, currModule)) {
          // The variable reference violates a declared module dependency.
          t.report(n, VIOLATED_MODULE_DEP_ERROR,
                   currModule.getName(), varModule.getName(), varName);
        } else {
          // The variable reference is between two modules that have no
          // dependency relationship. This should probably be considered an
          // error, but just issue a warning for now.
          t.report(n, MISSING_MODULE_DEP_ERROR,
                   currModule.getName(), varModule.getName(), varName);
        }
      } else {
        t.report(n, STRICT_MODULE_DEP_ERROR,
                 currModule.getName(), varModule.getName(), varName);
      }
    }
  }
}
 
Example 4
Source File: Closure_79_VarCheck_s.java    From coming with MIT License 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (n.getType() != Token.NAME) {
    return;
  }

  String varName = n.getString();

  // Only a function can have an empty name.
  if (varName.isEmpty()) {
    Preconditions.checkState(NodeUtil.isFunction(parent));

    // A function declaration with an empty name passes Rhino,
    // but is supposed to be a syntax error according to the spec.
    if (!NodeUtil.isFunctionExpression(parent)) {
      t.report(n, INVALID_FUNCTION_DECL);
    }
    return;
  }

  // Check if this is a declaration for a var that has been declared
  // elsewhere. If so, mark it as a duplicate.
  if ((parent.getType() == Token.VAR ||
       NodeUtil.isFunctionDeclaration(parent)) &&
      varsToDeclareInExterns.contains(varName)) {
    createSynthesizedExternVar(varName);

    n.addSuppression("duplicate");
  }

  // Check that the var has been declared.
  Scope scope = t.getScope();
  Scope.Var var = scope.getVar(varName);
  if (var == null) {
    if (NodeUtil.isFunctionExpression(parent)) {
      // e.g. [ function foo() {} ], it's okay if "foo" isn't defined in the
      // current scope.
    } else {
      // The extern checks are stricter, don't report a second error.
      if (!strictExternCheck || !t.getInput().isExtern()) {
        t.report(n, UNDEFINED_VAR_ERROR, varName);
      }

      if (sanityCheck) {
        throw new IllegalStateException("Unexpected variable " + varName);
      } else {
        createSynthesizedExternVar(varName);
        scope.getGlobalScope().declare(varName, n,
            null, getSynthesizedExternsInput());
      }
    }
    return;
  }

  CompilerInput currInput = t.getInput();
  CompilerInput varInput = var.input;
  if (currInput == varInput || currInput == null || varInput == null) {
    // The variable was defined in the same file. This is fine.
    return;
  }

  // Check module dependencies.
  JSModule currModule = currInput.getModule();
  JSModule varModule = varInput.getModule();
  JSModuleGraph moduleGraph = compiler.getModuleGraph();
  if (varModule != currModule && varModule != null && currModule != null) {
    if (moduleGraph.dependsOn(currModule, varModule)) {
      // The module dependency was properly declared.
    } else {
      if (!sanityCheck && scope.isGlobal()) {
        if (moduleGraph.dependsOn(varModule, currModule)) {
          // The variable reference violates a declared module dependency.
          t.report(n, VIOLATED_MODULE_DEP_ERROR,
                   currModule.getName(), varModule.getName(), varName);
        } else {
          // The variable reference is between two modules that have no
          // dependency relationship. This should probably be considered an
          // error, but just issue a warning for now.
          t.report(n, MISSING_MODULE_DEP_ERROR,
                   currModule.getName(), varModule.getName(), varName);
        }
      } else {
        t.report(n, STRICT_MODULE_DEP_ERROR,
                 currModule.getName(), varModule.getName(), varName);
      }
    }
  }
}
 
Example 5
Source File: VarCheck.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  if (!n.isName()) {
    return;
  }

  String varName = n.getString();

  // Only a function can have an empty name.
  if (varName.isEmpty()) {
    Preconditions.checkState(parent.isFunction());
    Preconditions.checkState(NodeUtil.isFunctionExpression(parent));
    return;
  }

  // Check if this is a declaration for a var that has been declared
  // elsewhere. If so, mark it as a duplicate.
  if ((parent.isVar() ||
       NodeUtil.isFunctionDeclaration(parent)) &&
      varsToDeclareInExterns.contains(varName)) {
    createSynthesizedExternVar(varName);

    n.addSuppression("duplicate");
  }

  // Check that the var has been declared.
  Scope scope = t.getScope();
  Scope.Var var = scope.getVar(varName);
  if (var == null) {
    if (NodeUtil.isFunctionExpression(parent)) {
      // e.g. [ function foo() {} ], it's okay if "foo" isn't defined in the
      // current scope.
    } else {
      // The extern checks are stricter, don't report a second error.
      if (!strictExternCheck || !t.getInput().isExtern()) {
        t.report(n, UNDEFINED_VAR_ERROR, varName);
      }

      if (sanityCheck) {
        throw new IllegalStateException("Unexpected variable " + varName);
      } else {
        createSynthesizedExternVar(varName);
        scope.getGlobalScope().declare(varName, n,
            null, getSynthesizedExternsInput());
      }
    }
    return;
  }

  CompilerInput currInput = t.getInput();
  CompilerInput varInput = var.input;
  if (currInput == varInput || currInput == null || varInput == null) {
    // The variable was defined in the same file. This is fine.
    return;
  }

  // Check module dependencies.
  JSModule currModule = currInput.getModule();
  JSModule varModule = varInput.getModule();
  JSModuleGraph moduleGraph = compiler.getModuleGraph();
  if (!sanityCheck &&
      varModule != currModule && varModule != null && currModule != null) {
    if (moduleGraph.dependsOn(currModule, varModule)) {
      // The module dependency was properly declared.
    } else {
      if (scope.isGlobal()) {
        if (moduleGraph.dependsOn(varModule, currModule)) {
          // The variable reference violates a declared module dependency.
          t.report(n, VIOLATED_MODULE_DEP_ERROR,
                   currModule.getName(), varModule.getName(), varName);
        } else {
          // The variable reference is between two modules that have no
          // dependency relationship. This should probably be considered an
          // error, but just issue a warning for now.
          t.report(n, MISSING_MODULE_DEP_ERROR,
                   currModule.getName(), varModule.getName(), varName);
        }
      } else {
        t.report(n, STRICT_MODULE_DEP_ERROR,
                 currModule.getName(), varModule.getName(), varName);
      }
    }
  }
}