mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Fix React auto-import blocking component imports in --preserve (#46368)
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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 />);`]);
|
||||
|
||||
Reference in New Issue
Block a user