Skip to content

Commit

Permalink
Add "foo might be a value, not a type" to JSC_UNRECOGNIZED_TYPE_ERROR…
Browse files Browse the repository at this point in the history
… message

PiperOrigin-RevId: 558071978
  • Loading branch information
frigus02 authored and copybara-github committed Aug 18, 2023
1 parent 457f3a8 commit 11f3f72
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/com/google/javascript/rhino/jstype/NamedType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 23 additions & 6 deletions test/com/google/javascript/jscomp/TypeCheckNoTranspileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand All @@ -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();
}

Expand All @@ -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();
}

Expand All @@ -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();
}

Expand Down
55 changes: 46 additions & 9 deletions test/com/google/javascript/jscomp/TypeCheckTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand All @@ -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();
}

Expand All @@ -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();
}

Expand All @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand All @@ -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
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 11f3f72

Please sign in to comment.