Skip to content

Ensure factory symbol remains unused in react-jsx mode when not referenced in user code #41905

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 1 commit into from
Dec 16, 2020
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
3 changes: 3 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26912,6 +26912,9 @@ namespace ts {
errorOutputContainer);

function checkTagNameDoesNotExpectTooManyArguments(): boolean {
if (getJsxNamespaceContainerForImplicitImport(node)) {
return true; // factory is implicitly jsx/jsxdev - assume it fits the bill, since we don't strongly look for the jsx/jsxs/jsxDEV factory APIs anywhere else (at least not yet)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense, but it’s unintuitive as to why it fixes this particular bug. Something later in this function marked React as referenced, for some reason?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - looking up it's type to see if it had call signatures.

}
const tagType = isJsxOpeningElement(node) || isJsxSelfClosingElement(node) && !isJsxIntrinsicIdentifier(node.tagName) ? checkExpression(node.tagName) : undefined;
if (!tagType) {
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
tests/cases/compiler/index.tsx(3,1): error TS6133: 'React' is declared but its value is never read.


==== tests/cases/compiler/index.tsx (1 errors) ====
/// <reference path="/.lib/react16.d.ts" />

import React from "react";
~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS6133: 'React' is declared but its value is never read.

function Bar() {
return <div />;
}

export function Foo() {
return <Bar />;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [index.tsx]
/// <reference path="/.lib/react16.d.ts" />

import React from "react";

function Bar() {
return <div />;
}

export function Foo() {
return <Bar />;
}

//// [index.js]
"use strict";
exports.__esModule = true;
exports.Foo = void 0;
var jsx_runtime_1 = require("react/jsx-runtime");
function Bar() {
return jsx_runtime_1.jsx("div", {}, void 0);
}
function Foo() {
return jsx_runtime_1.jsx(Bar, {}, void 0);
}
exports.Foo = Foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
=== tests/cases/compiler/index.tsx ===
/// <reference path="react16.d.ts" />

import React from "react";
>React : Symbol(React, Decl(index.tsx, 2, 6))

function Bar() {
>Bar : Symbol(Bar, Decl(index.tsx, 2, 26))

return <div />;
>div : Symbol(JSX.IntrinsicElements.div, Decl(react16.d.ts, 2420, 114))
}

export function Foo() {
>Foo : Symbol(Foo, Decl(index.tsx, 6, 1))

return <Bar />;
>Bar : Symbol(Bar, Decl(index.tsx, 2, 26))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
=== tests/cases/compiler/index.tsx ===
/// <reference path="react16.d.ts" />

import React from "react";
>React : typeof React

function Bar() {
>Bar : () => JSX.Element

return <div />;
><div /> : JSX.Element
>div : any
}

export function Foo() {
>Foo : () => JSX.Element

return <Bar />;
><Bar /> : JSX.Element
>Bar : () => JSX.Element
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
tests/cases/compiler/index.tsx(3,1): error TS6133: 'React' is declared but its value is never read.


==== tests/cases/compiler/index.tsx (1 errors) ====
/// <reference path="/.lib/react16.d.ts" />

import React from "react";
~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS6133: 'React' is declared but its value is never read.

function Bar() {
return <div />;
}

export function Foo() {
return <Bar />;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//// [index.tsx]
/// <reference path="/.lib/react16.d.ts" />

import React from "react";

function Bar() {
return <div />;
}

export function Foo() {
return <Bar />;
}

//// [index.js]
"use strict";
exports.__esModule = true;
exports.Foo = void 0;
var jsx_dev_runtime_1 = require("react/jsx-dev-runtime");
var _jsxFileName = "tests/cases/compiler/index.tsx";
function Bar() {
return jsx_dev_runtime_1.jsxDEV("div", {}, void 0, false, { fileName: _jsxFileName, lineNumber: 6, columnNumber: 9 }, this);
}
function Foo() {
return jsx_dev_runtime_1.jsxDEV(Bar, {}, void 0, false, { fileName: _jsxFileName, lineNumber: 10, columnNumber: 9 }, this);
}
exports.Foo = Foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
=== tests/cases/compiler/index.tsx ===
/// <reference path="react16.d.ts" />

import React from "react";
>React : Symbol(React, Decl(index.tsx, 2, 6))

function Bar() {
>Bar : Symbol(Bar, Decl(index.tsx, 2, 26))

return <div />;
>div : Symbol(JSX.IntrinsicElements.div, Decl(react16.d.ts, 2420, 114))
}

export function Foo() {
>Foo : Symbol(Foo, Decl(index.tsx, 6, 1))

return <Bar />;
>Bar : Symbol(Bar, Decl(index.tsx, 2, 26))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
=== tests/cases/compiler/index.tsx ===
/// <reference path="react16.d.ts" />

import React from "react";
>React : typeof React

function Bar() {
>Bar : () => JSX.Element

return <div />;
><div /> : JSX.Element
>div : any
}

export function Foo() {
>Foo : () => JSX.Element

return <Bar />;
><Bar /> : JSX.Element
>Bar : () => JSX.Element
}
16 changes: 16 additions & 0 deletions tests/cases/compiler/reactImportUnusedInNewJSXEmit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @esModuleInterop: true
// @noUnusedLocals: true
// @jsx: react-jsx,react-jsxdev
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤯 TIL

// @skipLibCheck: true
// @filename: index.tsx
/// <reference path="/.lib/react16.d.ts" />

import React from "react";

function Bar() {
return <div />;
}

export function Foo() {
return <Bar />;
}