Skip to content

Commit bedc110

Browse files
authored
Merge pull request #24311 from amcasey/GH23287
Preserve jsx imports even when the compiler option is not set
2 parents 7106a58 + 2e0cc63 commit bedc110

File tree

9 files changed

+103
-6
lines changed

9 files changed

+103
-6
lines changed

src/harness/unittests/organizeImports.ts

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,29 @@ D();
506506
},
507507
libFile);
508508

509-
testOrganizeImports("JsxFactoryUsed",
509+
testOrganizeImports("JsxFactoryUsedJsx",
510+
{
511+
path: "/test.jsx",
512+
content: `
513+
import { React, Other } from "react";
514+
515+
<div/>;
516+
`,
517+
},
518+
reactLibFile);
519+
520+
testOrganizeImports("JsxFactoryUsedJs",
521+
{
522+
path: "/test.js",
523+
content: `
524+
import { React, Other } from "react";
525+
526+
<div/>;
527+
`,
528+
},
529+
reactLibFile);
530+
531+
testOrganizeImports("JsxFactoryUsedTsx",
510532
{
511533
path: "/test.tsx",
512534
content: `
@@ -517,7 +539,39 @@ import { React, Other } from "react";
517539
},
518540
reactLibFile);
519541

520-
// This is descriptive, rather than normative
542+
// TS files are not JSX contexts, so the parser does not treat
543+
// `<div/>` as a JSX element.
544+
testOrganizeImports("JsxFactoryUsedTs",
545+
{
546+
path: "/test.ts",
547+
content: `
548+
import { React, Other } from "react";
549+
550+
<div/>;
551+
`,
552+
},
553+
reactLibFile);
554+
555+
testOrganizeImports("JsxFactoryUnusedJsx",
556+
{
557+
path: "/test.jsx",
558+
content: `
559+
import { React, Other } from "react";
560+
`,
561+
},
562+
reactLibFile);
563+
564+
// Note: Since the file extension does not end with "x", the jsx compiler option
565+
// will not be enabled. The import should be retained regardless.
566+
testOrganizeImports("JsxFactoryUnusedJs",
567+
{
568+
path: "/test.js",
569+
content: `
570+
import { React, Other } from "react";
571+
`,
572+
},
573+
reactLibFile);
574+
521575
testOrganizeImports("JsxFactoryUnusedTsx",
522576
{
523577
path: "/test.tsx",

src/services/organizeImports.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ namespace ts.OrganizeImports {
9292
function removeUnusedImports(oldImports: ReadonlyArray<ImportDeclaration>, sourceFile: SourceFile, program: Program) {
9393
const typeChecker = program.getTypeChecker();
9494
const jsxNamespace = typeChecker.getJsxNamespace();
95-
const jsxContext = sourceFile.languageVariant === LanguageVariant.JSX && program.getCompilerOptions().jsx;
95+
const jsxElementsPresent = !!(sourceFile.transformFlags & TransformFlags.ContainsJsx);
9696

9797
const usedImports: ImportDeclaration[] = [];
9898

@@ -138,8 +138,8 @@ namespace ts.OrganizeImports {
138138
return usedImports;
139139

140140
function isDeclarationUsed(identifier: Identifier) {
141-
// The JSX factory symbol is always used.
142-
return jsxContext && (identifier.text === jsxNamespace) || FindAllReferences.Core.isSymbolReferencedInFile(identifier, typeChecker, sourceFile);
141+
// The JSX factory symbol is always used if JSX elements are present - even if they are not allowed.
142+
return jsxElementsPresent && (identifier.text === jsxNamespace) || FindAllReferences.Core.isSymbolReferencedInFile(identifier, typeChecker, sourceFile);
143143
}
144144
}
145145

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// ==ORIGINAL==
2+
3+
import { React, Other } from "react";
4+
5+
// ==ORGANIZED==
6+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// ==ORIGINAL==
2+
3+
import { React, Other } from "react";
4+
5+
// ==ORGANIZED==
6+

tests/baselines/reference/organizeImports/JsxFactoryUnusedTsx.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ import { React, Other } from "react";
44

55
// ==ORGANIZED==
66

7-
import { React } from "react";
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// ==ORIGINAL==
2+
3+
import { React, Other } from "react";
4+
5+
<div/>;
6+
7+
// ==ORGANIZED==
8+
9+
import { React } from "react";
10+
11+
<div/>;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// ==ORIGINAL==
2+
3+
import { React, Other } from "react";
4+
5+
<div/>;
6+
7+
// ==ORGANIZED==
8+
9+
10+
<div/>;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// ==ORIGINAL==
2+
3+
import { React, Other } from "react";
4+
5+
<div/>;
6+
7+
// ==ORGANIZED==
8+
9+
import { React } from "react";
10+
11+
<div/>;

0 commit comments

Comments
 (0)