Fix React auto-import blocking component imports in --preserve (#46368)

This commit is contained in:
Andrew Branch 2021-10-15 09:31:47 -07:00 committed by GitHub
parent cf9d38fe52
commit b1f39a705e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 1 deletions

View file

@ -766,9 +766,13 @@ namespace ts.codefix {
return { fixes, symbolName };
}
function jsxModeNeedsExplicitImport(jsx: JsxEmit | undefined) {
return jsx === JsxEmit.React || jsx === JsxEmit.ReactNative;
}
function getSymbolName(sourceFile: SourceFile, checker: TypeChecker, symbolToken: Identifier, compilerOptions: CompilerOptions): string {
const parent = symbolToken.parent;
if ((isJsxOpeningLikeElement(parent) || isJsxClosingElement(parent)) && parent.tagName === symbolToken && compilerOptions.jsx !== JsxEmit.ReactJSX && compilerOptions.jsx !== JsxEmit.ReactJSXDev) {
if ((isJsxOpeningLikeElement(parent) || isJsxClosingElement(parent)) && parent.tagName === symbolToken && jsxModeNeedsExplicitImport(compilerOptions.jsx)) {
const jsxNamespace = checker.getJsxNamespace(sourceFile);
if (isIntrinsicJsxName(symbolToken.text) || !checker.resolveName(jsxNamespace, parent, SymbolFlags.Value, /*excludeGlobals*/ true)) {
return jsxNamespace;

View file

@ -0,0 +1,31 @@
/// <reference path="fourslash.ts" />
// @jsx: preserve
// @module: commonjs
// @Filename: /node_modules/@types/react/index.d.ts
//// declare namespace React {
//// function createElement(): any;
//// }
//// export = React;
//// export as namespace React;
////
//// declare global {
//// namespace JSX {
//// interface IntrinsicElements {}
//// interface IntrinsicAttributes {}
//// }
//// }
// @Filename: /component.tsx
//// import "react";
//// export declare function Component(): any;
// @Filename: /index.tsx
//// (<Component/**/ />);
goTo.marker("");
verify.importFixAtPosition([`import { Component } from "./component";
(<Component />);`]);