Skip to content

Commit 96a7b7b

Browse files
committed
Mark local "react" symbol as referenced since it might not be marked if there was no error message being displayed
Fixes microsoft#10312
1 parent 17cf435 commit 96a7b7b

4 files changed

Lines changed: 81 additions & 27 deletions

File tree

src/compiler/checker.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11070,14 +11070,20 @@ namespace ts {
1107011070
function checkJsxOpeningLikeElement(node: JsxOpeningLikeElement) {
1107111071
checkGrammarJsxElement(node);
1107211072
checkJsxPreconditions(node);
11073-
1107411073
// The reactNamespace symbol should be marked as 'used' so we don't incorrectly elide its import. And if there
1107511074
// is no reactNamespace symbol in scope when targeting React emit, we should issue an error.
1107611075
const reactRefErr = compilerOptions.jsx === JsxEmit.React ? Diagnostics.Cannot_find_name_0 : undefined;
1107711076
const reactNamespace = compilerOptions.reactNamespace ? compilerOptions.reactNamespace : "React";
1107811077
const reactSym = resolveName(node.tagName, reactNamespace, SymbolFlags.Value, reactRefErr, reactNamespace);
1107911078
if (reactSym) {
11080-
getSymbolLinks(reactSym).referenced = true;
11079+
// Mark local symbol as referenced here because it might not have been marked
11080+
// if jsx emit was not react as there wont be error being emitted
11081+
reactSym.isReferenced = true;
11082+
11083+
// If react symbol is alias, mark it as refereced
11084+
if (reactSym.flags & SymbolFlags.Alias && !isConstEnumOrConstEnumOnlyModule(resolveAlias(reactSym))) {
11085+
markAliasSymbolAsReferenced(reactSym);
11086+
}
1108111087
}
1108211088

1108311089
const targetAttributesType = getJsxElementAttributesType(node);

tests/baselines/reference/unusedImports13.errors.txt

Lines changed: 0 additions & 25 deletions
This file was deleted.
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+

0 commit comments

Comments
 (0)