/// <summary> /// 呼び出し式構文のTypeScript変換 /// </summary> /// <param name="condition">ExpressionSyntaxインスタンス</param> /// <param name="localDeclarationStatements">ローカル変数リスト</param> /// <returns>TypeScriptに変換した文字列</returns> private string ConvertExpression(InvocationExpressionSyntax condition, List <string> localDeclarationStatements) { var result = condition.ToString(); var memberAccessExpression = condition.Expression as MemberAccessExpressionSyntax; if (condition.Expression is MemberAccessExpressionSyntax || condition.Expression is InvocationExpressionSyntax) { if (memberAccessExpression.Expression is ThisExpressionSyntax) { result = $"{GetExpression(memberAccessExpression.Name, localDeclarationStatements)}("; } else { result = $"{GetExpression(memberAccessExpression, localDeclarationStatements)}("; } } else { result = $"{GetExpression(condition.Expression, localDeclarationStatements)}("; } // パラメータ設定 var argsText = condition.ArgumentList.Arguments.Select(arg => GetExpression(arg.Expression, localDeclarationStatements)); result += string.Join(", ", argsText); result += ")"; // メソッドをTypeScript用に置換え result = AnalyzeUtility.ReplaceMethodName(result); // thisをつけるかの判定 if (!IsLocalDeclarationStatement(condition, localDeclarationStatements)) { if (!result.StartsWith("this.", StringComparison.CurrentCulture)) { return("this." + result); } } return(result); }
/// <summary> /// メンバーアクセス式構文のTypeScript変換 /// </summary> /// <param name="condition">ExpressionSyntaxインスタンス</param> /// <param name="localDeclarationStatements">ローカル変数リスト</param> /// <returns>TypeScriptに変換した文字列</returns> private string ConvertExpression(MemberAccessExpressionSyntax condition, List <string> localDeclarationStatements) { var result = condition.ToString(); if (condition.Expression is IdentifierNameSyntax == false) { result = $"{GetExpression(condition.Expression, localDeclarationStatements)}{condition.OperatorToken}"; result += condition.Name; } // メソッドをTypeScript用に置換え result = AnalyzeUtility.ReplaceMethodName(result); // thisをつけるかの判定 var targetCondition = condition as ExpressionSyntax; if (condition.Expression is ElementAccessExpressionSyntax eaes) { targetCondition = eaes.Expression; } if (!IsLocalDeclarationStatement(targetCondition, localDeclarationStatements)) { if (!result.StartsWith("this.", StringComparison.CurrentCulture)) { return("this." + result); } } // 内部クラスのクラス名変更 if (condition.Expression is IdentifierNameSyntax identifier) { var identifierName = identifier.Identifier.ValueText; // ローカルフィールドの場合はそのまま返す if (localDeclarationStatements.Contains(identifierName)) { return(result); } var classObject = ClassObject.GetInstance(); // 別クラスの場合はクラス名置換えを実施 var otherClassNames = new List <string>() { identifierName, condition.ToString(), $"{classObject.ProcessClassName}.{identifierName}" }; foreach (var className in otherClassNames) { if (classObject.RenameClasseNames.Keys.Contains(className)) { result = classObject.RenameClasseNames[className]; if (className.StartsWith($"{classObject.ProcessClassName}.", StringComparison.CurrentCulture) && condition.Name.ToString() != identifierName && classObject.ProcessClassName != identifierName) { result += $".{condition.Name}"; } return(result); } } // クラスメンバの場合はそのまま返す var myMethodNames = new List <string>() { identifierName, condition.Name.ToString() }; foreach (var methodName in myMethodNames) { if (classObject.StaticMembers.Where(item => item.Key == classObject.ProcessClassName). Any(item => item.Value.Contains(methodName))) { return(result); } } } return(result); }
/// <summary> /// メソッド名単位のTSコード作成 /// </summary> /// <param name="methodName">メソッド名</param> /// <param name="methodDataList">メソッド名が同じメソッドコードリスト</param> /// <returns></returns> private string CreateMethodText(string methodName, List <MethodData> methodDataList) { var result = new StringBuilder(); var paramList = new List <HashSet <string> >(); var spaceIndex = " "; if (methodDataList.Count <= 1) { // メソッドが重複しない場合はそのまま出力 var item = methodDataList.FirstOrDefault(); if (item == null) { return(string.Empty); } // メソッド作成 result.Append(CreateMethodText(methodName, spaceIndex, item)); } else { // 複数メソッド名が重複している場合 // 初期化 var paramCount = methodDataList.Max(item => item.PramCount); var paramLists = new List <List <string> >(); for (var index = 0; index < paramCount; index++) { var dataList = new List <string>(); for (var dataIndex = 0; dataIndex < methodDataList.Count; dataIndex++) { dataList.Add("null"); } paramLists.Add(dataList); } // 仮メソッドを作成 var retunValue = string.Empty; var summaryMethod = new StringBuilder(); var dataListIndex = 0; foreach (var methodData in methodDataList) { var paramIndex = 0; foreach (var paramData in methodData.ParamList) { paramLists[paramIndex][dataListIndex] = paramData.LocalDeclaration; paramIndex++; } // 仮メソッド作成 var tempMethodName = $"{methodName}{dataListIndex}"; methodData.Scope = "private"; result.Append(CreateMethodText(tempMethodName, spaceIndex, methodData)); retunValue = methodData.ReturnValue; // 集約メソッド用処理を追加 summaryMethod.Append($"{spaceIndex}{spaceIndex}"); summaryMethod.Append("if ("); for (paramIndex = 0; paramIndex < paramCount; paramIndex++) { var paramType = paramLists[paramIndex][dataListIndex]; if (paramType == paramType.ToLower(CultureInfo.CurrentCulture)) { if (paramType == "null") { summaryMethod.Append($"p{paramIndex} === null"); } else { summaryMethod.Append($"typeof p{paramIndex} === '{paramType}'"); } } else { summaryMethod.Append($"p{paramIndex} instanceof {paramType}"); } if (paramCount > 1 && paramIndex < paramCount - 1) { summaryMethod.Append(" && "); } } summaryMethod.AppendLine(") {"); summaryMethod.Append($"{spaceIndex}{spaceIndex}"); summaryMethod.Append($"{spaceIndex}"); if (!string.IsNullOrEmpty(methodData.ReturnValue) && methodData.ReturnValue != "void") { summaryMethod.Append("return "); } summaryMethod.Append($"this.{tempMethodName}("); var tempIndex = 0; while (tempIndex < methodData.PramCount) { if (tempIndex > 0) { summaryMethod.Append(", "); } summaryMethod.Append($"p{tempIndex}"); tempIndex++; } summaryMethod.AppendLine(");"); summaryMethod.AppendLine($"{spaceIndex}{spaceIndex}" + "}"); dataListIndex++; } // 集約メソッド用パラメータの作成 var typeScriptComments = new List <string>(); var summaryParamIndex = 0; while (summaryParamIndex < paramCount) { typeScriptComments.Add(GetParam($"p{summaryParamIndex}", paramLists[summaryParamIndex])); summaryParamIndex++; } // 集約メソッド用コメントの作成 var summaryComments = new StringBuilder(); summaryComments.AppendLine($"/// <summary>{methodName} Summary Method</summary>"); foreach (var summaryComment in typeScriptComments) { var tsComment = summaryComment.Split(": "); var commentBody = tsComment[1].Replace(" | ", " or ", System.StringComparison.CurrentCulture); summaryComments.AppendLine($"/// <param name=\"{tsComment[0]}\">{commentBody}</param>"); } result.Append(AnalyzeUtility.GetComments(summaryComments.ToString())); // 集約メソッド作成 result.Append($"{spaceIndex}public {methodName}("); result.Append(string.Join(", ", typeScriptComments)); result.Append(")"); if (!string.IsNullOrEmpty(retunValue) && retunValue != "void") { result.Append($": {retunValue}"); } result.AppendLine(" {"); // スーパークラスのコンストラクタを設定 var superMethodArgCount = methodDataList.Max(item => item.BaseArgCount); if (superMethodArgCount >= 0) { result.Append($"{spaceIndex}{spaceIndex}super("); summaryParamIndex = 0; while (summaryParamIndex < superMethodArgCount) { if (summaryParamIndex > 0) { result.Append(", "); } result.Append($"p{summaryParamIndex}"); summaryParamIndex++; } result.AppendLine(");"); } result.Append(summaryMethod.ToString()); result.Append(GetDefaultReturnValue(spaceIndex, retunValue)); result.AppendLine($"{spaceIndex}" + "}"); } return(result.ToString()); }