diff --git a/tsc/internal/checker/jsx.go b/tsc/internal/checker/jsx.go index 1054f4d46f8c5..cc6fbe55a4733 100644 --- a/tsc/internal/checker/jsx.go +++ b/tsc/internal/checker/jsx.go @@ -1473,7 +1473,13 @@ func (c *Checker) getJsxNamespaceContainerForImplicitImport(location *ast.Node) if moduleReference == "" { return nil } + // A script file (non-module) cannot resolve imports, so the implicit JSX runtime import will + // not resolve regardless of whether the package is installed. Report that instead of the + // generic "module path does not exist" message. errorMessage := 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 + if !ast.IsExternalOrCommonJSModule(file) { + errorMessage = diagnostics.This_JSX_tag_requires_the_module_0_which_cannot_be_resolved_in_a_script_file_Add_an_empty_export_statement_to_make_this_file_a_module + } mod := c.resolveExternalModule(core.OrElse(specifier, canonicalErrorTag), moduleReference, errorMessage, canonicalErrorTag, false, nil /*importAttributesType*/) var result *ast.Symbol if mod != nil && mod != c.unknownSymbol { diff --git a/tsc/internal/diagnostics/diagnosticMessages.generated.json b/tsc/internal/diagnostics/diagnosticMessages.generated.json index 13e7ffe90d846..e502e62cd027f 100644 --- a/tsc/internal/diagnostics/diagnosticMessages.generated.json +++ b/tsc/internal/diagnostics/diagnosticMessages.generated.json @@ -1009,6 +1009,7 @@ "This_expression_is_never_nullish_2881": "This expression is never nullish.", "Cannot_find_module_or_type_declarations_for_side_effect_import_of_0_2882": "Cannot find module or type declarations for side-effect import of '{0}'.", "The_inferred_type_of_0_cannot_be_named_without_a_reference_to_2_from_1_This_is_likely_not_portable_A_2883": "The inferred type of '{0}' cannot be named without a reference to '{2}' from '{1}'. This is likely not portable. A type annotation is necessary.", + "This_JSX_tag_requires_the_module_0_which_cannot_be_resolved_in_a_script_file_Add_an_empty_export_sta_2884": "This JSX tag requires the module '{0}', which cannot be resolved in a script file. Add an empty 'export {}' statement to make this file a module.", "Import_declaration_0_is_using_private_name_1_4000": "Import declaration '{0}' is using private name '{1}'.", "Type_parameter_0_of_exported_class_has_or_is_using_private_name_1_4002": "Type parameter '{0}' of exported class has or is using private name '{1}'.", "Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1_4004": "Type parameter '{0}' of exported interface has or is using private name '{1}'.", diff --git a/tsc/internal/diagnostics/diagnosticMessages.json b/tsc/internal/diagnostics/diagnosticMessages.json index 3cb32a21c7bc0..a81a79e31776f 100644 --- a/tsc/internal/diagnostics/diagnosticMessages.json +++ b/tsc/internal/diagnostics/diagnosticMessages.json @@ -4050,6 +4050,10 @@ "category": "Error", "code": 2883 }, + "This JSX tag requires the module '{0}', which cannot be resolved in a script file. Add an empty 'export {}' statement to make this file a module.": { + "category": "Error", + "code": 2884 + }, "Import declaration '{0}' is using private name '{1}'.": { "category": "Error", diff --git a/tsc/internal/diagnostics/diagnostics_generated.go b/tsc/internal/diagnostics/diagnostics_generated.go index fe6657442d1e4..f61daa0445173 100644 --- a/tsc/internal/diagnostics/diagnostics_generated.go +++ b/tsc/internal/diagnostics/diagnostics_generated.go @@ -2022,6 +2022,8 @@ var Cannot_find_module_or_type_declarations_for_side_effect_import_of_0 = &Messa var The_inferred_type_of_0_cannot_be_named_without_a_reference_to_2_from_1_This_is_likely_not_portable_A_type_annotation_is_necessary = &Message{code: 2883, category: CategoryError, key: "The_inferred_type_of_0_cannot_be_named_without_a_reference_to_2_from_1_This_is_likely_not_portable_A_2883", text: "The inferred type of '{0}' cannot be named without a reference to '{2}' from '{1}'. This is likely not portable. A type annotation is necessary."} +var This_JSX_tag_requires_the_module_0_which_cannot_be_resolved_in_a_script_file_Add_an_empty_export_statement_to_make_this_file_a_module = &Message{code: 2884, category: CategoryError, key: "This_JSX_tag_requires_the_module_0_which_cannot_be_resolved_in_a_script_file_Add_an_empty_export_sta_2884", text: "This JSX tag requires the module '{0}', which cannot be resolved in a script file. Add an empty 'export {}' statement to make this file a module."} + var Import_declaration_0_is_using_private_name_1 = &Message{code: 4000, category: CategoryError, key: "Import_declaration_0_is_using_private_name_1_4000", text: "Import declaration '{0}' is using private name '{1}'."} var Type_parameter_0_of_exported_class_has_or_is_using_private_name_1 = &Message{code: 4002, category: CategoryError, key: "Type_parameter_0_of_exported_class_has_or_is_using_private_name_1_4002", text: "Type parameter '{0}' of exported class has or is using private name '{1}'."} @@ -5475,6 +5477,7 @@ var allMessages = [...]**Message{ &This_expression_is_never_nullish, &Cannot_find_module_or_type_declarations_for_side_effect_import_of_0, &The_inferred_type_of_0_cannot_be_named_without_a_reference_to_2_from_1_This_is_likely_not_portable_A_type_annotation_is_necessary, + &This_JSX_tag_requires_the_module_0_which_cannot_be_resolved_in_a_script_file_Add_an_empty_export_statement_to_make_this_file_a_module, &Import_declaration_0_is_using_private_name_1, &Type_parameter_0_of_exported_class_has_or_is_using_private_name_1, &Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1, diff --git a/tsc/testdata/baselines/reference/compiler/commentsOnJSXExpressionsArePreserved(jsx=react-jsx,module=commonjs,moduledetection=legacy).errors.txt b/tsc/testdata/baselines/reference/compiler/commentsOnJSXExpressionsArePreserved(jsx=react-jsx,module=commonjs,moduledetection=legacy).errors.txt index f710481bd1f17..b4cfa476faf78 100644 --- a/tsc/testdata/baselines/reference/compiler/commentsOnJSXExpressionsArePreserved(jsx=react-jsx,module=commonjs,moduledetection=legacy).errors.txt +++ b/tsc/testdata/baselines/reference/compiler/commentsOnJSXExpressionsArePreserved(jsx=react-jsx,module=commonjs,moduledetection=legacy).errors.txt @@ -1,5 +1,5 @@ commentsOnJSXExpressionsArePreserved.tsx(5,16): error TS7026: JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists. -commentsOnJSXExpressionsArePreserved.tsx(5,16): error TS2875: This JSX tag requires the module path 'react/jsx-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed. +commentsOnJSXExpressionsArePreserved.tsx(5,16): error TS2884: This JSX tag requires the module 'react/jsx-runtime', which cannot be resolved in a script file. Add an empty 'export {}' statement to make this file a module. commentsOnJSXExpressionsArePreserved.tsx(18,9): error TS7026: JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists. @@ -38,7 +38,7 @@ commentsOnJSXExpressionsArePreserved.tsx(18,9): error TS7026: JSX element implic ~~~~~~~~~~~~~~~~~~~~~~~~ ; ~~~~~~~~~~~~~~ -!!! error TS2875: This JSX tag requires the module path 'react/jsx-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed. +!!! error TS2884: This JSX tag requires the module 'react/jsx-runtime', which cannot be resolved in a script file. Add an empty 'export {}' statement to make this file a module. ~~~~~~ !!! error TS7026: JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists. } diff --git a/tsc/testdata/baselines/reference/compiler/commentsOnJSXExpressionsArePreserved(jsx=react-jsxdev,module=commonjs,moduledetection=legacy).errors.txt b/tsc/testdata/baselines/reference/compiler/commentsOnJSXExpressionsArePreserved(jsx=react-jsxdev,module=commonjs,moduledetection=legacy).errors.txt index 164f9da0004b7..c40d1341174c0 100644 --- a/tsc/testdata/baselines/reference/compiler/commentsOnJSXExpressionsArePreserved(jsx=react-jsxdev,module=commonjs,moduledetection=legacy).errors.txt +++ b/tsc/testdata/baselines/reference/compiler/commentsOnJSXExpressionsArePreserved(jsx=react-jsxdev,module=commonjs,moduledetection=legacy).errors.txt @@ -1,5 +1,5 @@ commentsOnJSXExpressionsArePreserved.tsx(5,16): error TS7026: JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists. -commentsOnJSXExpressionsArePreserved.tsx(5,16): error TS2875: This JSX tag requires the module path 'react/jsx-dev-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed. +commentsOnJSXExpressionsArePreserved.tsx(5,16): error TS2884: This JSX tag requires the module 'react/jsx-dev-runtime', which cannot be resolved in a script file. Add an empty 'export {}' statement to make this file a module. commentsOnJSXExpressionsArePreserved.tsx(18,9): error TS7026: JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists. @@ -38,7 +38,7 @@ commentsOnJSXExpressionsArePreserved.tsx(18,9): error TS7026: JSX element implic ~~~~~~~~~~~~~~~~~~~~~~~~ ; ~~~~~~~~~~~~~~ -!!! error TS2875: This JSX tag requires the module path 'react/jsx-dev-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed. +!!! error TS2884: This JSX tag requires the module 'react/jsx-dev-runtime', which cannot be resolved in a script file. Add an empty 'export {}' statement to make this file a module. ~~~~~~ !!! error TS7026: JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists. } diff --git a/tsc/testdata/baselines/reference/compiler/jsxImportSourceModuleMissing.errors.txt b/tsc/testdata/baselines/reference/compiler/jsxImportSourceModuleMissing.errors.txt new file mode 100644 index 0000000000000..5567f41c9bb6c --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsxImportSourceModuleMissing.errors.txt @@ -0,0 +1,11 @@ +index.tsx(1,18): error TS2875: This JSX tag requires the module path 'solid-js/jsx-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed. +index.tsx(1,18): error TS7026: JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists. + + +==== index.tsx (2 errors) ==== + export const x =
; + ~~~~~~~ +!!! error TS2875: This JSX tag requires the module path 'solid-js/jsx-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed. + ~~~~~~~ +!!! error TS7026: JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists. + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/jsxImportSourceModuleMissing.js b/tsc/testdata/baselines/reference/compiler/jsxImportSourceModuleMissing.js new file mode 100644 index 0000000000000..9b601d7203ad8 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsxImportSourceModuleMissing.js @@ -0,0 +1,12 @@ +//// [tests/cases/compiler/jsxImportSourceModuleMissing.tsx] //// + +//// [index.tsx] +export const x = ; + + +//// [index.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.x = void 0; +const jsx_runtime_1 = require("solid-js/jsx-runtime"); +exports.x = (0, jsx_runtime_1.jsx)("div", {}); diff --git a/tsc/testdata/baselines/reference/compiler/jsxImportSourceModuleMissing.symbols b/tsc/testdata/baselines/reference/compiler/jsxImportSourceModuleMissing.symbols new file mode 100644 index 0000000000000..b77ce4bf5dda8 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsxImportSourceModuleMissing.symbols @@ -0,0 +1,6 @@ +//// [tests/cases/compiler/jsxImportSourceModuleMissing.tsx] //// + +=== index.tsx === +export const x = ; +>x : Symbol(x, Decl(index.tsx, 0, 12)) + diff --git a/tsc/testdata/baselines/reference/compiler/jsxImportSourceModuleMissing.types b/tsc/testdata/baselines/reference/compiler/jsxImportSourceModuleMissing.types new file mode 100644 index 0000000000000..f44f6838d0790 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsxImportSourceModuleMissing.types @@ -0,0 +1,8 @@ +//// [tests/cases/compiler/jsxImportSourceModuleMissing.tsx] //// + +=== index.tsx === +export const x = ; +>x : any +> : any +>div : any + diff --git a/tsc/testdata/baselines/reference/compiler/jsxScriptFile.errors.txt b/tsc/testdata/baselines/reference/compiler/jsxScriptFile.errors.txt new file mode 100644 index 0000000000000..e059ab3c3ac8e --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsxScriptFile.errors.txt @@ -0,0 +1,11 @@ +index.tsx(1,11): error TS2884: This JSX tag requires the module 'solid-js/jsx-runtime', which cannot be resolved in a script file. Add an empty 'export {}' statement to make this file a module. +index.tsx(1,11): error TS7026: JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists. + + +==== index.tsx (2 errors) ==== + const x = ; + ~~~~~~~ +!!! error TS2884: This JSX tag requires the module 'solid-js/jsx-runtime', which cannot be resolved in a script file. Add an empty 'export {}' statement to make this file a module. + ~~~~~~~ +!!! error TS7026: JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists. + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/jsxScriptFile.js b/tsc/testdata/baselines/reference/compiler/jsxScriptFile.js new file mode 100644 index 0000000000000..2af91f4bb3b51 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsxScriptFile.js @@ -0,0 +1,9 @@ +//// [tests/cases/compiler/jsxScriptFile.tsx] //// + +//// [index.tsx] +const x = ; + + +//// [index.js] +"use strict"; +const x = _jsx("div", {}); diff --git a/tsc/testdata/baselines/reference/compiler/jsxScriptFile.symbols b/tsc/testdata/baselines/reference/compiler/jsxScriptFile.symbols new file mode 100644 index 0000000000000..dc8cd94cabf1b --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsxScriptFile.symbols @@ -0,0 +1,6 @@ +//// [tests/cases/compiler/jsxScriptFile.tsx] //// + +=== index.tsx === +const x = ; +>x : Symbol(x, Decl(index.tsx, 0, 5)) + diff --git a/tsc/testdata/baselines/reference/compiler/jsxScriptFile.types b/tsc/testdata/baselines/reference/compiler/jsxScriptFile.types new file mode 100644 index 0000000000000..e204386a19f3c --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsxScriptFile.types @@ -0,0 +1,8 @@ +//// [tests/cases/compiler/jsxScriptFile.tsx] //// + +=== index.tsx === +const x = ; +>x : any +> : any +>div : any + diff --git a/tsc/testdata/baselines/reference/compiler/jsxScriptFileResolvableImportSource.js b/tsc/testdata/baselines/reference/compiler/jsxScriptFileResolvableImportSource.js new file mode 100644 index 0000000000000..fffb426321d2b --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsxScriptFileResolvableImportSource.js @@ -0,0 +1,18 @@ +//// [tests/cases/compiler/jsxScriptFileResolvableImportSource.tsx] //// + +//// [jsx-runtime.d.ts] +export namespace JSX { + interface IntrinsicElements { + div: {}; + } +} +export function jsx(type: any, props: any, key?: any): any; +export const Fragment: unique symbol; + +//// [index.tsx] +const x = ; + + +//// [index.js] +"use strict"; +const x = _jsx("div", {}); diff --git a/tsc/testdata/baselines/reference/compiler/jsxScriptFileResolvableImportSource.symbols b/tsc/testdata/baselines/reference/compiler/jsxScriptFileResolvableImportSource.symbols new file mode 100644 index 0000000000000..15d86aa0f5b59 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsxScriptFileResolvableImportSource.symbols @@ -0,0 +1,27 @@ +//// [tests/cases/compiler/jsxScriptFileResolvableImportSource.tsx] //// + +=== node_modules/solid-js/jsx-runtime.d.ts === +export namespace JSX { +>JSX : Symbol(JSX, Decl(jsx-runtime.d.ts, 0, 0)) + + interface IntrinsicElements { +>IntrinsicElements : Symbol(IntrinsicElements, Decl(jsx-runtime.d.ts, 0, 22)) + + div: {}; +>div : Symbol(IntrinsicElements.div, Decl(jsx-runtime.d.ts, 1, 33)) + } +} +export function jsx(type: any, props: any, key?: any): any; +>jsx : Symbol(jsx, Decl(jsx-runtime.d.ts, 4, 1)) +>type : Symbol(type, Decl(jsx-runtime.d.ts, 5, 20)) +>props : Symbol(props, Decl(jsx-runtime.d.ts, 5, 30)) +>key : Symbol(key, Decl(jsx-runtime.d.ts, 5, 42)) + +export const Fragment: unique symbol; +>Fragment : Symbol(Fragment, Decl(jsx-runtime.d.ts, 6, 12)) + +=== index.tsx === +const x = ; +>x : Symbol(x, Decl(index.tsx, 0, 5)) +>div : Symbol(JSX.IntrinsicElements.div, Decl(jsx-runtime.d.ts, 1, 33)) + diff --git a/tsc/testdata/baselines/reference/compiler/jsxScriptFileResolvableImportSource.types b/tsc/testdata/baselines/reference/compiler/jsxScriptFileResolvableImportSource.types new file mode 100644 index 0000000000000..f3dc0528eee47 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsxScriptFileResolvableImportSource.types @@ -0,0 +1,24 @@ +//// [tests/cases/compiler/jsxScriptFileResolvableImportSource.tsx] //// + +=== node_modules/solid-js/jsx-runtime.d.ts === +export namespace JSX { + interface IntrinsicElements { + div: {}; +>div : {} + } +} +export function jsx(type: any, props: any, key?: any): any; +>jsx : (type: any, props: any, key?: any) => any +>type : any +>props : any +>key : any + +export const Fragment: unique symbol; +>Fragment : unique symbol + +=== index.tsx === +const x = ; +>x : error +> : error +>div : any + diff --git a/tsc/testdata/tests/cases/compiler/jsxImportSourceModuleMissing.tsx b/tsc/testdata/tests/cases/compiler/jsxImportSourceModuleMissing.tsx new file mode 100644 index 0000000000000..5b82e52f96f86 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/jsxImportSourceModuleMissing.tsx @@ -0,0 +1,6 @@ +// @jsx: react-jsx +// @jsxImportSource: solid-js +// @module: nodenext + +// @filename: index.tsx +export const x = ; diff --git a/tsc/testdata/tests/cases/compiler/jsxScriptFile.tsx b/tsc/testdata/tests/cases/compiler/jsxScriptFile.tsx new file mode 100644 index 0000000000000..604bfb979cda3 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/jsxScriptFile.tsx @@ -0,0 +1,7 @@ +// @jsx: react-jsx +// @jsxImportSource: solid-js +// @module: commonjs +// @moduleDetection: legacy + +// @filename: index.tsx +const x = ; diff --git a/tsc/testdata/tests/cases/compiler/jsxScriptFileResolvableImportSource.tsx b/tsc/testdata/tests/cases/compiler/jsxScriptFileResolvableImportSource.tsx new file mode 100644 index 0000000000000..6ef280feaec1c --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/jsxScriptFileResolvableImportSource.tsx @@ -0,0 +1,16 @@ +// @jsx: react-jsx +// @jsxImportSource: solid-js +// @module: commonjs +// @moduleDetection: legacy + +// @filename: node_modules/solid-js/jsx-runtime.d.ts +export namespace JSX { + interface IntrinsicElements { + div: {}; + } +} +export function jsx(type: any, props: any, key?: any): any; +export const Fragment: unique symbol; + +// @filename: index.tsx +const x = ;