-
-
Notifications
You must be signed in to change notification settings - Fork 1
Tutorial 02 Play with Types
Sam Cao edited this page Jun 28, 2023
·
3 revisions
In this tutorial, you are going to learn:
- How to get
TypeScript
support. - How to make changes to Java types.
- Download this folder to a local folder along with your code.
- Add the following line to your JavaScript or TypeScript code to get intellisense in VS Code.
/// <reference types="../jaspiler/index.d.ts"/>
- Add the following line to your JavaScript code to get additional type support.
const { JTKind, /* You may add more exports. */ } = require('./jaspiler/jaspiler');
- Listen to
Class(node)
and change the Java types.
/// <reference types="../jaspiler/index.d.ts"/>
const { JTKind } = require('./jaspiler/jaspiler');
const result = jaspiler.transformSync(
`package com.test;
public class A {}
public interface B {}
public record C() {}
public class D {}
`,
{
plugins: [{
visitor: {
Class(node) {
const simpleName = node.simpleName.value;
switch (simpleName) {
case 'A':
node.kind = JTKind.INTERFACE;
break;
case 'B':
node.kind = JTKind.CLASS;
break;
case 'C':
node.kind = JTKind.ENUM;
break;
case 'D':
node.kind = JTKind.RECORD;
break;
}
},
},
}],
sourceType: 'string',
});
console.info(result.code);
- The output is:
package com.test;
public interface A { // class is converted to interface.
}
public class B { // interface is converted to class.
}
public enum C { // record is converted to enum.
}
public record D { // class is converted to record.
}
The complete code is at 02_play_with_types.js
- It's easy to get intellisense.
- Almost all nodes in the visitor events can be consumed and changed.