/// <summary> /// 宣言部の確認 /// </summary> /// <param name="targetInstance">対象のインスタンス</param> /// <param name="expectedList">パラメータの期待値<</param> private void CheckDeclarationsCount(IItemFor targetInstance, string expectedType, List <string> expectedList) { // 期待値とローカルフィールドの数が同じか確認 Assert.Equal(expectedList.Count, targetInstance.Declarations.Count); // 宣言部の型の確認 var actualType = string.Empty; if (targetInstance.Types.Any()) { Assert.Single(targetInstance.Types); actualType = targetInstance.Types.Single().Name; } Assert.Equal(expectedType, actualType); // ローカルフィールドの数を取得 var existsCount = 0; for (var index = 0; index < targetInstance.Declarations.Count; index++) { var expectedTargets = expectedList.Where(item => item == GetExpressionsToString(targetInstance.Declarations[index])); if (expectedTargets.Any()) { existsCount++; } } Assert.Equal(existsCount, targetInstance.Declarations.Count); }
/// <summary> /// 変換メソッド:for /// </summary> /// <param name="item">C#解析結果</param> /// <param name="config">設定情報</param> /// <param name="indent">インデント数</param> /// <param name="otherScripts">その他のスクリプト(内部クラスなど)</param> /// <returns>TypeScript変換結果</returns> private string Convert(IItemFor item, Config config, int indent, List <string> otherScripts) { var result = new StringBuilder(); var indentSpace = GetIndentSpace(indent); // コメント result.Append(GetTypeScriptComments(item, indentSpace)); // 定義 var declarations = GetDeclarations(item); var letKeyword = item.IsVar ? "let " : string.Empty; var incrementors = item.Incrementors.Select(incrementor => ExpressionsToString(incrementor)); result.AppendLine($"{indentSpace}for ({letKeyword}{string.Join(", ", declarations)}; {ExpressionsToString(item.Conditions)}; {string.Join(", ", incrementors)}) {{"); // メンバー追加 foreach (var member in item.Members) { result.Append(ConvertUtility.Convert(member, config, indent + 1, otherScripts)); } result.AppendLine($"{indentSpace}}}"); return(result.ToString()); }
/// <summary> /// 計算部の確認 /// </summary> /// <param name="targetInstance">対象のインスタンス</param> /// <param name="expectedList">パラメータの期待値<</param> private void CheckIncrementorsCount(IItemFor targetInstance, List <string> expectedList) { // 数が同じか確認 Assert.Equal(expectedList.Count, targetInstance.Incrementors.Count); // 対象を文字列に変換 var actualList = targetInstance.Incrementors.Select(item => GetExpressionsToString(item)); // 同じリストか確認 Assert.Equal(expectedList, actualList); }
/// <summary> /// ローカルフィールド群に型情報を追加 /// </summary> /// <param name="item">反復処理(for)インスタンス</param> /// <returns>ローカルフィールド情報</returns> private List <string> GetDeclarations(IItemFor item) { var result = new List <string>(); foreach (var declaration in item.Declarations) { if (!item.IsVar) { // ローカルフィールド宣言ではない場合、そのまますべてを文字列に置き換える result.Add($"{ExpressionsToString(declaration)}"); continue; } // イコールキーワードで各要素に分割 var equalKeywordIndex = declaration.FindIndex(exp => exp.Name == "="); var localField = declaration.GetRange(0, equalKeywordIndex); var valueList = declaration.GetRange(equalKeywordIndex + 1, declaration.Count - equalKeywordIndex - 1); // 型情報取得 var declarationTypes = valueList.Select(exp => exp.TypeName); var declarationType = GetTypeScriptType(declarationTypes.First()); if (declarationType == declarationTypes.First()) { declarationType = string.Empty; } else { declarationType = $": {declarationType}"; } // 再結合して式を作成 result.Add($"{ExpressionsToString(localField)}{declarationType} = {ExpressionsToString(valueList)}"); } return(result); }