예제 #1
0
        private static void GenerateProperty(Context context, OutputBuffer output, PropertyDeclarationSyntax property, bool asStatic)
        {
            if (property.HasDocumentation())
            {
                output.AppendLine(property.GetDocumentation(output.GetIndent()));
            }

            if (property.AccessorList == null)
            {
                throw new InvalidOperationException(CreateErrorMessage(property, $"AccessorList was expected."));
            }

            string?wrappedType = null;

            foreach (var attribute in property.AttributeLists.Select(x => x.ToString().Trim('[', ']')))
            {
                if (attribute.StartsWith("Wrap(") && attribute.EndsWith(")"))
                {
                    wrappedType = attribute.Substring("Wrap(".Length, attribute.Length - "Wrap(".Length - ")".Length);
                }
            }

            bool canRead  = property.AccessorList.Accessors.Any(x => x.Keyword.ToString() == "get");
            bool canWrite = property.AccessorList.Accessors.Any(x => x.Keyword.ToString() == "set");

            if (canRead && !canWrite) // readonly
            {
                output.Append($"private ");

                if (asStatic)
                {
                    output.Append("static ");
                }

                output.AppendLine($"{property.Type}? _{property.Identifier};");
                output.AppendLine();
            }

            output.Append($"public ");

            if (asStatic)
            {
                output.Append("static ");
            }

            output.AppendLine($"{property.Type} {property.Identifier}");
            output.AppendLine("{");

            if (canRead && canWrite)
            {
                if (wrappedType == null)
                {
                    output.AppendLine($"\tget => _js.GetObjectProperty<{property.Type}>(nameof({property.Identifier}));");
                    output.AppendLine($"\tset => _js.SetObjectProperty(nameof({property.Identifier}), value);");
                }
                else
                {
                    throw new NotImplementedException("Wrap not implemented for read / write properties.");
                }
            }
            else if (canRead && !canWrite) // readonly
            {
                output.Append($"\tget => _{property.Identifier} ?? (_{property.Identifier} = ");

                if (wrappedType == null)
                {
                    output.Append($"_js.GetObjectProperty<{property.Type}>(nameof({property.Identifier}))");
                }
                else
                {
                    output.Append($"new {property.Type}(_js.GetObjectProperty<JSObject>(nameof({property.Identifier})))");
                }

                output.AppendLine(");");
            }

            output.AppendLine("}");
            output.AppendLine();
        }