Skip to content

Commit 279aa82

Browse files
Don't throw when enabling both syntax-import-{assertions,attributes} (#16781)
* Don't throw when enabling both syntax-import-{assertions,attributes} * Update lockfile
1 parent 1cab561 commit 279aa82

11 files changed

Lines changed: 44 additions & 3 deletions

File tree

β€Žpackages/babel-plugin-syntax-import-assertions/src/index.tsβ€Ž

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,20 @@ export default declare(api => {
66
return {
77
name: "syntax-import-assertions",
88

9-
manipulateOptions(opts, parserOpts) {
10-
parserOpts.plugins.push("importAssertions");
9+
manipulateOptions(opts, { plugins }) {
10+
for (let i = 0; i < plugins.length; i++) {
11+
const plugin = plugins[i];
12+
if (plugin === "importAttributes") {
13+
plugins[i] = ["importAttributes", { deprecatedAssertSyntax: true }];
14+
return;
15+
}
16+
if (Array.isArray(plugin) && plugin[0] === "importAttributes") {
17+
if (plugin.length < 2) (plugins[i] as any[]).push({});
18+
plugin[1].deprecatedAssertSyntax = true;
19+
return;
20+
}
21+
}
22+
plugins.push("importAssertions");
1123
},
1224
};
1325
});

β€Žpackages/babel-plugin-syntax-import-attributes/package.jsonβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"@babel/core": "^7.0.0-0"
2323
},
2424
"devDependencies": {
25-
"@babel/core": "workspace:^"
25+
"@babel/core": "workspace:^",
26+
"@babel/helper-plugin-test-runner": "workspace:^"
2627
},
2728
"engines": {
2829
"node": ">=6.9.0"

β€Žpackages/babel-plugin-syntax-import-attributes/src/index.tsβ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ export default declare((api, { deprecatedAssertSyntax }: Options) => {
2121

2222
manipulateOptions({ parserOpts, generatorOpts }) {
2323
generatorOpts.importAttributesKeyword ??= "with";
24+
25+
const importAssertionsPluginIndex =
26+
parserOpts.plugins.indexOf("importAssertions");
27+
if (importAssertionsPluginIndex !== -1) {
28+
parserOpts.plugins.splice(importAssertionsPluginIndex, 1);
29+
deprecatedAssertSyntax = true;
30+
}
31+
2432
parserOpts.plugins.push([
2533
"importAttributes",
2634
{ deprecatedAssertSyntax: Boolean(deprecatedAssertSyntax) },
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import "x" with { type: "json" };
2+
import "x" assert { type: "json" };
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"sourceType": "module",
3+
"plugins": ["syntax-import-assertions", "syntax-import-attributes"]
4+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import "x" with { type: "json" };
2+
import "x" with { type: "json" };
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import "x" with { type: "json" };
2+
import "x" assert { type: "json" };
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"sourceType": "module",
3+
"plugins": ["syntax-import-attributes", "syntax-import-assertions"]
4+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import "x" with { type: "json" };
2+
import "x" with { type: "json" };
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import runner from "@babel/helper-plugin-test-runner";
2+
3+
runner(import.meta.url);

0 commit comments

Comments
Β (0)