Skip to content

Commit

Permalink
feat(babel-plugin-jsx-pragmatic): convert to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Nov 27, 2021
1 parent ffc6a46 commit 339a3ef
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
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.16"
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,47 @@
import type {
NodePath,
PluginObj,
PluginPass as BabelPluginPass,
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
}

// todo: PR these to @types/babel__core - PluginPass should extend Store
// https://github.com/babel/babel/blob/4e50b2d9d9c376cee7a2cbf56553fe5b982ea53c/packages/babel-core/src/transformation/store.js
interface BabelStore {
setDynamic(key: string, fn: () => unknown): void
set(key: string, val: unknown): void
get(key: string): unknown
}

export default function jsxPragmatic(babel) {
type PluginPass = BabelPluginPass &
BabelStore & {
opts: {
module: string
import: string
export?: string
}
}

export default function jsxPragmatic(babel: {
types: typeof BabelTypes
}): PluginObj<PluginPass> {
const t = babel.types

function addPragmaImport(path, state) {
function addPragmaImport(
path: NodePath<BabelTypes.Program>,
state: PluginPass
) {
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.16":
version "7.1.16"
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.16.tgz#bc12c74b7d65e82d29876b5d0baf5c625ac58702"
integrity sha512-EAEHtisTMM+KaKwfWdC3oyllIqswlznXCIVCt7/oRNrh+DhgT4UEBNC/jlADNjvw7UnfbcdkGQcPVZ1xYiLcrQ==
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 339a3ef

Please sign in to comment.