Skip to content

Commit 2e0cc63

Browse files
committed
Check TransformFlags.ContainsJsx, rather than LanguageVariant.JSX
1 parent edd31a1 commit 2e0cc63

File tree

9 files changed

+71
-8
lines changed

9 files changed

+71
-8
lines changed

src/harness/unittests/organizeImports.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,12 +506,47 @@ 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: `
513535
import { React, Other } from "react";
514536
537+
<div/>;
538+
`,
539+
},
540+
reactLibFile);
541+
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+
515550
<div/>;
516551
`,
517552
},
@@ -537,7 +572,6 @@ import { React, Other } from "react";
537572
},
538573
reactLibFile);
539574

540-
// This is descriptive, rather than normative
541575
testOrganizeImports("JsxFactoryUnusedTsx",
542576
{
543577
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;
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

tests/baselines/reference/organizeImports/JsxFactoryUnusedJs.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";

tests/baselines/reference/organizeImports/JsxFactoryUnusedJsx.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";

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)