Skip to content

Commit

Permalink
[Plugin] [1/9] Remove passing a 'method holder' object and properly u…
Browse files Browse the repository at this point in the history
…se TypeScript for plugin. (#4210)

## Summary

Since we are using TypeScript for plugin now we should get rid of
passing the magic `t` object as a function argument and use proper
imports instead. Also we no longer import whole `BabelCore` and
`BabelTypes` but just the required objects. This is a direct follow up
to #4209

## Test plan

`Yarn jest`, these changes don't touch plugin's logic.
  • Loading branch information
tjzel authored Mar 29, 2023
1 parent f4976d9 commit c9dd729
Show file tree
Hide file tree
Showing 12 changed files with 476 additions and 643 deletions.
431 changes: 119 additions & 312 deletions plugin/build/plugin.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions plugin/build/plugin.js.map

Large diffs are not rendered by default.

29 changes: 16 additions & 13 deletions plugin/src/injectVersion.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import * as BabelCore from '@babel/core';
import * as BabelTypes from '@babel/types';
import { NodePath } from '@babel/core';
import {
DirectiveLiteral,
expressionStatement,
assignmentExpression,
memberExpression,
identifier,
stringLiteral,
FunctionDeclaration,
} from '@babel/types';

export function injectVersion(
path: BabelCore.NodePath<BabelTypes.DirectiveLiteral>
) {
export function injectVersion(path: NodePath<DirectiveLiteral>) {
// We want to inject plugin's version only once,
// hence we have a Directive Literal line in Reanimated code.
// See src/reanimated2/platform-specific/checkPluginVersion.ts
Expand All @@ -14,19 +20,16 @@ export function injectVersion(
const injectedName = '_REANIMATED_VERSION_BABEL_PLUGIN';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const versionString = require('../../package.json').version;
const pluginVersionNode = BabelTypes.expressionStatement(
BabelTypes.assignmentExpression(
const pluginVersionNode = expressionStatement(
assignmentExpression(
'=',
BabelTypes.memberExpression(
BabelTypes.identifier('global'),
BabelTypes.identifier(injectedName)
),
BabelTypes.stringLiteral(versionString)
memberExpression(identifier('global'), identifier(injectedName)),
stringLiteral(versionString)
)
);

const functionParent = (
path.getFunctionParent() as BabelCore.NodePath<BabelTypes.FunctionDeclaration>
path.getFunctionParent() as NodePath<FunctionDeclaration>
).node;
// DirectiveLiteral is in property of its function parent 'directives' hence we cannot just replace it.
functionParent.body.directives = [];
Expand Down
Loading

0 comments on commit c9dd729

Please sign in to comment.