Allow `import =` in module augmentations (#57704)

This commit is contained in:
Ryan Cavanaugh 2024-08-02 12:55:27 -07:00 коммит произвёл GitHub
Родитель 6f646429e0
Коммит 5d545aa9b3
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
9 изменённых файлов: 167 добавлений и 38 удалений

Просмотреть файл

@ -47029,6 +47029,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
grammarErrorOnFirstToken(node, Diagnostics.Exports_and_export_assignments_are_not_permitted_in_module_augmentations);
break;
case SyntaxKind.ImportEqualsDeclaration:
// import a = e.x; in module augmentation is ok, but not import a = require('fs)
if (isInternalModuleImportEqualsDeclaration(node)) break;
// falls through
case SyntaxKind.ImportDeclaration:
grammarErrorOnFirstToken(node, Diagnostics.Imports_are_not_permitted_in_module_augmentations_Consider_moving_them_to_the_enclosing_external_module);
break;

Просмотреть файл

@ -0,0 +1,26 @@
importAliasInModuleAugmentation.ts(12,5): error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
importAliasInModuleAugmentation.ts(12,24): error TS2307: Cannot find module 'fs' or its corresponding type declarations.
==== importAliasInModuleAugmentation.ts (2 errors) ====
export { }
namespace A {
export const y = 34;
export interface y { s: string }
}
declare global {
export import x = A.y;
// Should still error
import f = require("fs");
~~~~~~
!!! error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
~~~~
!!! error TS2307: Cannot find module 'fs' or its corresponding type declarations.
}
const m: number = x;
let s: x = { s: "" };
void s.s;

Просмотреть файл

@ -0,0 +1,31 @@
//// [tests/cases/compiler/importAliasInModuleAugmentation.ts] ////
//// [importAliasInModuleAugmentation.ts]
export { }
namespace A {
export const y = 34;
export interface y { s: string }
}
declare global {
export import x = A.y;
// Should still error
import f = require("fs");
}
const m: number = x;
let s: x = { s: "" };
void s.s;
//// [importAliasInModuleAugmentation.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var A;
(function (A) {
A.y = 34;
})(A || (A = {}));
var m = x;
var s = { s: "" };
void s.s;

Просмотреть файл

@ -0,0 +1,43 @@
//// [tests/cases/compiler/importAliasInModuleAugmentation.ts] ////
=== importAliasInModuleAugmentation.ts ===
export { }
namespace A {
>A : Symbol(A, Decl(importAliasInModuleAugmentation.ts, 0, 10))
export const y = 34;
>y : Symbol(y, Decl(importAliasInModuleAugmentation.ts, 3, 16), Decl(importAliasInModuleAugmentation.ts, 3, 24))
export interface y { s: string }
>y : Symbol(y, Decl(importAliasInModuleAugmentation.ts, 3, 16), Decl(importAliasInModuleAugmentation.ts, 3, 24))
>s : Symbol(y.s, Decl(importAliasInModuleAugmentation.ts, 4, 24))
}
declare global {
>global : Symbol(global, Decl(importAliasInModuleAugmentation.ts, 5, 1))
export import x = A.y;
>x : Symbol(x, Decl(importAliasInModuleAugmentation.ts, 7, 16))
>A : Symbol(A, Decl(importAliasInModuleAugmentation.ts, 0, 10))
>y : Symbol(x, Decl(importAliasInModuleAugmentation.ts, 3, 16), Decl(importAliasInModuleAugmentation.ts, 3, 24))
// Should still error
import f = require("fs");
>f : Symbol(f, Decl(importAliasInModuleAugmentation.ts, 8, 26))
}
const m: number = x;
>m : Symbol(m, Decl(importAliasInModuleAugmentation.ts, 14, 5))
>x : Symbol(x, Decl(importAliasInModuleAugmentation.ts, 7, 16))
let s: x = { s: "" };
>s : Symbol(s, Decl(importAliasInModuleAugmentation.ts, 15, 3))
>x : Symbol(x, Decl(importAliasInModuleAugmentation.ts, 7, 16))
>s : Symbol(s, Decl(importAliasInModuleAugmentation.ts, 15, 12))
void s.s;
>s.s : Symbol(x.s, Decl(importAliasInModuleAugmentation.ts, 4, 24))
>s : Symbol(s, Decl(importAliasInModuleAugmentation.ts, 15, 3))
>s : Symbol(x.s, Decl(importAliasInModuleAugmentation.ts, 4, 24))

Просмотреть файл

@ -0,0 +1,45 @@
//// [tests/cases/compiler/importAliasInModuleAugmentation.ts] ////
=== importAliasInModuleAugmentation.ts ===
export { }
namespace A {
>A : typeof A
export const y = 34;
>y : 34
>34 : 34
export interface y { s: string }
>s : string
}
declare global {
>global : typeof global
export import x = A.y;
>x : 34
>A : typeof A
>y : x
// Should still error
import f = require("fs");
>f : any
}
const m: number = x;
>m : number
>x : 34
let s: x = { s: "" };
>s : x
>{ s: "" } : { s: string; }
>s : string
>"" : ""
void s.s;
>void s.s : undefined
>s.s : string
>s : x
>s : string

Просмотреть файл

@ -3,8 +3,6 @@ f3.ts(11,5): error TS2667: Imports are not permitted in module augmentations. Co
f3.ts(11,21): error TS2307: Cannot find module './f2' or its corresponding type declarations.
f3.ts(12,5): error TS2666: Exports and export assignments are not permitted in module augmentations.
f3.ts(12,21): error TS2307: Cannot find module './f2' or its corresponding type declarations.
f3.ts(13,5): error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
f3.ts(14,5): error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
f4.ts(5,11): error TS2339: Property 'foo' does not exist on type 'A'.
@ -16,7 +14,7 @@ f4.ts(5,11): error TS2339: Property 'foo' does not exist on type 'A'.
n: number;
}
==== f3.ts (7 errors) ====
==== f3.ts (5 errors) ====
import {A} from "./f1";
A.prototype.foo = function () { return undefined; }
@ -40,11 +38,7 @@ f4.ts(5,11): error TS2339: Property 'foo' does not exist on type 'A'.
~~~~~~
!!! error TS2307: Cannot find module './f2' or its corresponding type declarations.
import I = N.Ifc;
~~~~~~
!!! error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
import C = N.Cls;
~~~~~~
!!! error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
// should have explicit export
interface A {
foo(): B;

Просмотреть файл

@ -1,7 +1,5 @@
f3.ts(11,5): error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
f3.ts(11,21): error TS2307: Cannot find module './f2' or its corresponding type declarations.
f3.ts(12,5): error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
f3.ts(13,5): error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
==== f1.ts (0 errors) ====
@ -12,7 +10,7 @@ f3.ts(13,5): error TS2667: Imports are not permitted in module augmentations. Co
n: number;
}
==== f3.ts (4 errors) ====
==== f3.ts (2 errors) ====
import {A} from "./f1";
A.prototype.foo = function () { return undefined; }
@ -29,11 +27,7 @@ f3.ts(13,5): error TS2667: Imports are not permitted in module augmentations. Co
~~~~~~
!!! error TS2307: Cannot find module './f2' or its corresponding type declarations.
import I = N.Ifc;
~~~~~~
!!! error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
import C = N.Cls;
~~~~~~
!!! error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
interface A {
foo(): B;
bar(): I;

Просмотреть файл

@ -1,24 +0,0 @@
f1.d.ts(12,5): error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
==== f1.d.ts (1 errors) ====
export {};
declare module M.M1 {
export let x: number;
}
declare global {
interface SymbolConstructor {
observable: symbol;
}
class Cls {x}
let [a, b]: number[];
export import X = M.M1.x;
~~~~~~
!!! error TS2667: Imports are not permitted in module augmentations. Consider moving them to the enclosing external module.
}
==== main.ts (0 errors) ====
Symbol.observable;
new Cls().x
let c = a + b + X;

Просмотреть файл

@ -0,0 +1,17 @@
export { }
namespace A {
export const y = 34;
export interface y { s: string }
}
declare global {
export import x = A.y;
// Should still error
import f = require("fs");
}
const m: number = x;
let s: x = { s: "" };
void s.s;