com.google.javascript.rhino.JSDocInfo.Visibility Java Examples

The following examples show how to use com.google.javascript.rhino.JSDocInfo.Visibility. 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: Closure_71_CheckAccessControls_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Determines whether the given name is visible in the current context.
 * @param t The current traversal.
 * @param name The name node.
 */
private void checkNameVisibility(NodeTraversal t, Node name, Node parent) {
  Var var = t.getScope().getVar(name.getString());
  if (var != null) {
    JSDocInfo docInfo = var.getJSDocInfo();
    if (docInfo != null) {
      // If a name is private, make sure that we're in the same file.
      Visibility visibility = docInfo.getVisibility();
      if (visibility == Visibility.PRIVATE &&
          !t.getInput().getName().equals(docInfo.getSourceName())) {
        if (docInfo.isConstructor() &&
            isValidPrivateConstructorAccess(parent)) {
          return;
        }

        compiler.report(
            t.makeError(name, BAD_PRIVATE_GLOBAL_ACCESS,
                name.getString(), docInfo.getSourceName()));
      }
    }
  }
}
 
Example #2
Source File: JsDocInfoParserTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testRegression2() throws Exception {
  String comment =
      " * @return {boolean} whatever\n" +
      " * but important\n" +
      " *\n" +
      " * @param {number} index the index of blah\n" +
      " * some more comments here\n" +
      " * @param name the name of the guy\n" +
      " *\n" +
      " * @protected\n" +
      " */";

  JSDocInfo info = parse(comment);
  assertEquals(2, info.getParameterCount());
  assertTypeEquals(NUMBER_TYPE, info.getParameterType("index"));
  assertEquals(null, info.getParameterType("name"));
  assertTypeEquals(BOOLEAN_TYPE, info.getReturnType());
  assertEquals(Visibility.PROTECTED, info.getVisibility());
}
 
Example #3
Source File: JsDocInfoParserTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testRegression3() throws Exception {
  String comment =
      " * @param mediaTag this specified whether the @media tag is ....\n" +
      " *\n" +
      "\n" +
      "@public\n" +
      " *\n" +
      "\n" +
      " **********\n" +
      " * @final\n" +
      " */";

  JSDocInfo info = parse(comment);
  assertEquals(1, info.getParameterCount());
  assertEquals(null, info.getParameterType("mediaTag"));
  assertEquals(Visibility.PUBLIC, info.getVisibility());
  assertTrue(info.isConstant());
}
 
Example #4
Source File: CheckProvides.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void visitFunctionNode(Node n, Node parent) {
  Node name = null;
  JSDocInfo info = parent.getJSDocInfo();
  if (info != null && info.isConstructor()) {
    name = parent.getFirstChild();
  } else {
    // look to the child, maybe it's a named function
    info = n.getJSDocInfo();
    if (info != null && info.isConstructor()) {
      name = n.getFirstChild();
    }
  }
  if (name != null && name.isQualifiedName()) {
    String qualifiedName = name.getQualifiedName();
    if (!this.convention.isPrivate(qualifiedName)) {
      Visibility visibility = info.getVisibility();
      if (!visibility.equals(JSDocInfo.Visibility.PRIVATE)) {
        ctors.put(qualifiedName, name);
      }
    }
  }
}
 
Example #5
Source File: JsDocInfoParserTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testRegression9() throws Exception {
  JSDocInfo jsdoc = parse(
      " * @param {string} p0 blah blah blah\n" +
      " */");

  assertNull(jsdoc.getBaseType());
  assertFalse(jsdoc.isConstant());
  assertNull(jsdoc.getDescription());
  assertNull(jsdoc.getEnumParameterType());
  assertFalse(jsdoc.isHidden());
  assertEquals(1, jsdoc.getParameterCount());
  assertTypeEquals(STRING_TYPE, jsdoc.getParameterType("p0"));
  assertNull(jsdoc.getReturnType());
  assertNull(jsdoc.getType());
  assertEquals(Visibility.INHERITED, jsdoc.getVisibility());
}
 
Example #6
Source File: JsDocInfoParserTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testRegression10() throws Exception {
  JSDocInfo jsdoc = parse(
      " * @param {!String} p0 blah blah blah\n" +
      " * @param {boolean} p1 fobar\n" +
      " * @return {!Date} jksjkash dshad\n" +
      " */");

  assertNull(jsdoc.getBaseType());
  assertFalse(jsdoc.isConstant());
  assertNull(jsdoc.getDescription());
  assertNull(jsdoc.getEnumParameterType());
  assertFalse(jsdoc.isHidden());
  assertEquals(2, jsdoc.getParameterCount());
  assertTypeEquals(STRING_OBJECT_TYPE, jsdoc.getParameterType("p0"));
  assertTypeEquals(BOOLEAN_TYPE, jsdoc.getParameterType("p1"));
  assertTypeEquals(DATE_TYPE, jsdoc.getReturnType());
  assertNull(jsdoc.getType());
  assertEquals(Visibility.INHERITED, jsdoc.getVisibility());
}
 
Example #7
Source File: Closure_71_CheckAccessControls_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Determines whether the given name is visible in the current context.
 * @param t The current traversal.
 * @param name The name node.
 */
private void checkNameVisibility(NodeTraversal t, Node name, Node parent) {
  Var var = t.getScope().getVar(name.getString());
  if (var != null) {
    JSDocInfo docInfo = var.getJSDocInfo();
    if (docInfo != null) {
      // If a name is private, make sure that we're in the same file.
      Visibility visibility = docInfo.getVisibility();
      if (visibility == Visibility.PRIVATE &&
          !t.getInput().getName().equals(docInfo.getSourceName())) {
        if (docInfo.isConstructor() &&
            isValidPrivateConstructorAccess(parent)) {
          return;
        }

        compiler.report(
            t.makeError(name, BAD_PRIVATE_GLOBAL_ACCESS,
                name.getString(), docInfo.getSourceName()));
      }
    }
  }
}
 
Example #8
Source File: JsDocInfoParserTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testRegression12() throws Exception {
  JSDocInfo jsdoc = parse(
      " * @extends FooBar\n" +
      " */");

  assertTypeEquals(registry.createNamedType("FooBar", null, 0, 0),
      jsdoc.getBaseType());
  assertFalse(jsdoc.isConstant());
  assertNull(jsdoc.getDescription());
  assertNull(jsdoc.getEnumParameterType());
  assertFalse(jsdoc.isHidden());
  assertEquals(0, jsdoc.getParameterCount());
  assertNull(jsdoc.getReturnType());
  assertNull(jsdoc.getType());
  assertEquals(Visibility.INHERITED, jsdoc.getVisibility());
}
 
Example #9
Source File: JsDocInfoParserTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testRegression13() throws Exception {
  JSDocInfo jsdoc = parse(
      " * @type {!RegExp}\n" +
      " * @protected\n" +
      " */");

  assertNull(jsdoc.getBaseType());
  assertFalse(jsdoc.isConstant());
  assertNull(jsdoc.getDescription());
  assertNull(jsdoc.getEnumParameterType());
  assertFalse(jsdoc.isHidden());
  assertEquals(0, jsdoc.getParameterCount());
  assertNull(jsdoc.getReturnType());
  assertTypeEquals(REGEXP_TYPE, jsdoc.getType());
  assertEquals(Visibility.PROTECTED, jsdoc.getVisibility());
}
 
Example #10
Source File: JsDocInfoParserTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testRegression14() throws Exception {
  JSDocInfo jsdoc = parse(
      " * @const\n" +
      " * @private\n" +
      " */");

  assertNull(jsdoc.getBaseType());
  assertTrue(jsdoc.isConstant());
  assertNull(jsdoc.getDescription());
  assertNull(jsdoc.getEnumParameterType());
  assertFalse(jsdoc.isHidden());
  assertEquals(0, jsdoc.getParameterCount());
  assertNull(jsdoc.getReturnType());
  assertNull(jsdoc.getType());
  assertEquals(Visibility.PRIVATE, jsdoc.getVisibility());
}
 
Example #11
Source File: JsDocInfoParserTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testRegression15() throws Exception {
  JSDocInfo jsdoc = parse(
      " * @desc Hello,\n" +
      " * World!\n" +
      " */");

  assertNull(jsdoc.getBaseType());
  assertFalse(jsdoc.isConstant());
  assertEquals("Hello, World!", jsdoc.getDescription());
  assertNull(jsdoc.getEnumParameterType());
  assertFalse(jsdoc.isHidden());
  assertEquals(0, jsdoc.getParameterCount());
  assertNull(jsdoc.getReturnType());
  assertNull(jsdoc.getType());
  assertEquals(Visibility.INHERITED, jsdoc.getVisibility());
  assertFalse(jsdoc.isExport());
}
 
Example #12
Source File: FileVisibilityPass.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
@Override
public void process(DossierCompiler compiler, Node root) {
  NodeTraversal.traverse(
      compiler,
      root,
      new NodeTraversal.AbstractShallowCallback() {
        @Override
        public void visit(NodeTraversal t, Node n, Node parent) {
          if (n.isScript()) {
            JsDoc docs = JsDoc.from(n.getJSDocInfo());
            Visibility visibility = docs.getVisibility();
            if (visibility != null && visibility != Visibility.INHERITED) {
              Path path = inputFs.getPath(n.getSourceFileName());
              typeRegistry.setDefaultVisibility(path, visibility);
            }
          }
        }
      });
}
 
Example #13
Source File: GentsCodeGenerator.java    From clutz with MIT License 6 votes vote down vote up
void addVisibility(Node n) {
  Visibility visibility = (Visibility) n.getProp(Node.ACCESS_MODIFIER);
  if (visibility != null) {
    switch (visibility) {
      case PRIVATE:
        add("private ");
        break;
      case PROTECTED:
        add("protected ");
        break;
      case PUBLIC:
        add("public ");
        break;
      default:
        break;
    }
  }
}
 
Example #14
Source File: TypeAnnotationPass.java    From clutz with MIT License 6 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  JSDocInfo bestJSDocInfo = NodeUtil.getBestJSDocInfo(n);
  if (bestJSDocInfo == null) {
    return;
  }

  // Add visibility for private and protected.
  if (Visibility.PRIVATE.equals(bestJSDocInfo.getVisibility())) {
    n.putProp(Node.ACCESS_MODIFIER, Visibility.PRIVATE);
  } else if (Visibility.PROTECTED.equals(bestJSDocInfo.getVisibility())) {
    n.putProp(Node.ACCESS_MODIFIER, Visibility.PROTECTED);
  }

  // Change variable declarations to constants
  if (bestJSDocInfo.isConstant() && (n.isVar() || n.isLet())) {
    n.setToken(Token.CONST);
  }
}
 
Example #15
Source File: TypeConversionPass.java    From clutz with MIT License 5 votes vote down vote up
/** Moves the access modifier from the original declaration to the constructor parameter */
void moveAccessModifier(ClassMemberDeclaration declaration, Node param) {
  if (Visibility.PRIVATE.equals(declaration.jsDoc.getVisibility())) {
    param.putProp(Node.ACCESS_MODIFIER, Visibility.PRIVATE);
  } else if (Visibility.PROTECTED.equals(declaration.jsDoc.getVisibility())) {
    param.putProp(Node.ACCESS_MODIFIER, Visibility.PROTECTED);
  } else {
    param.putProp(Node.ACCESS_MODIFIER, Visibility.PUBLIC);
  }
}
 
Example #16
Source File: JsDocInfoParserTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testRegression16() throws Exception {
  JSDocInfo jsdoc = parse(
      " Email is [email protected]\n" +
      " @type {string}\n" +
      " */");

  assertNull(jsdoc.getBaseType());
  assertFalse(jsdoc.isConstant());
  assertTypeEquals(STRING_TYPE, jsdoc.getType());
  assertFalse(jsdoc.isHidden());
  assertEquals(0, jsdoc.getParameterCount());
  assertNull(jsdoc.getReturnType());
  assertEquals(Visibility.INHERITED, jsdoc.getVisibility());
}
 
Example #17
Source File: JsDocInfoParserTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testRegression11() throws Exception {
  JSDocInfo jsdoc = parse(
      " * @constructor\n" +
      " */");

  assertNull(jsdoc.getBaseType());
  assertFalse(jsdoc.isConstant());
  assertNull(jsdoc.getDescription());
  assertNull(jsdoc.getEnumParameterType());
  assertFalse(jsdoc.isHidden());
  assertEquals(0, jsdoc.getParameterCount());
  assertNull(jsdoc.getReturnType());
  assertNull(jsdoc.getType());
  assertEquals(Visibility.INHERITED, jsdoc.getVisibility());
}
 
Example #18
Source File: TypeRegistry.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
/** Returns the effective visibility for the given type. */
public Visibility getDefaultVisibility(Path path) {
  if (defaultVisibilities.containsKey(path)) {
    return defaultVisibilities.get(path);
  }
  return Visibility.PUBLIC;
}
 
Example #19
Source File: JsDocInfoParserTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testRegression6() throws Exception {
  String comment = "@hidden\n@enum\n@public*/";

  JSDocInfo info = parse(comment);
  assertTrue(info.isHidden());
  assertTypeEquals(NUMBER_TYPE, info.getEnumParameterType());
  assertEquals(Visibility.PUBLIC, info.getVisibility());
}
 
Example #20
Source File: JsDocInfoParserTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testRegression5() throws Exception {
  String comment = "@const\n@enum {string}\n@public*/";

  JSDocInfo info = parse(comment);
  assertTrue(info.isConstant());
  assertFalse(info.isDefine());
  assertTypeEquals(STRING_TYPE, info.getEnumParameterType());
  assertEquals(Visibility.PUBLIC, info.getVisibility());
}
 
Example #21
Source File: TypeRegistry.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
/** Returns the effective visibility for the given type. */
public Visibility getVisibility(NominalType type) {
  JsDoc docs = type.getJsDoc();
  Visibility visibility = docs.getVisibility();

  if (visibility == Visibility.INHERITED
      && defaultVisibilities.containsKey(type.getSourceFile())) {
    visibility = defaultVisibilities.get(type.getSourceFile());
  }

  if (visibility == Visibility.INHERITED) {
    visibility = Visibility.PUBLIC;
  }
  return visibility;
}
 
Example #22
Source File: JsDocInfoParserTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testRegression1() throws Exception {
  String comment =
      " * @param {number} index the index of blah\n" +
      " * @return {boolean} whatever\n" +
      " * @private\n" +
      " */";

  JSDocInfo info = parse(comment);
  assertEquals(1, info.getParameterCount());
  assertTypeEquals(NUMBER_TYPE, info.getParameterType("index"));
  assertTypeEquals(BOOLEAN_TYPE, info.getReturnType());
  assertEquals(Visibility.PRIVATE, info.getVisibility());
}
 
Example #23
Source File: CheckAccessControls.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Determines whether the given name is visible in the current context.
 * @param t The current traversal.
 * @param name The name node.
 */
private void checkNameVisibility(NodeTraversal t, Node name, Node parent) {
  Var var = t.getScope().getVar(name.getString());
  if (var != null) {
    JSDocInfo docInfo = var.getJSDocInfo();
    if (docInfo != null) {
      // If a name is private, make sure that we're in the same file.
      Visibility visibility = docInfo.getVisibility();
      if (visibility == Visibility.PRIVATE) {
        StaticSourceFile varSrc = var.getSourceFile();
        StaticSourceFile refSrc = name.getStaticSourceFile();
        if (varSrc != null &&
            refSrc != null &&
            !varSrc.getName().equals(refSrc.getName())) {
          if (docInfo.isConstructor() &&
              isValidPrivateConstructorAccess(parent)) {
            return;
          }

          compiler.report(
              t.makeError(name, BAD_PRIVATE_GLOBAL_ACCESS,
                  name.getString(), varSrc.getName()));
        }
      }
    }
  }
}
 
Example #24
Source File: JSDocInfoBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Records a visibility.
 *
 * @return {@code true} if the visibility was recorded and {@code false}
 *     if it was already defined
 */
public boolean recordVisibility(Visibility visibility) {
  if (currentInfo.getVisibility() == null) {
    populated = true;
    currentInfo.setVisibility(visibility);
    return true;
  } else {
    return false;
  }
}
 
Example #25
Source File: Closure_106_JSDocInfoBuilder_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Records a visibility.
 *
 * @return {@code true} if the visibility was recorded and {@code false}
 *     if it was already defined
 */
public boolean recordVisibility(Visibility visibility) {
  if (currentInfo.getVisibility() == null) {
    populated = true;
    currentInfo.setVisibility(visibility);
    return true;
  } else {
    return false;
  }
}
 
Example #26
Source File: Closure_106_JSDocInfoBuilder_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Records a visibility.
 *
 * @return {@code true} if the visibility was recorded and {@code false}
 *     if it was already defined
 */
public boolean recordVisibility(Visibility visibility) {
  if (currentInfo.getVisibility() == null) {
    populated = true;
    currentInfo.setVisibility(visibility);
    return true;
  } else {
    return false;
  }
}
 
Example #27
Source File: DeclarationGenerator.java    From clutz with MIT License 5 votes vote down vote up
/**
 * Look up the visibility of the overridden property recursively. In Closure, a child class can
 * override an implicit public property with tighter visibility, but it is not allowed in
 * TypeScript. So clutz ignores and emits it as a public property.
 */
private boolean isProtectedProperty(ObjectType prototype, final String propName) {
  Visibility visibility = Visibility.INHERITED;
  while (prototype != null) {
    if (prototype.hasOwnProperty(propName)) {
      final JSDocInfo jsDocInfo = prototype.getOwnPropertyJSDocInfo(propName);
      if (jsDocInfo != null) {
        visibility = jsDocInfo.getVisibility();
      }
    }
    prototype = prototype.getImplicitPrototype();
  }
  return visibility == Visibility.PROTECTED;
}
 
Example #28
Source File: DeclarationGenerator.java    From clutz with MIT License 5 votes vote down vote up
private boolean isPrivate(@Nullable JSDocInfo docInfo) {
  if (docInfo == null) {
    return false;
  }
  // Closure Compiler has @package visibility, which makes symbols accessible to code in the same
  // package. ES6 modules do not have this concept at all, as there are no packages to begin with.
  // As TypeScript code can never be in the same package as JavaScript code, because TS code is in
  // no package at all, Clutz considers @package visible fields to be private.
  return docInfo.getVisibility() == Visibility.PRIVATE
      || docInfo.getVisibility() == Visibility.PACKAGE;
}
 
Example #29
Source File: MemberCollector.java    From jsinterop-generator with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean visitConstructor(FunctionType constructor) {
  Method constructorMethod = Method.newConstructor();
  if (constructor.getJSDocInfo().getVisibility() == Visibility.PRIVATE) {
    constructorMethod.setAccessModifier(AccessModifier.PRIVATE);
  }

  getCurrentJavaType().addMethod(constructorMethod);

  currentJavaMethodDeque.push(constructorMethod);
  return true;
}
 
Example #30
Source File: CheckAccessControls.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Determines whether the given property is visible in the current context.
 * @param t The current traversal.
 * @param getprop The getprop node.
 */
private void checkPropertyVisibility(NodeTraversal t,
    Node getprop, Node parent) {
  ObjectType objectType =
      ObjectType.cast(dereference(getprop.getFirstChild().getJSType()));
  String propertyName = getprop.getLastChild().getString();

  if (objectType != null) {
    // Is this a normal property access, or are we trying to override
    // an existing property?
    boolean isOverride = parent.getJSDocInfo() != null &&
        parent.isAssign() &&
        parent.getFirstChild() == getprop;

    // Find the lowest property defined on a class with visibility
    // information.
    if (isOverride) {
      objectType = objectType.getImplicitPrototype();
    }
    JSDocInfo docInfo = null;
    for (; objectType != null;
         objectType = objectType.getImplicitPrototype()) {
      docInfo = objectType.getOwnPropertyJSDocInfo(propertyName);
      if (docInfo != null &&
          docInfo.getVisibility() != Visibility.INHERITED) {
        break;
      }
    }

    if (objectType == null) {
      // We couldn't find a visibility modifier; assume it's public.
      return;
    }

    String referenceSource = getprop.getSourceFileName();
    String definingSource = docInfo.getSourceName();
    boolean sameInput = referenceSource != null
        && referenceSource.equals(definingSource);
    Visibility visibility = docInfo.getVisibility();
    JSType ownerType = normalizeClassType(objectType);
    if (isOverride) {
      // Check an ASSIGN statement that's trying to override a property
      // on a superclass.
      JSDocInfo overridingInfo = parent.getJSDocInfo();
      Visibility overridingVisibility = overridingInfo == null ?
          Visibility.INHERITED : overridingInfo.getVisibility();

      // Check that (a) the property *can* be overridden, and
      // (b) that the visibility of the override is the same as the
      // visibility of the original property.
      if (visibility == Visibility.PRIVATE && !sameInput) {
        compiler.report(
            t.makeError(getprop, PRIVATE_OVERRIDE,
                objectType.toString()));
      } else if (overridingVisibility != Visibility.INHERITED &&
          overridingVisibility != visibility) {
        compiler.report(
            t.makeError(getprop, VISIBILITY_MISMATCH,
                visibility.name(), objectType.toString(),
                overridingVisibility.name()));
      }
    } else {
      if (sameInput) {
        // private access is always allowed in the same file.
        return;
      } else if (visibility == Visibility.PRIVATE &&
          (currentClass == null || !ownerType.isEquivalentTo(currentClass))) {
        if (docInfo.isConstructor() &&
            isValidPrivateConstructorAccess(parent)) {
          return;
        }

        // private access is not allowed outside the file from a different
        // enclosing class.
        compiler.report(
            t.makeError(getprop,
                BAD_PRIVATE_PROPERTY_ACCESS,
                propertyName,
                validator.getReadableJSTypeName(
                    getprop.getFirstChild(), true)));
      } else if (visibility == Visibility.PROTECTED) {
        // There are 3 types of legal accesses of a protected property:
        // 1) Accesses in the same file
        // 2) Overriding the property in a subclass
        // 3) Accessing the property from inside a subclass
        // The first two have already been checked for.
        if (currentClass == null || !currentClass.isSubtype(ownerType)) {
          compiler.report(
              t.makeError(getprop,  BAD_PROTECTED_PROPERTY_ACCESS,
                  propertyName,
                  validator.getReadableJSTypeName(
                      getprop.getFirstChild(), true)));
        }
      }
    }
  }
}