Content-Length: 1054117 | pFad | https://github.com/babel/babel/commit/#start-of-content

6844A1FD fix: disable using in ambient context (#17371) · babel/babel@0f50baf · GitHub
Skip to content

Commit 0f50baf

Browse files
authored
fix: disable using in ambient context (#17371)
* fix: disable using in ambient context * update allowlist
1 parent 4a6c347 commit 0f50baf

File tree

12 files changed

+320
-65
lines changed

12 files changed

+320
-65
lines changed

eslint/babel-eslint-parser/test/typescript-estree.test.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,6 @@ function deeplyRemoveProperties(obj, props) {
338338
);
339339
const fixtures = getFixtures(parserTestFixtureRoot);
340340
const FAILURES = new Set([
341-
// Todo: Remove when https://github.com/typescript-eslint/typescript-eslint/issues/11244 is resolved
342-
"typescript/declare/valid-namespace-var/input.ts",
343-
344341
// ts-eslint/tsc does not support arrow generic in tsx mode
345342
"typescript/arrow-function/async-await-null/input.ts",
346343
"typescript/arrow-function/async-generic-after-await/input.ts",

packages/babel-parser/src/plugins/typescript/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ const TSErrors = ParseErrorEnum`typescript`({
221221
"A parameter property may not be declared using a binding pattern.",
222222
UnsupportedSignatureParameterKind: ({ type }: { type: string }) =>
223223
`Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got ${type}.`,
224+
UsingDeclarationInAmbientContext: (kind: "using" | "await using") =>
225+
`'${kind}' declarations are not allowed in ambient contexts.`,
224226
});
225227

226228
/* eslint-disable sort-keys */
@@ -3131,6 +3133,12 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
31313133

31323134
if (!isAmbientContext) return declaration;
31333135

3136+
// If node.declare is true, the error has already been raised in tsTryParseDeclare.
3137+
if (!node.declare && (kind === "using" || kind === "await using")) {
3138+
this.raise(TSErrors.UsingDeclarationInAmbientContext, node, kind);
3139+
return declaration;
3140+
}
3141+
31343142
for (const { id, init } of declaration.declarations) {
31353143
// Empty initializer is the easy case that we want.
31363144
if (!init) continue;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
declare namespace invalid_namespace_var {
2+
await using A;
3+
await using A1 = 0;
4+
await using A2: number = 0;
5+
await using B, C;
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"plugins": ["explicitResourceManagement", "typescript"],
3+
"allowAwaitOutsideFunction": true,
4+
"sourceType": "module"
5+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
{
2+
"type": "File",
3+
"start":0,"end":132,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":6,"column":1,"index":132}},
4+
"errors": [
5+
"SyntaxError: 'await using' declarations are not allowed in ambient contexts. (2:2)",
6+
"SyntaxError: 'await using' declarations are not allowed in ambient contexts. (3:2)",
7+
"SyntaxError: 'await using' declarations are not allowed in ambient contexts. (4:2)",
8+
"SyntaxError: 'await using' declarations are not allowed in ambient contexts. (5:2)"
9+
],
10+
"program": {
11+
"type": "Program",
12+
"start":0,"end":132,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":6,"column":1,"index":132}},
13+
"sourceType": "module",
14+
"interpreter": null,
15+
"body": [
16+
{
17+
"type": "TSModuleDeclaration",
18+
"start":0,"end":132,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":6,"column":1,"index":132}},
19+
"kind": "namespace",
20+
"id": {
21+
"type": "Identifier",
22+
"start":18,"end":39,"loc":{"start":{"line":1,"column":18,"index":18},"end":{"line":1,"column":39,"index":39},"identifierName":"invalid_namespace_var"},
23+
"name": "invalid_namespace_var"
24+
},
25+
"body": {
26+
"type": "TSModuleBlock",
27+
"start":40,"end":132,"loc":{"start":{"line":1,"column":40,"index":40},"end":{"line":6,"column":1,"index":132}},
28+
"body": [
29+
{
30+
"type": "VariableDeclaration",
31+
"start":44,"end":58,"loc":{"start":{"line":2,"column":2,"index":44},"end":{"line":2,"column":16,"index":58}},
32+
"declarations": [
33+
{
34+
"type": "VariableDeclarator",
35+
"start":56,"end":57,"loc":{"start":{"line":2,"column":14,"index":56},"end":{"line":2,"column":15,"index":57}},
36+
"id": {
37+
"type": "Identifier",
38+
"start":56,"end":57,"loc":{"start":{"line":2,"column":14,"index":56},"end":{"line":2,"column":15,"index":57},"identifierName":"A"},
39+
"name": "A"
40+
},
41+
"init": null
42+
}
43+
],
44+
"kind": "await using"
45+
},
46+
{
47+
"type": "VariableDeclaration",
48+
"start":61,"end":80,"loc":{"start":{"line":3,"column":2,"index":61},"end":{"line":3,"column":21,"index":80}},
49+
"declarations": [
50+
{
51+
"type": "VariableDeclarator",
52+
"start":73,"end":79,"loc":{"start":{"line":3,"column":14,"index":73},"end":{"line":3,"column":20,"index":79}},
53+
"id": {
54+
"type": "Identifier",
55+
"start":73,"end":75,"loc":{"start":{"line":3,"column":14,"index":73},"end":{"line":3,"column":16,"index":75},"identifierName":"A1"},
56+
"name": "A1"
57+
},
58+
"init": {
59+
"type": "NumericLiteral",
60+
"start":78,"end":79,"loc":{"start":{"line":3,"column":19,"index":78},"end":{"line":3,"column":20,"index":79}},
61+
"extra": {
62+
"rawValue": 0,
63+
"raw": "0"
64+
},
65+
"value": 0
66+
}
67+
}
68+
],
69+
"kind": "await using"
70+
},
71+
{
72+
"type": "VariableDeclaration",
73+
"start":83,"end":110,"loc":{"start":{"line":4,"column":2,"index":83},"end":{"line":4,"column":29,"index":110}},
74+
"declarations": [
75+
{
76+
"type": "VariableDeclarator",
77+
"start":95,"end":109,"loc":{"start":{"line":4,"column":14,"index":95},"end":{"line":4,"column":28,"index":109}},
78+
"id": {
79+
"type": "Identifier",
80+
"start":95,"end":105,"loc":{"start":{"line":4,"column":14,"index":95},"end":{"line":4,"column":24,"index":105},"identifierName":"A2"},
81+
"name": "A2",
82+
"typeAnnotation": {
83+
"type": "TSTypeAnnotation",
84+
"start":97,"end":105,"loc":{"start":{"line":4,"column":16,"index":97},"end":{"line":4,"column":24,"index":105}},
85+
"typeAnnotation": {
86+
"type": "TSNumberKeyword",
87+
"start":99,"end":105,"loc":{"start":{"line":4,"column":18,"index":99},"end":{"line":4,"column":24,"index":105}}
88+
}
89+
}
90+
},
91+
"init": {
92+
"type": "NumericLiteral",
93+
"start":108,"end":109,"loc":{"start":{"line":4,"column":27,"index":108},"end":{"line":4,"column":28,"index":109}},
94+
"extra": {
95+
"rawValue": 0,
96+
"raw": "0"
97+
},
98+
"value": 0
99+
}
100+
}
101+
],
102+
"kind": "await using"
103+
},
104+
{
105+
"type": "VariableDeclaration",
106+
"start":113,"end":130,"loc":{"start":{"line":5,"column":2,"index":113},"end":{"line":5,"column":19,"index":130}},
107+
"declarations": [
108+
{
109+
"type": "VariableDeclarator",
110+
"start":125,"end":126,"loc":{"start":{"line":5,"column":14,"index":125},"end":{"line":5,"column":15,"index":126}},
111+
"id": {
112+
"type": "Identifier",
113+
"start":125,"end":126,"loc":{"start":{"line":5,"column":14,"index":125},"end":{"line":5,"column":15,"index":126},"identifierName":"B"},
114+
"name": "B"
115+
},
116+
"init": null
117+
},
118+
{
119+
"type": "VariableDeclarator",
120+
"start":128,"end":129,"loc":{"start":{"line":5,"column":17,"index":128},"end":{"line":5,"column":18,"index":129}},
121+
"id": {
122+
"type": "Identifier",
123+
"start":128,"end":129,"loc":{"start":{"line":5,"column":17,"index":128},"end":{"line":5,"column":18,"index":129},"identifierName":"C"},
124+
"name": "C"
125+
},
126+
"init": null
127+
}
128+
],
129+
"kind": "await using"
130+
}
131+
]
132+
},
133+
"declare": true
134+
}
135+
],
136+
"directives": [],
137+
"extra": {
138+
"topLevelAwait": true
139+
}
140+
}
141+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
declare namespace invalid_namespace_var {
2+
using A;
3+
using A1 = 0;
4+
using A2: number = 0;
5+
using B, C;
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"plugins": ["explicitResourceManagement", "typescript"],
3+
"sourceType": "module"
4+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
{
2+
"type": "File",
3+
"start":0,"end":108,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":6,"column":1,"index":108}},
4+
"errors": [
5+
"SyntaxError: 'using' declarations are not allowed in ambient contexts. (2:2)",
6+
"SyntaxError: 'using' declarations are not allowed in ambient contexts. (3:2)",
7+
"SyntaxError: 'using' declarations are not allowed in ambient contexts. (4:2)",
8+
"SyntaxError: 'using' declarations are not allowed in ambient contexts. (5:2)"
9+
],
10+
"program": {
11+
"type": "Program",
12+
"start":0,"end":108,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":6,"column":1,"index":108}},
13+
"sourceType": "module",
14+
"interpreter": null,
15+
"body": [
16+
{
17+
"type": "TSModuleDeclaration",
18+
"start":0,"end":108,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":6,"column":1,"index":108}},
19+
"kind": "namespace",
20+
"id": {
21+
"type": "Identifier",
22+
"start":18,"end":39,"loc":{"start":{"line":1,"column":18,"index":18},"end":{"line":1,"column":39,"index":39},"identifierName":"invalid_namespace_var"},
23+
"name": "invalid_namespace_var"
24+
},
25+
"body": {
26+
"type": "TSModuleBlock",
27+
"start":40,"end":108,"loc":{"start":{"line":1,"column":40,"index":40},"end":{"line":6,"column":1,"index":108}},
28+
"body": [
29+
{
30+
"type": "VariableDeclaration",
31+
"start":44,"end":52,"loc":{"start":{"line":2,"column":2,"index":44},"end":{"line":2,"column":10,"index":52}},
32+
"declarations": [
33+
{
34+
"type": "VariableDeclarator",
35+
"start":50,"end":51,"loc":{"start":{"line":2,"column":8,"index":50},"end":{"line":2,"column":9,"index":51}},
36+
"id": {
37+
"type": "Identifier",
38+
"start":50,"end":51,"loc":{"start":{"line":2,"column":8,"index":50},"end":{"line":2,"column":9,"index":51},"identifierName":"A"},
39+
"name": "A"
40+
},
41+
"init": null
42+
}
43+
],
44+
"kind": "using"
45+
},
46+
{
47+
"type": "VariableDeclaration",
48+
"start":55,"end":68,"loc":{"start":{"line":3,"column":2,"index":55},"end":{"line":3,"column":15,"index":68}},
49+
"declarations": [
50+
{
51+
"type": "VariableDeclarator",
52+
"start":61,"end":67,"loc":{"start":{"line":3,"column":8,"index":61},"end":{"line":3,"column":14,"index":67}},
53+
"id": {
54+
"type": "Identifier",
55+
"start":61,"end":63,"loc":{"start":{"line":3,"column":8,"index":61},"end":{"line":3,"column":10,"index":63},"identifierName":"A1"},
56+
"name": "A1"
57+
},
58+
"init": {
59+
"type": "NumericLiteral",
60+
"start":66,"end":67,"loc":{"start":{"line":3,"column":13,"index":66},"end":{"line":3,"column":14,"index":67}},
61+
"extra": {
62+
"rawValue": 0,
63+
"raw": "0"
64+
},
65+
"value": 0
66+
}
67+
}
68+
],
69+
"kind": "using"
70+
},
71+
{
72+
"type": "VariableDeclaration",
73+
"start":71,"end":92,"loc":{"start":{"line":4,"column":2,"index":71},"end":{"line":4,"column":23,"index":92}},
74+
"declarations": [
75+
{
76+
"type": "VariableDeclarator",
77+
"start":77,"end":91,"loc":{"start":{"line":4,"column":8,"index":77},"end":{"line":4,"column":22,"index":91}},
78+
"id": {
79+
"type": "Identifier",
80+
"start":77,"end":87,"loc":{"start":{"line":4,"column":8,"index":77},"end":{"line":4,"column":18,"index":87},"identifierName":"A2"},
81+
"name": "A2",
82+
"typeAnnotation": {
83+
"type": "TSTypeAnnotation",
84+
"start":79,"end":87,"loc":{"start":{"line":4,"column":10,"index":79},"end":{"line":4,"column":18,"index":87}},
85+
"typeAnnotation": {
86+
"type": "TSNumberKeyword",
87+
"start":81,"end":87,"loc":{"start":{"line":4,"column":12,"index":81},"end":{"line":4,"column":18,"index":87}}
88+
}
89+
}
90+
},
91+
"init": {
92+
"type": "NumericLiteral",
93+
"start":90,"end":91,"loc":{"start":{"line":4,"column":21,"index":90},"end":{"line":4,"column":22,"index":91}},
94+
"extra": {
95+
"rawValue": 0,
96+
"raw": "0"
97+
},
98+
"value": 0
99+
}
100+
}
101+
],
102+
"kind": "using"
103+
},
104+
{
105+
"type": "VariableDeclaration",
106+
"start":95,"end":106,"loc":{"start":{"line":5,"column":2,"index":95},"end":{"line":5,"column":13,"index":106}},
107+
"declarations": [
108+
{
109+
"type": "VariableDeclarator",
110+
"start":101,"end":102,"loc":{"start":{"line":5,"column":8,"index":101},"end":{"line":5,"column":9,"index":102}},
111+
"id": {
112+
"type": "Identifier",
113+
"start":101,"end":102,"loc":{"start":{"line":5,"column":8,"index":101},"end":{"line":5,"column":9,"index":102},"identifierName":"B"},
114+
"name": "B"
115+
},
116+
"init": null
117+
},
118+
{
119+
"type": "VariableDeclarator",
120+
"start":104,"end":105,"loc":{"start":{"line":5,"column":11,"index":104},"end":{"line":5,"column":12,"index":105}},
121+
"id": {
122+
"type": "Identifier",
123+
"start":104,"end":105,"loc":{"start":{"line":5,"column":11,"index":104},"end":{"line":5,"column":12,"index":105},"identifierName":"C"},
124+
"name": "C"
125+
},
126+
"init": null
127+
}
128+
],
129+
"kind": "using"
130+
}
131+
]
132+
},
133+
"declare": true
134+
}
135+
],
136+
"directives": [],
137+
"extra": {
138+
"topLevelAwait": false
139+
}
140+
}
141+
}

packages/babel-parser/test/fixtures/typescript/declare/invalid-namespace-var/input.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ declare namespace invalid_namespace_var {
44
let B = 0;
55
let B1: number = 0;
66
const C: number = 0;
7-
using D: number = 0;
87
}

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/babel/babel/commit/#start-of-content

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy