diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts
index 9347310350d..2faa3f1f879 100644
--- a/src/compiler/checker.ts
+++ b/src/compiler/checker.ts
@@ -3467,8 +3467,14 @@ namespace ts {
const mode = contextSpecifier && isStringLiteralLike(contextSpecifier) ? getModeForUsageLocation(currentSourceFile, contextSpecifier) : currentSourceFile.impliedNodeFormat;
const resolvedModule = getResolvedModule(currentSourceFile, moduleReference, mode);
const resolutionDiagnostic = resolvedModule && getResolutionDiagnostic(compilerOptions, resolvedModule);
- const sourceFile = resolvedModule && !resolutionDiagnostic && host.getSourceFile(resolvedModule.resolvedFileName);
+ const sourceFile = resolvedModule
+ && (!resolutionDiagnostic || resolutionDiagnostic === Diagnostics.Module_0_was_resolved_to_1_but_jsx_is_not_set)
+ && host.getSourceFile(resolvedModule.resolvedFileName);
if (sourceFile) {
+ // If there's a resolutionDiagnostic we need to report it even if a sourceFile is found.
+ if (resolutionDiagnostic) {
+ error(errorNode, resolutionDiagnostic, moduleReference, resolvedModule.resolvedFileName);
+ }
if (sourceFile.symbol) {
if (resolvedModule.isExternalLibraryImport && !resolutionExtensionIsTSOrJson(resolvedModule.extension)) {
errorOnImplicitAnyModule(/*isError*/ false, errorNode, resolvedModule, moduleReference);
diff --git a/tests/baselines/reference/checkJsxNotSetError.errors.txt b/tests/baselines/reference/checkJsxNotSetError.errors.txt
new file mode 100644
index 00000000000..55dbe1b239f
--- /dev/null
+++ b/tests/baselines/reference/checkJsxNotSetError.errors.txt
@@ -0,0 +1,20 @@
+/bar.jsx(1,17): error TS6142: Module '/foo' was resolved to '/foo.jsx', but '--jsx' is not set.
+/bar.jsx(2,11): error TS17004: Cannot use JSX unless the '--jsx' flag is provided.
+/foo.jsx(2,5): error TS17004: Cannot use JSX unless the '--jsx' flag is provided.
+
+
+==== /foo.jsx (1 errors) ====
+ const Foo = () => (
+
foo
+ ~~~~~
+!!! error TS17004: Cannot use JSX unless the '--jsx' flag is provided.
+ );
+ export default Foo;
+
+==== /bar.jsx (2 errors) ====
+ import Foo from '/foo';
+ ~~~~~~
+!!! error TS6142: Module '/foo' was resolved to '/foo.jsx', but '--jsx' is not set.
+ const a =
+ ~~~~~~~
+!!! error TS17004: Cannot use JSX unless the '--jsx' flag is provided.
\ No newline at end of file
diff --git a/tests/baselines/reference/checkJsxNotSetError.js b/tests/baselines/reference/checkJsxNotSetError.js
new file mode 100644
index 00000000000..5273963c87a
--- /dev/null
+++ b/tests/baselines/reference/checkJsxNotSetError.js
@@ -0,0 +1,22 @@
+//// [tests/cases/compiler/checkJsxNotSetError.ts] ////
+
+//// [foo.jsx]
+const Foo = () => (
+ foo
+);
+export default Foo;
+
+//// [bar.jsx]
+import Foo from '/foo';
+const a =
+
+//// [foo.js]
+"use strict";
+exports.__esModule = true;
+var Foo = function () { return (foo
); };
+exports["default"] = Foo;
+//// [bar.js]
+"use strict";
+exports.__esModule = true;
+var foo_1 = require("/foo");
+var a = ;
diff --git a/tests/baselines/reference/checkJsxNotSetError.symbols b/tests/baselines/reference/checkJsxNotSetError.symbols
new file mode 100644
index 00000000000..2aa436b2a26
--- /dev/null
+++ b/tests/baselines/reference/checkJsxNotSetError.symbols
@@ -0,0 +1,17 @@
+=== /foo.jsx ===
+const Foo = () => (
+>Foo : Symbol(Foo, Decl(foo.jsx, 0, 5))
+
+ foo
+);
+export default Foo;
+>Foo : Symbol(Foo, Decl(foo.jsx, 0, 5))
+
+=== /bar.jsx ===
+import Foo from '/foo';
+>Foo : Symbol(Foo, Decl(bar.jsx, 0, 6))
+
+const a =
+>a : Symbol(a, Decl(bar.jsx, 1, 5))
+>Foo : Symbol(Foo, Decl(bar.jsx, 0, 6))
+
diff --git a/tests/baselines/reference/checkJsxNotSetError.types b/tests/baselines/reference/checkJsxNotSetError.types
new file mode 100644
index 00000000000..8922347d8a9
--- /dev/null
+++ b/tests/baselines/reference/checkJsxNotSetError.types
@@ -0,0 +1,24 @@
+=== /foo.jsx ===
+const Foo = () => (
+>Foo : () => any
+>() => ( foo
) : () => any
+>( foo
) : any
+
+ foo
+>foo
: any
+>div : any
+>div : any
+
+);
+export default Foo;
+>Foo : () => any
+
+=== /bar.jsx ===
+import Foo from '/foo';
+>Foo : () => any
+
+const a =
+>a : any
+> : any
+>Foo : () => any
+
diff --git a/tests/cases/compiler/checkJsxNotSetError.ts b/tests/cases/compiler/checkJsxNotSetError.ts
new file mode 100644
index 00000000000..824a68d4ff1
--- /dev/null
+++ b/tests/cases/compiler/checkJsxNotSetError.ts
@@ -0,0 +1,12 @@
+// @allowJs: true
+// @checkJs: true
+
+// @Filename: /foo.jsx
+const Foo = () => (
+ foo
+);
+export default Foo;
+
+// @Filename: /bar.jsx
+import Foo from '/foo';
+const a =
\ No newline at end of file
diff --git a/tests/cases/fourslash/goToDefinitionJsxNotSet.ts b/tests/cases/fourslash/goToDefinitionJsxNotSet.ts
new file mode 100644
index 00000000000..f0daa71b6fd
--- /dev/null
+++ b/tests/cases/fourslash/goToDefinitionJsxNotSet.ts
@@ -0,0 +1,17 @@
+///
+
+// Regresion tests for GH#46854
+
+// @allowJs: true
+
+// @Filename: /foo.jsx
+//// const /*def*/Foo = () => (
+//// foo
+//// );
+//// export default Foo;
+
+// @Filename: /bar.jsx
+//// import Foo from './foo';
+//// const a = <[|/*use*/Foo|] />
+
+verify.goToDefinition("use", "def");
\ No newline at end of file