Skip to content

Commit 0debd2b

Browse files
committed
Merge pull request microsoft#6290 from RyanCavanaugh/fix5865
Tag 'react' import as used if it exists when targeting --jsx preserve
2 parents 6be0206 + 7aa8ba6 commit 0debd2b

10 files changed

Lines changed: 137 additions & 9 deletions

File tree

src/compiler/checker.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8363,14 +8363,12 @@ namespace ts {
83638363
checkGrammarJsxElement(node);
83648364
checkJsxPreconditions(node);
83658365

8366-
// If we're compiling under --jsx react, the symbol 'React' should
8367-
// be marked as 'used' so we don't incorrectly elide its import. And if there
8368-
// is no 'React' symbol in scope, we should issue an error.
8369-
if (compilerOptions.jsx === JsxEmit.React) {
8370-
const reactSym = resolveName(node.tagName, "React", SymbolFlags.Value, Diagnostics.Cannot_find_name_0, "React");
8371-
if (reactSym) {
8372-
getSymbolLinks(reactSym).referenced = true;
8373-
}
8366+
// The symbol 'React' should be marked as 'used' so we don't incorrectly elide its import. And if there
8367+
// is no 'React' symbol in scope when targeting React emit, we should issue an error.
8368+
const reactRefErr = compilerOptions.jsx === JsxEmit.React ? Diagnostics.Cannot_find_name_0 : undefined;
8369+
const reactSym = resolveName(node.tagName, "React", SymbolFlags.Value, reactRefErr, "React");
8370+
if (reactSym) {
8371+
getSymbolLinks(reactSym).referenced = true;
83748372
}
83758373

83768374
const targetAttributesType = getJsxElementAttributesType(node);

tests/baselines/reference/tsxPreserveEmit1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module M {
3434

3535

3636
//// [test.jsx]
37-
define(["require", "exports", 'react-router'], function (require, exports, ReactRouter) {
37+
define(["require", "exports", 'react', 'react-router'], function (require, exports, React, ReactRouter) {
3838
"use strict";
3939
var Route = ReactRouter.Route;
4040
var routes1 = <Route />;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [test.tsx]
2+
3+
4+
var Route: any;
5+
var routes1 = <Route />;
6+
7+
8+
//// [test.jsx]
9+
var Route;
10+
var routes1 = <Route />;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/conformance/jsx/test.tsx ===
2+
3+
4+
var Route: any;
5+
>Route : Symbol(Route, Decl(test.tsx, 2, 3))
6+
7+
var routes1 = <Route />;
8+
>routes1 : Symbol(routes1, Decl(test.tsx, 3, 3))
9+
>Route : Symbol(Route, Decl(test.tsx, 2, 3))
10+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/conformance/jsx/test.tsx ===
2+
3+
4+
var Route: any;
5+
>Route : any
6+
7+
var routes1 = <Route />;
8+
>routes1 : any
9+
><Route /> : any
10+
>Route : any
11+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//// [tests/cases/conformance/jsx/tsxPreserveEmit3.tsx] ////
2+
3+
//// [file.tsx]
4+
5+
declare module JSX {
6+
interface Element { }
7+
interface IntrinsicElements {
8+
[s: string]: any;
9+
}
10+
}
11+
12+
//// [test.d.ts]
13+
export var React;
14+
15+
//// [react-consumer.tsx]
16+
// This import should be elided
17+
import {React} from "./test";
18+
19+
20+
//// [file.jsx]
21+
//// [react-consumer.jsx]
22+
define(["require", "exports"], function (require, exports) {
23+
"use strict";
24+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/conformance/jsx/file.tsx ===
2+
3+
declare module JSX {
4+
>JSX : Symbol(JSX, Decl(file.tsx, 0, 0))
5+
6+
interface Element { }
7+
>Element : Symbol(Element, Decl(file.tsx, 1, 20))
8+
9+
interface IntrinsicElements {
10+
>IntrinsicElements : Symbol(IntrinsicElements, Decl(file.tsx, 2, 22))
11+
12+
[s: string]: any;
13+
>s : Symbol(s, Decl(file.tsx, 4, 3))
14+
}
15+
}
16+
17+
=== tests/cases/conformance/jsx/test.d.ts ===
18+
export var React;
19+
>React : Symbol(React, Decl(test.d.ts, 0, 10))
20+
21+
=== tests/cases/conformance/jsx/react-consumer.tsx ===
22+
// This import should be elided
23+
import {React} from "./test";
24+
>React : Symbol(React, Decl(react-consumer.tsx, 1, 8))
25+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/conformance/jsx/file.tsx ===
2+
3+
declare module JSX {
4+
>JSX : any
5+
6+
interface Element { }
7+
>Element : Element
8+
9+
interface IntrinsicElements {
10+
>IntrinsicElements : IntrinsicElements
11+
12+
[s: string]: any;
13+
>s : string
14+
}
15+
}
16+
17+
=== tests/cases/conformance/jsx/test.d.ts ===
18+
export var React;
19+
>React : any
20+
21+
=== tests/cases/conformance/jsx/react-consumer.tsx ===
22+
// This import should be elided
23+
import {React} from "./test";
24+
>React : any
25+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//@module: amd
2+
//@jsx: preserve
3+
//@target: ES5
4+
5+
//@Filename: test.tsx
6+
7+
var Route: any;
8+
var routes1 = <Route />;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@jsx: preserve
2+
//@module: amd
3+
4+
//@filename: file.tsx
5+
declare module JSX {
6+
interface Element { }
7+
interface IntrinsicElements {
8+
[s: string]: any;
9+
}
10+
}
11+
12+
//@filename: test.d.ts
13+
export var React;
14+
15+
//@filename: react-consumer.tsx
16+
// This import should be elided
17+
import {React} from "./test";

0 commit comments

Comments
 (0)