From 09baab3c9e35b5cc9a2d34b7ae9c09c2ec802b26 Mon Sep 17 00:00:00 2001 From: Sam Cao Date: Wed, 28 Jun 2023 15:55:47 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=83=20docs:=20Add=20Tutorial=2002:=20P?= =?UTF-8?q?lay=20with=20Types?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/node/jaspiler/index.d.ts | 4 +- scripts/node/test/test_transform.js | 1 - scripts/node/tutorials/01_quick_start.js | 2 + scripts/node/tutorials/02_play_with_types.js | 54 +++++++++++++++++++ .../caoccao/jaspiler/TestJaspilerMain.java | 9 ++++ 5 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 scripts/node/tutorials/02_play_with_types.js diff --git a/scripts/node/jaspiler/index.d.ts b/scripts/node/jaspiler/index.d.ts index ca35620..3fe9390 100644 --- a/scripts/node/jaspiler/index.d.ts +++ b/scripts/node/jaspiler/index.d.ts @@ -276,7 +276,7 @@ interface JTCharacter { interface JTClassDecl extends JTStatement { extendsClause: JTExpression; implementsClauses: JTExpression[]; - kind: JTKind.ANNOTATED_TYPE | JTKind.CLASS | JTKind.ENUM | JTKind.INTERFACE | JTKind.RECORD; + kind: JTKind.ANNOTATION_TYPE | JTKind.CLASS | JTKind.ENUM | JTKind.INTERFACE | JTKind.RECORD; members: JTTree[]; modifiers: JTModifiers; permitsClauses: JTExpression[]; @@ -397,7 +397,7 @@ interface JTLambda extends JTFunctionalExpression { } interface JTLiteral extends JTExpression { - kind: JTKind; + kind: JTKind.BOOLEAN_LITERAL | JTKind.CHAR_LITERAL | JTKind.DOUBLE_LITERAL | JTKind.FLOAT_LITERAL | JTKind.INT_LITERAL | JTKind.LONG_LITERAL | JTKind.NULL_LITERAL | JTKind.STRING_LITERAL; value: number | string | boolean | JTFloat | JTCharacter | null; } diff --git a/scripts/node/test/test_transform.js b/scripts/node/test/test_transform.js index 5f242c0..3b44c80 100644 --- a/scripts/node/test/test_transform.js +++ b/scripts/node/test/test_transform.js @@ -173,7 +173,6 @@ function testContractChangeMethod() { { plugins: [PluginContractChangeMethod], context: context, ast: true, fileName: 'A', sourceType: 'string' }); // Assert { ast, code } assert.isObject(result); - console.info(result.code); assert.include(result.code, 'public void testVoid() {\n }'); assert.include(result.code, 'public boolean testBoolean() {\n return false;\n }'); assert.include(result.code, 'public char testChar() {\n return \'\\0\';\n }'); diff --git a/scripts/node/tutorials/01_quick_start.js b/scripts/node/tutorials/01_quick_start.js index 18a99e1..f19e1d9 100644 --- a/scripts/node/tutorials/01_quick_start.js +++ b/scripts/node/tutorials/01_quick_start.js @@ -14,6 +14,8 @@ * limitations under the License. */ +// Tutorial 01: Quick Start + const result = jaspiler.transformSync( `package com.test; public class A { diff --git a/scripts/node/tutorials/02_play_with_types.js b/scripts/node/tutorials/02_play_with_types.js new file mode 100644 index 0000000..3bc0360 --- /dev/null +++ b/scripts/node/tutorials/02_play_with_types.js @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023. caoccao.com Sam Cao + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Tutorial 02: Play with Types + +/// + +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); diff --git a/src/test/java/com/caoccao/jaspiler/TestJaspilerMain.java b/src/test/java/com/caoccao/jaspiler/TestJaspilerMain.java index 62fd19f..7a40633 100644 --- a/src/test/java/com/caoccao/jaspiler/TestJaspilerMain.java +++ b/src/test/java/com/caoccao/jaspiler/TestJaspilerMain.java @@ -39,4 +39,13 @@ public void testTutorials01QuickStart() throws Exception { .toAbsolutePath().toFile()); assertEquals(0, jaspilerMain.call()); } + + @Test + public void testTutorials02PlayWithTypes() throws Exception { + var jaspilerMain = new JaspilerMain(); + jaspilerMain.setFile( + SystemUtils.INITIAL_WORKING_DIRECTORY.resolve("scripts/node/tutorials/02_play_with_types.js") + .toAbsolutePath().toFile()); + assertEquals(0, jaspilerMain.call()); + } }