mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 12:51:30 -05:00
Merge pull request #11721 from Microsoft/unusedLocals
Mark "React" symbol as referenced
This commit is contained in:
@@ -11079,14 +11079,20 @@ namespace ts {
|
||||
function checkJsxOpeningLikeElement(node: JsxOpeningLikeElement) {
|
||||
checkGrammarJsxElement(node);
|
||||
checkJsxPreconditions(node);
|
||||
|
||||
// The reactNamespace symbol should be marked as 'used' so we don't incorrectly elide its import. And if there
|
||||
// is no reactNamespace symbol in scope when targeting React emit, we should issue an error.
|
||||
const reactRefErr = compilerOptions.jsx === JsxEmit.React ? Diagnostics.Cannot_find_name_0 : undefined;
|
||||
const reactNamespace = compilerOptions.reactNamespace ? compilerOptions.reactNamespace : "React";
|
||||
const reactSym = resolveName(node.tagName, reactNamespace, SymbolFlags.Value, reactRefErr, reactNamespace);
|
||||
if (reactSym) {
|
||||
getSymbolLinks(reactSym).referenced = true;
|
||||
// Mark local symbol as referenced here because it might not have been marked
|
||||
// if jsx emit was not react as there wont be error being emitted
|
||||
reactSym.isReferenced = true;
|
||||
|
||||
// If react symbol is alias, mark it as refereced
|
||||
if (reactSym.flags & SymbolFlags.Alias && !isConstEnumOrConstEnumOnlyModule(resolveAlias(reactSym))) {
|
||||
markAliasSymbolAsReferenced(reactSym);
|
||||
}
|
||||
}
|
||||
|
||||
const targetAttributesType = getJsxElementAttributesType(node);
|
||||
|
||||
27
tests/baselines/reference/unusedImports13.js
Normal file
27
tests/baselines/reference/unusedImports13.js
Normal file
@@ -0,0 +1,27 @@
|
||||
//// [tests/cases/compiler/unusedImports13.ts] ////
|
||||
|
||||
//// [foo.tsx]
|
||||
|
||||
import React = require("react");
|
||||
|
||||
export const FooComponent = <div></div>
|
||||
|
||||
//// [index.d.ts]
|
||||
export = React;
|
||||
export as namespace React;
|
||||
|
||||
declare namespace React {
|
||||
function createClass<P, S>(spec);
|
||||
}
|
||||
declare global {
|
||||
namespace JSX {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//// [foo.jsx]
|
||||
"use strict";
|
||||
var React = require("react");
|
||||
exports.FooComponent = <div></div>;
|
||||
36
tests/baselines/reference/unusedImports13.symbols
Normal file
36
tests/baselines/reference/unusedImports13.symbols
Normal file
@@ -0,0 +1,36 @@
|
||||
=== tests/cases/compiler/foo.tsx ===
|
||||
|
||||
import React = require("react");
|
||||
>React : Symbol(React, Decl(foo.tsx, 0, 0))
|
||||
|
||||
export const FooComponent = <div></div>
|
||||
>FooComponent : Symbol(FooComponent, Decl(foo.tsx, 3, 12))
|
||||
>div : Symbol(unknown)
|
||||
>div : Symbol(unknown)
|
||||
|
||||
=== tests/cases/compiler/node_modules/@types/react/index.d.ts ===
|
||||
export = React;
|
||||
>React : Symbol(React, Decl(index.d.ts, 1, 26))
|
||||
|
||||
export as namespace React;
|
||||
>React : Symbol(React, Decl(index.d.ts, 0, 15))
|
||||
|
||||
declare namespace React {
|
||||
>React : Symbol(React, Decl(index.d.ts, 1, 26))
|
||||
|
||||
function createClass<P, S>(spec);
|
||||
>createClass : Symbol(createClass, Decl(index.d.ts, 3, 25))
|
||||
>P : Symbol(P, Decl(index.d.ts, 4, 25))
|
||||
>S : Symbol(S, Decl(index.d.ts, 4, 27))
|
||||
>spec : Symbol(spec, Decl(index.d.ts, 4, 31))
|
||||
}
|
||||
declare global {
|
||||
>global : Symbol(global, Decl(index.d.ts, 5, 1))
|
||||
|
||||
namespace JSX {
|
||||
>JSX : Symbol(JSX, Decl(index.d.ts, 6, 16))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
37
tests/baselines/reference/unusedImports13.types
Normal file
37
tests/baselines/reference/unusedImports13.types
Normal file
@@ -0,0 +1,37 @@
|
||||
=== tests/cases/compiler/foo.tsx ===
|
||||
|
||||
import React = require("react");
|
||||
>React : typeof React
|
||||
|
||||
export const FooComponent = <div></div>
|
||||
>FooComponent : any
|
||||
><div></div> : any
|
||||
>div : any
|
||||
>div : any
|
||||
|
||||
=== tests/cases/compiler/node_modules/@types/react/index.d.ts ===
|
||||
export = React;
|
||||
>React : typeof React
|
||||
|
||||
export as namespace React;
|
||||
>React : typeof React
|
||||
|
||||
declare namespace React {
|
||||
>React : typeof React
|
||||
|
||||
function createClass<P, S>(spec);
|
||||
>createClass : <P, S>(spec: any) => any
|
||||
>P : P
|
||||
>S : S
|
||||
>spec : any
|
||||
}
|
||||
declare global {
|
||||
>global : any
|
||||
|
||||
namespace JSX {
|
||||
>JSX : any
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
27
tests/baselines/reference/unusedImports14.js
Normal file
27
tests/baselines/reference/unusedImports14.js
Normal file
@@ -0,0 +1,27 @@
|
||||
//// [tests/cases/compiler/unusedImports14.ts] ////
|
||||
|
||||
//// [foo.tsx]
|
||||
|
||||
import React = require("react");
|
||||
|
||||
export const FooComponent = <div></div>
|
||||
|
||||
//// [index.d.ts]
|
||||
export = React;
|
||||
export as namespace React;
|
||||
|
||||
declare namespace React {
|
||||
function createClass<P, S>(spec);
|
||||
}
|
||||
declare global {
|
||||
namespace JSX {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//// [foo.js]
|
||||
"use strict";
|
||||
var React = require("react");
|
||||
exports.FooComponent = React.createElement("div", null);
|
||||
36
tests/baselines/reference/unusedImports14.symbols
Normal file
36
tests/baselines/reference/unusedImports14.symbols
Normal file
@@ -0,0 +1,36 @@
|
||||
=== tests/cases/compiler/foo.tsx ===
|
||||
|
||||
import React = require("react");
|
||||
>React : Symbol(React, Decl(foo.tsx, 0, 0))
|
||||
|
||||
export const FooComponent = <div></div>
|
||||
>FooComponent : Symbol(FooComponent, Decl(foo.tsx, 3, 12))
|
||||
>div : Symbol(unknown)
|
||||
>div : Symbol(unknown)
|
||||
|
||||
=== tests/cases/compiler/node_modules/@types/react/index.d.ts ===
|
||||
export = React;
|
||||
>React : Symbol(React, Decl(index.d.ts, 1, 26))
|
||||
|
||||
export as namespace React;
|
||||
>React : Symbol(React, Decl(index.d.ts, 0, 15))
|
||||
|
||||
declare namespace React {
|
||||
>React : Symbol(React, Decl(index.d.ts, 1, 26))
|
||||
|
||||
function createClass<P, S>(spec);
|
||||
>createClass : Symbol(createClass, Decl(index.d.ts, 3, 25))
|
||||
>P : Symbol(P, Decl(index.d.ts, 4, 25))
|
||||
>S : Symbol(S, Decl(index.d.ts, 4, 27))
|
||||
>spec : Symbol(spec, Decl(index.d.ts, 4, 31))
|
||||
}
|
||||
declare global {
|
||||
>global : Symbol(global, Decl(index.d.ts, 5, 1))
|
||||
|
||||
namespace JSX {
|
||||
>JSX : Symbol(JSX, Decl(index.d.ts, 6, 16))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
37
tests/baselines/reference/unusedImports14.types
Normal file
37
tests/baselines/reference/unusedImports14.types
Normal file
@@ -0,0 +1,37 @@
|
||||
=== tests/cases/compiler/foo.tsx ===
|
||||
|
||||
import React = require("react");
|
||||
>React : typeof React
|
||||
|
||||
export const FooComponent = <div></div>
|
||||
>FooComponent : any
|
||||
><div></div> : any
|
||||
>div : any
|
||||
>div : any
|
||||
|
||||
=== tests/cases/compiler/node_modules/@types/react/index.d.ts ===
|
||||
export = React;
|
||||
>React : typeof React
|
||||
|
||||
export as namespace React;
|
||||
>React : typeof React
|
||||
|
||||
declare namespace React {
|
||||
>React : typeof React
|
||||
|
||||
function createClass<P, S>(spec);
|
||||
>createClass : <P, S>(spec: any) => any
|
||||
>P : P
|
||||
>S : S
|
||||
>spec : any
|
||||
}
|
||||
declare global {
|
||||
>global : any
|
||||
|
||||
namespace JSX {
|
||||
>JSX : any
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
27
tests/baselines/reference/unusedImports15.js
Normal file
27
tests/baselines/reference/unusedImports15.js
Normal file
@@ -0,0 +1,27 @@
|
||||
//// [tests/cases/compiler/unusedImports15.ts] ////
|
||||
|
||||
//// [foo.tsx]
|
||||
|
||||
import Element = require("react");
|
||||
|
||||
export const FooComponent = <div></div>
|
||||
|
||||
//// [index.d.ts]
|
||||
export = React;
|
||||
export as namespace React;
|
||||
|
||||
declare namespace React {
|
||||
function createClass<P, S>(spec);
|
||||
}
|
||||
declare global {
|
||||
namespace JSX {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//// [foo.jsx]
|
||||
"use strict";
|
||||
var Element = require("react");
|
||||
exports.FooComponent = <div></div>;
|
||||
36
tests/baselines/reference/unusedImports15.symbols
Normal file
36
tests/baselines/reference/unusedImports15.symbols
Normal file
@@ -0,0 +1,36 @@
|
||||
=== tests/cases/compiler/foo.tsx ===
|
||||
|
||||
import Element = require("react");
|
||||
>Element : Symbol(Element, Decl(foo.tsx, 0, 0))
|
||||
|
||||
export const FooComponent = <div></div>
|
||||
>FooComponent : Symbol(FooComponent, Decl(foo.tsx, 3, 12))
|
||||
>div : Symbol(unknown)
|
||||
>div : Symbol(unknown)
|
||||
|
||||
=== tests/cases/compiler/node_modules/@types/react/index.d.ts ===
|
||||
export = React;
|
||||
>React : Symbol(React, Decl(index.d.ts, 1, 26))
|
||||
|
||||
export as namespace React;
|
||||
>React : Symbol(React, Decl(index.d.ts, 0, 15))
|
||||
|
||||
declare namespace React {
|
||||
>React : Symbol(React, Decl(index.d.ts, 1, 26))
|
||||
|
||||
function createClass<P, S>(spec);
|
||||
>createClass : Symbol(createClass, Decl(index.d.ts, 3, 25))
|
||||
>P : Symbol(P, Decl(index.d.ts, 4, 25))
|
||||
>S : Symbol(S, Decl(index.d.ts, 4, 27))
|
||||
>spec : Symbol(spec, Decl(index.d.ts, 4, 31))
|
||||
}
|
||||
declare global {
|
||||
>global : Symbol(global, Decl(index.d.ts, 5, 1))
|
||||
|
||||
namespace JSX {
|
||||
>JSX : Symbol(JSX, Decl(index.d.ts, 6, 16))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
37
tests/baselines/reference/unusedImports15.types
Normal file
37
tests/baselines/reference/unusedImports15.types
Normal file
@@ -0,0 +1,37 @@
|
||||
=== tests/cases/compiler/foo.tsx ===
|
||||
|
||||
import Element = require("react");
|
||||
>Element : typeof Element
|
||||
|
||||
export const FooComponent = <div></div>
|
||||
>FooComponent : any
|
||||
><div></div> : any
|
||||
>div : any
|
||||
>div : any
|
||||
|
||||
=== tests/cases/compiler/node_modules/@types/react/index.d.ts ===
|
||||
export = React;
|
||||
>React : typeof React
|
||||
|
||||
export as namespace React;
|
||||
>React : typeof React
|
||||
|
||||
declare namespace React {
|
||||
>React : typeof React
|
||||
|
||||
function createClass<P, S>(spec);
|
||||
>createClass : <P, S>(spec: any) => any
|
||||
>P : P
|
||||
>S : S
|
||||
>spec : any
|
||||
}
|
||||
declare global {
|
||||
>global : any
|
||||
|
||||
namespace JSX {
|
||||
>JSX : any
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
27
tests/baselines/reference/unusedImports16.js
Normal file
27
tests/baselines/reference/unusedImports16.js
Normal file
@@ -0,0 +1,27 @@
|
||||
//// [tests/cases/compiler/unusedImports16.ts] ////
|
||||
|
||||
//// [foo.tsx]
|
||||
|
||||
import Element = require("react");
|
||||
|
||||
export const FooComponent = <div></div>
|
||||
|
||||
//// [index.d.ts]
|
||||
export = React;
|
||||
export as namespace React;
|
||||
|
||||
declare namespace React {
|
||||
function createClass<P, S>(spec);
|
||||
}
|
||||
declare global {
|
||||
namespace JSX {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//// [foo.js]
|
||||
"use strict";
|
||||
var Element = require("react");
|
||||
exports.FooComponent = Element.createElement("div", null);
|
||||
36
tests/baselines/reference/unusedImports16.symbols
Normal file
36
tests/baselines/reference/unusedImports16.symbols
Normal file
@@ -0,0 +1,36 @@
|
||||
=== tests/cases/compiler/foo.tsx ===
|
||||
|
||||
import Element = require("react");
|
||||
>Element : Symbol(Element, Decl(foo.tsx, 0, 0))
|
||||
|
||||
export const FooComponent = <div></div>
|
||||
>FooComponent : Symbol(FooComponent, Decl(foo.tsx, 3, 12))
|
||||
>div : Symbol(unknown)
|
||||
>div : Symbol(unknown)
|
||||
|
||||
=== tests/cases/compiler/node_modules/@types/react/index.d.ts ===
|
||||
export = React;
|
||||
>React : Symbol(React, Decl(index.d.ts, 1, 26))
|
||||
|
||||
export as namespace React;
|
||||
>React : Symbol(React, Decl(index.d.ts, 0, 15))
|
||||
|
||||
declare namespace React {
|
||||
>React : Symbol(React, Decl(index.d.ts, 1, 26))
|
||||
|
||||
function createClass<P, S>(spec);
|
||||
>createClass : Symbol(createClass, Decl(index.d.ts, 3, 25))
|
||||
>P : Symbol(P, Decl(index.d.ts, 4, 25))
|
||||
>S : Symbol(S, Decl(index.d.ts, 4, 27))
|
||||
>spec : Symbol(spec, Decl(index.d.ts, 4, 31))
|
||||
}
|
||||
declare global {
|
||||
>global : Symbol(global, Decl(index.d.ts, 5, 1))
|
||||
|
||||
namespace JSX {
|
||||
>JSX : Symbol(JSX, Decl(index.d.ts, 6, 16))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
37
tests/baselines/reference/unusedImports16.types
Normal file
37
tests/baselines/reference/unusedImports16.types
Normal file
@@ -0,0 +1,37 @@
|
||||
=== tests/cases/compiler/foo.tsx ===
|
||||
|
||||
import Element = require("react");
|
||||
>Element : typeof Element
|
||||
|
||||
export const FooComponent = <div></div>
|
||||
>FooComponent : any
|
||||
><div></div> : any
|
||||
>div : any
|
||||
>div : any
|
||||
|
||||
=== tests/cases/compiler/node_modules/@types/react/index.d.ts ===
|
||||
export = React;
|
||||
>React : typeof React
|
||||
|
||||
export as namespace React;
|
||||
>React : typeof React
|
||||
|
||||
declare namespace React {
|
||||
>React : typeof React
|
||||
|
||||
function createClass<P, S>(spec);
|
||||
>createClass : <P, S>(spec: any) => any
|
||||
>P : P
|
||||
>S : S
|
||||
>spec : any
|
||||
}
|
||||
declare global {
|
||||
>global : any
|
||||
|
||||
namespace JSX {
|
||||
>JSX : any
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
22
tests/cases/compiler/unusedImports13.ts
Normal file
22
tests/cases/compiler/unusedImports13.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
//@noUnusedLocals:true
|
||||
//@module: commonjs
|
||||
//@jsx: preserve
|
||||
|
||||
// @filename: foo.tsx
|
||||
import React = require("react");
|
||||
|
||||
export const FooComponent = <div></div>
|
||||
|
||||
// @filename: node_modules/@types/react/index.d.ts
|
||||
export = React;
|
||||
export as namespace React;
|
||||
|
||||
declare namespace React {
|
||||
function createClass<P, S>(spec);
|
||||
}
|
||||
declare global {
|
||||
namespace JSX {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
22
tests/cases/compiler/unusedImports14.ts
Normal file
22
tests/cases/compiler/unusedImports14.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
//@noUnusedLocals:true
|
||||
//@module: commonjs
|
||||
//@jsx: react
|
||||
|
||||
// @filename: foo.tsx
|
||||
import React = require("react");
|
||||
|
||||
export const FooComponent = <div></div>
|
||||
|
||||
// @filename: node_modules/@types/react/index.d.ts
|
||||
export = React;
|
||||
export as namespace React;
|
||||
|
||||
declare namespace React {
|
||||
function createClass<P, S>(spec);
|
||||
}
|
||||
declare global {
|
||||
namespace JSX {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
23
tests/cases/compiler/unusedImports15.ts
Normal file
23
tests/cases/compiler/unusedImports15.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
//@noUnusedLocals:true
|
||||
//@module: commonjs
|
||||
//@reactNamespace: Element
|
||||
//@jsx: preserve
|
||||
|
||||
// @filename: foo.tsx
|
||||
import Element = require("react");
|
||||
|
||||
export const FooComponent = <div></div>
|
||||
|
||||
// @filename: node_modules/@types/react/index.d.ts
|
||||
export = React;
|
||||
export as namespace React;
|
||||
|
||||
declare namespace React {
|
||||
function createClass<P, S>(spec);
|
||||
}
|
||||
declare global {
|
||||
namespace JSX {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
23
tests/cases/compiler/unusedImports16.ts
Normal file
23
tests/cases/compiler/unusedImports16.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
//@noUnusedLocals:true
|
||||
//@module: commonjs
|
||||
//@reactNamespace: Element
|
||||
//@jsx: react
|
||||
|
||||
// @filename: foo.tsx
|
||||
import Element = require("react");
|
||||
|
||||
export const FooComponent = <div></div>
|
||||
|
||||
// @filename: node_modules/@types/react/index.d.ts
|
||||
export = React;
|
||||
export as namespace React;
|
||||
|
||||
declare namespace React {
|
||||
function createClass<P, S>(spec);
|
||||
}
|
||||
declare global {
|
||||
namespace JSX {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user