Skip to content

Commit 8780eaf

Browse files
remove propsTypes and use typescript
1 parent ee128c1 commit 8780eaf

File tree

9 files changed

+57
-64
lines changed

9 files changed

+57
-64
lines changed

.babelrc.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,7 @@ const options = {
1515
},
1616
],
1717
],
18-
plugins: [
19-
'@babel/plugin-transform-react-jsx',
20-
[
21-
'transform-react-remove-prop-types',
22-
{
23-
mode: 'remove',
24-
ignoreFilenames: ['node_modules'],
25-
},
26-
],
27-
],
18+
plugins: ['@babel/plugin-transform-react-jsx'],
2819
env: {
2920
test: {
3021
// extra configuration for process.env.NODE_ENV === 'test'

index.d.ts

Whitespace-only changes.

package-lock.json

Lines changed: 13 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"name": "dev-javascript",
77
"email": "javascript.code.dev@gmail.com"
88
},
9+
"types": "./index.d.ts",
910
"description": "Create React component from string",
1011
"main": "lib/cjs/index.js",
1112
"module": "lib/esm/index.js",
@@ -53,7 +54,6 @@
5354
"@types/react": "^18.2.55",
5455
"@types/react-dom": "^18.2.19",
5556
"babel-loader": "^8.1.0",
56-
"babel-plugin-transform-react-remove-prop-types": "^0.4.24",
5757
"cross-env": "^7.0.3",
5858
"esbuild": "^0.20.0",
5959
"eslint": "^8.56.0",
@@ -104,8 +104,5 @@
104104
"setupFilesAfterEnv": [
105105
"jest-extended"
106106
]
107-
},
108-
"dependencies": {
109-
"prop-types": "^15.8.1"
110107
}
111108
}

src/ctx.ts renamed to src/ctx.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import { type TBabel } from './index.d';
2-
class Ctx {
1+
import type {TransformOptions} from '@babel/core';
2+
import type {TBabel, TReact, IStringToReactApi} from './index.d';
3+
import React, {FC, PropsWithChildren} from 'react';
4+
class Ctx implements IStringToReactApi {
35
_temp: string = '';
46
_parentTemp: string = `"use strict";\nreturn @temp;`;
5-
_com: Function | null = null;
7+
_com: FC<PropsWithChildren<{}>> = function () {
8+
return null;
9+
};
610
_getBabel: () => TBabel;
7-
constructor(React: object, Babel: TBabel) {
11+
constructor(React: TReact, Babel: TBabel) {
812
window.React = window.React || React;
913
if (!Babel) {
1014
throw new Error(
@@ -13,23 +17,23 @@ class Ctx {
1317
}
1418
this._getBabel = () => Babel;
1519
}
16-
_checkBabelOptions(babelOptions: any) {
20+
_checkBabelOptions(babelOptions: TransformOptions) {
1721
if (Object.prototype.toString.call(babelOptions) !== '[object Object]') {
1822
throw new Error(`babelOptions prop of string-to-react-component element should be an object.`);
1923
}
2024
if (Object.prototype.hasOwnProperty.call(babelOptions, 'presets') === false) {
2125
babelOptions.presets = ['react'];
2226
} else {
2327
//check if babelOptions.presets is not type of Array
24-
if (!(typeof babelOptions.presets === 'object' && babelOptions.presets.constructor == Array)) {
28+
if (!(typeof babelOptions.presets === 'object' && babelOptions.presets?.constructor == Array)) {
2529
throw new Error(`string-to-react-component Error : presets property of babelOptions prop should be an array`);
2630
}
2731
if (babelOptions.presets.indexOf('react') === -1) {
2832
babelOptions.presets.push('react');
2933
}
3034
}
3135
}
32-
_transpile(babelOptions: any): string {
36+
_transpile(babelOptions: TransformOptions): string {
3337
// make sure react presets is registered in babelOptions
3438
this._checkBabelOptions(babelOptions);
3539
const resultObj = this._getBabel().transform(this._temp, babelOptions);
@@ -57,15 +61,15 @@ class Ctx {
5761
throw new Error(`passed string into string-to-react-component element can not be empty`);
5862
}
5963
}
60-
updateTemplate(template: any, babelOptions: any) {
64+
updateTemplate(template: string, babelOptions: TransformOptions): IStringToReactApi {
6165
this._validateTemplate(template);
6266
if (template !== this._temp) {
6367
this._temp = template;
6468
this._generateCom(babelOptions);
6569
}
6670
return this;
6771
}
68-
getComponent() {
72+
getComponent(): FC<PropsWithChildren<{}>> {
6973
return this._com;
7074
}
7175
}

src/index.d.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1-
export type TBabel = {
2-
transform: (temp: string, options: object) => ({
3-
code?: string
4-
})
5-
};
1+
import * as Babel from '@types/babel__standalone';
2+
import * as React from '@types/react';
3+
import { TransformOptions } from "@babel/core";
4+
import React, { FunctionComponent, FC, PropsWithChildren } from 'react';
5+
import { TransformOptions } from "@babel/core";
6+
export interface IStringToReactApi {
7+
updateTemplate: (template: string, babelOptions: TransformOptions) => IStringToReactApi;
8+
getComponent: () => FC<PropsWithChildren<{}>>;
9+
[x: string]: any;
10+
}
11+
export type TReact = typeof React;
12+
export type TBabel = typeof Babel;
13+
export interface StringToReactComponentProps {
14+
data?: object,
15+
babelOptions?: TransformOptions,
16+
children?: string,
17+
}

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import Ctx from './ctx';
33
import * as Babel from '@babel/standalone';
4-
import type { TBabel } from './index.d';
4+
import type { TBabel, TReact, IStringToReactApi } from './index.d';
55
import StringToReact from './strintToReact';
6-
const getCtx = (React: object, Babel: TBabel) => new Ctx(React, Babel);
7-
export default StringToReact.bind(null, { getCtx: getCtx, Babel: Babel, react: React });
6+
const getCtx: (React: TReact, Babel: TBabel) => IStringToReactApi = (React: TReact, Babel: TBabel) => new Ctx(React, Babel);
7+
export default StringToReact.bind(null, { getCtx: getCtx, Babel: Babel, react: React });

src/strintToReact.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import React, {useRef} from 'react';
2-
import PropTypes from 'prop-types';
3-
function StringToReactComponent(deps: any, props: any) {
2+
import type {StringToReactComponentProps, IStringToReactApi, TBabel, TReact} from './index.d';
3+
function StringToReactComponent(
4+
deps: {getCtx: (react: TReact, Babel: TBabel) => IStringToReactApi; react: TReact; Babel: TBabel},
5+
props: StringToReactComponentProps,
6+
) {
47
const {getCtx, Babel, react} = deps;
5-
const ref = useRef<any>(null);
8+
let ref = useRef<any>(null);
69
ref.current = ref.current || getCtx(react, Babel);
10+
const api = ref.current as IStringToReactApi;
711
const babelOptions = props.babelOptions || {};
8-
const GeneratedComponent = ref.current.updateTemplate(props.children, babelOptions).getComponent();
12+
const GeneratedComponent = api.updateTemplate(props.children || '', babelOptions).getComponent();
913
const data = props.data || {};
1014
return <GeneratedComponent {...data} />;
1115
}
12-
StringToReactComponent.propTypes /* remove-proptypes */ = {
13-
data: PropTypes.object,
14-
babelOptions: PropTypes.object,
15-
children: PropTypes.string,
16-
};
16+
1717
export default StringToReactComponent;

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,11 +2149,6 @@ babel-plugin-polyfill-regenerator@^0.3.0:
21492149
dependencies:
21502150
"@babel/helper-define-polyfill-provider" "^0.3.1"
21512151

2152-
babel-plugin-transform-react-remove-prop-types@^0.4.24:
2153-
version "0.4.24"
2154-
resolved "https://registry.npmjs.org/babel-plugin-transform-react-remove-prop-types/-/babel-plugin-transform-react-remove-prop-types-0.4.24.tgz"
2155-
integrity sha512-eqj0hVcJUR57/Ug2zE1Yswsw4LhuqqHhD+8v120T1cl3kjg76QwtyBrdIk4WVwK+lAhBJVYCd/v+4nc4y+8JsA==
2156-
21572152
babel-preset-current-node-syntax@^1.0.0:
21582153
version "1.0.1"
21592154
resolved "https://registry.npmjs.org/babel-preset-current-node-syntax/-/babel-preset-current-node-syntax-1.0.1.tgz"

0 commit comments

Comments
 (0)