public JsProperty Convert() { var propName = PropertyNameConverter.GetPropertyName(propertyInfo); var propValue = originalValue; var jsProp = new JsProperty { Name = propName, OriginalValue = propValue, PropertyInfo = propertyInfo }; //Nested complex type which should be instantiated through an import if (propValue != null && !propertyInfo.PropertyType.IsEnum && !propertyInfo.PropertyType.IsGenericType && !excludedNamespaces.Contains(propertyInfo.PropertyType.Namespace) && includedNamespaces.Any(a => propertyInfo.PropertyType.Namespace.Contains(a))) { jsProp.Value = $"new {propertyInfo.PropertyType.Name}()"; jsProp.PropertyType = JsPropertyType.Instance; } else { jsProp.Value = JsonConvert.SerializeObject(propValue, Formatting.None, SerializerSettings); jsProp.PropertyType = JsPropertyType.Plain; } return(jsProp); }
public override object VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration, object data) { Contract.Requires(propertyDeclaration != null); propertyDeclaration.AcceptChildren(this, data); if (propertyDeclaration.Modifier != Modifiers.Private) { var jsProperty = new JsProperty() { Name = propertyDeclaration.Name, IsArray = (propertyDeclaration.TypeReference.GenericTypes.Count > 0 || propertyDeclaration.TypeReference.IsArrayType) }; CurrentParent.Properties.Add(jsProperty); if (propertyDeclaration.Attributes.Count > 0) { var jsAttributes = new List <JsAttribute>(); foreach (var attribute in propertyDeclaration.Attributes) { var a = attribute.Attributes[0]; jsAttributes.Add(new JsAttribute() { Name = a.Name }); } jsProperty.Attributes = jsAttributes; } } return(null); }
public void SetProperty(string propertyName, JsValue value) { if (!_properties.TryGetValue(propertyName, out JsProperty prop)) { prop = new JsProperty(this); _properties.Add(propertyName, prop); } prop.Value = value; }
public void JsPropertyToString() { var value = "value"; var name = "name"; var jsProperty = new JsProperty { Value = value, Name = name }; var expected = $"this.{name} = {value}"; var asString = jsProperty.ToString(); Assert.Equal(expected, asString); }
public void DefaultPropertyWriter() { var propertyWriter = new JsPropertyWriter(); var value = "value"; var name = "name"; var property = new JsProperty { Value = value, Name = name }; var expected = $"this.{name} = {value}"; var result = propertyWriter.Write(property); Assert.Equal(expected, result); }
public override object VisitFieldDeclaration(FieldDeclaration fieldsDeclaration, object data) { Contract.Requires(fieldsDeclaration != null); fieldsDeclaration.AcceptChildren(this, data); if (fieldsDeclaration.Attributes.Any(s => s.Attributes.Any(a => a.Name == "DataMember"))) { foreach (var fieldDeclaration in fieldsDeclaration.Fields) { var jsProperty = new JsProperty() { OfType = fieldsDeclaration.TypeReference.GenericTypes.Count == 0 ? fieldsDeclaration.TypeReference.Type : fieldsDeclaration.TypeReference.GenericTypes[0].Type, Name = fieldDeclaration.Name, IsArray = (fieldsDeclaration.TypeReference.GenericTypes.Count > 0 || fieldsDeclaration.TypeReference.IsArrayType) }; CurrentParent.Properties.Add(jsProperty); // I can't figure out what this part is for, but I'm preserving it anyway if (fieldsDeclaration.Attributes.Count > 0) { var jsAttributes = new List <JsAttribute>(); foreach (var attribute in fieldsDeclaration.Attributes) { var a = attribute.Attributes[0]; jsAttributes.Add(new JsAttribute() { Name = a.Name }); } jsProperty.Attributes = jsAttributes; } } } return(null); }
public JsProperty CreateProperty(JsFormat format, string name, object value) { JsProperty p = new JsProperty(this, name, value); p.SetFormat(format); return p; }
/// <summary> /// Equalses the specified other. /// </summary> /// <param name="other">The other.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> protected bool Equals(JsProperty other) { return(Equals(Key, other.Key) && Equals(Value, other.Value) && Shorthand == other.Shorthand); }
public string Write(JsProperty jsProperty) => jsProperty.ToString();
private JsObject GeneratePrototype(Type type) { var ret = new JsObject(GlobalScope); var methods = type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance).Where(m => m.GetCustomAttribute(typeof(JsMethodAttribute)) != null); var properties = type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance).Where(m => m.GetCustomAttribute(typeof(JsPropertyAttribute)) != null); if (type == typeof(JsObject)) { ret.JsProto = JsNull.Instance; } else { ret.JsProto = GetPrototype(type.BaseType); } foreach (var method in methods) { var methodProps = method.GetCustomAttribute <JsMethodAttribute>(); var parameters = method.GetParameters(); if (method.ReturnType != typeof(JsValue)) { throw new JsInternalException("Method of native javascript type must have JsValue return type"); } if (parameters.Length != 1 || parameters[0].ParameterType != typeof(LexicalEnvironment)) { throw new JsInternalException("Method of native javascript type must have one input paramter with LexicalEnvironment type"); } var jsFunc = new JsNativeFunction(CreateMethodDelegate(method), methodProps.Name, GlobalScope); SetProperty(methodProps.Name, jsFunc); } foreach (var property in properties) { var propProps = property.GetCustomAttribute <JsPropertyAttribute>(); var setter = property.SetMethod; var getter = property.GetMethod; var jsProperty = new JsProperty(ret); if (property.PropertyType != typeof(JsValue)) { throw new JsInternalException("Javascript native property type must be JsValue"); } if (propProps.IsInline) { jsProperty.RawValue = (JsValue)getter.Invoke(ret, null); } else { if (getter != null) { jsProperty.Getter = new JsNativeFunction(CreateGetterDelegate(getter), "", GlobalScope); } if (setter != null) { jsProperty.Setter = new JsNativeFunction(CreateSetterDelegate(setter), "", GlobalScope); } } ret._properties.Add(propProps.Name, jsProperty); } return(ret); }
public static JsProperty PropertyInline(string name, object value) { JsProperty p = new JsProperty(name, value); p.MultiLine = false; return p; }