Modifying the names of JavaScript variables and functions by leveraging Rhino or Closure Compiler in the Java environment

I am looking for a way to dynamically change variable and function names in a JavaScript file under certain conditions, similar to how a JavaScript minifier operates. Here is an example: I start with the following JavaScript code:

var something=5;
something++;
function doSomething(n){
   alert(n);
}
doSomething(something):

Now, I want to swap out something and doSomething with somethingElse and doSomethingElse, resulting in the modified Javascript code below:

var somethingElse=5;
somethingElse++;
function doSomethingElse(n){
   alert(n);
}
doSomethingElse(somethingElse):

I believe this task can be accomplished using Rhino or Google Closure Compiler (which is Rhino-based), although I'm uncertain of the exact process. Any alternative suggestions are also welcome.

Thank you

Answer №1

While Rhino is primarily a parser, it may not provide significant assistance except in basic scenarios.

Although Closure-compiler has the capability to handle variable renaming, it does not officially endorse directed renaming.

This particular method is often utilized for incremental compilation. The compiler attempts (without guarantee) to maintain previous variable and property renaming by utilizing map files. By following these steps, you might be able to achieve this, but success is not assured in all scenarios:

Step 1

Generate a variable map file using the syntax outlined below (one entry per line)

varname:renamedname

Step 2

Incorporate variable renaming into the compilation command

java -jar compiler.jar --js inputfile.js \
  --variable_map_input_file varrenaming.txt

It is important to note that standard renaming rules still apply. If a variable was not previously renamed, applying this technique will not result in its renaming.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What's the best way to navigate across by simply pressing a button located nearby?

Hey there! I'm new to JavaScript and I'm working on a shopping list page for practice. In my code, I can add new items to the list, but what I really want is to be able to cross out an item when I click the "Done" button next to it, and then uncr ...

Performing mathematical calculations with numerical values

This piece of code was created as a component of a calculator project for learning purposes. It functions correctly for the most part. However, I noticed that the addition operation seems to concatenate the two numbers together instead of actually adding ...

Experiencing an issue with the countdown timer while utilizing react-countdown library

Currently, I am in the process of creating a countdown timer that allows users to input time in minutes and start or stop the clock. However, I have encountered two challenges: I defined a state running to determine if the clock is running, and based on t ...

Stop node.js from automatically converting a nested object containing numeric keys into an array

Every time I send an object with a nested object containing keys that are numbers to my node.js server, the nested object gets converted into an array. Is there a way to prevent this from happening? Client: $.ajax({ url : `/cctool/report`, method ...

Detect the initial collision exclusively (Collision detection using raycasting)

I am currently working on a never-ending runner game where a character is positioned at (0,0) and can only move along the X-axis by using arrow keys or the mouse. There are two different types of objects moving from z = -10000 towards the character, and on ...

Determine the true size of a hidden element that was dynamically generated

I've designed a div element that initially displays only 180 characters, hiding the rest of the content. When a user clicks on the 'viewmore..' link within this partially hidden div, all the content becomes visible and a 'showless...&a ...

In the Vue mounted hook, the term "TradingView" has not been declared

I am unsure if this is the right place to ask this question, but I am currently using the tradingview library. While it is working for me, it is not functioning the way I intend it to. As per the documentation, I have placed my code in the index.html file ...

Selenium - Implementing browser shutdown cleanup steps in Selenium tests

I have encountered a challenge where I need to execute some tasks before the close button on Google Chrome browser is clicked and the window is closed. This involves logging out of the website among other things. Completely clearing cookies is not an opti ...

What is the best way to calculate the total number of results using ajax?

Encountering an issue with the total count of ajax search results. Getting an error message "Method Illuminate\Database\Eloquent\Collection::total does not exist." when using directives, for example: <div class="searched-item"&g ...

Node - Employing the exported function within the current file

While attempting to utilize the function I have exported within the same file, I encounter an undefined error: $(document).ready(function(){ $.get('https://apiEndpoint.com) .done(function(data) { for (var key in data.subscript ...

Maximize parallel execution efficiency with dynamic WebDriver in Selenium using TestNG with Java

Is this the proper method for initializing a ThreadLocal RemoteWebDriver? I have come across posts advising against having a static WebDriver, yet when I remove the static modifier, I encounter NULL pointer exceptions. Most of the time everything runs sm ...

Adaptable Semantic UI form design

Welcome, internet friends! If anyone out there has a moment to spare and is familiar with Semantic UI, I could really use some assistance... Currently, I am working on a form that looks great on larger screens like this: https://i.stack.imgur.com/cafc5.j ...

Executing JavaScript function from external SVG file globally

I am working on an HTML page that includes an external JavaScript module <script type="text/javascript" src="js/js.js"></script> and also an external SVG map called "img/map.svg". My goal is to create clickable objects on the map that will t ...

Enhance your Vue.js application by dynamically adding a class when a computed value

Just delving into the world of vue.js and I have a simple query. I've been following a tutorial but now I'd like to add my own touch to it :-P Whenever there is a change in my rank, I would like to include a CSS class for animating the label. Ho ...

Export data table from HTML to Excel successfully implemented with C#

I am currently working on an Umbraco website and developing a custom plugin for the backend that allows users to export an Excel worksheet from an HTML table. In order to achieve this functionality, I am utilizing AngularJS along with a C# controller. Belo ...

ERROR UnhandledTypeError: Unable to access attributes of null (attempting to retrieve 'pipe')

When I include "{ observe: 'response' }" in my request, why do I encounter an error (ERROR TypeError: Cannot read properties of undefined (reading 'pipe'))? This is to retrieve all headers. let answer = this.http.post<ResponseLog ...

Using Servlet to implement a login authentication system that can be shared between a web browser and

Hey there! Currently, I am in the process of creating a Login Module for an Android native app. We already have a login module set up for the website. My goal is to utilize the same servlet for validating user IDs and passwords, whether it be for the brow ...

Is it acceptable to reference all JS files in index.html in Angular?

Just dove into my first AngularJS project! Following a tutorial where they put everything in one controller, but now I want to separate controllers and services. I managed to split them into different JS files, but is it best practice to list all these f ...

Error! The function worker.recognize(...).progress is throwing an error. Any ideas on how to resolve this

Here is the code snippet: //Imports const express = require('express'); const app = express(); const fs = require("fs"); const multer = require('multer'); const { createWorker } = require("tesseract.js"); co ...

Is there a way to retrieve the real-world position in threeJS without having a mesh directly under

code.js // ... this.width = 2000 this.height = 2000 // ... this.camera = new THREE.OrthographicCamera(this.width / -2, this.width / 2, this.height / 2, this.height / -2, 1, 1000 ); this.camera.position.z = 100; this.camera.position.x = 600; this.camera.p ...