diff --git a/.changeset/wet-boxes-raise.md b/.changeset/wet-boxes-raise.md new file mode 100644 index 000000000..32c7a707c --- /dev/null +++ b/.changeset/wet-boxes-raise.md @@ -0,0 +1,5 @@ +--- +'@emotion/babel-plugin-jsx-pragmatic': minor +--- + +Source code has been migrated to TypeScript. From now on type declarations will be emitted based on that, instead of being hand-written. diff --git a/packages/babel-plugin-jsx-pragmatic/babel__plugin-syntax-jsx.d.ts b/packages/babel-plugin-jsx-pragmatic/babel__plugin-syntax-jsx.d.ts new file mode 100644 index 000000000..26446987a --- /dev/null +++ b/packages/babel-plugin-jsx-pragmatic/babel__plugin-syntax-jsx.d.ts @@ -0,0 +1,4 @@ +declare module '@babel/plugin-syntax-jsx' { + const plugin: any + export default plugin +} diff --git a/packages/babel-plugin-jsx-pragmatic/package.json b/packages/babel-plugin-jsx-pragmatic/package.json index 7195057ff..40c97472f 100644 --- a/packages/babel-plugin-jsx-pragmatic/package.json +++ b/packages/babel-plugin-jsx-pragmatic/package.json @@ -23,6 +23,7 @@ "access": "public" }, "devDependencies": { - "@babel/core": "^7.13.10" + "@babel/core": "^7.13.10", + "@types/babel__core": "^7.1.18" } } diff --git a/packages/babel-plugin-jsx-pragmatic/src/index.js b/packages/babel-plugin-jsx-pragmatic/src/index.ts similarity index 72% rename from packages/babel-plugin-jsx-pragmatic/src/index.js rename to packages/babel-plugin-jsx-pragmatic/src/index.ts index c2523b9d9..2315326e7 100644 --- a/packages/babel-plugin-jsx-pragmatic/src/index.js +++ b/packages/babel-plugin-jsx-pragmatic/src/index.ts @@ -1,17 +1,38 @@ +import type { + NodePath, + PluginObj, + PluginPass, + types as BabelTypes +} from '@babel/core' import syntaxJsx from '@babel/plugin-syntax-jsx' -const findLast = (arr, predicate) => { +const findLast = (arr: T[], predicate: (item: T) => boolean): T | null => { for (let i = arr.length - 1; i >= 0; i--) { if (predicate(arr[i])) { return arr[i] } } + + return null +} + +interface PluginPassWithOpts extends PluginPass { + opts: { + module: string + import: string + export?: string + } } -export default function jsxPragmatic(babel) { +export default function jsxPragmatic(babel: { + types: typeof BabelTypes +}): PluginObj { const t = babel.types - function addPragmaImport(path, state) { + function addPragmaImport( + path: NodePath, + state: PluginPassWithOpts + ) { const importDeclar = t.importDeclaration( [ t.importSpecifier( diff --git a/yarn.lock b/yarn.lock index 413d6c020..bcb31ea06 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5594,6 +5594,17 @@ "@types/babel__template" "*" "@types/babel__traverse" "*" +"@types/babel__core@^7.1.18": + version "7.1.18" + resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.18.tgz#1a29abcc411a9c05e2094c98f9a1b7da6cdf49f8" + integrity sha512-S7unDjm/C7z2A2R9NzfKCK1I+BAALDtxEmsJBwlB3EzNfb929ykjL++1CK9LO++EIp2fQrC8O+BwjKvz6UeDyQ== + dependencies: + "@babel/parser" "^7.1.0" + "@babel/types" "^7.0.0" + "@types/babel__generator" "*" + "@types/babel__template" "*" + "@types/babel__traverse" "*" + "@types/babel__generator@*": version "7.6.2" resolved "https://registry.yarnpkg.com/@types/babel__generator/-/babel__generator-7.6.2.tgz#f3d71178e187858f7c45e30380f8f1b7415a12d8"