/// <summary> /// /// </summary> /// <param name="type"></param> /// <param name="name"></param> /// <returns></returns> public static ITranslationUnit BuildVariableDeclarationTranslationUnit(string type, string name, ITranslationUnit expression = null) { return(VariableDeclarationTranslationUnit.Create( type == null ? null : TypeIdentifierTranslationUnit.Create(type), IdentifierTranslationUnit.Create(name), expression)); }
public static ITranslationUnit BuildCastExpressionTranslationUnit(CastExpressionSyntax castExpression, SemanticModel semanticModel) { var helper = new CastExpression(castExpression, semanticModel); return(CastExpressionTranslationUnit.Create( TypeIdentifierTranslationUnit.Create(helper.Type.MapType()), new ExpressionTranslationUnitBuilder(helper.Expression, semanticModel).Build())); }
/// <summary> /// /// </summary> /// <param name="modifiers"></param> /// <param name="returnType"></param> /// <param name="name"></param> /// <returns></returns> public static ITranslationUnit BuildMethodSignatureTranslationUnit(ModifierTokens modifiers, string returnType, string name) { if (name == null) { throw new ArgumentNullException(nameof(name)); } return(MethodSignatureDeclarationTranslationUnit.Create( modifiers, returnType == null ? null : TypeIdentifierTranslationUnit.Create(returnType), IdentifierTranslationUnit.Create(name))); }
/// <summary> /// /// </summary> /// <param name="visibility"></param> /// <param name="type"></param> /// <param name="name"></param> /// <returns></returns> public static ITranslationUnit BuildMemberTranslationUnit(ModifierTokens modifiers, string type, string name) { if (name == null) { throw new ArgumentNullException(nameof(name)); } if (type == null) { throw new ArgumentNullException(nameof(type)); } return(FieldDeclarationTranslationUnit.Create( modifiers, TypeIdentifierTranslationUnit.Create(type), IdentifierTranslationUnit.Create(name))); }
public void MethodAddsStatement() { var translationUnit = MethodDeclarationTranslationUnit.Create( VisibilityToken.None, null, IdentifierTranslationUnit.Create("Method1"));; TestInitialNestingLevel(translationUnit); var nestedTranslationUnit = VariableDeclarationTranslationUnit.Create( TypeIdentifierTranslationUnit.Create("int"), IdentifierTranslationUnit.Create("var1")); translationUnit.AddStatement(nestedTranslationUnit); TestNestingLevels(translationUnit, nestedTranslationUnit); }
/// <summary> /// /// </summary> /// <param name="node"></param> /// <remarks> /// We don't need to go further deeper in the tree by visiting the node. /// </remarks> public override void VisitMethodDeclaration(MethodDeclarationSyntax node) { // TODO: Use translation unit factories var helper = new MethodDeclaration(node, this.semanticModel); // Properties in TypeScript will be translated as methods as // TypeScript does not support properties in interfaces var translationUnit = MethodSignatureDeclarationTranslationUnit.Create( helper.Visibility, TypeIdentifierTranslationUnit.Create(helper.ReturnType.Name), IdentifierTranslationUnit.Create(helper.Name)); this.interfaceDeclaration.AddSignature(translationUnit); this.InvokePropertyDeclarationVisited(this, new WalkerEventArgs()); }
private static ITranslationUnit BuildObjectCreationExpressionTranslationUnit(ObjectCreationExpressionSyntax expression, SemanticModel semanticModel) { ObjectCreationExpression helper = new ObjectCreationExpression(expression, semanticModel); var translationUnit = ObjectCreationExpressionTranslationUnit.Create( TypeIdentifierTranslationUnit.Create(helper.Type.FullName.MapType().MappedType.Translate())); // TODO: Create factory for TypeReference foreach (var argument in helper.Arguments) { var argumentTranslationUnit = new ExpressionTranslationUnitBuilder(argument.Expression, semanticModel).Build(); translationUnit.AddArgument(argumentTranslationUnit); } return(translationUnit); }
/// <summary> /// Creates a <see cref="MethodDeclarationTranslationUnit"/>. /// </summary> /// <returns>A <see cref="MethodDeclarationTranslationUnit"/>.</returns> public ITranslationUnit Create() { if (this.DoNotCreateTranslationUnit) { return(null); } var helper = this.CreateHelper(this.Node as FieldDeclarationSyntax, this.SemanticModel); var fieldDeclaration = this.CreateTranslationUnit( helper.Visibility, TypeIdentifierTranslationUnit.Create(helper.Type.FullName.MapType()), IdentifierTranslationUnit.Create(helper.Name)); return(fieldDeclaration); }
/// <summary> /// Creates a <see cref="PropertyDeclarationTranslationUnitFactory"/>. /// </summary> /// <returns>A <see cref="PropertyDeclarationTranslationUnitFactory"/>.</returns> public ITranslationUnit Create() { if (this.DoNotCreateTranslationUnit) { return(null); } PropertyDeclaration helper = this.CreateHelper(this.Node as PropertyDeclarationSyntax, this.SemanticModel); var propertyDeclaration = this.CreateTranslationUnit( helper.Visibility, TypeIdentifierTranslationUnit.Create(helper.Type.FullName.MapType()), IdentifierTranslationUnit.Create(helper.Name), helper.HasGet, helper.HasSet); return(propertyDeclaration); }
/// <summary> /// Creates a <see cref="ConstructorDeclarationTranslationUnitFactory"/>. /// </summary> /// <returns>A <see cref="ConstructorDeclarationTranslationUnitFactory"/>.</returns> public ITranslationUnit Create() { if (this.DoNotCreateTranslationUnit) { return(null); } ConstructorDeclaration helper = this.CreateHelper(this.Node as ConstructorDeclarationSyntax, this.SemanticModel); var constructorDeclaration = this.CreateTranslationUnit(helper.Visibility) as MethodSignatureDeclarationTranslationUnit; foreach (Parameter parameter in helper.Parameters) { constructorDeclaration.AddArgument(ArgumentDefinitionTranslationUnit.Create( TypeIdentifierTranslationUnit.Create(parameter.Type.FullName.MapType()), IdentifierTranslationUnit.Create(parameter.IdentifierName))); } return(constructorDeclaration); }
/// <summary> /// Factory method for class <see cref="LocalDeclarationStatementASTWalker"/>. /// </summary> /// <param name="node"><see cref="CSharpSyntaxNode"/> Used to initialize the walker.</param> /// <param name="semanticModel">The semantic model.</param> /// <returns></returns> public static LocalDeclarationStatementASTWalker Create(CSharpSyntaxNode node, SemanticModel semanticModel = null) { // TODO: Use TranslationUnitFactory in order to have AST walkers decoupled from helpers // via factories (which will be using helpers) var variableDeclaration = new VariableDeclaration((node as LocalDeclarationStatementSyntax).Declaration, semanticModel); ExpressionSyntax expression = variableDeclaration.Expressions[0]; // This can contain null, so need to act accordingly ITranslationUnit expressionTranslationUnit = expression == null ? null : new ExpressionTranslationUnitBuilder(expression, semanticModel).Build(); var variableDeclarationTranslationUnit = VariableDeclarationTranslationUnit.Create( TypeIdentifierTranslationUnit.Create(variableDeclaration.Type.Name), IdentifierTranslationUnit.Create(variableDeclaration.Names[0]), expressionTranslationUnit); var statement = LocalDeclarationStatementTranslationUnit.Create(variableDeclarationTranslationUnit); return(new LocalDeclarationStatementASTWalker(node, statement, semanticModel)); }
/// <summary> /// /// </summary> /// <param name="modifiers"></param> /// <param name="returnType"></param> /// <param name="name"></param> /// <param name="statements"></param> /// <returns></returns> public static ITranslationUnit BuildMethodTranslationUnit(ModifierTokens modifiers, string returnType, string name, ITranslationUnit[] statements = null) { if (name == null) { throw new ArgumentNullException(nameof(name)); } MethodDeclarationTranslationUnit translationUnit = MethodDeclarationTranslationUnit.Create( modifiers, returnType == null ? null : TypeIdentifierTranslationUnit.Create(returnType), IdentifierTranslationUnit.Create(name)); if (statements != null) { foreach (ITranslationUnit statement in statements) { translationUnit.AddStatement(statement); } } return(translationUnit); }
/// <summary> /// Creates a <see cref="MethodDeclarationTranslationUnit"/>. /// </summary> /// <returns>A <see cref="MethodDeclarationTranslationUnit"/>.</returns> public ITranslationUnit Create() { if (this.DoNotCreateTranslationUnit) { return(null); } AnonymousMethodDeclaration helper = new AnonymousMethodDeclaration(this.Node as AnonymousMethodExpressionSyntax, this.SemanticModel); var lambdaDeclaration = LambdaTranslationUnit.Create(null); //var lambdaDeclaration = LambdaTranslationUnit.Create(TypeIdentifierTranslationUnit.Create(helper.ReturnType.FullName.MapType())); foreach (Parameter parameter in helper.Parameters) { lambdaDeclaration.AddArgument(ArgumentDefinitionTranslationUnit.Create( TypeIdentifierTranslationUnit.Create(parameter.Type.TypeSyntaxNode.MapType()), IdentifierTranslationUnit.Create(parameter.IdentifierName))); } return(lambdaDeclaration); }
/// <summary> /// Creates a <see cref="MethodDeclarationTranslationUnit"/>. /// </summary> /// <returns>A <see cref="MethodDeclarationTranslationUnit"/>.</returns> public ITranslationUnit Create() { if (this.DoNotCreateTranslationUnit) { return(null); } var helper = this.CreateHelper(this.Node as FieldDeclarationSyntax, this.SemanticModel); ITranslationUnit fieldDeclaration; ExpressionSyntax expression = helper.Expressions[0]; // This can contain null, so need to act accordingly ITranslationUnit expressionTranslationUnit = expression == null ? null : new ExpressionTranslationUnitBuilder(expression, this.SemanticModel).Build(); fieldDeclaration = this.CreateTranslationUnit( helper.Visibility, TypeIdentifierTranslationUnit.Create(helper.Type.TypeSyntaxNode.MapType()), IdentifierTranslationUnit.Create(helper.Name), expressionTranslationUnit); return(fieldDeclaration); }
/// <summary> /// Maps a type into another. /// </summary> /// <param name="originalType">The original type.</param> /// <returns>The new type, or the original type if no mapping is possible.</returns> public static MappingResult MapType(this string originalType) { if (IsDictionary(originalType)) { return(new MappingResult() { MappedType = StringDictionaryTypeTranslationUnit.Create(), MappingApplied = true }); } if (IsArrayList(originalType)) { return(new MappingResult() { MappedType = AnyArrayTypeTranslationUnit.Create(), MappingApplied = true }); } // No type could be found return(new MappingResult() { MappedType = TypeIdentifierTranslationUnit.Create(originalType), MappingApplied = false }); }
/// <summary> /// /// </summary> /// <param name="visibility"></param> /// <param name="returnType"></param> /// <param name="name"></param> /// <param name="getStatements"></param> /// <param name="setStatements"></param> /// <returns></returns> public static ITranslationUnit BuildPropertyTranslationUnit(ModifierTokens modifiers, string returnType, string name, ITranslationUnit[] getStatements = null, ITranslationUnit[] setStatements = null) { if (name == null) { throw new ArgumentNullException(nameof(name)); } if (returnType == null) { throw new ArgumentNullException(nameof(returnType)); } PropertyDeclarationTranslationUnit translationUnit = PropertyDeclarationTranslationUnit.Create( modifiers, TypeIdentifierTranslationUnit.Create(returnType), IdentifierTranslationUnit.Create(name), true, true); if (getStatements != null) { var statementsGroup = StatementsGroupTranslationUnit.Create(); foreach (ITranslationUnit statement in getStatements) { statementsGroup.AddStatement(statement); } translationUnit.SetGetAccessor(statementsGroup); } if (setStatements != null) { var statementsGroup = StatementsGroupTranslationUnit.Create(); foreach (ITranslationUnit statement in setStatements) { statementsGroup.AddStatement(statement); } translationUnit.SetSetAccessor(statementsGroup); } return(translationUnit); }
/// <summary> /// / /// </summary> /// <param name="type"></param> /// <param name="variableName"></param> /// <returns></returns> public static ITranslationUnit BuildExpressionTranslationUnit(string type, string variableName) { return(CastExpressionTranslationUnit.Create( TypeIdentifierTranslationUnit.Create(type), IdentifierTranslationUnit.Create(variableName))); }
/// <summary> /// /// </summary> /// <param name="translationUnit"></param> /// <param name="returnType"></param> /// <param name="name"></param> /// <returns></returns> public static MethodDeclarationTranslationUnit AddMethod(this ClassDeclarationTranslationUnit translationUnit, string returnType, string name) { var methodTranslationUnit = MethodDeclarationTranslationUnit.Create(VisibilityToken.Public, returnType != null ? TypeIdentifierTranslationUnit.Create(returnType) : null, IdentifierTranslationUnit.Create(name)); translationUnit.AddMethodDeclaration(methodTranslationUnit); return(methodTranslationUnit); }
/// <summary> /// Maps a type into another. /// </summary> /// <param name="originalType">The original type (fully qualified name).</param> /// <returns>The new type, or the original type if no mapping is possible.</returns> public static MappingResult MapType(this string originalType) { if (IsVoid(originalType)) { return(new MappingResult() { MappedType = VoidTypeTranslationUnit.Create(), MappingApplied = true }); } if (IsString(originalType)) { return(new MappingResult() { MappedType = StringTypeTranslationUnit.Create(), MappingApplied = true }); } if (IsInt(originalType)) { return(new MappingResult() { MappedType = NumberTypeTranslationUnit.Create(), MappingApplied = true }); } if (IsDouble(originalType)) { return(new MappingResult() { MappedType = NumberTypeTranslationUnit.Create(), MappingApplied = true }); } if (IsFloat(originalType)) { return(new MappingResult() { MappedType = NumberTypeTranslationUnit.Create(), MappingApplied = true }); } if (IsBool(originalType)) { return(new MappingResult() { MappedType = BooleanTypeTranslationUnit.Create(), MappingApplied = true }); } if (IsObject(originalType)) { return(new MappingResult() { MappedType = AnyTypeTranslationUnit.Create(), MappingApplied = true }); } // No type could be found return(new MappingResult() { MappedType = TypeIdentifierTranslationUnit.Create(originalType), MappingApplied = false }); }