Skip to content

Commit

Permalink
Migrate @emotion/babel-plugin-jsx-pragmatic to TypeScript (#2570)
Browse files Browse the repository at this point in the history
* feat(babel-plugin-jsx-pragmatic): add changeset

* feat(babel-plugin-jsx-pragmatic): convert to typescript

* fix(babel-plugin-jsx-pragmatic): update babel core types

* Update .changeset/wet-boxes-raise.md

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
  • Loading branch information
G-Rath and Andarist committed Mar 18, 2022
1 parent 85772c3 commit ea84c40
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-boxes-raise.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module '@babel/plugin-syntax-jsx' {
const plugin: any
export default plugin
}
3 changes: 2 additions & 1 deletion packages/babel-plugin-jsx-pragmatic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"access": "public"
},
"devDependencies": {
"@babel/core": "^7.13.10"
"@babel/core": "^7.13.10",
"@types/babel__core": "^7.1.18"
}
}
Original file line number Diff line number Diff line change
@@ -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 = <T>(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<PluginPassWithOpts> {
const t = babel.types

function addPragmaImport(path, state) {
function addPragmaImport(
path: NodePath<BabelTypes.Program>,
state: PluginPassWithOpts
) {
const importDeclar = t.importDeclaration(
[
t.importSpecifier(
Expand Down
11 changes: 11 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit ea84c40

Please sign in to comment.