Skip to content
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
58 changes: 56 additions & 2 deletions src/harness/unittests/organizeImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,29 @@ D();
},
libFile);

testOrganizeImports("JsxFactoryUsed",
testOrganizeImports("JsxFactoryUsedJsx",
{
path: "/test.jsx",
content: `
import { React, Other } from "react";

<div/>;
`,
},
reactLibFile);

testOrganizeImports("JsxFactoryUsedJs",
{
path: "/test.js",
content: `
import { React, Other } from "react";

<div/>;
`,
},
reactLibFile);

testOrganizeImports("JsxFactoryUsedTsx",
{
path: "/test.tsx",
content: `
Expand All @@ -517,7 +539,39 @@ import { React, Other } from "react";
},
reactLibFile);

// This is descriptive, rather than normative
// TS files are not JSX contexts, so the parser does not treat
// `<div/>` as a JSX element.
testOrganizeImports("JsxFactoryUsedTs",
{
path: "/test.ts",
content: `
import { React, Other } from "react";

<div/>;
`,
},
reactLibFile);

testOrganizeImports("JsxFactoryUnusedJsx",
{
path: "/test.jsx",
content: `
import { React, Other } from "react";
`,
},
reactLibFile);

// Note: Since the file extension does not end with "x", the jsx compiler option
// will not be enabled. The import should be retained regardless.
testOrganizeImports("JsxFactoryUnusedJs",
{
path: "/test.js",
content: `
import { React, Other } from "react";
`,
},
reactLibFile);

testOrganizeImports("JsxFactoryUnusedTsx",
{
path: "/test.tsx",
Expand Down
6 changes: 3 additions & 3 deletions src/services/organizeImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ namespace ts.OrganizeImports {
function removeUnusedImports(oldImports: ReadonlyArray<ImportDeclaration>, sourceFile: SourceFile, program: Program) {
const typeChecker = program.getTypeChecker();
const jsxNamespace = typeChecker.getJsxNamespace();
const jsxContext = sourceFile.languageVariant === LanguageVariant.JSX && program.getCompilerOptions().jsx;
const jsxElementsPresent = !!(sourceFile.transformFlags & TransformFlags.ContainsJsx);

const usedImports: ImportDeclaration[] = [];

Expand Down Expand Up @@ -138,8 +138,8 @@ namespace ts.OrganizeImports {
return usedImports;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// ==ORIGINAL==

import { React, Other } from "react";

// ==ORGANIZED==

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// ==ORIGINAL==

import { React, Other } from "react";

// ==ORGANIZED==

Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ import { React, Other } from "react";

// ==ORGANIZED==

import { React } from "react";
11 changes: 11 additions & 0 deletions tests/baselines/reference/organizeImports/JsxFactoryUsedJsx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// ==ORIGINAL==

import { React, Other } from "react";

<div/>;

// ==ORGANIZED==

import { React } from "react";

<div/>;
10 changes: 10 additions & 0 deletions tests/baselines/reference/organizeImports/JsxFactoryUsedTs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// ==ORIGINAL==

import { React, Other } from "react";

<div/>;

// ==ORGANIZED==


<div/>;
11 changes: 11 additions & 0 deletions tests/baselines/reference/organizeImports/JsxFactoryUsedTsx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// ==ORIGINAL==

import { React, Other } from "react";

<div/>;

// ==ORGANIZED==

import { React } from "react";

<div/>;