java2typescript icon indicating copy to clipboard operation
java2typescript copied to clipboard

support global replace (instead of once replacement)

Open x2nie opened this issue 1 year ago • 2 comments

supposed we have file:

package org.antlr.v4.tool;

public class Native2 {

	double getLabeledNodeVoltage(String name) {
		Integer node = LabeledNodeElm.getByName(name);
		if (node == null || node == 0)
			return 0;
		// subtract one because ground is not included in nodeVoltages[]
		return nodeVoltages[node.intValue() - 1];
	}
	
    native void callTimeStepHook() /*-{
	    var hook = $wnd.CircuitJS1.ontimestep;
	    if (hook)
	    	hook($wnd.CircuitJS1);
	}-*/;

	native void callSVGRenderedHook(String svgData) /*-{
		var hook = $wnd.CircuitJS1.onsvgrendered;
		if (hook)
			hook($wnd.CircuitJS1, svgData);
	}-*/;
}

What expected output is :


import { java, JavaObject, type double } from "jree";



export  class Native2 extends JavaObject {

	protected  getLabeledNodeVoltage(name: string): double {
		let  node: java.lang.Integer = LabeledNodeElm.getByName(name);
		if (node === null || node === 0)
			return 0;
		// subtract one because ground is not included in nodeVoltages[]
		return nodeVoltages[node.intValue() - 1];
	}
	
    /*-na-ti-ve-*/ protected  callTimeStepHook(): void {
	    var hook = $wnd.CircuitJS1.ontimestep;
	    if (hook)
	    	hook($wnd.CircuitJS1);
	};

	/*-na-ti-ve-*/ protected  callSVGRenderedHook(svgData: string): void {
		var hook = $wnd.CircuitJS1.onsvgrendered;
		if (hook)
			hook($wnd.CircuitJS1, svgData);
	};
}

But without this PR, will generates :


import { java, JavaObject, type double } from "jree";



export  class Native2 extends JavaObject {

	protected  getLabeledNodeVoltage(name: string): double {
		let  node: java.lang.Integer = LabeledNodeElm.getByName(name);
		if (node === null || node === 0)
			return 0;
		// subtract one because ground is not included in nodeVoltages[]
		return nodeVoltages[node.intValue() - 1];
	}
	
    /*-na-ti-ve-*/ protected  callTimeStepHook(): void {
	    var hook = $wnd.CircuitJS1.ontimestep;
	    if (hook)
	    	hook($wnd.CircuitJS1);
	};

	protected  callSVGRenderedHook(svgData: java.lang.String):  void /*-{
		var hook = $wnd.CircuitJS1.onsvgrendered;
		if (hook)
			hook($wnd.CircuitJS1, svgData);
	}-*/;
}

OPTIONS I am using :

{
    "packageRoot": "./migration/simple",
    "outputPath": "./output/simple/output",
    "sourceReplace": {
        "(native)": "/*-na-ti-ve-*/"
    },
    "targetReplace": {
        "java\\.lang\\.Enum": "Enum",
        "java\\.lang\\.String": "string",
        "--\\/\\*-\\{[\\s\\S]*?\\}-\\*\\/": "{}",
        "\\/\\*-\\{": "{",
        "\\}-\\*\\/": "}"
    },
    "options": {
        "addNullUnionType": false
    }
}

x2nie avatar Aug 16 '24 19:08 x2nie

simply put, my mod is to not stop replacing the string (if there are more than one match). Otherwise, the replacement still works for first replacement, then do not replace next matches.

x2nie avatar Aug 16 '24 19:08 x2nie

@x2nie patch looks good, but don't forget to sign the patch (check out the DCO check, which contains a link to how you can sign it).

mike-lischke avatar Aug 17 '24 08:08 mike-lischke

Okay, I might do that today or tomorrow. I m sorry for slow response, I just installed another linux (for unrelated reason).

x2nie avatar Aug 29 '24 05:08 x2nie

Yeah, finally I did it. Apologize for long waiting.

Intermezzo: I was confusing of how to resolve this (the DCO), I planned to do that (DCO while committing) in another branch by start over modifying - testing - commiting with DCO - and then create new PR. However suddenly my disk is full, and I turn busy with the disk/ OS stuffs. But today, by asking AI (chatgpt) it actually can be done with very simple command:

git commit --amend -s
git push --force

x2nie avatar Sep 15 '24 00:09 x2nie

When I first hit the DCO barrier I just followed the link beside the error (Details) and quickly found the necessary git commands.

mike-lischke avatar Sep 15 '24 09:09 mike-lischke