diff --git a/packages/babel-plugin-proposal-import-attributes-to-assertions/.npmignore b/packages/babel-plugin-proposal-import-attributes-to-assertions/.npmignore new file mode 100644 index 000000000000..f9806945836e --- /dev/null +++ b/packages/babel-plugin-proposal-import-attributes-to-assertions/.npmignore @@ -0,0 +1,3 @@ +src +test +*.log diff --git a/packages/babel-plugin-proposal-import-attributes-to-assertions/README.md b/packages/babel-plugin-proposal-import-attributes-to-assertions/README.md new file mode 100644 index 000000000000..93ad3ad25385 --- /dev/null +++ b/packages/babel-plugin-proposal-import-attributes-to-assertions/README.md @@ -0,0 +1,19 @@ +# @babel/plugin-proposal-import-attributes-to-assertions + +> Transform optional chaining operators to workaround https://crbug.com/v8/11558 + +See our website [@babel/plugin-proposal-import-attributes-to-assertions](https://babeljs.io/docs/en/babel-plugin-proposal-import-attributes-to-assertions) for more information. + +## Install + +Using npm: + +```sh +npm install --save-dev @babel/plugin-proposal-import-attributes-to-assertions +``` + +or using yarn: + +```sh +yarn add @babel/plugin-proposal-import-attributes-to-assertions --dev +``` diff --git a/packages/babel-plugin-proposal-import-attributes-to-assertions/package.json b/packages/babel-plugin-proposal-import-attributes-to-assertions/package.json new file mode 100644 index 000000000000..122820e0224e --- /dev/null +++ b/packages/babel-plugin-proposal-import-attributes-to-assertions/package.json @@ -0,0 +1,52 @@ +{ + "name": "@babel/plugin-proposal-import-attributes-to-assertions", + "version": "7.21.8", + "description": "Transform the Import Attributes proposal (`import ... from '...' with { ... }`) to the previous version known as Import Assertions (`import ... from '...' assert { ... }`).", + "repository": { + "type": "git", + "url": "https://github.com/babel/babel.git", + "directory": "packages/babel-plugin-proposal-import-attributes-to-assertions" + }, + "homepage": "https://babel.dev/docs/en/next/babel-plugin-proposal-import-attributes-to-assertions", + "license": "MIT", + "publishConfig": { + "access": "public" + }, + "main": "./lib/index.js", + "exports": { + ".": "./lib/index.js", + "./package.json": "./package.json" + }, + "keywords": [ + "babel-plugin", + "import", + "attributes", + "assertions", + "proposal", + "stage-3" + ], + "dependencies": { + "@babel/helper-plugin-utils": "workspace:^", + "@babel/plugin-syntax-import-attributes": "workspace:^" + }, + "peerDependencies": { + "@babel/core": "^7.22.0" + }, + "devDependencies": { + "@babel/core": "workspace:^", + "@babel/helper-plugin-test-runner": "workspace:^" + }, + "engines": { + "node": ">=6.9.0" + }, + "author": "The Babel Team (https://babel.dev/team)", + "type": "commonjs", + "conditions": { + "USE_ESM": [ + { + "type": "module" + }, + null + ] + } +} diff --git a/packages/babel-plugin-proposal-import-attributes-to-assertions/src/index.ts b/packages/babel-plugin-proposal-import-attributes-to-assertions/src/index.ts new file mode 100644 index 000000000000..836bd1118e49 --- /dev/null +++ b/packages/babel-plugin-proposal-import-attributes-to-assertions/src/index.ts @@ -0,0 +1,33 @@ +import { declare } from "@babel/helper-plugin-utils"; +import type { NodePath } from "@babel/traverse"; +import type * as t from "@babel/types"; +import syntaxImportAttributes from "@babel/plugin-syntax-import-attributes"; + +export default declare(api => { + api.assertVersion(7); + + return { + name: "proposal-import-attributes-to-assertions", + + inherits: syntaxImportAttributes, + + manipulateOptions({ generatorOpts }) { + generatorOpts.importAttributesKeyword = "assert"; + }, + + visitor: { + "ImportDeclaration|ExportNamedDeclaration|ExportAllDeclaration"( + path: NodePath< + | t.ImportDeclaration + | t.ExportNamedDeclaration + | t.ExportAllDeclaration + >, + ) { + const { node } = path; + if (!node.attributes) return; + node.assertions = node.attributes; + node.attributes = null; + }, + }, + }; +}); diff --git a/packages/babel-plugin-proposal-import-attributes-to-assertions/test/fixtures/basic/exports/input.js b/packages/babel-plugin-proposal-import-attributes-to-assertions/test/fixtures/basic/exports/input.js new file mode 100644 index 000000000000..c1218f9d9e34 --- /dev/null +++ b/packages/babel-plugin-proposal-import-attributes-to-assertions/test/fixtures/basic/exports/input.js @@ -0,0 +1,2 @@ +export { a } from "a" with { type: "json" }; +export * as ns from "c" with { type: "json" }; diff --git a/packages/babel-plugin-proposal-import-attributes-to-assertions/test/fixtures/basic/exports/output.mjs b/packages/babel-plugin-proposal-import-attributes-to-assertions/test/fixtures/basic/exports/output.mjs new file mode 100644 index 000000000000..eb225756d840 --- /dev/null +++ b/packages/babel-plugin-proposal-import-attributes-to-assertions/test/fixtures/basic/exports/output.mjs @@ -0,0 +1,2 @@ +export { a } from "a" assert { type: "json" }; +export * as ns from "c" assert { type: "json" }; diff --git a/packages/babel-plugin-proposal-import-attributes-to-assertions/test/fixtures/basic/imports/input.js b/packages/babel-plugin-proposal-import-attributes-to-assertions/test/fixtures/basic/imports/input.js new file mode 100644 index 000000000000..e1e41f2b7dce --- /dev/null +++ b/packages/babel-plugin-proposal-import-attributes-to-assertions/test/fixtures/basic/imports/input.js @@ -0,0 +1,4 @@ +import { a } from "a" with { type: "json" }; +import b from "b" with { type: "json" }; +import * as ns from "c" with { type: "json" }; +import "d" with { type: "json" }; diff --git a/packages/babel-plugin-proposal-import-attributes-to-assertions/test/fixtures/basic/imports/output.mjs b/packages/babel-plugin-proposal-import-attributes-to-assertions/test/fixtures/basic/imports/output.mjs new file mode 100644 index 000000000000..122072827c41 --- /dev/null +++ b/packages/babel-plugin-proposal-import-attributes-to-assertions/test/fixtures/basic/imports/output.mjs @@ -0,0 +1,4 @@ +import { a } from "a" assert { type: "json" }; +import b from "b" assert { type: "json" }; +import * as ns from "c" assert { type: "json" }; +import "d" assert { type: "json" }; diff --git a/packages/babel-plugin-proposal-import-attributes-to-assertions/test/fixtures/options.json b/packages/babel-plugin-proposal-import-attributes-to-assertions/test/fixtures/options.json new file mode 100644 index 000000000000..90d453fb28bb --- /dev/null +++ b/packages/babel-plugin-proposal-import-attributes-to-assertions/test/fixtures/options.json @@ -0,0 +1,4 @@ +{ + "sourceType": "module", + "plugins": ["proposal-import-attributes-to-assertions"] +} diff --git a/packages/babel-plugin-proposal-import-attributes-to-assertions/test/index.js b/packages/babel-plugin-proposal-import-attributes-to-assertions/test/index.js new file mode 100644 index 000000000000..21a55ce6b5e7 --- /dev/null +++ b/packages/babel-plugin-proposal-import-attributes-to-assertions/test/index.js @@ -0,0 +1,3 @@ +import runner from "@babel/helper-plugin-test-runner"; + +runner(import.meta.url); diff --git a/packages/babel-plugin-proposal-import-attributes-to-assertions/test/package.json b/packages/babel-plugin-proposal-import-attributes-to-assertions/test/package.json new file mode 100644 index 000000000000..5ffd9800b97c --- /dev/null +++ b/packages/babel-plugin-proposal-import-attributes-to-assertions/test/package.json @@ -0,0 +1 @@ +{ "type": "module" } diff --git a/tsconfig.json b/tsconfig.json index 19f12a365303..fa142c229a25 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -49,6 +49,7 @@ "./packages/babel-plugin-proposal-export-default-from/src/**/*.ts", "./packages/babel-plugin-proposal-function-bind/src/**/*.ts", "./packages/babel-plugin-proposal-function-sent/src/**/*.ts", + "./packages/babel-plugin-proposal-import-attributes-to-assertions/src/**/*.ts", "./packages/babel-plugin-proposal-partial-application/src/**/*.ts", "./packages/babel-plugin-proposal-pipeline-operator/src/**/*.ts", "./packages/babel-plugin-proposal-record-and-tuple/src/**/*.ts", @@ -313,6 +314,9 @@ "@babel/plugin-proposal-function-sent": [ "./packages/babel-plugin-proposal-function-sent/src" ], + "@babel/plugin-proposal-import-attributes-to-assertions": [ + "./packages/babel-plugin-proposal-import-attributes-to-assertions/src" + ], "@babel/plugin-proposal-partial-application": [ "./packages/babel-plugin-proposal-partial-application/src" ], diff --git a/yarn.lock b/yarn.lock index 3bb09077b242..63da0d20bc26 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1401,6 +1401,19 @@ __metadata: languageName: unknown linkType: soft +"@babel/plugin-proposal-import-attributes-to-assertions@workspace:packages/babel-plugin-proposal-import-attributes-to-assertions": + version: 0.0.0-use.local + resolution: "@babel/plugin-proposal-import-attributes-to-assertions@workspace:packages/babel-plugin-proposal-import-attributes-to-assertions" + dependencies: + "@babel/core": "workspace:^" + "@babel/helper-plugin-test-runner": "workspace:^" + "@babel/helper-plugin-utils": "workspace:^" + "@babel/plugin-syntax-import-attributes": "workspace:^" + peerDependencies: + "@babel/core": ^7.22.0 + languageName: unknown + linkType: soft + "@babel/plugin-proposal-json-strings@npm:^7.18.6": version: 7.18.6 resolution: "@babel/plugin-proposal-json-strings@npm:7.18.6"