diff --git a/packages/unminify/README.md b/packages/unminify/README.md index 5c5af1ba..52d0e149 100644 --- a/packages/unminify/README.md +++ b/packages/unminify/README.md @@ -344,11 +344,19 @@ React Hooks: ```diff - const th = createContext('light'); -- const [e, f] = useState(0); -- const g = useRef(null); + const ThContext = createContext('light'); + +- const [e, f] = useState(0); + const [e, setE] = useState(0); + +- const g = useRef(null); + const gRef = useRef(null); + +- const [e, f] = o.useReducer(reducer, initialArg, init?); ++ const [eState, fDispatch] = o.useReducer(reducer, initialArg, init?); + +- const Z = o.forwardRef((e, t) => { ... }) ++ const Z = o.forwardRef((props, ref) => { ... }) ``` ### `un-iife` @@ -418,6 +426,9 @@ Restore TypeScript enum syntax. + var Direction = { + Up: "UP", + Down: 2, + ++ // reverse mapping ++ 2: "Down" + } ``` @@ -443,7 +454,7 @@ Converts object property accesses and array index accesses to destructuring. + console.log(t, n, r); ``` -Inline reassigned variables. +Inline reassigned temp variables. ```diff - const a = d; @@ -502,10 +513,10 @@ Converts CommonJS's `require` and `module.exports` to ES6's `import` and `export + export default foo ``` -Note: Please aware that CJS and ESM are not fully compatible, and this transformation is not perfect. We have a comprehensive test suite to ensure the correctness of this transformation, but there are still some edge cases that are not covered. Feel free to open an issue if you find any bugs. +Note: Please aware that CJS and ESM are not fully compatible, and this transformation is not perfect. We have a comprehensive test suite to ensure the correctness of the transformation, but there are still some edge cases that are not covered. Feel free to open an issue if you find any bugs. Limitations: -- `require(dynamic)` is not supported as ESM does not support dynamic imports. Convert it to `await import()` is not appropriate as it require the whole execution context to be async. +- `require(dynamic)` is not supported as ESM does not support dynamic imports. Convert it to `await import()` is not appropriate as it require the whole execution context to be **async**. - Some packages require `import * as name from 'package'` instead of `import name from 'package'`. We cannot detect this automatically, so you might need to fix it manually. - Currently, it won't aware the exports format of other files generated by our `unpacker`. PRs are welcome. @@ -565,7 +576,7 @@ Currently, this transformation only supports output from **TypeScript**. And it does not handled control flow properly, as it needs graph analysis. -Please aware there are tons of edge cases that are not covered by this rule. +Please aware there are **tons of edge cases** that are not covered by this rule. ```diff -function func() { @@ -601,7 +612,7 @@ Please aware there are tons of edge cases that are not covered by this rule. ### `un-jsx` -Converts `React.createElement` to JSX. +Converts `React.createElement` and `jsxRuntime.jsx` back to JSX. ```diff - React.createElement("div", { className: "title" }, "Hello World"); @@ -620,7 +631,7 @@ Pass `pragmaFrag` option to specify the JSX fragment pragma. + ``` -It will also automatically guess the component name from the `displayName` property. +It will automatically guess the component name from the `displayName` property. ```diff - var S = /*#__PURE__*/React.createElement("div", null); diff --git a/packages/unminify/src/transformations/smart-rename.ts b/packages/unminify/src/transformations/smart-rename.ts index 2bce74d1..3a154a41 100644 --- a/packages/unminify/src/transformations/smart-rename.ts +++ b/packages/unminify/src/transformations/smart-rename.ts @@ -35,9 +35,21 @@ const MINIFIED_IDENTIFIER_THRESHOLD = 2 * -> * const uContext = o.createContext(u); * + * const d = o.useRef(u); + * -> + * const uRef = o.useRef(u); + * * const [e, f] = o.useState(0); * -> * const [e, SetE] = o.useState(0); + * + * const [e, f] = o.useReducer(reducer, initialArg, init?); + * -> + * const [eState, fDispatch] = o.useReducer(reducer, initialArg, init?); + * + * const Z = o.forwardRef((e, t) => { ... }) + * -> + * const Z = o.forwardRef((props, ref) => { ... }) */ export const transformAST: ASTTransformation = (context) => { const { root, j } = context @@ -264,11 +276,9 @@ function handleReactRename(j: JSCodeshift, root: Collection) { }) /** - * const Z = o.forwardRef((e, t) => { - * }) + * const Z = o.forwardRef((e, t) => { ... }) * -> - * const Z = o.forwardRef((props, ref) => { - * }) + * const Z = o.forwardRef((props, ref) => { ... }) * * @see https://react.dev/reference/react/forwardRef */ diff --git a/packages/unminify/src/transformations/un-jsx.ts b/packages/unminify/src/transformations/un-jsx.ts index d983526e..c26f76fd 100644 --- a/packages/unminify/src/transformations/un-jsx.ts +++ b/packages/unminify/src/transformations/un-jsx.ts @@ -53,7 +53,7 @@ const DEFAULT_PRAGMA_FRAG_CANDIDATES = [ // ] /** - * Converts `React.createElement` to JSX. + * Converts `React.createElement` and `jsxRuntime.jsx` back to JSX. */ export const transformAST: ASTTransformation = (context, params) => { const { root, j } = context