Skip to content

fix(46803): unused React import doesn't get removed running organize imports with new react jsx transform #47247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
3 changes: 2 additions & 1 deletion src/services/organizeImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3335,5 +3335,9 @@ namespace ts {
};
}

export function jsxModeNeedsExplicitImport(jsx: JsxEmit | undefined) {
return jsx === JsxEmit.React || jsx === JsxEmit.ReactNative;
}

// #endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ import { React, Other } from "react";

// ==ORGANIZED==

import { React } from "react";

<div/>;
33 changes: 33 additions & 0 deletions tests/cases/fourslash/organizeImportsReactJsx.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 33 additions & 0 deletions tests/cases/fourslash/organizeImportsReactJsxDev.ts
Original file line number Diff line number Diff line change
@@ -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>`);