mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-04 21:53:42 -06:00
fix(46803): remove unused react imports with enabled react-jsxdev, react-jsx (#47247)
This commit is contained in:
parent
4c80036b7d
commit
73506f3512
@ -869,10 +869,6 @@ namespace ts.codefix {
|
||||
return { kind: ImportFixKind.PromoteTypeOnly, typeOnlyAliasDeclaration };
|
||||
}
|
||||
|
||||
function jsxModeNeedsExplicitImport(jsx: JsxEmit | undefined) {
|
||||
return jsx === JsxEmit.React || jsx === JsxEmit.ReactNative;
|
||||
}
|
||||
|
||||
function getSymbolName(sourceFile: SourceFile, checker: TypeChecker, symbolToken: Identifier, compilerOptions: CompilerOptions): string {
|
||||
const parent = symbolToken.parent;
|
||||
if ((isJsxOpeningLikeElement(parent) || isJsxClosingElement(parent)) && parent.tagName === symbolToken && jsxModeNeedsExplicitImport(compilerOptions.jsx)) {
|
||||
|
||||
@ -95,6 +95,7 @@ namespace ts.OrganizeImports {
|
||||
}
|
||||
|
||||
const typeChecker = program.getTypeChecker();
|
||||
const compilerOptions = program.getCompilerOptions();
|
||||
const jsxNamespace = typeChecker.getJsxNamespace(sourceFile);
|
||||
const jsxFragmentFactory = typeChecker.getJsxFragmentFactory(sourceFile);
|
||||
const jsxElementsPresent = !!(sourceFile.transformFlags & TransformFlags.ContainsJsx);
|
||||
@ -162,7 +163,7 @@ namespace ts.OrganizeImports {
|
||||
|
||||
function isDeclarationUsed(identifier: Identifier) {
|
||||
// The JSX factory symbol is always used if JSX elements are present - even if they are not allowed.
|
||||
return jsxElementsPresent && (identifier.text === jsxNamespace || jsxFragmentFactory && identifier.text === jsxFragmentFactory) ||
|
||||
return jsxElementsPresent && (identifier.text === jsxNamespace || jsxFragmentFactory && identifier.text === jsxFragmentFactory) && jsxModeNeedsExplicitImport(compilerOptions.jsx) ||
|
||||
FindAllReferences.Core.isSymbolReferencedInFile(identifier, typeChecker, sourceFile);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3335,5 +3335,9 @@ namespace ts {
|
||||
};
|
||||
}
|
||||
|
||||
export function jsxModeNeedsExplicitImport(jsx: JsxEmit | undefined) {
|
||||
return jsx === JsxEmit.React || jsx === JsxEmit.ReactNative;
|
||||
}
|
||||
|
||||
// #endregion
|
||||
}
|
||||
|
||||
@ -6,6 +6,5 @@ import { React, Other } from "react";
|
||||
|
||||
// ==ORGANIZED==
|
||||
|
||||
import { React } from "react";
|
||||
|
||||
<div/>;
|
||||
|
||||
33
tests/cases/fourslash/organizeImportsReactJsx.ts
Normal file
33
tests/cases/fourslash/organizeImportsReactJsx.ts
Normal file
@ -0,0 +1,33 @@
|
||||
/// <reference path='fourslash.ts' />
|
||||
|
||||
// @allowSyntheticDefaultImports: true
|
||||
// @moduleResolution: node
|
||||
// @noUnusedLocals: true
|
||||
// @target: es2018
|
||||
// @jsx: react-jsx
|
||||
|
||||
// @filename: test.tsx
|
||||
////import React from 'react';
|
||||
////export default () => <div></div>
|
||||
|
||||
// @filename: node_modules/react/package.json
|
||||
////{
|
||||
//// "name": "react",
|
||||
//// "types": "index.d.ts",
|
||||
////}
|
||||
|
||||
// @filename: node_modules/react/index.d.ts
|
||||
////export = React;
|
||||
////declare namespace JSX {
|
||||
//// interface IntrinsicElements { [x: string]: any; }
|
||||
////}
|
||||
////declare namespace React {}
|
||||
|
||||
// @filename: node_modules/react/jsx-runtime.d.ts
|
||||
////import './';
|
||||
|
||||
// @filename: node_modules/react/jsx-dev-runtime.d.ts
|
||||
////import './';
|
||||
|
||||
goTo.file("test.tsx");
|
||||
verify.organizeImports(`export default () => <div></div>`);
|
||||
33
tests/cases/fourslash/organizeImportsReactJsxDev.ts
Normal file
33
tests/cases/fourslash/organizeImportsReactJsxDev.ts
Normal file
@ -0,0 +1,33 @@
|
||||
/// <reference path='fourslash.ts' />
|
||||
|
||||
// @allowSyntheticDefaultImports: true
|
||||
// @moduleResolution: node
|
||||
// @noUnusedLocals: true
|
||||
// @target: es2018
|
||||
// @jsx: react-jsxdev
|
||||
|
||||
// @filename: test.tsx
|
||||
////import React from 'react';
|
||||
////export default () => <div></div>
|
||||
|
||||
// @filename: node_modules/react/package.json
|
||||
////{
|
||||
//// "name": "react",
|
||||
//// "types": "index.d.ts",
|
||||
////}
|
||||
|
||||
// @filename: node_modules/react/index.d.ts
|
||||
////export = React;
|
||||
////declare namespace JSX {
|
||||
//// interface IntrinsicElements { [x: string]: any; }
|
||||
////}
|
||||
////declare namespace React {}
|
||||
|
||||
// @filename: node_modules/react/jsx-runtime.d.ts
|
||||
////import './';
|
||||
|
||||
// @filename: node_modules/react/jsx-dev-runtime.d.ts
|
||||
////import './';
|
||||
|
||||
goTo.file("test.tsx");
|
||||
verify.organizeImports(`export default () => <div></div>`);
|
||||
Loading…
x
Reference in New Issue
Block a user