From 738bd605dfab2102cad7a8702ceae1ed2f55fbec Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Wed, 26 Jun 2024 10:37:58 -0700 Subject: [PATCH 1/2] Cherry-pick #58966 to release-5.5 (#59002) Co-authored-by: Oleksandr T --- src/compiler/checker.ts | 2 +- src/compiler/program.ts | 2 +- src/compiler/utilities.ts | 2 +- .../reference/importTag17.errors.txt | 44 +++++++++++++++++++ tests/baselines/reference/importTag17.symbols | 32 ++++++++++++++ tests/baselines/reference/importTag17.types | 40 +++++++++++++++++ tests/cases/conformance/jsdoc/importTag17.ts | 40 +++++++++++++++++ 7 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 tests/baselines/reference/importTag17.errors.txt create mode 100644 tests/baselines/reference/importTag17.symbols create mode 100644 tests/baselines/reference/importTag17.types create mode 100644 tests/cases/conformance/jsdoc/importTag17.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 57cca12c38c29..1db88a48abcab 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4590,7 +4590,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } if (moduleResolutionKind === ModuleResolutionKind.Node16 || moduleResolutionKind === ModuleResolutionKind.NodeNext) { const isSyncImport = (currentSourceFile.impliedNodeFormat === ModuleKind.CommonJS && !findAncestor(location, isImportCall)) || !!findAncestor(location, isImportEqualsDeclaration); - const overrideHost = findAncestor(location, l => isImportTypeNode(l) || isExportDeclaration(l) || isImportDeclaration(l)) as ImportTypeNode | ImportDeclaration | ExportDeclaration | undefined; + const overrideHost = findAncestor(location, l => isImportTypeNode(l) || isExportDeclaration(l) || isImportDeclaration(l) || isJSDocImportTag(l)) as ImportTypeNode | ImportDeclaration | ExportDeclaration | JSDocImportTag | undefined; // An override clause will take effect for type-only imports and import types, and allows importing the types across formats, regardless of // normal mode restrictions if (isSyncImport && sourceFile.impliedNodeFormat === ModuleKind.ESNext && !hasResolutionModeOverride(overrideHost)) { diff --git a/src/compiler/program.ts b/src/compiler/program.ts index e61a93bf871a2..023013b64b641 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -902,7 +902,7 @@ export function getModeForUsageLocation(file: { impliedNodeFormat?: ResolutionMo } function getModeForUsageLocationWorker(file: { impliedNodeFormat?: ResolutionMode; }, usage: StringLiteralLike, compilerOptions?: CompilerOptions) { - if ((isImportDeclaration(usage.parent) || isExportDeclaration(usage.parent))) { + if (isImportDeclaration(usage.parent) || isExportDeclaration(usage.parent) || isJSDocImportTag(usage.parent)) { const isTypeOnly = isExclusivelyTypeOnlyImportOrExport(usage.parent); if (isTypeOnly) { const override = getResolutionModeOverride(usage.parent.attributes); diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 81cfddfc0578a..ad32f1903ec56 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -10850,7 +10850,7 @@ export function isExpandoPropertyDeclaration(declaration: Declaration | undefine } /** @internal */ -export function hasResolutionModeOverride(node: ImportTypeNode | ImportDeclaration | ExportDeclaration | undefined) { +export function hasResolutionModeOverride(node: ImportTypeNode | ImportDeclaration | ExportDeclaration | JSDocImportTag | undefined) { if (node === undefined) { return false; } diff --git a/tests/baselines/reference/importTag17.errors.txt b/tests/baselines/reference/importTag17.errors.txt new file mode 100644 index 0000000000000..126faa85d899d --- /dev/null +++ b/tests/baselines/reference/importTag17.errors.txt @@ -0,0 +1,44 @@ +/a.js(8,5): error TS2322: Type '1' is not assignable to type '"module"'. +/a.js(15,5): error TS2322: Type '1' is not assignable to type '"script"'. + + +==== /node_modules/@types/foo/package.json (0 errors) ==== + { + "name": "@types/foo", + "version": "1.0.0", + "exports": { + ".": { + "import": "./index.d.mts", + "require": "./index.d.cts" + } + } + } + +==== /node_modules/@types/foo/index.d.mts (0 errors) ==== + export declare const Import: "module"; + +==== /node_modules/@types/foo/index.d.cts (0 errors) ==== + export declare const Require: "script"; + +==== /a.js (2 errors) ==== + /** @import { Import } from 'foo' with { 'resolution-mode': 'import' } */ + /** @import { Require } from 'foo' with { 'resolution-mode': 'require' } */ + + /** + * @returns { Import } + */ + export function f1() { + return 1; + ~~~~~~ +!!! error TS2322: Type '1' is not assignable to type '"module"'. + } + + /** + * @returns { Require } + */ + export function f2() { + return 1; + ~~~~~~ +!!! error TS2322: Type '1' is not assignable to type '"script"'. + } + \ No newline at end of file diff --git a/tests/baselines/reference/importTag17.symbols b/tests/baselines/reference/importTag17.symbols new file mode 100644 index 0000000000000..8cb42c771b67a --- /dev/null +++ b/tests/baselines/reference/importTag17.symbols @@ -0,0 +1,32 @@ +//// [tests/cases/conformance/jsdoc/importTag17.ts] //// + +=== /node_modules/@types/foo/index.d.mts === +export declare const Import: "module"; +>Import : Symbol(Import, Decl(index.d.mts, 0, 20)) + +=== /node_modules/@types/foo/index.d.cts === +export declare const Require: "script"; +>Require : Symbol(Require, Decl(index.d.cts, 0, 20)) + +=== /a.js === +/** @import { Import } from 'foo' with { 'resolution-mode': 'import' } */ +/** @import { Require } from 'foo' with { 'resolution-mode': 'require' } */ + +/** + * @returns { Import } + */ +export function f1() { +>f1 : Symbol(f1, Decl(a.js, 0, 0)) + + return 1; +} + +/** + * @returns { Require } + */ +export function f2() { +>f2 : Symbol(f2, Decl(a.js, 8, 1)) + + return 1; +} + diff --git a/tests/baselines/reference/importTag17.types b/tests/baselines/reference/importTag17.types new file mode 100644 index 0000000000000..7e30a956fa0f6 --- /dev/null +++ b/tests/baselines/reference/importTag17.types @@ -0,0 +1,40 @@ +//// [tests/cases/conformance/jsdoc/importTag17.ts] //// + +=== /node_modules/@types/foo/index.d.mts === +export declare const Import: "module"; +>Import : "module" +> : ^^^^^^^^ + +=== /node_modules/@types/foo/index.d.cts === +export declare const Require: "script"; +>Require : "script" +> : ^^^^^^^^ + +=== /a.js === +/** @import { Import } from 'foo' with { 'resolution-mode': 'import' } */ +/** @import { Require } from 'foo' with { 'resolution-mode': 'require' } */ + +/** + * @returns { Import } + */ +export function f1() { +>f1 : () => "module" +> : ^^^^^^^^^^^^^^ + + return 1; +>1 : 1 +> : ^ +} + +/** + * @returns { Require } + */ +export function f2() { +>f2 : () => "script" +> : ^^^^^^^^^^^^^^ + + return 1; +>1 : 1 +> : ^ +} + diff --git a/tests/cases/conformance/jsdoc/importTag17.ts b/tests/cases/conformance/jsdoc/importTag17.ts new file mode 100644 index 0000000000000..dd695504090e7 --- /dev/null +++ b/tests/cases/conformance/jsdoc/importTag17.ts @@ -0,0 +1,40 @@ +// @module: node16 +// @checkJs: true +// @allowJs: true +// @noEmit: true + +// @Filename: /node_modules/@types/foo/package.json +{ + "name": "@types/foo", + "version": "1.0.0", + "exports": { + ".": { + "import": "./index.d.mts", + "require": "./index.d.cts" + } + } +} + +// @Filename: /node_modules/@types/foo/index.d.mts +export declare const Import: "module"; + +// @Filename: /node_modules/@types/foo/index.d.cts +export declare const Require: "script"; + +// @Filename: /a.js +/** @import { Import } from 'foo' with { 'resolution-mode': 'import' } */ +/** @import { Require } from 'foo' with { 'resolution-mode': 'require' } */ + +/** + * @returns { Import } + */ +export function f1() { + return 1; +} + +/** + * @returns { Require } + */ +export function f2() { + return 1; +} From f0e992167440686f948965e5441a918b34251886 Mon Sep 17 00:00:00 2001 From: TypeScript Bot Date: Wed, 26 Jun 2024 23:17:57 +0000 Subject: [PATCH 2/2] Bump version to 5.5.3 and LKG --- lib/tsc.js | 6 +++--- lib/typescript.js | 6 +++--- package-lock.json | 4 ++-- package.json | 2 +- src/compiler/corePublic.ts | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/tsc.js b/lib/tsc.js index e3dc24d79de13..779e380ad8ad6 100644 --- a/lib/tsc.js +++ b/lib/tsc.js @@ -18,7 +18,7 @@ and limitations under the License. // src/compiler/corePublic.ts var versionMajorMinor = "5.5"; -var version = "5.5.2"; +var version = "5.5.3"; // src/compiler/core.ts var emptyArray = []; @@ -48048,7 +48048,7 @@ function createTypeChecker(host) { } if (moduleResolutionKind === 3 /* Node16 */ || moduleResolutionKind === 99 /* NodeNext */) { const isSyncImport = currentSourceFile.impliedNodeFormat === 1 /* CommonJS */ && !findAncestor(location, isImportCall) || !!findAncestor(location, isImportEqualsDeclaration); - const overrideHost = findAncestor(location, (l) => isImportTypeNode(l) || isExportDeclaration(l) || isImportDeclaration(l)); + const overrideHost = findAncestor(location, (l) => isImportTypeNode(l) || isExportDeclaration(l) || isImportDeclaration(l) || isJSDocImportTag(l)); if (isSyncImport && sourceFile.impliedNodeFormat === 99 /* ESNext */ && !hasResolutionModeOverride(overrideHost)) { if (findAncestor(location, isImportEqualsDeclaration)) { error(errorNode, Diagnostics.Module_0_cannot_be_imported_using_this_construct_The_specifier_only_resolves_to_an_ES_module_which_cannot_be_imported_with_require_Use_an_ECMAScript_import_instead, moduleReference); @@ -118604,7 +118604,7 @@ function getModeForUsageLocation(file, usage, compilerOptions) { } function getModeForUsageLocationWorker(file, usage, compilerOptions) { var _a; - if (isImportDeclaration(usage.parent) || isExportDeclaration(usage.parent)) { + if (isImportDeclaration(usage.parent) || isExportDeclaration(usage.parent) || isJSDocImportTag(usage.parent)) { const isTypeOnly = isExclusivelyTypeOnlyImportOrExport(usage.parent); if (isTypeOnly) { const override = getResolutionModeOverride(usage.parent.attributes); diff --git a/lib/typescript.js b/lib/typescript.js index 714953f466fc4..4baad59329e68 100644 --- a/lib/typescript.js +++ b/lib/typescript.js @@ -2366,7 +2366,7 @@ module.exports = __toCommonJS(typescript_exports); // src/compiler/corePublic.ts var versionMajorMinor = "5.5"; -var version = "5.5.2"; +var version = "5.5.3"; var Comparison = /* @__PURE__ */ ((Comparison3) => { Comparison3[Comparison3["LessThan"] = -1] = "LessThan"; Comparison3[Comparison3["EqualTo"] = 0] = "EqualTo"; @@ -52812,7 +52812,7 @@ function createTypeChecker(host) { } if (moduleResolutionKind === 3 /* Node16 */ || moduleResolutionKind === 99 /* NodeNext */) { const isSyncImport = currentSourceFile.impliedNodeFormat === 1 /* CommonJS */ && !findAncestor(location, isImportCall) || !!findAncestor(location, isImportEqualsDeclaration); - const overrideHost = findAncestor(location, (l) => isImportTypeNode(l) || isExportDeclaration(l) || isImportDeclaration(l)); + const overrideHost = findAncestor(location, (l) => isImportTypeNode(l) || isExportDeclaration(l) || isImportDeclaration(l) || isJSDocImportTag(l)); if (isSyncImport && sourceFile.impliedNodeFormat === 99 /* ESNext */ && !hasResolutionModeOverride(overrideHost)) { if (findAncestor(location, isImportEqualsDeclaration)) { error2(errorNode, Diagnostics.Module_0_cannot_be_imported_using_this_construct_The_specifier_only_resolves_to_an_ES_module_which_cannot_be_imported_with_require_Use_an_ECMAScript_import_instead, moduleReference); @@ -123600,7 +123600,7 @@ function getModeForUsageLocation(file, usage, compilerOptions) { } function getModeForUsageLocationWorker(file, usage, compilerOptions) { var _a; - if (isImportDeclaration(usage.parent) || isExportDeclaration(usage.parent)) { + if (isImportDeclaration(usage.parent) || isExportDeclaration(usage.parent) || isJSDocImportTag(usage.parent)) { const isTypeOnly = isExclusivelyTypeOnlyImportOrExport(usage.parent); if (isTypeOnly) { const override = getResolutionModeOverride(usage.parent.attributes); diff --git a/package-lock.json b/package-lock.json index 0fd6163cf5fc5..d7df8349855ff 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "typescript", - "version": "5.5.2", + "version": "5.5.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "typescript", - "version": "5.5.2", + "version": "5.5.3", "license": "Apache-2.0", "bin": { "tsc": "bin/tsc", diff --git a/package.json b/package.json index db4d4f448de29..e12a7ad7ff291 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "typescript", "author": "Microsoft Corp.", "homepage": "https://www.typescriptlang.org/", - "version": "5.5.2", + "version": "5.5.3", "license": "Apache-2.0", "description": "TypeScript is a language for application scale JavaScript development", "keywords": [ diff --git a/src/compiler/corePublic.ts b/src/compiler/corePublic.ts index 3d8748fc6e6d1..c425dd37a96a0 100644 --- a/src/compiler/corePublic.ts +++ b/src/compiler/corePublic.ts @@ -3,7 +3,7 @@ export const versionMajorMinor = "5.5"; // The following is baselined as a literal template type without intervention /** The version of the TypeScript compiler release */ -export const version = "5.5.2" as string; +export const version = "5.5.3" as string; /** * Type of objects whose values are all of the same type.