Skip to content

Commit

Permalink
Add features up to import attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Aug 13, 2024
1 parent 5769aee commit 52aa545
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 8 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
Expand All @@ -35,6 +38,8 @@
"devDependencies": {
"@babel/core": "7.25.2",
"@babel/parser-7.0.0": "npm:@babel/[email protected]",
"@babel/parser-7.12.0": "npm:@babel/[email protected]",
"@babel/parser-7.22.0": "npm:@babel/[email protected]",
"@babel/parser-7.9.0": "npm:@babel/[email protected]"
},
"license": "MIT",
Expand Down
23 changes: 19 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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 });
10 changes: 8 additions & 2 deletions test/fixtures.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
{
"#______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 {}"],
"JSON superset": [10.0, "'\\u2028'"],
"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"
]
}
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}

Expand Down
58 changes: 57 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -61,6 +61,24 @@ __metadata:
languageName: node
linkType: hard

"@babel/parser-7.12.0@npm:@babel/[email protected]":
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/[email protected]":
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/[email protected]":
version: 7.9.0
resolution: "@babel/parser@npm:7.9.0"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -230,10 +281,14 @@ __metadata:
dependencies:
"@babel/core": 7.25.2
"@babel/parser-7.0.0": "npm:@babel/[email protected]"
"@babel/parser-7.12.0": "npm:@babel/[email protected]"
"@babel/parser-7.22.0": "npm:@babel/[email protected]"
"@babel/parser-7.9.0": "npm:@babel/[email protected]"
"@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
Expand All @@ -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
Expand Down

0 comments on commit 52aa545

Please sign in to comment.