Skip to content

Commit 5135b83

Browse files
authored
Merge pull request #29266 from Microsoft/jsxNamespace
Use the sourceFile to determine the jsxNamespace at the location for organizingImports
2 parents dcf825e + 93249db commit 5135b83

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

src/services/codefixes/importFixes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ namespace ts.codefix {
375375
const symbolName = isJsxOpeningLikeElement(symbolToken.parent)
376376
&& symbolToken.parent.tagName === symbolToken
377377
&& (isIntrinsicJsxName(symbolToken.text) || checker.resolveName(symbolToken.text, symbolToken, SymbolFlags.All, /*excludeGlobals*/ false))
378-
? checker.getJsxNamespace()
378+
? checker.getJsxNamespace(sourceFile)
379379
: symbolToken.text;
380380
// "default" is a keyword and not a legal identifier for the import, so we don't expect it here
381381
Debug.assert(symbolName !== InternalSymbolName.Default);

src/services/organizeImports.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ namespace ts.OrganizeImports {
8383

8484
function removeUnusedImports(oldImports: ReadonlyArray<ImportDeclaration>, sourceFile: SourceFile, program: Program) {
8585
const typeChecker = program.getTypeChecker();
86-
const jsxNamespace = typeChecker.getJsxNamespace();
86+
const jsxNamespace = typeChecker.getJsxNamespace(sourceFile);
8787
const jsxElementsPresent = !!(sourceFile.transformFlags & TransformFlags.ContainsJsx);
8888

8989
const usedImports: ImportDeclaration[] = [];

src/testRunner/unittests/services/organizeImports.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,34 @@ import { React, Other } from "react";
596596
},
597597
reactLibFile);
598598

599+
testOrganizeImports("JsxPragmaTsx",
600+
{
601+
path: "/test.tsx",
602+
content: `/** @jsx jsx */
603+
604+
import { Global, jsx } from '@emotion/core';
605+
import * as React from 'react';
606+
607+
export const App: React.FunctionComponent = _ => <Global><h1>Hello!</h1></Global>
608+
`,
609+
},
610+
{
611+
path: "/@emotion/core/index.d.ts",
612+
content: `import { createElement } from 'react'
613+
export const jsx: typeof createElement;
614+
export function Global(props: any): ReactElement<any>;`
615+
},
616+
{
617+
path: reactLibFile.path,
618+
content: `${reactLibFile.content}
619+
export namespace React {
620+
interface FunctionComponent {
621+
}
622+
}
623+
`
624+
}
625+
);
626+
599627
describe("Exports", () => {
600628

601629
testOrganizeExports("MoveToTop",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// ==ORIGINAL==
2+
/** @jsx jsx */
3+
4+
import { Global, jsx } from '@emotion/core';
5+
import * as React from 'react';
6+
7+
export const App: React.FunctionComponent = _ => <Global><h1>Hello!</h1></Global>
8+
9+
// ==ORGANIZED==
10+
/** @jsx jsx */
11+
12+
import { Global, jsx } from '@emotion/core';
13+
import * as React from 'react';
14+
15+
export const App: React.FunctionComponent = _ => <Global><h1>Hello!</h1></Global>

0 commit comments

Comments
 (0)