From 11f3f724caea33b1f0f6da6bf4c308335b567190 Mon Sep 17 00:00:00 2001 From: Jan Kuehle Date: Fri, 18 Aug 2023 01:18:11 -0700 Subject: [PATCH] Add "foo might be a value, not a type" to JSC_UNRECOGNIZED_TYPE_ERROR message PiperOrigin-RevId: 558071978 --- .../javascript/rhino/jstype/NamedType.java | 2 + .../jscomp/TypeCheckNoTranspileTest.java | 29 ++++++++-- .../javascript/jscomp/TypeCheckTest.java | 55 ++++++++++++++++--- 3 files changed, 71 insertions(+), 15 deletions(-) diff --git a/src/com/google/javascript/rhino/jstype/NamedType.java b/src/com/google/javascript/rhino/jstype/NamedType.java index ffafee5f704..1b885168da8 100644 --- a/src/com/google/javascript/rhino/jstype/NamedType.java +++ b/src/com/google/javascript/rhino/jstype/NamedType.java @@ -401,6 +401,8 @@ private void handleUnresolvedType(ErrorReporter reporter) { if (localVariableShadowsGlobalNamespace(root)) { msg += "\nIt's possible that a local variable called '" + root + "' is shadowing the intended global namespace."; + } else if (resolutionScope.getSlot(root) != null) { + msg += "\nIt's possible that '" + reference + "' refers to a value, not a type."; } warning(reporter, msg); } else { diff --git a/test/com/google/javascript/jscomp/TypeCheckNoTranspileTest.java b/test/com/google/javascript/jscomp/TypeCheckNoTranspileTest.java index 6fd61f97348..2efc711c404 100644 --- a/test/com/google/javascript/jscomp/TypeCheckNoTranspileTest.java +++ b/test/com/google/javascript/jscomp/TypeCheckNoTranspileTest.java @@ -922,7 +922,9 @@ public void testTypedefDestructuredAlias_deeplyNested() { .addDiagnostic( // TODO(sdh): Should parse correctly and give an initializing variable // error. // It looks like this is a result of the `const` being ignored. - "Bad type annotation. Unknown type alias.ns.MyNumber") + lines( + "Bad type annotation. Unknown type alias.ns.MyNumber", + "It's possible that 'alias.ns.MyNumber' refers to a value, not a type.")) .run(); } @@ -1042,7 +1044,10 @@ public void testTypedefOnClassSideInheritedSubtypeInaccessible() { "class Sub extends Base {}", "/** @type {Sub.MyNumber} */ let x;", "") - .addDiagnostic("Bad type annotation. Unknown type Sub.MyNumber") + .addDiagnostic( + lines( + "Bad type annotation. Unknown type Sub.MyNumber", + "It's possible that 'Sub.MyNumber' refers to a value, not a type.")) .run(); } @@ -8273,7 +8278,10 @@ public void testMethodWithAtConstructorDoesNotDeclareType_staticClassMethod() { "", "var /** !Foo.Bar */ x;", "") - .addDiagnostic("Bad type annotation. Unknown type Foo.Bar") + .addDiagnostic( + lines( + "Bad type annotation. Unknown type Foo.Bar", + "It's possible that 'Foo.Bar' refers to a value, not a type.")) .run(); } @@ -8288,7 +8296,10 @@ public void testMethodWithAtConstructorDoesNotDeclareType_namespaceMemberMethod( "", "var /** !ns.Bar */ x;", "") - .addDiagnostic("Bad type annotation. Unknown type ns.Bar") + .addDiagnostic( + lines( + "Bad type annotation. Unknown type ns.Bar", + "It's possible that 'ns.Bar' refers to a value, not a type.")) .run(); } @@ -8303,7 +8314,10 @@ public void testMethodWithAtInterfaceDoesNotDeclareType() { "", "var /** !Foo.Bar */ x;", "") - .addDiagnostic("Bad type annotation. Unknown type Foo.Bar") + .addDiagnostic( + lines( + "Bad type annotation. Unknown type Foo.Bar", + "It's possible that 'Foo.Bar' refers to a value, not a type.")) .run(); } @@ -8318,7 +8332,10 @@ public void testMethodWithAtRecordDoesNotDeclareType() { "", "var /** !Foo.Bar */ x;", "") - .addDiagnostic("Bad type annotation. Unknown type Foo.Bar") + .addDiagnostic( + lines( + "Bad type annotation. Unknown type Foo.Bar", + "It's possible that 'Foo.Bar' refers to a value, not a type.")) .run(); } diff --git a/test/com/google/javascript/jscomp/TypeCheckTest.java b/test/com/google/javascript/jscomp/TypeCheckTest.java index e453170a697..254a5681d80 100644 --- a/test/com/google/javascript/jscomp/TypeCheckTest.java +++ b/test/com/google/javascript/jscomp/TypeCheckTest.java @@ -9185,7 +9185,10 @@ public void testAliasedNonTypedef() { "/** @type {string} */ var notTypeName;", "/** @const */ var alias = notTypeName;", "/** @type {alias} */ var x;") - .addDiagnostic("Bad type annotation. Unknown type alias") + .addDiagnostic( + lines( + "Bad type annotation. Unknown type alias", + "It's possible that 'alias' refers to a value, not a type.")) .run(); } @@ -12357,7 +12360,10 @@ public void testCast8() { public void testCast9() { newTest() .addSource("var foo = {};" + "function f() { return /** @type {foo} */ (new Object()); }") - .addDiagnostic("Bad type annotation. Unknown type foo") + .addDiagnostic( + lines( + "Bad type annotation. Unknown type foo", + "It's possible that 'foo' refers to a value, not a type.")) .run(); } @@ -12367,7 +12373,10 @@ public void testCast10() { .addSource( "var foo = function() {};" + "function f() { return /** @type {foo} */ (new Object()); }") - .addDiagnostic("Bad type annotation. Unknown type foo") + .addDiagnostic( + lines( + "Bad type annotation. Unknown type foo", + "It's possible that 'foo' refers to a value, not a type.")) .run(); } @@ -12377,7 +12386,10 @@ public void testCast11() { .addSource( "var goog = {}; goog.foo = {};" + "function f() { return /** @type {goog.foo} */ (new Object()); }") - .addDiagnostic("Bad type annotation. Unknown type goog.foo") + .addDiagnostic( + lines( + "Bad type annotation. Unknown type goog.foo", + "It's possible that 'goog.foo' refers to a value, not a type.")) .run(); } @@ -12387,7 +12399,10 @@ public void testCast12() { .addSource( "var goog = {}; goog.foo = function() {};" + "function f() { return /** @type {goog.foo} */ (new Object()); }") - .addDiagnostic("Bad type annotation. Unknown type goog.foo") + .addDiagnostic( + lines( + "Bad type annotation. Unknown type goog.foo", + "It's possible that 'goog.foo' refers to a value, not a type.")) .run(); } @@ -13850,7 +13865,10 @@ public void testInheritanceCheck13() { "var goog = {};\n" + "/** @constructor\n @extends {goog.Missing} */function Sub() {};" + "/** @override */Sub.prototype.foo = function() {};") - .addDiagnostic("Bad type annotation. Unknown type goog.Missing") + .addDiagnostic( + lines( + "Bad type annotation. Unknown type goog.Missing", + "It's possible that 'goog.Missing' refers to a value, not a type.")) .run(); } @@ -13862,7 +13880,9 @@ public void testInheritanceCheck14() { "goog.Super = function() {};", "/** @constructor\n @extends {goog.Super} */function Sub() {};", "/** @override */ Sub.prototype.foo = function() {};"), - "Bad type annotation. Unknown type goog.Missing"); + lines( + "Bad type annotation. Unknown type goog.Missing", + "It's possible that 'goog.Missing' refers to a value, not a type.")); } @Test @@ -21534,7 +21554,10 @@ public void testMixinApplication3() { MIXIN_DEFINITIONS, "var MyElementWithToggle = addToggle(MyElement);", "/** @type {MyElementWithToggle} */ var x = 123;") - .addDiagnostic("Bad type annotation. Unknown type MyElementWithToggle") + .addDiagnostic( + lines( + "Bad type annotation. Unknown type MyElementWithToggle", + "It's possible that 'MyElementWithToggle' refers to a value, not a type.")) .run(); } @@ -22514,7 +22537,10 @@ public void testQuotedPropertyWithDotInType() { " 'y.A': function() {},", "};", "var /** x.y.A */ a;") - .addDiagnostic("Bad type annotation. Unknown type x.y.A") + .addDiagnostic( + lines( + "Bad type annotation. Unknown type x.y.A", + "It's possible that 'x.y.A' refers to a value, not a type.")) .run(); } @@ -23050,6 +23076,17 @@ public void testParameterShadowsNamespace() { .run(); } + @Test + public void testValueUsedAsType() { + newTest() + .addSource("/** @type {?} */ var Foo = 'Foo';", "/** @type {!Foo} */ var foo = 'foo';") + .addDiagnostic( + lines( + "Bad type annotation. Unknown type Foo", + "It's possible that 'Foo' refers to a value, not a type.")) + .run(); + } + @Test public void testBangOperatorOnForwardReferencedType() { newTest()