fix: not exist type mapping

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-09-25 10:10:22 +08:00
parent 7bb4d1f6a3
commit fdf1088564
2 changed files with 69 additions and 15 deletions

View File

@ -1,9 +1,11 @@
# sponge bukkit
TYPE=sponge
npx tsc src/build.ts --outDir dist
cd dist
rm -rf temp
mkdir -p temp
node build.js
node build.js ${TYPE}
cd ../
rm -rf src/typings
mkdir -p src/typings/bukkit
cp dist/temp/* src/typings/bukkit/ -R
mkdir -p src/typings/${TYPE}
cp dist/temp/* src/typings/${TYPE}/ -R

View File

@ -1,10 +1,8 @@
import * as fs from "fs";
let whiteKey = ['shift', "map", 'filter', 'values', 'valueOf', 'toString', 'length', 'includes']
function convertJson2TypeDefiend(infile: string, outDir: string) {
const file = infile.split(".json")[0];
const json = fs.readFileSync(`../docs/${file}.json`).toString();
const json = fs.readFileSync(`${inDir}/${file}.json`).toString();
const obj = JSON.parse(json);
const qnas: string[] = obj.qualifiedName.split(".");
let closeBuk = 0;
@ -16,7 +14,36 @@ function convertJson2TypeDefiend(infile: string, outDir: string) {
closeBuk++;
}
let classModifier = formatClassModifier(obj.modifiers)
temp += `${' '.repeat(closeBuk)}${classModifier}class ${qnas[qnas.length - 1]} {\n`;
temp += `${' '.repeat(closeBuk)}// @ts-ignore\n`
temp += `${' '.repeat(closeBuk)}${classModifier}${qnas[qnas.length - 1]}`
let isInterface = classModifier.includes('interface')
let safeInterface = [];
for (const ifs of obj.interfaces) {
if (!ifs.qualifiedName.startsWith('java')) {
safeInterface.push(ifs)
}
}
if (isInterface) {
if (safeInterface.length > 0) {
temp += ' extends '
for (const ifs of safeInterface) {
temp += ifs.qualifiedName;
temp += ', '
}
temp = temp.substr(0, temp.length - 2);
}
} else {
temp += `${(obj.superclass && obj.superclass.qualifiedName.startsWith('org.')) ? (' extends ' + obj.superclass.qualifiedName) : ''}`;
if (safeInterface.length > 0) {
temp += ' implements '
for (const ifs of safeInterface) {
temp += ifs.qualifiedName;
temp += ', '
}
temp = temp.substr(0, temp.length - 2);
}
}
temp += ' {\n'
closeBuk++;
for (const constructor of obj.constructors) {
temp += `${formatDoc(constructor.docString, closeBuk)}${' '.repeat(closeBuk)}constructor(${formatParameters(constructor.parameters)})\n`;
@ -26,14 +53,14 @@ function convertJson2TypeDefiend(infile: string, outDir: string) {
let methods = '';
for (const method of obj.methods) {
let methodModifier = replaceModifiers(method.modifiers, classModifier.includes('abstract'))
let methodModifier = isInterface ? '' : replaceModifiers(method.modifiers, classModifier.includes('abstract'))
if (!whiteKey.includes(method.name)) {
if (members[method.name] && methodModifier.includes('abstract')) {
continue;
}
members[method.name] = methodModifier;
}
methods += `${formatDoc(method.docString, closeBuk)}${' '.repeat(closeBuk)}${methodModifier} ${method.name}(${formatParameters(method.parameters)}): ${mappingType(method.returnType.type)};\n`;
methods += `${formatDoc(method.docString, closeBuk)}${' '.repeat(closeBuk)}// @ts-ignore\n${' '.repeat(closeBuk)}${methodModifier} ${method.name}(${formatParameters(method.parameters)}): ${mappingType(method.returnType.type)};\n`;
}
let fields = '';
@ -41,7 +68,7 @@ function convertJson2TypeDefiend(infile: string, outDir: string) {
if (members[field.name]) {
continue;
}
fields += `${' '.repeat(closeBuk)}${replaceModifiers(field.modifiers)} ${field.name}: ${mappingType(field.type ? field.type.type : "any")};\n`;
fields += `${' '.repeat(closeBuk)}// @ts-ignore\n${' '.repeat(closeBuk)}${isInterface ? '' : replaceModifiers(field.modifiers)} ${field.name}: ${mappingType(field.type ? field.type.type : "any")};\n`;
}
temp += fields + methods;
@ -55,7 +82,8 @@ function convertJson2TypeDefiend(infile: string, outDir: string) {
}
function formatClassModifier(modifiers: string) {
let tempm = modifiers.replace('public', '').replace('interface', '').replace('static', '').replace('final', '').trim();
let tempm = modifiers.replace('public', '').replace('static', '').replace('final', '').trim();
if (!modifiers.includes('interface')) { tempm += ' class' }
return tempm.length > 0 ? (tempm + ' ') : '';
}
@ -96,6 +124,8 @@ function mappingName(name: string) {
return outName;
}
let whiteKey = ['shift', "map", 'filter', 'values', 'valueOf', 'toString', 'length', 'includes', 'entries','keys']
const typeMap = [];
typeMap['int'] = 'number';
typeMap['int[]'] = 'number[]';
@ -114,17 +144,39 @@ typeMap['java.util.List'] = 'any[] /*java.util.List*/'
typeMap['java.util.Set'] = 'any[] /*java.util.Set*/'
typeMap['java.util.Collection'] = 'any[] /*java.util.Collection*/'
typeMap['java.util.Map'] = 'Map<any, any> /*java.util.Map*/'
// Sponge
typeMap['Vectori'] = 'any /*Vector3i*/'
typeMap['Vectord'] = 'any /*Vector3d*/'
typeMap['Vectorf'] = 'any /*Vector3f*/'
typeMap['Vector2i'] = 'any /*Vector2i*/'
typeMap['Vector2d'] = 'any /*Vector2d*/'
typeMap['Vector2f'] = 'any /*Vector2f*/'
typeMap['Vector3i'] = 'any /*Vector3i*/'
typeMap['Vector3d'] = 'any /*Vector3d*/'
typeMap['Vector3f'] = 'any /*Vector3f*/'
typeMap['Type'] = 'any /*Type*/'
typeMap['Gson'] = 'any /*Gson*/'
typeMap['Logger'] = 'any /*Logger*/'
typeMap['MethodVisitor'] = 'any /*MethodVisitor*/'
typeMap['ConfigurationNode'] = 'any /*ConfigurationNode*/'
typeMap['TypeSerializerCollection'] = 'any /*TypeSerializerCollection*/'
typeMap['Quaterniond'] = 'any /*Quaterniond*/'
typeMap['Matrix2d'] = 'any /*Matrix2d*/'
typeMap['Matrix3d'] = 'any /*Matrix3d*/'
typeMap['Matrix4d'] = 'any /*Matrix4d*/'
function mappingType(type: string): string {
let outType = typeMap[type] || type || 'any';
outType = outType.startsWith('java.') ? 'any' : outType;
outType = outType.startsWith('java.') || outType.startsWith('javax.') ? 'any' : outType;
return outType;
}
const outDir = "./temp";
var args = process.argv.splice(2)
const files = fs.readdirSync("../docs");
let index = ``;
const inDir = `../docs/${args[0]}`
const outDir = "./temp";
const files = fs.readdirSync(inDir);
let index = '';
for (const file of files) {
index += `/// <reference path="./${convertJson2TypeDefiend(file, outDir)}" />\n`;
}