Giter VIP home page Giter VIP logo

unjquerify's Introduction

unjQuerify

UnjQuerify is a project that can assist developers in migrating code from jQuery to vanilla DOM APIs.

In:

$("#submit").click(function() {
    $("#output").text("The button was clicked!");
    $(".post").hide();
});

Output:

document.getElementById("submit").addEventListener("click", function () {
    document.getElementById("output").textContent = "The button was clicked!";
    document.getElementsByClassName("post").map((_element) => _element.style.display = "none");
});

Built With

  • babel to build an AST and to transform nodes.

Usage

CLI

  • Ensure that npm is available.
  • Use npm install to install dependencies.
  • Run unjQuerify with the provided example script: npm --silent start sample/simple.js.
  • Verify that the console shows the transformed script.

As dependency

  • Ensure that npm is available.
  • Run npm install unjquerify.
  • Import unjQuerify's plugins using `require('unjquerify/build/src/all-plugins')`` or a similar method.

Contributing

Contributions are always welcome. Please see this project's issue tracker.

Some useful resources are the following:

Before submitting a pull request, please verify that your changes pass linting (run with npm run lint). Please include tests for new features.

unjquerify's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

unjquerify's Issues

Transformer overly enthousiastic

At the moment, any MemberExpression that matches a transformable function signature will be transformed. This includes expressions like [0,0].hide().

One implementation suggestion is that the engine should walk up a tree first to see if the left-most expression (in this case, the object of the callexpression body of the top statement) appears to be a jQuery.

The performance of such an approach should be checked.

Unchaining adds superfluous newlines

When transforming $("a").hide().hide().hide().hide(), extra newlines will be inserted between each output statement, resulting in the following:

const element = document.querySelectorAll("a");

element.style.display = "none";

element.style.display = "none";

...

As the output will be very verbose with many chained statements, this is undesirable. When removing the outer callExpression so that the outer expression is a memberExpression, no newlines seem to be inserted.

Possible babel-core bug. See related issue.

What is exceptional is that each statement separately will not generate a newline when used with babel-generate to output each statement, however, joining them with insertBefore, insertAfter, or replaceWithMultiple with or without the use of a BlockStatement will cause them to appear.

Some examples do not actually change when the transformation is applied

Example: DocumentReadyPlugin.

The example $(document).ready(e) does not transform due to a isFunctionExpression check on the first argument.

Whether or not this check should be done at all is one thing, but all examples should demonstrate a mutation.

Check the following plugins:

  • DocumentReadyPlugin
  • GetElementByIdPlugin
  • GetElementsByClassNamePlugin
  • QuerySelectorAllPlugin
  • CssGetPlugin
  • OnPlugin()
  • ClickHandlerPlugin
  • AddClassPlugin
  • TextSetPlugin
  • TextGetPlugin
  • HidePlugin
  • UnwrapPlugin
  • IsPlugin

Follow simple unmutable assignments

Example:

const x = $("a.nav-link");
x.hide();
x.show();

For simple assignments, the engine should be able to follow references to statically determine if an element is a jQuery where possible.

Chained jQuery calls transform incorrectly

For input $("a").hide().hide();, the transformation given is document.querySelectorAll("a").map((_element) => (_element.style.display = "none")[0].style.display = "none");.

Possibly very high-effort fix.

Unchain optimization

Currently, the UnchainPlugin will always cause a reassignment for each chained method call. However, many methods do not return a different collection on each call.

For methods that do not return a new collection upon transformation, a reassignment is unnecessary. Consider $("a").hide().hide().

Collection collectors are not always correct

For functions that cause mutations, the current approach of translating $("a").hide() to document.querySelectorAll("a").forEach(e => e.style.display = "none"); is possible, but functions that return values will not be transformed as expected.

Consider the following cases:

Therefore, the current forEach approach does not work for all cases.

Each transformation should be augmented with a transformation that should be used to collect the results of an application to a collection.

Some things are not being converted

In the character counter example, for example the jQuery each function is not converted to forEach

And it looks like while converting jQuery using this the converted code isn't working as it should.

Plugin checklist

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.