Skip to content

Commit

Permalink
Make sure to mark const declarations as @const in the type-only summa…
Browse files Browse the repository at this point in the history
…ries

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=185759522
  • Loading branch information
blickly authored and brad4d committed Feb 15, 2018
1 parent 966373b commit 5595b19
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/com/google/javascript/jscomp/ijs/JsdocUtil.java
Expand Up @@ -63,6 +63,12 @@ private static JSDocInfo getConstJSDoc(JSDocInfo oldJSDoc, JSTypeExpression newT
return builder.build(); return builder.build();
} }


static JSDocInfo markConstant(JSDocInfo oldJSDoc) {
JSDocInfoBuilder builder = JSDocInfoBuilder.maybeCopyFrom(oldJSDoc);
builder.recordConstancy();
return builder.build();
}

static JSDocInfo mergeJsdocs(@Nullable JSDocInfo classicJsdoc, @Nullable JSDocInfo inlineJsdoc) { static JSDocInfo mergeJsdocs(@Nullable JSDocInfo classicJsdoc, @Nullable JSDocInfo inlineJsdoc) {
if (inlineJsdoc == null || !inlineJsdoc.hasType()) { if (inlineJsdoc == null || !inlineJsdoc.hasType()) {
return classicJsdoc; return classicJsdoc;
Expand Down
Expand Up @@ -208,6 +208,9 @@ void simplify(AbstractCompiler compiler) {
simplifySymbol(compiler); simplifySymbol(compiler);
return; return;
} }
if (getLhs().getParent().isConst()) {
jsdoc = JsdocUtil.markConstant(jsdoc);
}
// Just completely remove the RHS, and replace with a getprop. // Just completely remove the RHS, and replace with a getprop.
Node newStatement = Node newStatement =
NodeUtil.newQNameDeclaration(compiler, nameNode.getQualifiedName(), null, jsdoc); NodeUtil.newQNameDeclaration(compiler, nameNode.getQualifiedName(), null, jsdoc);
Expand Down
Expand Up @@ -74,8 +74,7 @@ public void testSimpleConstJsdocPropagation() {
warning(ConvertToTypedInterface.CONSTANT_WITHOUT_EXPLICIT_TYPE)); warning(ConvertToTypedInterface.CONSTANT_WITHOUT_EXPLICIT_TYPE));
} }


public void disabled_testConstKeywordWithAnnotatedType() { public void testConstKeywordWithAnnotatedType() {
// TODO(b/73306304): The @const is currently missing here. Fix this!
test("/** @type {number} */ const x = 5;", "/** @const {number} */ var x;"); test("/** @type {number} */ const x = 5;", "/** @const {number} */ var x;");
test("/** @type {!Foo} */ const f = new Foo;", "/** @const {!Foo} */ var f;"); test("/** @type {!Foo} */ const f = new Foo;", "/** @const {!Foo} */ var f;");
} }
Expand Down

0 comments on commit 5595b19

Please sign in to comment.