Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions src/harness/unittests/organizeImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,26 @@ D();
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);
Expand Down
2 changes: 1 addition & 1 deletion 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 jsxContext = sourceFile.languageVariant === LanguageVariant.JSX;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wounder if a better check here is sourceFile.transformFlags & TransformFlags.ContainsJsx !== 0.. this way we only keep it if we know at least one JSX element exist..


const usedImports: ImportDeclaration[] = [];

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

import { React, Other } from "react";

// ==ORGANIZED==

import { React } from "react";
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// ==ORIGINAL==

import { React, Other } from "react";

// ==ORGANIZED==

import { React } from "react";