Skip to content

Commit a7a262f

Browse files
committed
Fix or ignore type error
1 parent d3743ad commit a7a262f

14 files changed

+124
-78
lines changed

.babelrc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
{
2-
"presets": ["@babel/preset-env", "@babel/preset-react", "@babel/preset-flow"]
2+
"presets": [
3+
["@babel/preset-env", { "targets": { "node": "current" } }],
4+
"@babel/preset-react",
5+
"@babel/preset-typescript"
6+
]
37
}

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"@babel/preset-env": "7.26.7",
3232
"@babel/preset-flow": "7.25.9",
3333
"@babel/preset-react": "7.26.3",
34+
"@babel/preset-typescript": "^7.15.0",
3435
"@commitlint/cli": "8.3.6",
3536
"@commitlint/config-angular": "8.3.6",
3637
"@testing-library/dom": "10.4.0",
@@ -39,6 +40,7 @@
3940
"@khanacademy/flow-to-ts": "^0.5.2",
4041
"@types/enzyme": "^3.10.9",
4142
"@types/enzyme-adapter-react-16": "^1.0.6",
43+
"@types/jest": "^27.0.2",
4244
"@types/react": "^17.0.26",
4345
"@types/react-is": "^17.0.2",
4446
"babel-eslint": "10.1.0",
@@ -76,7 +78,7 @@
7678
"rollup-plugin-node-globals": "1.4.0",
7779
"rollup-plugin-node-resolve": "5.2.0",
7880
"rollup-plugin-sourcemaps": "0.6.3",
79-
"typescript": "4.2.4"
81+
"typescript": "4.4.3"
8082
},
8183
"peerDependencies": {
8284
"react": "^19.0.0",
@@ -87,6 +89,6 @@
8789
"@base2/pretty-print-object": "1.0.2"
8890
},
8991
"jest": {
90-
"setupFilesAfterEnv": ["<rootDir>tests/setupTests.js"]
92+
"setupFilesAfterEnv": ["<rootDir>tests/setupTests.ts"]
9193
}
9294
}

src/AnonymousStatelessComponent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'; // eslint-disable-next-line react/display-name
22

3-
export default function (props) {
3+
export default function (props: { children: React.ReactNode }) {
44
const { children } = props; // eslint-disable-line react/prop-types
55

66
return <div>{children}</div>;

src/formatter/createPropFilter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export default function createPropFilter(
55
if (Array.isArray(filter)) {
66
return (key: string) => filter.indexOf(key) === -1;
77
} else {
8+
// @ts-expect-error: flow to TS
89
return (key: string) => filter(props[key], key);
910
}
1011
}

src/formatter/formatFunction.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ import type { Options } from './../options';
22

33
function noRefCheck() {}
44

5-
export const inlineFunction = (fn: any): string =>
5+
type FunctionType = (...args: Array<any>) => any;
6+
7+
export const inlineFunction = (fn: FunctionType): string =>
68
fn
79
.toString()
810
.split('\n')
911
.map((line) => line.trim())
1012
.join('');
11-
export const preserveFunctionLineBreak = (fn: any): string => fn.toString();
13+
14+
export const preserveFunctionLineBreak = (fn: FunctionType): string =>
15+
fn.toString();
16+
1217
const defaultFunctionValue = inlineFunction;
13-
export default (fn: (...args: Array<any>) => any, options: Options): string => {
18+
19+
export default (fn: FunctionType, options: Options): string => {
1420
const { functionValue = defaultFunctionValue, showFunctions } = options;
1521

1622
if (!showFunctions && functionValue === defaultFunctionValue) {

src/formatter/formatReactElementNode.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { Options } from './../options';
88
import type { ReactElementTreeNode } from './../tree';
99

1010
const compensateMultilineStringElementIndentation = (
11-
element,
11+
element: any,
1212
formattedElement: string,
1313
inline: boolean,
1414
lvl: number,
@@ -33,7 +33,7 @@ const compensateMultilineStringElementIndentation = (
3333
};
3434

3535
const formatOneChildren =
36-
(inline: boolean, lvl: number, options: Options) => (element) =>
36+
(inline: boolean, lvl: number, options: Options) => (element: any) =>
3737
compensateMultilineStringElementIndentation(
3838
element,
3939
formatTreeNode(element, inline, lvl, options),
@@ -42,13 +42,15 @@ const formatOneChildren =
4242
options
4343
);
4444

45-
const onlyPropsWithOriginalValue = (defaultProps, props) => (propName) => {
46-
const haveDefaultValue = Object.keys(defaultProps).includes(propName);
47-
return (
48-
!haveDefaultValue ||
49-
(haveDefaultValue && defaultProps[propName] !== props[propName])
50-
);
51-
};
45+
const onlyPropsWithOriginalValue =
46+
(defaultProps: Record<string, any>, props: Record<string, any>) =>
47+
(propName: string) => {
48+
const haveDefaultValue = Object.keys(defaultProps).includes(propName);
49+
return (
50+
!haveDefaultValue ||
51+
(haveDefaultValue && defaultProps[propName] !== props[propName])
52+
);
53+
};
5254

5355
const isInlineAttributeTooLong = (
5456
attributes: string[],
@@ -117,7 +119,7 @@ export default (
117119
let outInlineAttr = out;
118120
let outMultilineAttr = out;
119121
let containsMultilineAttr = false;
120-
const visibleAttributeNames = [];
122+
const visibleAttributeNames: Array<string> = [];
121123
const propFilter = createPropFilter(props, filterProps);
122124
Object.keys(props)
123125
.filter(propFilter)

src/formatter/formatTreeNode.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import formatReactElementNode from './formatReactElementNode';
22
import formatReactFragmentNode from './formatReactFragmentNode';
33
import type { Options } from './../options';
44
import type { TreeNode } from './../tree';
5+
56
const jsxStopChars = ['<', '>', '{', '}'];
67

78
const shouldBeEscaped = (s: string) =>
@@ -53,5 +54,6 @@ export default (
5354
return formatReactFragmentNode(node, inline, lvl, options);
5455
}
5556

57+
// @ts-expect-error: So should never be executed
5658
throw new TypeError(`Unknow format type "${node.type}"`);
5759
};

src/formatter/sortObject.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function safeSortObject(value: any, seen: WeakSet<any>): any {
1919

2020
// make a copy of array with each item passed through the sorting algorithm
2121
if (Array.isArray(value)) {
22-
return value.map(v => safeSortObject(v, seen));
22+
return value.map((v) => safeSortObject(v, seen));
2323
}
2424

2525
// make a copy of object with key sorted
@@ -31,9 +31,11 @@ function safeSortObject(value: any, seen: WeakSet<any>): any {
3131
}
3232
if (key === 'current' || seen.has(value[key])) {
3333
// eslint-disable-next-line no-param-reassign
34+
// @ts-expect-error: flow to TS
3435
result[key] = '[Circular]';
3536
} else {
3637
// eslint-disable-next-line no-param-reassign
38+
// @ts-expect-error: flow to TS
3739
result[key] = safeSortObject(value[key], seen);
3840
}
3941

src/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import formatTree from './formatter/formatTree';
22
import parseReactElement from './parser/parseReactElement';
3-
import type { Element as ReactElement } from 'react';
3+
import type { ReactElement } from 'react';
44
import type { Options } from './options';
55

66
const reactElementToJsxString = (
7-
element: ReactElement<any>,
7+
element: ReactElement<any> | string | number,
88
{
99
filterProps = [],
1010
showDefaultProps = true,
@@ -16,13 +16,13 @@ const reactElementToJsxString = (
1616
sortProps = true,
1717
maxInlineAttributesLineLength,
1818
displayName,
19-
}: Options = {}
20-
) => {
19+
}: Partial<Options> = {}
20+
): string => {
2121
if (!element) {
2222
throw new Error('react-element-to-jsx-string: Expected a ReactElement');
2323
}
2424

25-
const options = {
25+
const options: Options = {
2626
filterProps,
2727
showDefaultProps,
2828
showFunctions,
@@ -34,10 +34,12 @@ const reactElementToJsxString = (
3434
maxInlineAttributesLineLength,
3535
displayName,
3636
};
37+
3738
return formatTree(parseReactElement(element, options), options);
3839
};
3940

4041
export default reactElementToJsxString;
42+
4143
export {
4244
inlineFunction,
4345
preserveFunctionLineBreak,

src/options.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import * as React from 'react';
1+
import React from 'react';
2+
23
export type Options = {
34
filterProps: string[];
45
showDefaultProps: boolean;
56
showFunctions: boolean;
6-
functionValue: (...args: Array<any>) => any;
7+
functionValue?: (...args: Array<any>) => any;
78
tabStop: number;
89
useBooleanShorthandSyntax: boolean;
910
useFragmentShortSyntax: boolean;
1011
sortProps: boolean;
12+
1113
maxInlineAttributesLineLength?: number;
12-
displayName?: (element: React.ReactElement<any>) => string;
14+
displayName?: (element: React.ReactElement) => string;
1315
};

0 commit comments

Comments
 (0)