mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-17 00:34:47 -05:00
Check TransformFlags.ContainsJsx, rather than LanguageVariant.JSX
This commit is contained in:
@@ -506,12 +506,47 @@ 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: `
|
||||
import { React, Other } from "react";
|
||||
|
||||
<div/>;
|
||||
`,
|
||||
},
|
||||
reactLibFile);
|
||||
|
||||
// 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/>;
|
||||
`,
|
||||
},
|
||||
@@ -537,7 +572,6 @@ import { React, Other } from "react";
|
||||
},
|
||||
reactLibFile);
|
||||
|
||||
// This is descriptive, rather than normative
|
||||
testOrganizeImports("JsxFactoryUnusedTsx",
|
||||
{
|
||||
path: "/test.tsx",
|
||||
|
||||
@@ -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;
|
||||
const jsxElementsPresent = !!(sourceFile.transformFlags & TransformFlags.ContainsJsx);
|
||||
|
||||
const usedImports: ImportDeclaration[] = [];
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user