Skip to content

Commit

Permalink
Merge pull request #39 from iffa/feat/cleanup-compatibility
Browse files Browse the repository at this point in the history
cleanup & compatibility fixes
  • Loading branch information
Skn0tt authored Feb 22, 2021
2 parents 9353d36 + 5ada380 commit 8698545
Show file tree
Hide file tree
Showing 5 changed files with 752 additions and 592 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules
dist
*.tgz
coverage
.idea
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"typings": "dist/index.d.ts",
"files": [
"dist",
"src",
"tools.js"
],
"engines": {
Expand All @@ -32,6 +31,7 @@
"author": "Simon Knott",
"module": "dist/babel-plugin-superjson-next.esm.js",
"devDependencies": {
"@babel/core": "^7.12.17",
"@testing-library/react-native": "^7.1.0",
"@types/hoist-non-react-statics": "^3.3.1",
"@types/react": "^17.0.1",
Expand All @@ -51,10 +51,10 @@
},
"peerDependencies": {
"next": ">=9.0.0",
"react": ">=16.X",
"superjson": "1.x"
},
"dependencies": {
"@babel/types": "^7.12.17",
"@babel/helper-module-imports": "^7.12.13",
"hoist-non-react-statics": "^3.3.2"
}
Expand Down
64 changes: 41 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
import { PluginObj, types as t, NodePath, PluginPass } from '@babel/core';
import type { NodePath, PluginObj, PluginPass } from '@babel/core';
import { addNamed as addNamedImport } from '@babel/helper-module-imports';
import {
callExpression,
ClassDeclaration,
classExpression,
ExportNamedDeclaration,
Expression,
FunctionDeclaration,
functionExpression,
isClassDeclaration,
isExportDefaultDeclaration,
isExportNamedDeclaration,
isFunctionDeclaration,
isFunctionExpression,
isIdentifier,
isVariableDeclaration,
variableDeclaration,
variableDeclarator,
} from '@babel/types';
import * as nodePath from 'path';

function functionDeclarationToExpression(declaration: t.FunctionDeclaration) {
return t.functionExpression(
function functionDeclarationToExpression(declaration: FunctionDeclaration) {
return functionExpression(
declaration.id,
declaration.params,
declaration.body,
Expand All @@ -12,8 +30,8 @@ function functionDeclarationToExpression(declaration: t.FunctionDeclaration) {
);
}

function classDeclarationToExpression(declaration: t.ClassDeclaration) {
return t.classExpression(
function classDeclarationToExpression(declaration: ClassDeclaration) {
return classExpression(
declaration.id,
declaration.superClass,
declaration.body,
Expand All @@ -38,12 +56,12 @@ function getFileName(state: PluginPass) {
const functionsToReplace = ['getServerSideProps', 'getStaticProps'];

function transformPropGetters(
path: NodePath<t.ExportNamedDeclaration>,
transform: (v: t.Expression) => t.Expression
path: NodePath<ExportNamedDeclaration>,
transform: (v: Expression) => Expression
) {
const { node } = path;

if (t.isFunctionDeclaration(node.declaration)) {
if (isFunctionDeclaration(node.declaration)) {
const { id: functionId } = node.declaration;
if (!functionId) {
return;
Expand All @@ -53,8 +71,8 @@ function transformPropGetters(
return;
}

node.declaration = t.variableDeclaration('const', [
t.variableDeclarator(
node.declaration = variableDeclaration('const', [
variableDeclarator(
functionId,
transform(functionDeclarationToExpression(node.declaration))
),
Expand All @@ -63,10 +81,10 @@ function transformPropGetters(
return;
}

if (t.isVariableDeclaration(node.declaration)) {
if (isVariableDeclaration(node.declaration)) {
node.declaration.declarations.forEach((declaration) => {
if (
t.isIdentifier(declaration.id) &&
isIdentifier(declaration.id) &&
functionsToReplace.includes(declaration.id.name) &&
declaration.init
) {
Expand All @@ -93,29 +111,29 @@ function addWithSuperJSONPageImport(path: NodePath<any>) {
}

function wrapExportDefaultDeclaration(path: NodePath<any>) {
function wrapInHOC(expr: t.Expression): t.Expression {
return t.callExpression(addWithSuperJSONPageImport(path), [expr]);
function wrapInHOC(expr: Expression): Expression {
return callExpression(addWithSuperJSONPageImport(path), [expr]);
}

const { node } = path;

if (t.isIdentifier(node.declaration)) {
if (isIdentifier(node.declaration)) {
node.declaration = wrapInHOC(node.declaration);
}

if (t.isFunctionExpression(node.declaration)) {
if (isFunctionExpression(node.declaration)) {
node.declaration = wrapInHOC(node.declaration);
}

if (
t.isFunctionDeclaration(node.declaration) ||
t.isClassDeclaration(node.declaration)
isFunctionDeclaration(node.declaration) ||
isClassDeclaration(node.declaration)
) {
if (node.declaration.id) {
path.insertBefore(node.declaration);
node.declaration = wrapInHOC(node.declaration.id);
} else {
if (t.isFunctionDeclaration(node.declaration)) {
if (isFunctionDeclaration(node.declaration)) {
node.declaration = wrapInHOC(
functionDeclarationToExpression(node.declaration)
);
Expand Down Expand Up @@ -162,20 +180,20 @@ function superJsonWithNext(): PluginObj {
const body = path.get('body');

body
.filter((path) => t.isExportNamedDeclaration(path))
.filter((path) => isExportNamedDeclaration(path))
.forEach((path) => {
transformPropGetters(
path as NodePath<t.ExportNamedDeclaration>,
path as NodePath<ExportNamedDeclaration>,
(decl) => {
return t.callExpression(addWithSuperJSONPropsImport(path), [
return callExpression(addWithSuperJSONPropsImport(path), [
decl,
]);
}
);
});

const exportDefaultDeclaration = body.find((path) =>
t.isExportDefaultDeclaration(path)
isExportDefaultDeclaration(path)
);
if (!exportDefaultDeclaration) {
return;
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"compilerOptions": {
"declaration": true,
"sourceMap": true,
"target": "ES5",
"rootDir": "src",
"outDir": "dist",
"strict": true,
Expand Down
Loading

0 comments on commit 8698545

Please sign in to comment.