From 52aa5459ee3c6098761f3a879daf95a7027cb775 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Tue, 13 Aug 2024 16:54:20 +0200 Subject: [PATCH] Add features up to import attributes --- package.json | 5 ++++ src/index.js | 23 ++++++++++++++---- test/fixtures.json | 10 ++++++-- test/index.js | 2 +- yarn.lock | 58 +++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 90 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index b51f0eb..b89337e 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,8 @@ "@babel/plugin-syntax-async-generators": "^7.8.4", "@babel/plugin-syntax-bigint": "^7.8.3", "@babel/plugin-syntax-class-properties": "^7.12.13", + "@babel/plugin-syntax-class-static-block": "^7.14.5", + "@babel/plugin-syntax-import-attributes": "^7.24.7", "@babel/plugin-syntax-import-meta": "^7.10.4", "@babel/plugin-syntax-json-strings": "^7.8.3", "@babel/plugin-syntax-logical-assignment-operators": "^7.10.4", @@ -27,6 +29,7 @@ "@babel/plugin-syntax-object-rest-spread": "^7.8.3", "@babel/plugin-syntax-optional-catch-binding": "^7.8.3", "@babel/plugin-syntax-optional-chaining": "^7.8.3", + "@babel/plugin-syntax-private-property-in-object": "^7.14.5", "@babel/plugin-syntax-top-level-await": "^7.14.5" }, "peerDependencies": { @@ -35,6 +38,8 @@ "devDependencies": { "@babel/core": "7.25.2", "@babel/parser-7.0.0": "npm:@babel/parser@7.0.0", + "@babel/parser-7.12.0": "npm:@babel/parser@7.12.0", + "@babel/parser-7.22.0": "npm:@babel/parser@7.22.0", "@babel/parser-7.9.0": "npm:@babel/parser@7.9.0" }, "license": "MIT", diff --git a/src/index.js b/src/index.js index e0243bd..8523890 100644 --- a/src/index.js +++ b/src/index.js @@ -8,19 +8,27 @@ const tests = { "json-strings": ["'\\u2028'"], // Babel 7.2.0 // ECMAScript 2020 - "bigint": ["1n"], // Babel 7.8.0 + bigint: ["1n"], // Babel 7.8.0 "optional-chaining": ["a?.b"], // Babel 7.9.0 "nullish-coalescing-operator": ["a ?? b"], // Babel 7.9.0 // import.meta is handled manually - // Stage 3 + // ECMAScript 2021 "numeric-separator": ["1_2"], + "logical-assignment-operators": ["a ||= b", "a &&= b", "a ??= c"], + + // ECMAScript 2022 "class-properties": [ "(class { x = 1 })", "(class { #x = 1 })", "(class { #x() {} })", ], - "logical-assignment-operators": ["a ||= b", "a &&= b", "a ??= c"], + "private-property-in-object": ["(class { #x; m() { #x in y } })"], + "class-static-block": ["(class { static {} })"], + // top-level await is handled manually + + // Stage 3 + // import attributes is handled manually }; const plugins = []; @@ -53,5 +61,12 @@ if (major > 10 || (major === 10 && minor >= 4)) { if (major > 14 || (major === 14 && minor >= 3)) { plugins.push(require.resolve("@babel/plugin-syntax-top-level-await")); } - +// Similar for import attributes +if ( + major > 20 || + (major === 20 && minor >= 10) || + (major === 18 && minor >= 20) +) { + plugins.push(require.resolve("@babel/plugin-syntax-import-attributes")); +} module.exports = () => ({ plugins }); diff --git a/test/fixtures.json b/test/fixtures.json index 61151bc..7b520ae 100644 --- a/test/fixtures.json +++ b/test/fixtures.json @@ -1,5 +1,5 @@ { - "#______name_______": [ "min node version", "code", "@babel/parser" ], + "#______name_______": ["min node version", "code", "@babel/parser"], "object rest/spread": [8.6, "({...x})"], "async generators": [10.0, "async function* f() {}"], "optional catch binding": [10.0, "try {} catch {}"], @@ -7,9 +7,15 @@ "bigint": [10.8, "10n"], "public class fields": [12.4, "class A { x = 1 }"], "private class fields": [12.4, "class A { #x = 1 }"], + "class blocks": [12.4, "class A { static {} }", "@babel/parser-7.12.0"], "numeric separators": [12.7, "1_2"], "optional chaining": [14.0, "a?.b"], "nullish coalescing": [14.0, "a ?? b"], "import.meta": [10.4, "import.meta"], - "top level await": [14.3, "await Promise.resolve()", "@babel/parser-7.9.0"] + "top level await": [14.3, "await Promise.resolve()", "@babel/parser-7.9.0"], + "import attributes": [ + "18.20", + "import 'x' with { type: 'json' }", + "@babel/parser-7.22.0" + ] } diff --git a/test/index.js b/test/index.js index 58ce391..ca3b073 100644 --- a/test/index.js +++ b/test/index.js @@ -35,7 +35,7 @@ for (const [name, [version, code, parser]] of Object.entries(fixtures)) { function selectParser(version = "@babel/parser-7.0.0") { return () => ({ - parserOverride: require(version).parse + parserOverride: require(version).parse, }); } diff --git a/yarn.lock b/yarn.lock index 6b406ff..9a18a2b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -24,7 +24,7 @@ __metadata: languageName: node linkType: hard -"@babel/helper-plugin-utils@npm:^7.8.3": +"@babel/helper-plugin-utils@npm:^7.14.5, @babel/helper-plugin-utils@npm:^7.24.7, @babel/helper-plugin-utils@npm:^7.8.3": version: 7.24.8 resolution: "@babel/helper-plugin-utils@npm:7.24.8" checksum: c480fd570fe91d45ec9de4e9af65ab63b186cff0d2e921cdba7b194316ae2f4359e540bf5d91b46a46046d178eae5711f24858276b2cdc2afb8dc6798fe2d67d @@ -61,6 +61,24 @@ __metadata: languageName: node linkType: hard +"@babel/parser-7.12.0@npm:@babel/parser@7.12.0": + version: 7.12.0 + resolution: "@babel/parser@npm:7.12.0" + bin: + parser: ./bin/babel-parser.js + checksum: 0b656c02c810b485481ace5258bd56a008f6e31c14711818c81b7446b6d639c4c1f21c7243072d28a356274bb83d9d81c9eeff12ed3419fcdff8362551c2822b + languageName: node + linkType: hard + +"@babel/parser-7.22.0@npm:@babel/parser@7.22.0": + version: 7.22.0 + resolution: "@babel/parser@npm:7.22.0" + bin: + parser: ./bin/babel-parser.js + checksum: 187df5cd67bd099f3067400b89b47c77bcde38a5936a6c05a97df34f6bfb3fa62c3bda9bb06eee0760966d0e5c8347d5fafd283b74d7d3b0a20d289fcc489798 + languageName: node + linkType: hard + "@babel/parser-7.9.0@npm:@babel/parser@7.9.0": version: 7.9.0 resolution: "@babel/parser@npm:7.9.0" @@ -103,6 +121,28 @@ __metadata: languageName: node linkType: hard +"@babel/plugin-syntax-class-static-block@npm:^7.14.5": + version: 7.14.5 + resolution: "@babel/plugin-syntax-class-static-block@npm:7.14.5" + dependencies: + "@babel/helper-plugin-utils": "npm:^7.14.5" + peerDependencies: + "@babel/core": ^7.0.0-0 + checksum: 43bef611dd3912e3adcea1bcb587eaba1b8c8f24dfb134f191488e3521453d4f0a36408e6a3e27b512bee6e1b569c889db31a270d5576e9c34a80326bbc6bd7d + languageName: node + linkType: hard + +"@babel/plugin-syntax-import-attributes@npm:^7.24.7": + version: 7.24.7 + resolution: "@babel/plugin-syntax-import-attributes@npm:7.24.7" + dependencies: + "@babel/helper-plugin-utils": "npm:^7.24.7" + peerDependencies: + "@babel/core": ^7.0.0-0 + checksum: 4fba0afdb9b29b7769190d434ae30934b33675dd67e7665c655d9b9b11732f3f96b98a18556788b32b83798f887bd0d9d8abe51e6b3a6143fcf5b8d7df608339 + languageName: node + linkType: hard + "@babel/plugin-syntax-import-meta@npm:^7.10.4": version: 7.10.4 resolution: "@babel/plugin-syntax-import-meta@npm:7.10.4" @@ -191,6 +231,17 @@ __metadata: languageName: node linkType: hard +"@babel/plugin-syntax-private-property-in-object@npm:^7.14.5": + version: 7.14.5 + resolution: "@babel/plugin-syntax-private-property-in-object@npm:7.14.5" + dependencies: + "@babel/helper-plugin-utils": "npm:^7.14.5" + peerDependencies: + "@babel/core": ^7.0.0-0 + checksum: 1f987725a8e705c255bd3f47d4154445ccc8706c021e4abd0e3ac28bd2ea15f2dd9f44a4db04ca476909c86a366c737a9fcd808a38cfd84ee670c817ef05207f + languageName: node + linkType: hard + "@babel/plugin-syntax-top-level-await@npm:^7.14.5": version: 7.14.5 resolution: "@babel/plugin-syntax-top-level-await@npm:7.14.5" @@ -230,10 +281,14 @@ __metadata: dependencies: "@babel/core": 7.25.2 "@babel/parser-7.0.0": "npm:@babel/parser@7.0.0" + "@babel/parser-7.12.0": "npm:@babel/parser@7.12.0" + "@babel/parser-7.22.0": "npm:@babel/parser@7.22.0" "@babel/parser-7.9.0": "npm:@babel/parser@7.9.0" "@babel/plugin-syntax-async-generators": ^7.8.4 "@babel/plugin-syntax-bigint": ^7.8.3 "@babel/plugin-syntax-class-properties": ^7.12.13 + "@babel/plugin-syntax-class-static-block": ^7.14.5 + "@babel/plugin-syntax-import-attributes": ^7.24.7 "@babel/plugin-syntax-import-meta": ^7.10.4 "@babel/plugin-syntax-json-strings": ^7.8.3 "@babel/plugin-syntax-logical-assignment-operators": ^7.10.4 @@ -242,6 +297,7 @@ __metadata: "@babel/plugin-syntax-object-rest-spread": ^7.8.3 "@babel/plugin-syntax-optional-catch-binding": ^7.8.3 "@babel/plugin-syntax-optional-chaining": ^7.8.3 + "@babel/plugin-syntax-private-property-in-object": ^7.14.5 "@babel/plugin-syntax-top-level-await": ^7.14.5 peerDependencies: "@babel/core": ^7.0.0