Skip to content

Commit f4a3a8a

Browse files
🤖 Pick PR #61175 (Ban import=require and export= unde...) into release-5.8 (#61178)
Co-authored-by: Jake Bailey <[email protected]>
1 parent 420ff06 commit f4a3a8a

10 files changed

+341
-89
lines changed

Diff for: ‎src/compiler/checker.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -47830,11 +47830,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4783047830
}
4783147831

4783247832
checkGrammarModifiers(node);
47833-
const isImportEquals = isInternalModuleImportEqualsDeclaration(node);
47834-
if (compilerOptions.erasableSyntaxOnly && isImportEquals && !(node.flags & NodeFlags.Ambient)) {
47833+
if (compilerOptions.erasableSyntaxOnly && !(node.flags & NodeFlags.Ambient)) {
4783547834
error(node, Diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled);
4783647835
}
47837-
if (isImportEquals || checkExternalImportOrExportDeclaration(node)) {
47836+
if (isInternalModuleImportEqualsDeclaration(node) || checkExternalImportOrExportDeclaration(node)) {
4783847837
checkImportBinding(node);
4783947838
markLinkedReferences(node, ReferenceHint.ExportImportEquals);
4784047839
if (node.moduleReference.kind !== SyntaxKind.ExternalModuleReference) {
@@ -47975,6 +47974,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4797547974
return;
4797647975
}
4797747976

47977+
if (compilerOptions.erasableSyntaxOnly && !(node.flags & NodeFlags.Ambient)) {
47978+
error(node, Diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled);
47979+
}
4797847980
const container = node.parent.kind === SyntaxKind.SourceFile ? node.parent : node.parent.parent as ModuleDeclaration;
4797947981
if (container.kind === SyntaxKind.ModuleDeclaration && !isAmbientModule(container)) {
4798047982
if (node.isExportEquals) {

Diff for: ‎tests/baselines/reference/erasableSyntaxOnly.errors.txt

+24-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
erasableSyntaxOnly.ts(3,17): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
2-
erasableSyntaxOnly.ts(6,11): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
3-
erasableSyntaxOnly.ts(10,11): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
4-
erasableSyntaxOnly.ts(16,11): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
5-
erasableSyntaxOnly.ts(17,15): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
6-
erasableSyntaxOnly.ts(22,6): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
7-
erasableSyntaxOnly.ts(26,1): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
8-
erasableSyntaxOnly.ts(28,12): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
1+
commonjs.cts(1,1): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
2+
commonjs.cts(2,1): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
3+
index.ts(3,17): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
4+
index.ts(6,11): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
5+
index.ts(10,11): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
6+
index.ts(16,11): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
7+
index.ts(17,15): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
8+
index.ts(22,6): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
9+
index.ts(26,1): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
10+
index.ts(28,12): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
911

1012

11-
==== erasableSyntaxOnly.ts (8 errors) ====
13+
==== index.ts (8 errors) ====
1214
class MyClassErr {
1315
// No parameter properties
1416
constructor(public foo: string) { }
@@ -89,4 +91,17 @@ erasableSyntaxOnly.ts(28,12): error TS1294: This syntax is not allowed when 'era
8991

9092
import FineAlias = EnumInAmbientContext.B;
9193
}
94+
95+
==== commonjs.cts (2 errors) ====
96+
import foo = require("./other.cjs");
97+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
98+
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
99+
export = foo;
100+
~~~~~~~~~~~~~
101+
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
102+
103+
104+
==== other.d.cts (0 errors) ====
105+
declare function foo(): void;
106+
export = foo;
92107

Diff for: ‎tests/baselines/reference/erasableSyntaxOnly.js

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
//// [tests/cases/compiler/erasableSyntaxOnly.ts] ////
2+
3+
//// [index.ts]
4+
class MyClassErr {
5+
// No parameter properties
6+
constructor(public foo: string) { }
7+
}
8+
9+
namespace IllegalBecauseInstantiated {
10+
export const m = 1;
11+
}
12+
13+
namespace AlsoIllegalBecauseInstantiated {
14+
class PrivateClass {
15+
16+
}
17+
}
18+
19+
namespace IllegalBecauseNestedInstantiated {
20+
namespace Nested {
21+
export const m = 1;
22+
}
23+
}
24+
25+
enum NotLegalEnum {
26+
B = 1
27+
}
28+
29+
import NoGoodAlias = NotLegalEnum.B;
30+
31+
const enum NotLegalConstEnum {
32+
C = 2
33+
}
34+
35+
// No errors after this point
36+
class MyClassOk {
37+
// Not a parameter property, ok
38+
constructor(foo: string) { }
39+
}
40+
41+
// Note for implementors: This should not be an error
42+
// as this entire namespace block is fully erased
43+
namespace NotInstantiated {
44+
export interface JustAType { }
45+
export type ATypeInANamespace = {};
46+
namespace Nested {
47+
export type ATypeInANamespace = {};
48+
}
49+
}
50+
declare namespace AmbientIsNotInstantiated {
51+
export const stillOk = 12;
52+
}
53+
54+
declare enum LegalEnum {
55+
A = 1
56+
}
57+
58+
declare namespace AmbientStuff {
59+
namespace Nested {
60+
export const stillOk = 12;
61+
}
62+
enum EnumInAmbientContext {
63+
B = 1
64+
}
65+
66+
import FineAlias = EnumInAmbientContext.B;
67+
}
68+
69+
//// [commonjs.cts]
70+
import foo = require("./other.cjs");
71+
export = foo;
72+
73+
74+
//// [other.d.cts]
75+
declare function foo(): void;
76+
export = foo;
77+
78+
79+
//// [index.js]
80+
var MyClassErr = /** @class */ (function () {
81+
// No parameter properties
82+
function MyClassErr(foo) {
83+
this.foo = foo;
84+
}
85+
return MyClassErr;
86+
}());
87+
var IllegalBecauseInstantiated;
88+
(function (IllegalBecauseInstantiated) {
89+
IllegalBecauseInstantiated.m = 1;
90+
})(IllegalBecauseInstantiated || (IllegalBecauseInstantiated = {}));
91+
var AlsoIllegalBecauseInstantiated;
92+
(function (AlsoIllegalBecauseInstantiated) {
93+
var PrivateClass = /** @class */ (function () {
94+
function PrivateClass() {
95+
}
96+
return PrivateClass;
97+
}());
98+
})(AlsoIllegalBecauseInstantiated || (AlsoIllegalBecauseInstantiated = {}));
99+
var IllegalBecauseNestedInstantiated;
100+
(function (IllegalBecauseNestedInstantiated) {
101+
var Nested;
102+
(function (Nested) {
103+
Nested.m = 1;
104+
})(Nested || (Nested = {}));
105+
})(IllegalBecauseNestedInstantiated || (IllegalBecauseNestedInstantiated = {}));
106+
var NotLegalEnum;
107+
(function (NotLegalEnum) {
108+
NotLegalEnum[NotLegalEnum["B"] = 1] = "B";
109+
})(NotLegalEnum || (NotLegalEnum = {}));
110+
var NoGoodAlias = NotLegalEnum.B;
111+
// No errors after this point
112+
var MyClassOk = /** @class */ (function () {
113+
// Not a parameter property, ok
114+
function MyClassOk(foo) {
115+
}
116+
return MyClassOk;
117+
}());
118+
//// [commonjs.cjs]
119+
"use strict";
120+
var foo = require("./other.cjs");
121+
module.exports = foo;
+51-36
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,135 @@
11
//// [tests/cases/compiler/erasableSyntaxOnly.ts] ////
22

3-
=== erasableSyntaxOnly.ts ===
3+
=== index.ts ===
44
class MyClassErr {
5-
>MyClassErr : Symbol(MyClassErr, Decl(erasableSyntaxOnly.ts, 0, 0))
5+
>MyClassErr : Symbol(MyClassErr, Decl(index.ts, 0, 0))
66

77
// No parameter properties
88
constructor(public foo: string) { }
9-
>foo : Symbol(MyClassErr.foo, Decl(erasableSyntaxOnly.ts, 2, 16))
9+
>foo : Symbol(MyClassErr.foo, Decl(index.ts, 2, 16))
1010
}
1111

1212
namespace IllegalBecauseInstantiated {
13-
>IllegalBecauseInstantiated : Symbol(IllegalBecauseInstantiated, Decl(erasableSyntaxOnly.ts, 3, 1))
13+
>IllegalBecauseInstantiated : Symbol(IllegalBecauseInstantiated, Decl(index.ts, 3, 1))
1414

1515
export const m = 1;
16-
>m : Symbol(m, Decl(erasableSyntaxOnly.ts, 6, 16))
16+
>m : Symbol(m, Decl(index.ts, 6, 16))
1717
}
1818

1919
namespace AlsoIllegalBecauseInstantiated {
20-
>AlsoIllegalBecauseInstantiated : Symbol(AlsoIllegalBecauseInstantiated, Decl(erasableSyntaxOnly.ts, 7, 1))
20+
>AlsoIllegalBecauseInstantiated : Symbol(AlsoIllegalBecauseInstantiated, Decl(index.ts, 7, 1))
2121

2222
class PrivateClass {
23-
>PrivateClass : Symbol(PrivateClass, Decl(erasableSyntaxOnly.ts, 9, 42))
23+
>PrivateClass : Symbol(PrivateClass, Decl(index.ts, 9, 42))
2424

2525
}
2626
}
2727

2828
namespace IllegalBecauseNestedInstantiated {
29-
>IllegalBecauseNestedInstantiated : Symbol(IllegalBecauseNestedInstantiated, Decl(erasableSyntaxOnly.ts, 13, 1))
29+
>IllegalBecauseNestedInstantiated : Symbol(IllegalBecauseNestedInstantiated, Decl(index.ts, 13, 1))
3030

3131
namespace Nested {
32-
>Nested : Symbol(Nested, Decl(erasableSyntaxOnly.ts, 15, 44))
32+
>Nested : Symbol(Nested, Decl(index.ts, 15, 44))
3333

3434
export const m = 1;
35-
>m : Symbol(m, Decl(erasableSyntaxOnly.ts, 17, 20))
35+
>m : Symbol(m, Decl(index.ts, 17, 20))
3636
}
3737
}
3838

3939
enum NotLegalEnum {
40-
>NotLegalEnum : Symbol(NotLegalEnum, Decl(erasableSyntaxOnly.ts, 19, 1))
40+
>NotLegalEnum : Symbol(NotLegalEnum, Decl(index.ts, 19, 1))
4141

4242
B = 1
43-
>B : Symbol(NoGoodAlias, Decl(erasableSyntaxOnly.ts, 21, 19))
43+
>B : Symbol(NoGoodAlias, Decl(index.ts, 21, 19))
4444
}
4545

4646
import NoGoodAlias = NotLegalEnum.B;
47-
>NoGoodAlias : Symbol(NoGoodAlias, Decl(erasableSyntaxOnly.ts, 23, 1))
48-
>NotLegalEnum : Symbol(NotLegalEnum, Decl(erasableSyntaxOnly.ts, 19, 1))
49-
>B : Symbol(NoGoodAlias, Decl(erasableSyntaxOnly.ts, 21, 19))
47+
>NoGoodAlias : Symbol(NoGoodAlias, Decl(index.ts, 23, 1))
48+
>NotLegalEnum : Symbol(NotLegalEnum, Decl(index.ts, 19, 1))
49+
>B : Symbol(NoGoodAlias, Decl(index.ts, 21, 19))
5050

5151
const enum NotLegalConstEnum {
52-
>NotLegalConstEnum : Symbol(NotLegalConstEnum, Decl(erasableSyntaxOnly.ts, 25, 36))
52+
>NotLegalConstEnum : Symbol(NotLegalConstEnum, Decl(index.ts, 25, 36))
5353

5454
C = 2
55-
>C : Symbol(NotLegalConstEnum.C, Decl(erasableSyntaxOnly.ts, 27, 30))
55+
>C : Symbol(NotLegalConstEnum.C, Decl(index.ts, 27, 30))
5656
}
5757

5858
// No errors after this point
5959
class MyClassOk {
60-
>MyClassOk : Symbol(MyClassOk, Decl(erasableSyntaxOnly.ts, 29, 1))
60+
>MyClassOk : Symbol(MyClassOk, Decl(index.ts, 29, 1))
6161

6262
// Not a parameter property, ok
6363
constructor(foo: string) { }
64-
>foo : Symbol(foo, Decl(erasableSyntaxOnly.ts, 34, 16))
64+
>foo : Symbol(foo, Decl(index.ts, 34, 16))
6565
}
6666

6767
// Note for implementors: This should not be an error
6868
// as this entire namespace block is fully erased
6969
namespace NotInstantiated {
70-
>NotInstantiated : Symbol(NotInstantiated, Decl(erasableSyntaxOnly.ts, 35, 1))
70+
>NotInstantiated : Symbol(NotInstantiated, Decl(index.ts, 35, 1))
7171

7272
export interface JustAType { }
73-
>JustAType : Symbol(JustAType, Decl(erasableSyntaxOnly.ts, 39, 27))
73+
>JustAType : Symbol(JustAType, Decl(index.ts, 39, 27))
7474

7575
export type ATypeInANamespace = {};
76-
>ATypeInANamespace : Symbol(ATypeInANamespace, Decl(erasableSyntaxOnly.ts, 40, 34))
76+
>ATypeInANamespace : Symbol(ATypeInANamespace, Decl(index.ts, 40, 34))
7777

7878
namespace Nested {
79-
>Nested : Symbol(Nested, Decl(erasableSyntaxOnly.ts, 41, 39))
79+
>Nested : Symbol(Nested, Decl(index.ts, 41, 39))
8080

8181
export type ATypeInANamespace = {};
82-
>ATypeInANamespace : Symbol(ATypeInANamespace, Decl(erasableSyntaxOnly.ts, 42, 22))
82+
>ATypeInANamespace : Symbol(ATypeInANamespace, Decl(index.ts, 42, 22))
8383
}
8484
}
8585
declare namespace AmbientIsNotInstantiated {
86-
>AmbientIsNotInstantiated : Symbol(AmbientIsNotInstantiated, Decl(erasableSyntaxOnly.ts, 45, 1))
86+
>AmbientIsNotInstantiated : Symbol(AmbientIsNotInstantiated, Decl(index.ts, 45, 1))
8787

8888
export const stillOk = 12;
89-
>stillOk : Symbol(stillOk, Decl(erasableSyntaxOnly.ts, 47, 16))
89+
>stillOk : Symbol(stillOk, Decl(index.ts, 47, 16))
9090
}
9191

9292
declare enum LegalEnum {
93-
>LegalEnum : Symbol(LegalEnum, Decl(erasableSyntaxOnly.ts, 48, 1))
93+
>LegalEnum : Symbol(LegalEnum, Decl(index.ts, 48, 1))
9494

9595
A = 1
96-
>A : Symbol(LegalEnum.A, Decl(erasableSyntaxOnly.ts, 50, 24))
96+
>A : Symbol(LegalEnum.A, Decl(index.ts, 50, 24))
9797
}
9898

9999
declare namespace AmbientStuff {
100-
>AmbientStuff : Symbol(AmbientStuff, Decl(erasableSyntaxOnly.ts, 52, 1))
100+
>AmbientStuff : Symbol(AmbientStuff, Decl(index.ts, 52, 1))
101101

102102
namespace Nested {
103-
>Nested : Symbol(Nested, Decl(erasableSyntaxOnly.ts, 54, 32))
103+
>Nested : Symbol(Nested, Decl(index.ts, 54, 32))
104104

105105
export const stillOk = 12;
106-
>stillOk : Symbol(stillOk, Decl(erasableSyntaxOnly.ts, 56, 20))
106+
>stillOk : Symbol(stillOk, Decl(index.ts, 56, 20))
107107
}
108108
enum EnumInAmbientContext {
109-
>EnumInAmbientContext : Symbol(EnumInAmbientContext, Decl(erasableSyntaxOnly.ts, 57, 5))
109+
>EnumInAmbientContext : Symbol(EnumInAmbientContext, Decl(index.ts, 57, 5))
110110

111111
B = 1
112-
>B : Symbol(FineAlias, Decl(erasableSyntaxOnly.ts, 58, 31))
112+
>B : Symbol(FineAlias, Decl(index.ts, 58, 31))
113113
}
114114

115115
import FineAlias = EnumInAmbientContext.B;
116-
>FineAlias : Symbol(FineAlias, Decl(erasableSyntaxOnly.ts, 60, 5))
117-
>EnumInAmbientContext : Symbol(EnumInAmbientContext, Decl(erasableSyntaxOnly.ts, 57, 5))
118-
>B : Symbol(FineAlias, Decl(erasableSyntaxOnly.ts, 58, 31))
116+
>FineAlias : Symbol(FineAlias, Decl(index.ts, 60, 5))
117+
>EnumInAmbientContext : Symbol(EnumInAmbientContext, Decl(index.ts, 57, 5))
118+
>B : Symbol(FineAlias, Decl(index.ts, 58, 31))
119119
}
120120

121+
=== commonjs.cts ===
122+
import foo = require("./other.cjs");
123+
>foo : Symbol(foo, Decl(commonjs.cts, 0, 0))
124+
125+
export = foo;
126+
>foo : Symbol(foo, Decl(commonjs.cts, 0, 0))
127+
128+
129+
=== other.d.cts ===
130+
declare function foo(): void;
131+
>foo : Symbol(foo, Decl(other.d.cts, 0, 0))
132+
133+
export = foo;
134+
>foo : Symbol(foo, Decl(other.d.cts, 0, 0))
135+

Diff for: ‎tests/baselines/reference/erasableSyntaxOnly.types

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//// [tests/cases/compiler/erasableSyntaxOnly.ts] ////
22

3-
=== erasableSyntaxOnly.ts ===
3+
=== index.ts ===
44
class MyClassErr {
55
>MyClassErr : MyClassErr
66
> : ^^^^^^^^^^
@@ -160,3 +160,22 @@ declare namespace AmbientStuff {
160160
> : ^^^^^^^^^^^^^^^^^^^^^^
161161
}
162162

163+
=== commonjs.cts ===
164+
import foo = require("./other.cjs");
165+
>foo : () => void
166+
> : ^^^^^^
167+
168+
export = foo;
169+
>foo : () => void
170+
> : ^^^^^^
171+
172+
173+
=== other.d.cts ===
174+
declare function foo(): void;
175+
>foo : () => void
176+
> : ^^^^^^
177+
178+
export = foo;
179+
>foo : () => void
180+
> : ^^^^^^
181+

0 commit comments

Comments
 (0)