修复 TypeScript 类型转换错误地出现在 JavaScript 文件中的问题

修复在混合使用 enum 和 vector 时,js 文件中会出现 `as any` 的类型转换
This commit is contained in:
superzheng 2019-03-25 19:11:13 +08:00
parent 3aef9a315f
commit 3e45ea9b76
5 changed files with 11 additions and 18 deletions

View File

@ -157,9 +157,7 @@ private:
string getTsType(const TypePtr &pPtr, const bool bStream = true, const bool bBase = false); string getTsType(const TypePtr &pPtr, const bool bStream = true, const bool bBase = false);
string getDefault(const TypeIdPtr & pPtr, const string &sDefault, const string & sNamespace); string getDefault(const TypeIdPtr & pPtr, const string &sDefault, const string & sNamespace, const bool &bGlobal = true, const bool &bCastEnumAsAny = false);
string getDefault(const TypeIdPtr & pPtr, const string &sDefault, const string & sNamespace, const bool bGlobal);
private: private:
string generateJS(const StructPtr &pPtr, const string &sNamespace, bool &bNeedAssert, bool &bQuickFunc); string generateJS(const StructPtr &pPtr, const string &sNamespace, bool &bNeedAssert, bool &bQuickFunc);

View File

@ -136,7 +136,7 @@ string CodeGenerator::generateTSProxy(const NamespacePtr &nPtr, const InterfaceP
if (isSimple(oPtr->getReturnPtr()->getTypePtr())) if (isSimple(oPtr->getReturnPtr()->getTypePtr()))
{ {
str << getDefault(oPtr->getReturnPtr(), oPtr->getReturnPtr()->def(), nPtr->getId()) str << getDefault(oPtr->getReturnPtr(), oPtr->getReturnPtr()->def(), nPtr->getId(), true, true)
<< (isRawOrString(oPtr->getReturnPtr()->getTypePtr()) ? ", 1" : ""); << (isRawOrString(oPtr->getReturnPtr()->getTypePtr()) ? ", 1" : "");
} }
else else
@ -166,7 +166,7 @@ string CodeGenerator::generateTSProxy(const NamespacePtr &nPtr, const InterfaceP
if (isSimple(vParamDecl[i]->getTypeIdPtr()->getTypePtr())) if (isSimple(vParamDecl[i]->getTypeIdPtr()->getTypePtr()))
{ {
str << getDefault(vParamDecl[i]->getTypeIdPtr(), vParamDecl[i]->getTypeIdPtr()->def(), nPtr->getId()) str << getDefault(vParamDecl[i]->getTypeIdPtr(), vParamDecl[i]->getTypeIdPtr()->def(), nPtr->getId(), true, true)
<< (isRawOrString(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) ? ", 1" : ""); << (isRawOrString(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) ? ", 1" : "");
} }
else else
@ -257,7 +257,7 @@ string CodeGenerator::generateTSProxy(const NamespacePtr &nPtr, const InterfaceP
str << ", " << getDataType(oPtr->getReturnPtr()->getTypePtr(), true); str << ", " << getDataType(oPtr->getReturnPtr()->getTypePtr(), true);
} }
str << ", " << getDefault(oPtr->getReturnPtr(), "", nPtr->getId(), true) str << ", " << getDefault(oPtr->getReturnPtr(), "", nPtr->getId(), true, true)
<< (isRawOrString(oPtr->getReturnPtr()->getTypePtr()) ? ", 1" : ""); << (isRawOrString(oPtr->getReturnPtr()->getTypePtr()) ? ", 1" : "");
str << ")," << endl; str << ")," << endl;

View File

@ -141,7 +141,7 @@ string CodeGenerator::generateTSServerDispatch(const NamespacePtr &nPtr, const I
if (vParamDecl[i]->isOut()) if (vParamDecl[i]->isOut())
{ {
dstr << ", " << getDefault(vParamDecl[i]->getTypeIdPtr(), "", nPtr->getId(), true) dstr << ", " << getDefault(vParamDecl[i]->getTypeIdPtr(), "", nPtr->getId(), true, true)
<< (isRawOrString(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) ? ", 1" : ""); << (isRawOrString(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) ? ", 1" : "");
} }
@ -163,7 +163,7 @@ string CodeGenerator::generateTSServerDispatch(const NamespacePtr &nPtr, const I
if (isSimple(vParamDecl[i]->getTypeIdPtr()->getTypePtr())) if (isSimple(vParamDecl[i]->getTypeIdPtr()->getTypePtr()))
{ {
dstr << getDefault(vParamDecl[i]->getTypeIdPtr(), vParamDecl[i]->getTypeIdPtr()->def(), nPtr->getId()) dstr << getDefault(vParamDecl[i]->getTypeIdPtr(), vParamDecl[i]->getTypeIdPtr()->def(), nPtr->getId(), true, true)
<< (isRawOrString(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) ? ", 1" : ""); << (isRawOrString(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) ? ", 1" : "");
} }
else else

View File

@ -77,7 +77,7 @@ string CodeGenerator::generateTS(const ConstPtr &pPtr, const string &sNamespace,
INC_TAB; INC_TAB;
s << TAB << "export const " << pPtr->getTypeIdPtr()->getId() << ": " s << TAB << "export const " << pPtr->getTypeIdPtr()->getId() << ": "
<< getTsType(pPtr->getTypeIdPtr()->getTypePtr()) << " = " << getTsType(pPtr->getTypeIdPtr()->getTypePtr()) << " = "
<< getDefault(pPtr->getTypeIdPtr(), GET_CONST_GRAMMAR_PTR(pPtr)->v, sNamespace, false) << ";" << getDefault(pPtr->getTypeIdPtr(), GET_CONST_GRAMMAR_PTR(pPtr)->v, sNamespace, false, true) << ";"
<< endl; << endl;
DEL_TAB; DEL_TAB;
return s.str(); return s.str();
@ -104,7 +104,7 @@ string CodeGenerator::generateTS(const StructPtr &pPtr, const string &sNamespace
{ {
s << TAB << (member[i]->getId()) << ": " s << TAB << (member[i]->getId()) << ": "
<< getTsType(member[i]->getTypePtr()) << " = " << getTsType(member[i]->getTypePtr()) << " = "
<< getDefault(member[i], member[i]->def(), sNamespace) << ";" << endl; << getDefault(member[i], member[i]->def(), sNamespace, true, true) << ";" << endl;
} }
if (member.size() > 0) if (member.size() > 0)
{ {
@ -133,7 +133,7 @@ string CodeGenerator::generateTS(const StructPtr &pPtr, const string &sNamespace
if (isSimple(member[i]->getTypePtr())) if (isSimple(member[i]->getTypePtr()))
{ {
s << getDefault(member[i], member[i]->def(), sNamespace) s << getDefault(member[i], member[i]->def(), sNamespace, true, true)
<< (isRawOrString(member[i]->getTypePtr()) ? ", 1" : ""); << (isRawOrString(member[i]->getTypePtr()) ? ", 1" : "");
} }
else else

View File

@ -323,12 +323,7 @@ bool CodeGenerator::isBinBuffer(const TypePtr & pPtr) const
return false; return false;
} }
string CodeGenerator::getDefault(const TypeIdPtr & pPtr, const string &sDefault, const string& sNamespace) string CodeGenerator::getDefault(const TypeIdPtr & pPtr, const string &sDefault, const string& sNamespace, const bool &bGlobal, const bool &bCastEnumAsAny)
{
return getDefault(pPtr, sDefault, sNamespace, true);
}
string CodeGenerator::getDefault(const TypeIdPtr & pPtr, const string &sDefault, const string& sNamespace, const bool bGlobal)
{ {
BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr->getTypePtr()); BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr->getTypePtr());
if (bPtr) if (bPtr)
@ -438,7 +433,7 @@ string CodeGenerator::getDefault(const TypeIdPtr & pPtr, const string &sDefault,
if (bGlobal) if (bGlobal)
{ {
return "new " + getDataType(pPtr->getTypePtr(), true); return "new " + getDataType(pPtr->getTypePtr(), bCastEnumAsAny);
} }
return sDefault; return sDefault;