Skip to content

Commit fb60c9f

Browse files
authored
Use last detected JSX import source pragma as canonical source, rather than first (#43351)
1 parent 04205ca commit fb60c9f

6 files changed

+118
-1
lines changed

Diff for: src/compiler/utilities.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6054,7 +6054,7 @@ namespace ts {
60546054

60556055
export function getJSXImplicitImportBase(compilerOptions: CompilerOptions, file?: SourceFile): string | undefined {
60566056
const jsxImportSourcePragmas = file?.pragmas.get("jsximportsource");
6057-
const jsxImportSourcePragma = isArray(jsxImportSourcePragmas) ? jsxImportSourcePragmas[0] : jsxImportSourcePragmas;
6057+
const jsxImportSourcePragma = isArray(jsxImportSourcePragmas) ? jsxImportSourcePragmas[jsxImportSourcePragmas.length - 1] : jsxImportSourcePragmas;
60586058
return compilerOptions.jsx === JsxEmit.ReactJSX ||
60596059
compilerOptions.jsx === JsxEmit.ReactJSXDev ||
60606060
compilerOptions.jsxImportSource ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
tests/cases/compiler/jsxImportSourceNonPragmaComment.tsx(3,21): error TS2307: Cannot find module '@emotion/react' or its corresponding type declarations.
2+
tests/cases/compiler/jsxImportSourceNonPragmaComment.tsx(7,5): error TS2307: Cannot find module '@emotion/react/jsx-runtime' or its corresponding type declarations.
3+
4+
5+
==== tests/cases/compiler/jsxImportSourceNonPragmaComment.tsx (2 errors) ====
6+
/* eslint-disable react/react-in-jsx-scope -- Unaware of @jsxImportSource */
7+
/** @jsxImportSource @emotion/react */
8+
import { css } from "@emotion/react";
9+
~~~~~~~~~~~~~~~~
10+
!!! error TS2307: Cannot find module '@emotion/react' or its corresponding type declarations.
11+
12+
export default function Component() {
13+
return (
14+
<input
15+
~~~~~~
16+
css={css`
17+
~~~~~~~~~~~~~~~
18+
color: red;
19+
~~~~~~~~~~~~~~~~~~~
20+
`}
21+
~~~~~~~~
22+
/>
23+
~~~~~~
24+
!!! error TS2307: Cannot find module '@emotion/react/jsx-runtime' or its corresponding type declarations.
25+
);
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//// [jsxImportSourceNonPragmaComment.tsx]
2+
/* eslint-disable react/react-in-jsx-scope -- Unaware of @jsxImportSource */
3+
/** @jsxImportSource @emotion/react */
4+
import { css } from "@emotion/react";
5+
6+
export default function Component() {
7+
return (
8+
<input
9+
css={css`
10+
color: red;
11+
`}
12+
/>
13+
);
14+
}
15+
16+
//// [jsxImportSourceNonPragmaComment.jsx]
17+
"use strict";
18+
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
19+
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
20+
return cooked;
21+
};
22+
exports.__esModule = true;
23+
/* eslint-disable react/react-in-jsx-scope -- Unaware of @jsxImportSource */
24+
/** @jsxImportSource @emotion/react */
25+
var react_1 = require("@emotion/react");
26+
function Component() {
27+
return (<input css={(0, react_1.css)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n color: red;\n "], ["\n color: red;\n "])))}/>);
28+
}
29+
exports["default"] = Component;
30+
var templateObject_1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/compiler/jsxImportSourceNonPragmaComment.tsx ===
2+
/* eslint-disable react/react-in-jsx-scope -- Unaware of @jsxImportSource */
3+
/** @jsxImportSource @emotion/react */
4+
import { css } from "@emotion/react";
5+
>css : Symbol(css, Decl(jsxImportSourceNonPragmaComment.tsx, 2, 8))
6+
7+
export default function Component() {
8+
>Component : Symbol(Component, Decl(jsxImportSourceNonPragmaComment.tsx, 2, 37))
9+
10+
return (
11+
<input
12+
css={css`
13+
>css : Symbol(css, Decl(jsxImportSourceNonPragmaComment.tsx, 6, 10))
14+
>css : Symbol(css, Decl(jsxImportSourceNonPragmaComment.tsx, 2, 8))
15+
16+
color: red;
17+
`}
18+
/>
19+
);
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
=== tests/cases/compiler/jsxImportSourceNonPragmaComment.tsx ===
2+
/* eslint-disable react/react-in-jsx-scope -- Unaware of @jsxImportSource */
3+
/** @jsxImportSource @emotion/react */
4+
import { css } from "@emotion/react";
5+
>css : any
6+
7+
export default function Component() {
8+
>Component : () => any
9+
10+
return (
11+
>( <input css={css` color: red; `} /> ) : any
12+
13+
<input
14+
><input css={css` color: red; `} /> : any
15+
>input : any
16+
17+
css={css`
18+
>css : any
19+
>css` color: red; ` : any
20+
>css : any
21+
>` color: red; ` : "\n color: red;\n "
22+
23+
color: red;
24+
`}
25+
/>
26+
);
27+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @jsx: preserve
2+
/* eslint-disable react/react-in-jsx-scope -- Unaware of @jsxImportSource */
3+
/** @jsxImportSource @emotion/react */
4+
import { css } from "@emotion/react";
5+
6+
export default function Component() {
7+
return (
8+
<input
9+
css={css`
10+
color: red;
11+
`}
12+
/>
13+
);
14+
}

0 commit comments

Comments
 (0)