Skip to content

Commit 8180b7d

Browse files
committed
Don't error on JSX elements when JSX.Element isn't present
1 parent 65828c4 commit 8180b7d

26 files changed

+1126
-654
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3800,7 +3800,7 @@ namespace ts {
38003800
function getExportedTypeFromNamespace(namespace: string, name: string): Type {
38013801
var namespaceSymbol = getGlobalSymbol(namespace, SymbolFlags.Namespace, /*diagnosticMessage*/ undefined);
38023802
var typeSymbol = namespaceSymbol && getSymbol(namespaceSymbol.exports, name, SymbolFlags.Type);
3803-
return (typeSymbol && getDeclaredTypeOfSymbol(typeSymbol)) || unknownType;
3803+
return typeSymbol && getDeclaredTypeOfSymbol(typeSymbol);
38043804
}
38053805

38063806
function getGlobalESSymbolConstructorSymbol() {
@@ -6712,7 +6712,7 @@ namespace ts {
67126712

67136713
function checkJsxSelfClosingElement(node: JsxSelfClosingElement) {
67146714
checkJsxOpeningLikeElement(node);
6715-
return jsxElementType;
6715+
return jsxElementType || anyType;
67166716
}
67176717

67186718
function tagNamesAreEquivalent(lhs: EntityName, rhs: EntityName): boolean {
@@ -6755,7 +6755,7 @@ namespace ts {
67556755
}
67566756
}
67576757

6758-
return jsxElementType;
6758+
return jsxElementType || anyType;
67596759
}
67606760

67616761
/**
@@ -7087,8 +7087,10 @@ namespace ts {
70877087
error(errorNode, Diagnostics.Cannot_use_JSX_unless_the_jsx_flag_is_provided);
70887088
}
70897089

7090-
if (jsxElementType === unknownType) {
7091-
error(errorNode, Diagnostics.The_global_type_JSX_Element_must_exist_when_using_JSX);
7090+
if (jsxElementType === undefined) {
7091+
if(compilerOptions.noImplicitAny) {
7092+
error(errorNode, Diagnostics.JSX_element_implicitly_has_type_any_because_the_global_type_JSX_Element_does_not_exist);
7093+
}
70927094
}
70937095
}
70947096

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ namespace ts {
391391
Base_constructors_must_all_have_the_same_return_type: { code: 2510, category: DiagnosticCategory.Error, key: "Base constructors must all have the same return type." },
392392
JSX_element_attributes_type_0_must_be_an_object_type: { code: 2600, category: DiagnosticCategory.Error, key: "JSX element attributes type '{0}' must be an object type." },
393393
The_return_type_of_a_JSX_element_constructor_must_return_an_object_type: { code: 2601, category: DiagnosticCategory.Error, key: "The return type of a JSX element constructor must return an object type." },
394-
The_global_type_JSX_Element_must_exist_when_using_JSX: { code: 2602, category: DiagnosticCategory.Error, key: "The global type 'JSX.Element' must exist when using JSX." },
394+
JSX_element_implicitly_has_type_any_because_the_global_type_JSX_Element_does_not_exist: { code: 2602, category: DiagnosticCategory.Error, key: "JSX element implicitly has type 'any' because the global type 'JSX.Element' does not exist." },
395395
Property_0_in_type_1_is_not_assignable_to_type_2: { code: 2603, category: DiagnosticCategory.Error, key: "Property '{0}' in type '{1}' is not assignable to type '{2}'" },
396396
JSX_element_type_0_does_not_have_any_construct_or_call_signatures: { code: 2604, category: DiagnosticCategory.Error, key: "JSX element type '{0}' does not have any construct or call signatures." },
397397
JSX_element_type_0_is_not_a_constructor_function_for_JSX_elements: { code: 2605, category: DiagnosticCategory.Error, key: "JSX element type '{0}' is not a constructor function for JSX elements." },

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,7 @@
15541554
"category": "Error",
15551555
"code": 2601
15561556
},
1557-
"The global type 'JSX.Element' must exist when using JSX.": {
1557+
"JSX element implicitly has type 'any' because the global type 'JSX.Element' does not exist.": {
15581558
"category": "Error",
15591559
"code": 2602
15601560
},

tests/baselines/reference/jsxAndTypeAssertion.errors.txt

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,46 @@
1-
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(7,5): error TS2602: The global type 'JSX.Element' must exist when using JSX.
21
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(7,13): error TS2304: Cannot find name 'test'.
32
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(7,17): error TS1005: '}' expected.
4-
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(7,19): error TS2602: The global type 'JSX.Element' must exist when using JSX.
5-
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(9,5): error TS2602: The global type 'JSX.Element' must exist when using JSX.
6-
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(9,10): error TS2602: The global type 'JSX.Element' must exist when using JSX.
7-
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(11,5): error TS2602: The global type 'JSX.Element' must exist when using JSX.
8-
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(11,17): error TS2602: The global type 'JSX.Element' must exist when using JSX.
93
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(11,32): error TS1005: '}' expected.
10-
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(13,5): error TS2602: The global type 'JSX.Element' must exist when using JSX.
11-
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(13,16): error TS2602: The global type 'JSX.Element' must exist when using JSX.
124
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(13,36): error TS1005: '}' expected.
13-
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(15,5): error TS2602: The global type 'JSX.Element' must exist when using JSX.
14-
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(15,16): error TS2602: The global type 'JSX.Element' must exist when using JSX.
15-
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(15,31): error TS2602: The global type 'JSX.Element' must exist when using JSX.
165
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(15,45): error TS1005: '}' expected.
17-
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(17,5): error TS2602: The global type 'JSX.Element' must exist when using JSX.
18-
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(17,23): error TS2602: The global type 'JSX.Element' must exist when using JSX.
19-
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(19,1): error TS2602: The global type 'JSX.Element' must exist when using JSX.
20-
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(19,7): error TS2602: The global type 'JSX.Element' must exist when using JSX.
21-
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(19,12): error TS2602: The global type 'JSX.Element' must exist when using JSX.
22-
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(19,34): error TS2602: The global type 'JSX.Element' must exist when using JSX.
23-
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(19,39): error TS2602: The global type 'JSX.Element' must exist when using JSX.
24-
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(19,53): error TS2602: The global type 'JSX.Element' must exist when using JSX.
25-
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(19,58): error TS2602: The global type 'JSX.Element' must exist when using JSX.
266
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(22,1): error TS1005: ':' expected.
277
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(22,1): error TS17002: Expected corresponding JSX closing tag for 'any'.
288
tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx(22,1): error TS17002: Expected corresponding JSX closing tag for 'foo'.
299

3010

31-
==== tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx (28 errors) ====
11+
==== tests/cases/conformance/jsx/jsxAndTypeAssertion.tsx (8 errors) ====
3212

3313
declare var createElement: any;
3414

3515
class foo {}
3616

3717
var x: any;
3818
x = <any> { test: <any></any> };
39-
~~~~~
40-
!!! error TS2602: The global type 'JSX.Element' must exist when using JSX.
4119
~~~~
4220
!!! error TS2304: Cannot find name 'test'.
4321
~
4422
!!! error TS1005: '}' expected.
45-
~~~~~
46-
!!! error TS2602: The global type 'JSX.Element' must exist when using JSX.
4723

4824
x = <any><any></any>;
49-
~~~~~
50-
!!! error TS2602: The global type 'JSX.Element' must exist when using JSX.
51-
~~~~~
52-
!!! error TS2602: The global type 'JSX.Element' must exist when using JSX.
5325

5426
x = <foo>hello {<foo>{}} </foo>;
55-
~~~~~
56-
!!! error TS2602: The global type 'JSX.Element' must exist when using JSX.
57-
~~~~~
58-
!!! error TS2602: The global type 'JSX.Element' must exist when using JSX.
5927
~
6028
!!! error TS1005: '}' expected.
6129

6230
x = <foo test={<foo>{}}>hello</foo>;
63-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
64-
!!! error TS2602: The global type 'JSX.Element' must exist when using JSX.
65-
~~~~~
66-
!!! error TS2602: The global type 'JSX.Element' must exist when using JSX.
6731
~
6832
!!! error TS1005: '}' expected.
6933

7034
x = <foo test={<foo>{}}>hello{<foo>{}}</foo>;
71-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72-
~~~~~
73-
!!! error TS2602: The global type 'JSX.Element' must exist when using JSX.
74-
~~~~~
75-
!!! error TS2602: The global type 'JSX.Element' must exist when using JSX.
7635
~
7736
!!! error TS1005: '}' expected.
7837

79-
8038
x = <foo>x</foo>, x = <foo/>;
81-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
82-
~~~~~
83-
!!! error TS2602: The global type 'JSX.Element' must exist when using JSX.
84-
~~~~~~
85-
!!! error TS2602: The global type 'JSX.Element' must exist when using JSX.
86-
8739

8840
<foo>{<foo><foo>{/foo/.test(x) ? <foo><foo></foo> : <foo><foo></foo>}</foo>}</foo>
89-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
90-
~~~~~
91-
!!! error TS2602: The global type 'JSX.Element' must exist when using JSX.
92-
~~~~~
93-
!!! error TS2602: The global type 'JSX.Element' must exist when using JSX.
94-
~~~~~
95-
!!! error TS2602: The global type 'JSX.Element' must exist when using JSX.
96-
~~~~~
97-
!!! error TS2602: The global type 'JSX.Element' must exist when using JSX.
98-
~~~~~
99-
!!! error TS2602: The global type 'JSX.Element' must exist when using JSX.
100-
~~~~~
101-
!!! error TS2602: The global type 'JSX.Element' must exist when using JSX.
102-
~~~~~
103-
!!! error TS2602: The global type 'JSX.Element' must exist when using JSX.
104-
10541

10642

107-
~~~~
108-
10943

110-
!!! error TS2602: The global type 'JSX.Element' must exist when using JSX.
11144

11245
!!! error TS1005: ':' expected.
11346

tests/baselines/reference/jsxEsprimaFbTestSuite.errors.txt

Lines changed: 0 additions & 131 deletions
This file was deleted.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
=== tests/cases/conformance/jsx/jsxEsprimaFbTestSuite.tsx ===
2+
declare var React: any;
3+
>React : Symbol(React, Decl(jsxEsprimaFbTestSuite.tsx, 0, 11))
4+
5+
declare var 日本語;
6+
>日本語 : Symbol(日本語, Decl(jsxEsprimaFbTestSuite.tsx, 1, 11))
7+
8+
declare var AbC_def;
9+
>AbC_def : Symbol(AbC_def, Decl(jsxEsprimaFbTestSuite.tsx, 2, 11))
10+
11+
declare var LeftRight;
12+
>LeftRight : Symbol(LeftRight, Decl(jsxEsprimaFbTestSuite.tsx, 3, 11))
13+
14+
declare var x;
15+
>x : Symbol(x, Decl(jsxEsprimaFbTestSuite.tsx, 4, 11))
16+
17+
declare var a;
18+
>a : Symbol(a, Decl(jsxEsprimaFbTestSuite.tsx, 5, 11))
19+
20+
declare var props;
21+
>props : Symbol(props, Decl(jsxEsprimaFbTestSuite.tsx, 6, 11))
22+
23+
<a />;
24+
25+
//<n:a n:v />; Namespace unsuported
26+
27+
//<a n:foo="bar"> {value} <b><c /></b></a>; Namespace unsuported
28+
29+
<a b={" "} c=" " d="&amp;" e="id=1&group=2" f="&#123456789" g="&#123*;" h="&#x;" />;
30+
>b : Symbol(unknown)
31+
>c : Symbol(unknown)
32+
>d : Symbol(unknown)
33+
>e : Symbol(unknown)
34+
>f : Symbol(unknown)
35+
>g : Symbol(unknown)
36+
>h : Symbol(unknown)
37+
38+
<a b="&notanentity;" />;
39+
>b : Symbol(unknown)
40+
41+
<a
42+
/>;
43+
44+
<日本語></日本語>;
45+
46+
<AbC_def
47+
>AbC_def : Symbol(AbC_def, Decl(jsxEsprimaFbTestSuite.tsx, 2, 11))
48+
49+
test="&#x0026;&#38;">
50+
>test : Symbol(unknown)
51+
52+
bar
53+
baz
54+
</AbC_def>;
55+
56+
<a b={x ? <c /> : <d />} />;
57+
>b : Symbol(unknown)
58+
>x : Symbol(x, Decl(jsxEsprimaFbTestSuite.tsx, 4, 11))
59+
60+
<a>{}</a>;
61+
62+
<a>{/* this is a comment */}</a>;
63+
64+
<div>@test content</div>;
65+
66+
<div><br />7x invalid-js-identifier</div>;
67+
68+
<LeftRight left=<a /> right=<b>monkeys /> gorillas</b> />;
69+
>LeftRight : Symbol(LeftRight, Decl(jsxEsprimaFbTestSuite.tsx, 3, 11))
70+
>left : Symbol(unknown)
71+
>right : Symbol(unknown)
72+
73+
<a.b></a.b>;
74+
>b : Symbol(unknown)
75+
76+
<a.b.c></a.b.c>;
77+
>c : Symbol(unknown)
78+
79+
(<div />) < x;
80+
>x : Symbol(x, Decl(jsxEsprimaFbTestSuite.tsx, 4, 11))
81+
82+
<div {...props} />;
83+
84+
<div {...props} post="attribute" />;
85+
>post : Symbol(unknown)
86+
87+
<div pre="leading" pre2="attribute" {...props}></div>;
88+
>pre : Symbol(unknown)
89+
>pre2 : Symbol(unknown)
90+
91+
<a> </a>;
92+

0 commit comments

Comments
 (0)