Skip to content

Commit

Permalink
Allow @desc to be treated as a type annotation at .i.js generation time.
Browse files Browse the repository at this point in the history
This means that the pattern:
    /** @desc desc */
    const MSG_NAME = goog.getMsg('desc');
will be inferred to have string type, rather than requiring an explicit
type annotation.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=171081663
  • Loading branch information
blickly authored and dimvar committed Oct 5, 2017
1 parent 70f9ce6 commit 03617ec
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/com/google/javascript/jscomp/ConvertToTypedInterface.java
Expand Up @@ -365,13 +365,21 @@ JSDocInfo getJsDoc() {
return NodeUtil.getBestJSDocInfo(lhs); return NodeUtil.getBestJSDocInfo(lhs);
} }


/**
* Remove this "potential declaration" completely.
* Usually, this is because the same symbol has already been declared in this file.
*/
void remove(AbstractCompiler compiler) { void remove(AbstractCompiler compiler) {
Node statement = getStatement(); Node statement = getStatement();
NodeUtil.deleteNode(statement, compiler); NodeUtil.deleteNode(statement, compiler);
statement.removeChildren(); statement.removeChildren();
} }


void maybeRemoveRhs(AbstractCompiler compiler) { /**
* Simplify this declaration to only include what's necessary for typing.
* Usually, this means removing the RHS and leaving a type annotation.
*/
void simplify(AbstractCompiler compiler) {
Node nameNode = lhs; Node nameNode = lhs;
JSDocInfo jsdoc = getJsDoc(); JSDocInfo jsdoc = getJsDoc();
if (jsdoc != null && jsdoc.hasEnumParameterType()) { if (jsdoc != null && jsdoc.hasEnumParameterType()) {
Expand Down Expand Up @@ -503,7 +511,7 @@ private void processName(PotentialDeclaration decl) {
} }
break; break;
case REMOVE_RHS: case REMOVE_RHS:
decl.maybeRemoveRhs(compiler); decl.simplify(compiler);
break; break;
case REMOVE_ALL: case REMOVE_ALL:
decl.remove(compiler); decl.remove(compiler);
Expand Down Expand Up @@ -748,6 +756,9 @@ private static class JsdocUtil {
private static JSDocInfo pullJsdocTypeFromAst( private static JSDocInfo pullJsdocTypeFromAst(
AbstractCompiler compiler, JSDocInfo oldJSDoc, Node nameNode) { AbstractCompiler compiler, JSDocInfo oldJSDoc, Node nameNode) {
checkArgument(nameNode.isQualifiedName()); checkArgument(nameNode.isQualifiedName());
if (oldJSDoc != null && oldJSDoc.getDescription() != null) {
return getConstJSDoc(oldJSDoc, "string");
}
if (!nameNode.isFromExterns() && !isPrivate(oldJSDoc)) { if (!nameNode.isFromExterns() && !isPrivate(oldJSDoc)) {
compiler.report(JSError.make(nameNode, CONSTANT_WITHOUT_EXPLICIT_TYPE)); compiler.report(JSError.make(nameNode, CONSTANT_WITHOUT_EXPLICIT_TYPE));
} }
Expand Down
16 changes: 16 additions & 0 deletions test/com/google/javascript/jscomp/ConvertToTypedInterfaceTest.java
Expand Up @@ -1076,4 +1076,20 @@ public void testGoogScopeLeftoversAreRemoved() {
"", "",
"a.b.c.d.e.f.g.Foo = class {};")); "a.b.c.d.e.f.g.Foo = class {};"));
} }

public void testDescAnnotationCountsAsTyped() {
test(
LINE_JOINER.join(
"goog.module('a.b.c');",
"",
"/** @desc Some description */",
"exports.MSG_DESCRIPTION = goog.getMsg('Text');",
""),
LINE_JOINER.join(
"goog.module('a.b.c');",
"",
"/** @const {string} @desc Some description */",
"exports.MSG_DESCRIPTION;",
""));
}
} }

0 comments on commit 03617ec

Please sign in to comment.