Skip to content

Commit f848b62

Browse files
andrewbranchmprobst
authored andcommitted
Fix React auto-import blocking component imports in --preserve (microsoft#46368)
1 parent 5467405 commit f848b62

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/services/codefixes/importFixes.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -766,9 +766,13 @@ namespace ts.codefix {
766766
return { fixes, symbolName };
767767
}
768768

769+
function jsxModeNeedsExplicitImport(jsx: JsxEmit | undefined) {
770+
return jsx === JsxEmit.React || jsx === JsxEmit.ReactNative;
771+
}
772+
769773
function getSymbolName(sourceFile: SourceFile, checker: TypeChecker, symbolToken: Identifier, compilerOptions: CompilerOptions): string {
770774
const parent = symbolToken.parent;
771-
if ((isJsxOpeningLikeElement(parent) || isJsxClosingElement(parent)) && parent.tagName === symbolToken && compilerOptions.jsx !== JsxEmit.ReactJSX && compilerOptions.jsx !== JsxEmit.ReactJSXDev) {
775+
if ((isJsxOpeningLikeElement(parent) || isJsxClosingElement(parent)) && parent.tagName === symbolToken && jsxModeNeedsExplicitImport(compilerOptions.jsx)) {
772776
const jsxNamespace = checker.getJsxNamespace(sourceFile);
773777
if (isIntrinsicJsxName(symbolToken.text) || !checker.resolveName(jsxNamespace, parent, SymbolFlags.Value, /*excludeGlobals*/ true)) {
774778
return jsxNamespace;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @jsx: preserve
4+
// @module: commonjs
5+
6+
// @Filename: /node_modules/@types/react/index.d.ts
7+
//// declare namespace React {
8+
//// function createElement(): any;
9+
//// }
10+
//// export = React;
11+
//// export as namespace React;
12+
////
13+
//// declare global {
14+
//// namespace JSX {
15+
//// interface IntrinsicElements {}
16+
//// interface IntrinsicAttributes {}
17+
//// }
18+
//// }
19+
20+
// @Filename: /component.tsx
21+
//// import "react";
22+
//// export declare function Component(): any;
23+
24+
// @Filename: /index.tsx
25+
//// (<Component/**/ />);
26+
27+
goTo.marker("");
28+
verify.importFixAtPosition([`import { Component } from "./component";
29+
30+
(<Component />);`]);
31+

0 commit comments

Comments
 (0)