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 274d38a commit 114561d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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 @@ -26,6 +28,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 @@ -34,6 +37,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

0 comments on commit 114561d

Please sign in to comment.