Skip to content

Commit

Permalink
📃 docs: Add Tutorial 02: Play with Types
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Jun 28, 2023
1 parent 53d068f commit 09baab3
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
4 changes: 2 additions & 2 deletions scripts/node/jaspiler/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ interface JTCharacter {
interface JTClassDecl extends JTStatement<JTClassDecl> {
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<?>[];
Expand Down Expand Up @@ -397,7 +397,7 @@ interface JTLambda extends JTFunctionalExpression<JTLambda> {
}

interface JTLiteral extends JTExpression<JTLiteral> {
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;
}

Expand Down
1 change: 0 additions & 1 deletion scripts/node/test/test_transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }');
Expand Down
2 changes: 2 additions & 0 deletions scripts/node/tutorials/01_quick_start.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

// Tutorial 01: Quick Start

const result = jaspiler.transformSync(
`package com.test;
public class A {
Expand Down
54 changes: 54 additions & 0 deletions scripts/node/tutorials/02_play_with_types.js
Original file line number Diff line number Diff line change
@@ -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

/// <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);
9 changes: 9 additions & 0 deletions src/test/java/com/caoccao/jaspiler/TestJaspilerMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 09baab3

Please sign in to comment.