Skip to content

Commit 675cd4d

Browse files
authored
Ensure factory symbol remains unused in react-jsx mode when not referenced in user code (microsoft#41905)
1 parent 4b8b0c8 commit 675cd4d

10 files changed

+184
-0
lines changed

src/compiler/checker.ts

+3
Original file line numberDiff line numberDiff line change
@@ -26955,6 +26955,9 @@ namespace ts {
2695526955
errorOutputContainer);
2695626956

2695726957
function checkTagNameDoesNotExpectTooManyArguments(): boolean {
26958+
if (getJsxNamespaceContainerForImplicitImport(node)) {
26959+
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)
26960+
}
2695826961
const tagType = isJsxOpeningElement(node) || isJsxSelfClosingElement(node) && !isJsxIntrinsicIdentifier(node.tagName) ? checkExpression(node.tagName) : undefined;
2695926962
if (!tagType) {
2696026963
return true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/compiler/index.tsx(3,1): error TS6133: 'React' is declared but its value is never read.
2+
3+
4+
==== tests/cases/compiler/index.tsx (1 errors) ====
5+
/// <reference path="/.lib/react16.d.ts" />
6+
7+
import React from "react";
8+
~~~~~~~~~~~~~~~~~~~~~~~~~~
9+
!!! error TS6133: 'React' is declared but its value is never read.
10+
11+
function Bar() {
12+
return <div />;
13+
}
14+
15+
export function Foo() {
16+
return <Bar />;
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//// [index.tsx]
2+
/// <reference path="/.lib/react16.d.ts" />
3+
4+
import React from "react";
5+
6+
function Bar() {
7+
return <div />;
8+
}
9+
10+
export function Foo() {
11+
return <Bar />;
12+
}
13+
14+
//// [index.js]
15+
"use strict";
16+
exports.__esModule = true;
17+
exports.Foo = void 0;
18+
var jsx_runtime_1 = require("react/jsx-runtime");
19+
function Bar() {
20+
return jsx_runtime_1.jsx("div", {}, void 0);
21+
}
22+
function Foo() {
23+
return jsx_runtime_1.jsx(Bar, {}, void 0);
24+
}
25+
exports.Foo = Foo;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/compiler/index.tsx ===
2+
/// <reference path="react16.d.ts" />
3+
4+
import React from "react";
5+
>React : Symbol(React, Decl(index.tsx, 2, 6))
6+
7+
function Bar() {
8+
>Bar : Symbol(Bar, Decl(index.tsx, 2, 26))
9+
10+
return <div />;
11+
>div : Symbol(JSX.IntrinsicElements.div, Decl(react16.d.ts, 2420, 114))
12+
}
13+
14+
export function Foo() {
15+
>Foo : Symbol(Foo, Decl(index.tsx, 6, 1))
16+
17+
return <Bar />;
18+
>Bar : Symbol(Bar, Decl(index.tsx, 2, 26))
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/compiler/index.tsx ===
2+
/// <reference path="react16.d.ts" />
3+
4+
import React from "react";
5+
>React : typeof React
6+
7+
function Bar() {
8+
>Bar : () => JSX.Element
9+
10+
return <div />;
11+
><div /> : JSX.Element
12+
>div : any
13+
}
14+
15+
export function Foo() {
16+
>Foo : () => JSX.Element
17+
18+
return <Bar />;
19+
><Bar /> : JSX.Element
20+
>Bar : () => JSX.Element
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/compiler/index.tsx(3,1): error TS6133: 'React' is declared but its value is never read.
2+
3+
4+
==== tests/cases/compiler/index.tsx (1 errors) ====
5+
/// <reference path="/.lib/react16.d.ts" />
6+
7+
import React from "react";
8+
~~~~~~~~~~~~~~~~~~~~~~~~~~
9+
!!! error TS6133: 'React' is declared but its value is never read.
10+
11+
function Bar() {
12+
return <div />;
13+
}
14+
15+
export function Foo() {
16+
return <Bar />;
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//// [index.tsx]
2+
/// <reference path="/.lib/react16.d.ts" />
3+
4+
import React from "react";
5+
6+
function Bar() {
7+
return <div />;
8+
}
9+
10+
export function Foo() {
11+
return <Bar />;
12+
}
13+
14+
//// [index.js]
15+
"use strict";
16+
exports.__esModule = true;
17+
exports.Foo = void 0;
18+
var jsx_dev_runtime_1 = require("react/jsx-dev-runtime");
19+
var _jsxFileName = "tests/cases/compiler/index.tsx";
20+
function Bar() {
21+
return jsx_dev_runtime_1.jsxDEV("div", {}, void 0, false, { fileName: _jsxFileName, lineNumber: 6, columnNumber: 9 }, this);
22+
}
23+
function Foo() {
24+
return jsx_dev_runtime_1.jsxDEV(Bar, {}, void 0, false, { fileName: _jsxFileName, lineNumber: 10, columnNumber: 9 }, this);
25+
}
26+
exports.Foo = Foo;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/compiler/index.tsx ===
2+
/// <reference path="react16.d.ts" />
3+
4+
import React from "react";
5+
>React : Symbol(React, Decl(index.tsx, 2, 6))
6+
7+
function Bar() {
8+
>Bar : Symbol(Bar, Decl(index.tsx, 2, 26))
9+
10+
return <div />;
11+
>div : Symbol(JSX.IntrinsicElements.div, Decl(react16.d.ts, 2420, 114))
12+
}
13+
14+
export function Foo() {
15+
>Foo : Symbol(Foo, Decl(index.tsx, 6, 1))
16+
17+
return <Bar />;
18+
>Bar : Symbol(Bar, Decl(index.tsx, 2, 26))
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/compiler/index.tsx ===
2+
/// <reference path="react16.d.ts" />
3+
4+
import React from "react";
5+
>React : typeof React
6+
7+
function Bar() {
8+
>Bar : () => JSX.Element
9+
10+
return <div />;
11+
><div /> : JSX.Element
12+
>div : any
13+
}
14+
15+
export function Foo() {
16+
>Foo : () => JSX.Element
17+
18+
return <Bar />;
19+
><Bar /> : JSX.Element
20+
>Bar : () => JSX.Element
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @esModuleInterop: true
2+
// @noUnusedLocals: true
3+
// @jsx: react-jsx,react-jsxdev
4+
// @skipLibCheck: true
5+
// @filename: index.tsx
6+
/// <reference path="/.lib/react16.d.ts" />
7+
8+
import React from "react";
9+
10+
function Bar() {
11+
return <div />;
12+
}
13+
14+
export function Foo() {
15+
return <Bar />;
16+
}

0 commit comments

Comments
 (0)