Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document.createElement is converted to JSX #113

Closed
milahu opened this issue Feb 15, 2024 · 12 comments
Closed

document.createElement is converted to JSX #113

milahu opened this issue Feb 15, 2024 · 12 comments
Labels
bug Something isn't working scope: unminify

Comments

@milahu
Copy link

milahu commented Feb 15, 2024

Describe the bug

jsx is not javascript

with input.js i expect javascript in output.js

with input.jsx jsx output can be acceptable in output.jsx

Input code

const Name = "div";
const attrs = {id: "x"};
document.createElement(Name, attrs);

Reproduction

No response

Steps to reproduce

No response

Expected behavior

keep document.createElement

Actual behavior

const Name = "div";
const attrs = { id: "x" };
<Name {...attrs} />;
$ node out/test.js
out/test.js:3
<Name {...attrs} />;
^

SyntaxError: Unexpected token '<'

also the JSX is wrong, JSX has no dynamic tag names (?)
so it should be <div {...attrs} />

@pionxzh pionxzh added bug Something isn't working and removed pending triage labels Feb 15, 2024
@pionxzh
Copy link
Owner

pionxzh commented Feb 15, 2024

Rule un-jsx will guess JSX API by searching *.createElement without checking if the caller is document. I will fix it by excluding document. But it will still fail in the following case:

const d = document;
d.createElement(...)

I would like to introduce another rule to inline temp variables which were assigned to common global variables. For example:

- const w = window;
- w.setTimeout(...);
+ window.setTimeout(...);

I hope this can improve the readability and avoid misunderstanding the intention of the code.

@milahu
Copy link
Author

milahu commented Feb 15, 2024

aah, i was looking for

https://github.com/pionxzh/wakaru/blob/main/packages/unminify/README.md#un-jsx

... but i dont see a way to disable rules

@pionxzh
Copy link
Owner

pionxzh commented Feb 15, 2024

You can disable it on the playground. CLI has not supported configuring the rules yet.

If you are running this repo in local environment, simply disable it in file packages/unminify/src/transformations/index.ts

@pionxzh
Copy link
Owner

pionxzh commented Feb 15, 2024

JSX has no dynamic tag names (?)

This is a supported use case in JSX.

But your proposal is an improvement that we can make. I'm not sure if it's a common case that bundler/minifier will generate.

@pionxzh
Copy link
Owner

pionxzh commented Feb 15, 2024

Fixed

@pionxzh
Copy link
Owner

pionxzh commented Feb 15, 2024

I have created another feature request for the tag name in #115

@milahu
Copy link
Author

milahu commented Feb 15, 2024

similar case

wrong transformation from h(...) to JSX

wakaru assumes that h is the h function of hyperscript

to get this right, wakaru would have to check the actual implementation of the h function

input

let h = (w, x, z) => w.split(x).join(z);
let b = (w) => {
  w = w.toLowerCase();
  w = h(w, " ", "");
  w = h(w, "-", "");
  let x = "400";
  if ((w = h(w, "_", "")).includes("thin") || w.includes("hairline")) {
    x = "100";

output

let h = (w, x, z) => w.split(x).join(z);
let b = (W) => {
  W = W.toLowerCase();
  W = <W {..." "}>{""}</W>;
  W = <W {..."-"}>{""}</W>;
  let x = "400";
  if ((W = <W {..."_"}>{""}</W>).includes("thin") || W.includes("hairline")) {
    x = "100";

@pionxzh
Copy link
Owner

pionxzh commented Feb 16, 2024

hmm ye, h would be too common to hit on non-jsx code.

@pionxzh pionxzh reopened this Feb 16, 2024
@pionxzh
Copy link
Owner

pionxzh commented Feb 16, 2024

We also have the plan to do the module detection. Ideally, we should be able to detect which module is react/preact and convert the call to JSX precisely.

pionxzh added a commit that referenced this issue Feb 16, 2024
@pionxzh
Copy link
Owner

pionxzh commented Feb 16, 2024

preact's h pragma is disabled until we implement the module detection.

@pionxzh pionxzh closed this as completed Feb 16, 2024
@pionxzh
Copy link
Owner

pionxzh commented Feb 16, 2024

released

@pionxzh
Copy link
Owner

pionxzh commented Feb 26, 2024

I would like to introduce another rule to inline temp variables which were assigned to common global variables. For example:

- const w = window;
- w.setTimeout(...);
+ window.setTimeout(...);

This has been added in 1a91aa4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working scope: unminify
Projects
None yet
Development

No branches or pull requests

2 participants