Skip to content

[Master] Fix 11566 : SFC returns null #14152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ namespace ts {
let deferredGlobalAsyncIterableIteratorType: GenericType;
let deferredGlobalTemplateStringsArrayType: ObjectType;
let deferredJsxElementClassType: Type;
let deferredJsxElementType: Type;
let deferredJsxStatelessElementType: Type;

let deferredNodes: Node[];
let deferredUnusedIdentifierNodes: Node[];
Expand Down Expand Up @@ -404,7 +406,6 @@ namespace ts {
});
const typeofType = createTypeofType();

let jsxElementType: Type;
let _jsxNamespace: string;
let _jsxFactoryEntity: EntityName;

Expand Down Expand Up @@ -12453,12 +12454,12 @@ namespace ts {
type.flags & TypeFlags.UnionOrIntersection && !forEach((<UnionOrIntersectionType>type).types, t => !isValidSpreadType(t)));
}

function checkJsxSelfClosingElement(node: JsxSelfClosingElement) {
function checkJsxSelfClosingElement(node: JsxSelfClosingElement): Type {
checkJsxOpeningLikeElement(node);
return jsxElementType || anyType;
return getJsxGlobalElementType() || anyType;
}

function checkJsxElement(node: JsxElement) {
function checkJsxElement(node: JsxElement): Type {
// Check attributes
checkJsxOpeningLikeElement(node.openingElement);

Expand All @@ -12485,7 +12486,7 @@ namespace ts {
}
}

return jsxElementType || anyType;
return getJsxGlobalElementType() || anyType;
}

/**
Expand Down Expand Up @@ -12726,13 +12727,14 @@ namespace ts {
function defaultTryGetJsxStatelessFunctionAttributesType(openingLikeElement: JsxOpeningLikeElement, elementType: Type, elemInstanceType: Type, elementClassType?: Type): Type {
Debug.assert(!(elementType.flags & TypeFlags.Union));
if (!elementClassType || !isTypeAssignableTo(elemInstanceType, elementClassType)) {
if (jsxElementType) {
const jsxStatelessElementType = getJsxGlobalStatelessElementType();
if (jsxStatelessElementType) {
// We don't call getResolvedSignature here because we have already resolve the type of JSX Element.
const callSignature = getResolvedJsxStatelessFunctionSignature(openingLikeElement, elementType, /*candidatesOutArray*/ undefined);
if (callSignature !== unknownSignature) {
const callReturnType = callSignature && getReturnTypeOfSignature(callSignature);
let paramType = callReturnType && (callSignature.parameters.length === 0 ? emptyObjectType : getTypeOfSymbol(callSignature.parameters[0]));
if (callReturnType && isTypeAssignableTo(callReturnType, jsxElementType)) {
if (callReturnType && isTypeAssignableTo(callReturnType, jsxStatelessElementType)) {
// Intersect in JSX.IntrinsicAttributes if it exists
const intrinsicAttributes = getJsxType(JsxNames.IntrinsicAttributes);
if (intrinsicAttributes !== unknownType) {
Expand Down Expand Up @@ -12760,7 +12762,8 @@ namespace ts {
Debug.assert(!(elementType.flags & TypeFlags.Union));
if (!elementClassType || !isTypeAssignableTo(elemInstanceType, elementClassType)) {
// Is this is a stateless function component? See if its single signature's return type is assignable to the JSX Element Type
if (jsxElementType) {
const jsxStatelessElementType = getJsxGlobalStatelessElementType();
if (jsxStatelessElementType) {
// We don't call getResolvedSignature because here we have already resolve the type of JSX Element.
const candidatesOutArray: Signature[] = [];
getResolvedJsxStatelessFunctionSignature(openingLikeElement, elementType, candidatesOutArray);
Expand All @@ -12769,7 +12772,7 @@ namespace ts {
for (const candidate of candidatesOutArray) {
const callReturnType = getReturnTypeOfSignature(candidate);
const paramType = callReturnType && (candidate.parameters.length === 0 ? emptyObjectType : getTypeOfSymbol(candidate.parameters[0]));
if (callReturnType && isTypeAssignableTo(callReturnType, jsxElementType)) {
if (callReturnType && isTypeAssignableTo(callReturnType, jsxStatelessElementType)) {
let shouldBeCandidate = true;
for (const attribute of openingLikeElement.attributes.properties) {
if (isJsxAttribute(attribute) &&
Expand Down Expand Up @@ -13013,6 +13016,23 @@ namespace ts {
return deferredJsxElementClassType;
}

function getJsxGlobalElementType(): Type {
if (!deferredJsxElementType) {
deferredJsxElementType = getExportedTypeFromNamespace(JsxNames.JSX, JsxNames.Element);
}
return deferredJsxElementType;
}

function getJsxGlobalStatelessElementType(): Type {
if (!deferredJsxStatelessElementType) {
const jsxElementType = getJsxGlobalElementType();
if (jsxElementType) {
deferredJsxStatelessElementType = getUnionType([jsxElementType, nullType]);
}
}
return deferredJsxStatelessElementType;
}

/**
* Returns all the properties of the Jsx.IntrinsicElements interface
*/
Expand All @@ -13027,7 +13047,7 @@ namespace ts {
error(errorNode, Diagnostics.Cannot_use_JSX_unless_the_jsx_flag_is_provided);
}

if (jsxElementType === undefined) {
if (getJsxGlobalElementType() === undefined) {
if (noImplicitAny) {
error(errorNode, Diagnostics.JSX_element_implicitly_has_type_any_because_the_global_type_JSX_Element_does_not_exist);
}
Expand Down Expand Up @@ -22072,7 +22092,6 @@ namespace ts {
globalNumberType = getGlobalType("Number", /*arity*/ 0, /*reportErrors*/ true);
globalBooleanType = getGlobalType("Boolean", /*arity*/ 0, /*reportErrors*/ true);
globalRegExpType = getGlobalType("RegExp", /*arity*/ 0, /*reportErrors*/ true);
jsxElementType = getExportedTypeFromNamespace("JSX", JsxNames.Element);
anyArrayType = createArrayType(anyType);
autoArrayType = createArrayType(autoType);

Expand Down
24 changes: 24 additions & 0 deletions tests/baselines/reference/tsxSfcReturnNull.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//// [file.tsx]

import React = require('react');

const Foo = (props: any) => null;

function Greet(x: {name?: string}) {
return null;
}

const foo = <Foo />;
const G = <Greet />;

//// [file.jsx]
define(["require", "exports", "react"], function (require, exports, React) {
"use strict";
exports.__esModule = true;
var Foo = function (props) { return null; };
function Greet(x) {
return null;
}
var foo = <Foo />;
var G = <Greet />;
});
25 changes: 25 additions & 0 deletions tests/baselines/reference/tsxSfcReturnNull.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=== tests/cases/conformance/jsx/file.tsx ===

import React = require('react');
>React : Symbol(React, Decl(file.tsx, 0, 0))

const Foo = (props: any) => null;
>Foo : Symbol(Foo, Decl(file.tsx, 3, 5))
>props : Symbol(props, Decl(file.tsx, 3, 13))

function Greet(x: {name?: string}) {
>Greet : Symbol(Greet, Decl(file.tsx, 3, 33))
>x : Symbol(x, Decl(file.tsx, 5, 15))
>name : Symbol(name, Decl(file.tsx, 5, 19))

return null;
}

const foo = <Foo />;
>foo : Symbol(foo, Decl(file.tsx, 9, 5))
>Foo : Symbol(Foo, Decl(file.tsx, 3, 5))

const G = <Greet />;
>G : Symbol(G, Decl(file.tsx, 10, 5))
>Greet : Symbol(Greet, Decl(file.tsx, 3, 33))

30 changes: 30 additions & 0 deletions tests/baselines/reference/tsxSfcReturnNull.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
=== tests/cases/conformance/jsx/file.tsx ===

import React = require('react');
>React : typeof React

const Foo = (props: any) => null;
>Foo : (props: any) => any
>(props: any) => null : (props: any) => any
>props : any
>null : null

function Greet(x: {name?: string}) {
>Greet : (x: { name?: string; }) => any
>x : { name?: string; }
>name : string

return null;
>null : null
}

const foo = <Foo />;
>foo : JSX.Element
><Foo /> : JSX.Element
>Foo : (props: any) => any

const G = <Greet />;
>G : JSX.Element
><Greet /> : JSX.Element
>Greet : (x: { name?: string; }) => any

24 changes: 24 additions & 0 deletions tests/baselines/reference/tsxSfcReturnNullStrictNullChecks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//// [file.tsx]

import React = require('react');

const Foo = (props: any) => null;

function Greet(x: {name?: string}) {
return null;
}

const foo = <Foo />;
const G = <Greet />;

//// [file.jsx]
define(["require", "exports", "react"], function (require, exports, React) {
"use strict";
exports.__esModule = true;
var Foo = function (props) { return null; };
function Greet(x) {
return null;
}
var foo = <Foo />;
var G = <Greet />;
});
25 changes: 25 additions & 0 deletions tests/baselines/reference/tsxSfcReturnNullStrictNullChecks.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=== tests/cases/conformance/jsx/file.tsx ===

import React = require('react');
>React : Symbol(React, Decl(file.tsx, 0, 0))

const Foo = (props: any) => null;
>Foo : Symbol(Foo, Decl(file.tsx, 3, 5))
>props : Symbol(props, Decl(file.tsx, 3, 13))

function Greet(x: {name?: string}) {
>Greet : Symbol(Greet, Decl(file.tsx, 3, 33))
>x : Symbol(x, Decl(file.tsx, 5, 15))
>name : Symbol(name, Decl(file.tsx, 5, 19))

return null;
}

const foo = <Foo />;
>foo : Symbol(foo, Decl(file.tsx, 9, 5))
>Foo : Symbol(Foo, Decl(file.tsx, 3, 5))

const G = <Greet />;
>G : Symbol(G, Decl(file.tsx, 10, 5))
>Greet : Symbol(Greet, Decl(file.tsx, 3, 33))

30 changes: 30 additions & 0 deletions tests/baselines/reference/tsxSfcReturnNullStrictNullChecks.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
=== tests/cases/conformance/jsx/file.tsx ===

import React = require('react');
>React : typeof React

const Foo = (props: any) => null;
>Foo : (props: any) => null
>(props: any) => null : (props: any) => null
>props : any
>null : null

function Greet(x: {name?: string}) {
>Greet : (x: { name?: string | undefined; }) => null
>x : { name?: string | undefined; }
>name : string | undefined

return null;
>null : null
}

const foo = <Foo />;
>foo : JSX.Element
><Foo /> : JSX.Element
>Foo : (props: any) => null

const G = <Greet />;
>G : JSX.Element
><Greet /> : JSX.Element
>Greet : (x: { name?: string | undefined; }) => null

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
tests/cases/conformance/jsx/file.tsx(10,13): error TS2605: JSX element type 'undefined' is not a constructor function for JSX elements.
tests/cases/conformance/jsx/file.tsx(11,11): error TS2605: JSX element type 'undefined' is not a constructor function for JSX elements.


==== tests/cases/conformance/jsx/file.tsx (2 errors) ====

import React = require('react');

const Foo = (props: any) => undefined;
function Greet(x: {name?: string}) {
return undefined;
}

// Error
const foo = <Foo />;
~~~~~~~
!!! error TS2605: JSX element type 'undefined' is not a constructor function for JSX elements.
const G = <Greet />;
~~~~~~~~~
!!! error TS2605: JSX element type 'undefined' is not a constructor function for JSX elements.
25 changes: 25 additions & 0 deletions tests/baselines/reference/tsxSfcReturnUndefinedStrictNullChecks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [file.tsx]

import React = require('react');

const Foo = (props: any) => undefined;
function Greet(x: {name?: string}) {
return undefined;
}

// Error
const foo = <Foo />;
const G = <Greet />;

//// [file.jsx]
define(["require", "exports", "react"], function (require, exports, React) {
"use strict";
exports.__esModule = true;
var Foo = function (props) { return undefined; };
function Greet(x) {
return undefined;
}
// Error
var foo = <Foo />;
var G = <Greet />;
});
16 changes: 16 additions & 0 deletions tests/cases/conformance/jsx/tsxSfcReturnNull.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @filename: file.tsx
// @jsx: preserve
// @module: amd
// @noLib: true
// @libFiles: react.d.ts,lib.d.ts

import React = require('react');

const Foo = (props: any) => null;

function Greet(x: {name?: string}) {
return null;
}

const foo = <Foo />;
const G = <Greet />;
17 changes: 17 additions & 0 deletions tests/cases/conformance/jsx/tsxSfcReturnNullStrictNullChecks.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @filename: file.tsx
// @jsx: preserve
// @module: amd
// @noLib: true
// @strictNullChecks: true
// @libFiles: react.d.ts,lib.d.ts

import React = require('react');

const Foo = (props: any) => null;

function Greet(x: {name?: string}) {
return null;
}

const foo = <Foo />;
const G = <Greet />;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @filename: file.tsx
// @jsx: preserve
// @module: amd
// @noLib: true
// @strictNullChecks: true
// @libFiles: react.d.ts,lib.d.ts

import React = require('react');

const Foo = (props: any) => undefined;
function Greet(x: {name?: string}) {
return undefined;
}

// Error
const foo = <Foo />;
const G = <Greet />;