Skip to content

Commit 73cf670

Browse files
rahul-kamatcopybara-github
authored andcommitted
Improve JSC_NOT_A_CONSTRUCTOR error message by printing the found type
PiperOrigin-RevId: 563176327
1 parent d04cb02 commit 73cf670

File tree

4 files changed

+32
-15
lines changed

4 files changed

+32
-15
lines changed

src/com/google/javascript/jscomp/TypeCheck.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ public final class TypeCheck implements NodeTraversal.Callback, CompilerPass {
142142
"Property {0} never defined on {1}. Did you mean {2}?");
143143

144144
static final DiagnosticType NOT_A_CONSTRUCTOR =
145-
DiagnosticType.warning("JSC_NOT_A_CONSTRUCTOR", "cannot instantiate non-constructor");
145+
DiagnosticType.warning(
146+
"JSC_NOT_A_CONSTRUCTOR", "cannot instantiate non-constructor, found type: {0}");
146147

147148
static final DiagnosticType INSTANTIATE_ABSTRACT_CLASS =
148149
DiagnosticType.warning("JSC_INSTANTIATE_ABSTRACT_CLASS", "cannot instantiate abstract class");
@@ -2410,7 +2411,7 @@ private void visitNew(Node n) {
24102411
if (!couldBeAConstructor(type)
24112412
|| type.equals(typeRegistry.getNativeType(SYMBOL_OBJECT_FUNCTION_TYPE))
24122413
|| type.equals(typeRegistry.getNativeType(BIGINT_OBJECT_FUNCTION_TYPE))) {
2413-
report(n, NOT_A_CONSTRUCTOR);
2414+
report(n, NOT_A_CONSTRUCTOR, type.toString());
24142415
ensureTyped(n);
24152416
return;
24162417
}

test/com/google/javascript/jscomp/TypeCheckBigIntTest.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,12 @@ public void testBigIntArrayIndex() {
482482
@Test
483483
public void testBigIntConstructorWithNew() {
484484
// BigInt object function type cannot be called with "new" keyword
485-
newTest().addSource("new BigInt(1)").addDiagnostic("cannot instantiate non-constructor").run();
485+
newTest()
486+
.addSource("new BigInt(1)")
487+
.addDiagnostic(
488+
"cannot instantiate non-constructor, found type: function(new:BigInt,"
489+
+ " (bigint|number|string)): bigint")
490+
.run();
486491
}
487492

488493
@Test

test/com/google/javascript/jscomp/TypeCheckNoTranspileTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1978,7 +1978,8 @@ public void testGenerator_notAConstructor() {
19781978
" yield 1;",
19791979
"}",
19801980
"var g = new gen;")
1981-
.addDiagnostic("cannot instantiate non-constructor")
1981+
.addDiagnostic(
1982+
"cannot instantiate non-constructor, found type: function(): Generator<number,?,?>")
19821983
.run();
19831984
}
19841985

@@ -2147,7 +2148,7 @@ public void testMemberFunctionDef2() {
21472148
public void testMemberFunctionDef3() {
21482149
newTest()
21492150
.addSource("var obj = { method() {} }; new obj.method();")
2150-
.addDiagnostic("cannot instantiate non-constructor")
2151+
.addDiagnostic("cannot instantiate non-constructor, found type: function(): undefined")
21512152
.run();
21522153
}
21532154

test/com/google/javascript/jscomp/TypeCheckTest.java

+20-10
Original file line numberDiff line numberDiff line change
@@ -8921,15 +8921,18 @@ public void testHigherOrderFunctions2() {
89218921
public void testHigherOrderFunctions3() {
89228922
newTest()
89238923
.addSource("/** @type {function(this:Array):Date} */var f; new f")
8924-
.addDiagnostic("cannot instantiate non-constructor")
8924+
.addDiagnostic(
8925+
"cannot instantiate non-constructor, found type: function(this:Array): (Date|null)")
89258926
.run();
89268927
}
89278928

89288929
@Test
89298930
public void testHigherOrderFunctions4() {
89308931
newTest()
89318932
.addSource("/** @type {function(this:Array, ...number):Date} */var f; new f")
8932-
.addDiagnostic("cannot instantiate non-constructor")
8933+
.addDiagnostic(
8934+
"cannot instantiate non-constructor, found type: function(this:Array, ...number):"
8935+
+ " (Date|null)")
89338936
.run();
89348937
}
89358938

@@ -11535,14 +11538,21 @@ public void testAssignToUntypedProperty() {
1153511538

1153611539
@Test
1153711540
public void testNew1() {
11538-
newTest().addSource("new 4").addDiagnostic(TypeCheck.NOT_A_CONSTRUCTOR).run();
11541+
newTest()
11542+
.addSource("new 4")
11543+
.addDiagnostic("cannot instantiate non-constructor, found type: number")
11544+
.run();
11545+
newTest()
11546+
.addSource("new 4")
11547+
.addDiagnostic("cannot instantiate non-constructor, found type: number")
11548+
.run();
1153911549
}
1154011550

1154111551
@Test
1154211552
public void testNew2() {
1154311553
newTest()
1154411554
.addSource("var Math = {}; new Math()")
11545-
.addDiagnostic(TypeCheck.NOT_A_CONSTRUCTOR)
11555+
.addDiagnostic("cannot instantiate non-constructor, found type: {}")
1154611556
.run();
1154711557
}
1154811558

@@ -11560,7 +11570,7 @@ public void testNew4() {
1156011570
public void testNew5() {
1156111571
newTest()
1156211572
.addSource("function A(){}; new A();")
11563-
.addDiagnostic(TypeCheck.NOT_A_CONSTRUCTOR)
11573+
.addDiagnostic("cannot instantiate non-constructor, found type: function(): undefined")
1156411574
.run();
1156511575
}
1156611576

@@ -11629,7 +11639,7 @@ public void testNew11() {
1162911639
+ " var c2 = function(){};"
1163011640
+ " c1.prototype = new c2;"
1163111641
+ "}")
11632-
.addDiagnostic(TypeCheck.NOT_A_CONSTRUCTOR)
11642+
.addDiagnostic("cannot instantiate non-constructor, found type: function(): undefined")
1163311643
.run();
1163411644
}
1163511645

@@ -11697,7 +11707,7 @@ public void testNew16() {
1169711707
public void testNew17() {
1169811708
newTest()
1169911709
.addSource("var goog = {}; goog.x = 3; new goog.x")
11700-
.addDiagnostic("cannot instantiate non-constructor")
11710+
.addDiagnostic("cannot instantiate non-constructor, found type: number")
1170111711
.run();
1170211712
}
1170311713

@@ -14908,7 +14918,7 @@ public void testStaticDataPropertyOnNestedInterface() {
1490814918
public void testInterfaceInstantiation() {
1490914919
newTest()
1491014920
.addSource("/** @interface */var f = function(){}; new f")
14911-
.addDiagnostic("cannot instantiate non-constructor")
14921+
.addDiagnostic("cannot instantiate non-constructor, found type: (typeof f)")
1491214922
.run();
1491314923
}
1491414924

@@ -16935,7 +16945,7 @@ public void testTypeCheckStandaloneAST() {
1693516945

1693616946
assertThat(compiler.getWarningCount()).isEqualTo(1);
1693716947
assertThat(compiler.getWarnings().get(0).getDescription())
16938-
.isEqualTo("cannot instantiate non-constructor");
16948+
.isEqualTo("cannot instantiate non-constructor, found type: function(): undefined");
1693916949
}
1694016950

1694116951
@Test
@@ -21446,7 +21456,7 @@ public void testSymbol2() {
2144621456
.addSource(
2144721457
"/** @const */", //
2144821458
"var o = new Symbol();")
21449-
.addDiagnostic("cannot instantiate non-constructor")
21459+
.addDiagnostic("cannot instantiate non-constructor, found type: (typeof Symbol)")
2145021460
.includeDefaultExterns()
2145121461
.run();
2145221462
}

0 commit comments

Comments
 (0)