Skip to content

Commit

Permalink
Fix React auto-import blocking component imports in --preserve (micro…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch authored and mprobst committed Jan 10, 2022
1 parent 5467405 commit f848b62
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions tests/cases/fourslash/importNameCodeFix_jsxReact17.ts
Original file line number Diff line number Diff line change
@@ -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 />);`]);

0 comments on commit f848b62

Please sign in to comment.