Check TransformFlags.ContainsJsx, rather than LanguageVariant.JSX

This commit is contained in:
Andrew Casey
2018-05-22 10:51:19 -07:00
parent edd31a1505
commit 2e0cc63067
9 changed files with 71 additions and 8 deletions

View File

@@ -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",

View File

@@ -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);
}
}