Skip to content

Commit 67873ca

Browse files
authored
Merge pull request #11721 from Microsoft/unusedLocals
Mark "React" symbol as referenced
2 parents 66857b5 + 23e9e0b commit 67873ca

17 files changed

+498
-2
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11079,14 +11079,20 @@ namespace ts {
1107911079
function checkJsxOpeningLikeElement(node: JsxOpeningLikeElement) {
1108011080
checkGrammarJsxElement(node);
1108111081
checkJsxPreconditions(node);
11082-
1108311082
// The reactNamespace symbol should be marked as 'used' so we don't incorrectly elide its import. And if there
1108411083
// is no reactNamespace symbol in scope when targeting React emit, we should issue an error.
1108511084
const reactRefErr = compilerOptions.jsx === JsxEmit.React ? Diagnostics.Cannot_find_name_0 : undefined;
1108611085
const reactNamespace = compilerOptions.reactNamespace ? compilerOptions.reactNamespace : "React";
1108711086
const reactSym = resolveName(node.tagName, reactNamespace, SymbolFlags.Value, reactRefErr, reactNamespace);
1108811087
if (reactSym) {
11089-
getSymbolLinks(reactSym).referenced = true;
11088+
// Mark local symbol as referenced here because it might not have been marked
11089+
// if jsx emit was not react as there wont be error being emitted
11090+
reactSym.isReferenced = true;
11091+
11092+
// If react symbol is alias, mark it as refereced
11093+
if (reactSym.flags & SymbolFlags.Alias && !isConstEnumOrConstEnumOnlyModule(resolveAlias(reactSym))) {
11094+
markAliasSymbolAsReferenced(reactSym);
11095+
}
1109011096
}
1109111097

1109211098
const targetAttributesType = getJsxElementAttributesType(node);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [tests/cases/compiler/unusedImports13.ts] ////
2+
3+
//// [foo.tsx]
4+
5+
import React = require("react");
6+
7+
export const FooComponent = <div></div>
8+
9+
//// [index.d.ts]
10+
export = React;
11+
export as namespace React;
12+
13+
declare namespace React {
14+
function createClass<P, S>(spec);
15+
}
16+
declare global {
17+
namespace JSX {
18+
}
19+
}
20+
21+
22+
23+
24+
//// [foo.jsx]
25+
"use strict";
26+
var React = require("react");
27+
exports.FooComponent = <div></div>;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
=== tests/cases/compiler/foo.tsx ===
2+
3+
import React = require("react");
4+
>React : Symbol(React, Decl(foo.tsx, 0, 0))
5+
6+
export const FooComponent = <div></div>
7+
>FooComponent : Symbol(FooComponent, Decl(foo.tsx, 3, 12))
8+
>div : Symbol(unknown)
9+
>div : Symbol(unknown)
10+
11+
=== tests/cases/compiler/node_modules/@types/react/index.d.ts ===
12+
export = React;
13+
>React : Symbol(React, Decl(index.d.ts, 1, 26))
14+
15+
export as namespace React;
16+
>React : Symbol(React, Decl(index.d.ts, 0, 15))
17+
18+
declare namespace React {
19+
>React : Symbol(React, Decl(index.d.ts, 1, 26))
20+
21+
function createClass<P, S>(spec);
22+
>createClass : Symbol(createClass, Decl(index.d.ts, 3, 25))
23+
>P : Symbol(P, Decl(index.d.ts, 4, 25))
24+
>S : Symbol(S, Decl(index.d.ts, 4, 27))
25+
>spec : Symbol(spec, Decl(index.d.ts, 4, 31))
26+
}
27+
declare global {
28+
>global : Symbol(global, Decl(index.d.ts, 5, 1))
29+
30+
namespace JSX {
31+
>JSX : Symbol(JSX, Decl(index.d.ts, 6, 16))
32+
}
33+
}
34+
35+
36+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
=== tests/cases/compiler/foo.tsx ===
2+
3+
import React = require("react");
4+
>React : typeof React
5+
6+
export const FooComponent = <div></div>
7+
>FooComponent : any
8+
><div></div> : any
9+
>div : any
10+
>div : any
11+
12+
=== tests/cases/compiler/node_modules/@types/react/index.d.ts ===
13+
export = React;
14+
>React : typeof React
15+
16+
export as namespace React;
17+
>React : typeof React
18+
19+
declare namespace React {
20+
>React : typeof React
21+
22+
function createClass<P, S>(spec);
23+
>createClass : <P, S>(spec: any) => any
24+
>P : P
25+
>S : S
26+
>spec : any
27+
}
28+
declare global {
29+
>global : any
30+
31+
namespace JSX {
32+
>JSX : any
33+
}
34+
}
35+
36+
37+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [tests/cases/compiler/unusedImports14.ts] ////
2+
3+
//// [foo.tsx]
4+
5+
import React = require("react");
6+
7+
export const FooComponent = <div></div>
8+
9+
//// [index.d.ts]
10+
export = React;
11+
export as namespace React;
12+
13+
declare namespace React {
14+
function createClass<P, S>(spec);
15+
}
16+
declare global {
17+
namespace JSX {
18+
}
19+
}
20+
21+
22+
23+
24+
//// [foo.js]
25+
"use strict";
26+
var React = require("react");
27+
exports.FooComponent = React.createElement("div", null);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
=== tests/cases/compiler/foo.tsx ===
2+
3+
import React = require("react");
4+
>React : Symbol(React, Decl(foo.tsx, 0, 0))
5+
6+
export const FooComponent = <div></div>
7+
>FooComponent : Symbol(FooComponent, Decl(foo.tsx, 3, 12))
8+
>div : Symbol(unknown)
9+
>div : Symbol(unknown)
10+
11+
=== tests/cases/compiler/node_modules/@types/react/index.d.ts ===
12+
export = React;
13+
>React : Symbol(React, Decl(index.d.ts, 1, 26))
14+
15+
export as namespace React;
16+
>React : Symbol(React, Decl(index.d.ts, 0, 15))
17+
18+
declare namespace React {
19+
>React : Symbol(React, Decl(index.d.ts, 1, 26))
20+
21+
function createClass<P, S>(spec);
22+
>createClass : Symbol(createClass, Decl(index.d.ts, 3, 25))
23+
>P : Symbol(P, Decl(index.d.ts, 4, 25))
24+
>S : Symbol(S, Decl(index.d.ts, 4, 27))
25+
>spec : Symbol(spec, Decl(index.d.ts, 4, 31))
26+
}
27+
declare global {
28+
>global : Symbol(global, Decl(index.d.ts, 5, 1))
29+
30+
namespace JSX {
31+
>JSX : Symbol(JSX, Decl(index.d.ts, 6, 16))
32+
}
33+
}
34+
35+
36+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
=== tests/cases/compiler/foo.tsx ===
2+
3+
import React = require("react");
4+
>React : typeof React
5+
6+
export const FooComponent = <div></div>
7+
>FooComponent : any
8+
><div></div> : any
9+
>div : any
10+
>div : any
11+
12+
=== tests/cases/compiler/node_modules/@types/react/index.d.ts ===
13+
export = React;
14+
>React : typeof React
15+
16+
export as namespace React;
17+
>React : typeof React
18+
19+
declare namespace React {
20+
>React : typeof React
21+
22+
function createClass<P, S>(spec);
23+
>createClass : <P, S>(spec: any) => any
24+
>P : P
25+
>S : S
26+
>spec : any
27+
}
28+
declare global {
29+
>global : any
30+
31+
namespace JSX {
32+
>JSX : any
33+
}
34+
}
35+
36+
37+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [tests/cases/compiler/unusedImports15.ts] ////
2+
3+
//// [foo.tsx]
4+
5+
import Element = require("react");
6+
7+
export const FooComponent = <div></div>
8+
9+
//// [index.d.ts]
10+
export = React;
11+
export as namespace React;
12+
13+
declare namespace React {
14+
function createClass<P, S>(spec);
15+
}
16+
declare global {
17+
namespace JSX {
18+
}
19+
}
20+
21+
22+
23+
24+
//// [foo.jsx]
25+
"use strict";
26+
var Element = require("react");
27+
exports.FooComponent = <div></div>;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
=== tests/cases/compiler/foo.tsx ===
2+
3+
import Element = require("react");
4+
>Element : Symbol(Element, Decl(foo.tsx, 0, 0))
5+
6+
export const FooComponent = <div></div>
7+
>FooComponent : Symbol(FooComponent, Decl(foo.tsx, 3, 12))
8+
>div : Symbol(unknown)
9+
>div : Symbol(unknown)
10+
11+
=== tests/cases/compiler/node_modules/@types/react/index.d.ts ===
12+
export = React;
13+
>React : Symbol(React, Decl(index.d.ts, 1, 26))
14+
15+
export as namespace React;
16+
>React : Symbol(React, Decl(index.d.ts, 0, 15))
17+
18+
declare namespace React {
19+
>React : Symbol(React, Decl(index.d.ts, 1, 26))
20+
21+
function createClass<P, S>(spec);
22+
>createClass : Symbol(createClass, Decl(index.d.ts, 3, 25))
23+
>P : Symbol(P, Decl(index.d.ts, 4, 25))
24+
>S : Symbol(S, Decl(index.d.ts, 4, 27))
25+
>spec : Symbol(spec, Decl(index.d.ts, 4, 31))
26+
}
27+
declare global {
28+
>global : Symbol(global, Decl(index.d.ts, 5, 1))
29+
30+
namespace JSX {
31+
>JSX : Symbol(JSX, Decl(index.d.ts, 6, 16))
32+
}
33+
}
34+
35+
36+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
=== tests/cases/compiler/foo.tsx ===
2+
3+
import Element = require("react");
4+
>Element : typeof Element
5+
6+
export const FooComponent = <div></div>
7+
>FooComponent : any
8+
><div></div> : any
9+
>div : any
10+
>div : any
11+
12+
=== tests/cases/compiler/node_modules/@types/react/index.d.ts ===
13+
export = React;
14+
>React : typeof React
15+
16+
export as namespace React;
17+
>React : typeof React
18+
19+
declare namespace React {
20+
>React : typeof React
21+
22+
function createClass<P, S>(spec);
23+
>createClass : <P, S>(spec: any) => any
24+
>P : P
25+
>S : S
26+
>spec : any
27+
}
28+
declare global {
29+
>global : any
30+
31+
namespace JSX {
32+
>JSX : any
33+
}
34+
}
35+
36+
37+

0 commit comments

Comments
 (0)