Skip to content

Commit 278f260

Browse files
Specialize the message on JSX tags looking for the JSX factory namespace (#58870)
1 parent e5758ab commit 278f260

File tree

42 files changed

+180
-117
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+180
-117
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30132,7 +30132,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3013230132
if (!getJsxNamespaceContainerForImplicitImport(node)) {
3013330133
// The reactNamespace/jsxFactory's root symbol should be marked as 'used' so we don't incorrectly elide its import.
3013430134
// And if there is no reactNamespace/jsxFactory's symbol in scope when targeting React emit, we should issue an error.
30135-
const jsxFactoryRefErr = diagnostics && compilerOptions.jsx === JsxEmit.React ? Diagnostics.Cannot_find_name_0 : undefined;
30135+
const jsxFactoryRefErr = diagnostics && compilerOptions.jsx === JsxEmit.React ? Diagnostics.This_JSX_tag_requires_0_to_be_in_scope_but_it_could_not_be_found : undefined;
3013630136
const jsxFactoryNamespace = getJsxNamespace(node);
3013730137
const jsxFactoryLocation = isJsxOpeningLikeElement(node) ? node.tagName : node;
3013830138

@@ -33438,7 +33438,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3343833438
const isClassic = getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.Classic;
3343933439
const errorMessage = isClassic
3344033440
? Diagnostics.Cannot_find_module_0_Did_you_mean_to_set_the_moduleResolution_option_to_nodenext_or_to_add_aliases_to_the_paths_option
33441-
: Diagnostics.Cannot_find_module_0_or_its_corresponding_type_declarations;
33441+
: Diagnostics.This_JSX_tag_requires_the_module_path_0_to_exist_but_none_could_be_found_Make_sure_you_have_types_for_the_appropriate_package_installed;
3344233442
const specifier = getJSXRuntimeImportSpecifier(file, runtimeImportSpecifier);
3344333443
const mod = resolveExternalModule(specifier || location!, runtimeImportSpecifier, errorMessage, location);
3344433444
const result = mod && mod !== unknownSymbol ? getMergedSymbol(resolveSymbol(mod)) : undefined;

src/compiler/diagnosticMessages.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3948,6 +3948,15 @@
39483948
"category": "Error",
39493949
"code": 2873
39503950
},
3951+
"This JSX tag requires '{0}' to be in scope, but it could not be found.": {
3952+
"category": "Error",
3953+
"code": 2874
3954+
},
3955+
"This JSX tag requires the module path '{0}' to exist, but none could be found. Make sure you have types for the appropriate package installed.": {
3956+
"category": "Error",
3957+
"code": 2875
3958+
},
3959+
39513960
"Import declaration '{0}' is using private name '{1}'.": {
39523961
"category": "Error",
39533962
"code": 4000

src/services/codefixes/fixCannotFindModule.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
import {
77
Debug,
88
Diagnostics,
9+
getJSXImplicitImportBase,
910
getTokenAtPosition,
1011
getTypesPackageName,
1112
InstallPackageAction,
@@ -22,17 +23,23 @@ const fixName = "fixCannotFindModule";
2223
const fixIdInstallTypesPackage = "installTypesPackage";
2324

2425
const errorCodeCannotFindModule = Diagnostics.Cannot_find_module_0_or_its_corresponding_type_declarations.code;
26+
const errorCannotFindImplicitJsxImport = Diagnostics.This_JSX_tag_requires_the_module_path_0_to_exist_but_none_could_be_found_Make_sure_you_have_types_for_the_appropriate_package_installed.code;
2527
const errorCodes = [
2628
errorCodeCannotFindModule,
2729
Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type.code,
30+
errorCannotFindImplicitJsxImport,
2831
];
2932
registerCodeFix({
3033
errorCodes,
3134
getCodeActions: function getCodeActionsToFixNotFoundModule(context) {
32-
const { host, sourceFile, span: { start } } = context;
33-
const packageName = tryGetImportedPackageName(sourceFile, start);
35+
const { host, sourceFile, span: { start }, errorCode } = context;
36+
37+
const packageName = errorCode === errorCannotFindImplicitJsxImport ?
38+
getJSXImplicitImportBase(context.program.getCompilerOptions(), sourceFile) :
39+
tryGetImportedPackageName(sourceFile, start);
3440
if (packageName === undefined) return undefined;
35-
const typesPackageName = getTypesPackageNameToInstall(packageName, host, context.errorCode);
41+
42+
const typesPackageName = getTypesPackageNameToInstall(packageName, host, errorCode);
3643
return typesPackageName === undefined
3744
? []
3845
: [createCodeFixAction(fixName, /*changes*/ [], [Diagnostics.Install_0, typesPackageName], fixIdInstallTypesPackage, Diagnostics.Install_all_missing_types_packages, getInstallCommand(sourceFile.fileName, typesPackageName))];

src/services/codefixes/importFixes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ const errorCodes: readonly number[] = [
180180
Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_node_Try_npm_i_save_dev_types_Slashnode.code,
181181
Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_node_Try_npm_i_save_dev_types_Slashnode_and_then_add_node_to_the_types_field_in_your_tsconfig.code,
182182
Diagnostics.Cannot_find_namespace_0_Did_you_mean_1.code,
183+
Diagnostics.This_JSX_tag_requires_0_to_be_in_scope_but_it_could_not_be_found.code,
183184
];
184185

185186
registerCodeFix({

tests/baselines/reference/commentsOnJSXExpressionsArePreserved(jsx=react,module=commonjs,moduledetection=auto).errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
commentsOnJSXExpressionsArePreserved.tsx(5,17): error TS2304: Cannot find name 'React'.
1+
commentsOnJSXExpressionsArePreserved.tsx(5,17): error TS2874: This JSX tag requires 'React' to be in scope, but it could not be found.
22

33

44
==== commentsOnJSXExpressionsArePreserved.tsx (1 errors) ====
@@ -8,7 +8,7 @@ commentsOnJSXExpressionsArePreserved.tsx(5,17): error TS2304: Cannot find name '
88
render() {
99
return <div>
1010
~~~
11-
!!! error TS2304: Cannot find name 'React'.
11+
!!! error TS2874: This JSX tag requires 'React' to be in scope, but it could not be found.
1212
{/* missing */}
1313
{null/* preserved */}
1414
{

tests/baselines/reference/commentsOnJSXExpressionsArePreserved(jsx=react,module=commonjs,moduledetection=force).errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
commentsOnJSXExpressionsArePreserved.tsx(5,17): error TS2304: Cannot find name 'React'.
1+
commentsOnJSXExpressionsArePreserved.tsx(5,17): error TS2874: This JSX tag requires 'React' to be in scope, but it could not be found.
22

33

44
==== commentsOnJSXExpressionsArePreserved.tsx (1 errors) ====
@@ -8,7 +8,7 @@ commentsOnJSXExpressionsArePreserved.tsx(5,17): error TS2304: Cannot find name '
88
render() {
99
return <div>
1010
~~~
11-
!!! error TS2304: Cannot find name 'React'.
11+
!!! error TS2874: This JSX tag requires 'React' to be in scope, but it could not be found.
1212
{/* missing */}
1313
{null/* preserved */}
1414
{

tests/baselines/reference/commentsOnJSXExpressionsArePreserved(jsx=react,module=commonjs,moduledetection=legacy).errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
commentsOnJSXExpressionsArePreserved.tsx(5,17): error TS2304: Cannot find name 'React'.
1+
commentsOnJSXExpressionsArePreserved.tsx(5,17): error TS2874: This JSX tag requires 'React' to be in scope, but it could not be found.
22

33

44
==== commentsOnJSXExpressionsArePreserved.tsx (1 errors) ====
@@ -8,7 +8,7 @@ commentsOnJSXExpressionsArePreserved.tsx(5,17): error TS2304: Cannot find name '
88
render() {
99
return <div>
1010
~~~
11-
!!! error TS2304: Cannot find name 'React'.
11+
!!! error TS2874: This JSX tag requires 'React' to be in scope, but it could not be found.
1212
{/* missing */}
1313
{null/* preserved */}
1414
{

tests/baselines/reference/commentsOnJSXExpressionsArePreserved(jsx=react,module=system,moduledetection=auto).errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
commentsOnJSXExpressionsArePreserved.tsx(5,17): error TS2304: Cannot find name 'React'.
1+
commentsOnJSXExpressionsArePreserved.tsx(5,17): error TS2874: This JSX tag requires 'React' to be in scope, but it could not be found.
22

33

44
==== commentsOnJSXExpressionsArePreserved.tsx (1 errors) ====
@@ -8,7 +8,7 @@ commentsOnJSXExpressionsArePreserved.tsx(5,17): error TS2304: Cannot find name '
88
render() {
99
return <div>
1010
~~~
11-
!!! error TS2304: Cannot find name 'React'.
11+
!!! error TS2874: This JSX tag requires 'React' to be in scope, but it could not be found.
1212
{/* missing */}
1313
{null/* preserved */}
1414
{

tests/baselines/reference/commentsOnJSXExpressionsArePreserved(jsx=react,module=system,moduledetection=force).errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
commentsOnJSXExpressionsArePreserved.tsx(5,17): error TS2304: Cannot find name 'React'.
1+
commentsOnJSXExpressionsArePreserved.tsx(5,17): error TS2874: This JSX tag requires 'React' to be in scope, but it could not be found.
22

33

44
==== commentsOnJSXExpressionsArePreserved.tsx (1 errors) ====
@@ -8,7 +8,7 @@ commentsOnJSXExpressionsArePreserved.tsx(5,17): error TS2304: Cannot find name '
88
render() {
99
return <div>
1010
~~~
11-
!!! error TS2304: Cannot find name 'React'.
11+
!!! error TS2874: This JSX tag requires 'React' to be in scope, but it could not be found.
1212
{/* missing */}
1313
{null/* preserved */}
1414
{

tests/baselines/reference/commentsOnJSXExpressionsArePreserved(jsx=react,module=system,moduledetection=legacy).errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
commentsOnJSXExpressionsArePreserved.tsx(5,17): error TS2304: Cannot find name 'React'.
1+
commentsOnJSXExpressionsArePreserved.tsx(5,17): error TS2874: This JSX tag requires 'React' to be in scope, but it could not be found.
22

33

44
==== commentsOnJSXExpressionsArePreserved.tsx (1 errors) ====
@@ -8,7 +8,7 @@ commentsOnJSXExpressionsArePreserved.tsx(5,17): error TS2304: Cannot find name '
88
render() {
99
return <div>
1010
~~~
11-
!!! error TS2304: Cannot find name 'React'.
11+
!!! error TS2874: This JSX tag requires 'React' to be in scope, but it could not be found.
1212
{/* missing */}
1313
{null/* preserved */}
1414
{

0 commit comments

Comments
 (0)