public void DefaultImport() { var test = new StaticDependency { ImportPath = "mobx", DefaultExport = "Mobx" }; Assert.Equal("import Mobx from 'mobx';", test.ToString()); }
public void StarAsImport() { var test = new StaticDependency { ImportPath = "mobx", DefaultExport = "Mobx", UseStarAs = true }; Assert.Equal("import * as Mobx from 'mobx';", test.ToString()); }
public void SingleImport() { var test = new StaticDependency { ImportPath = "mobx", Exports = { "observable" } }; Assert.Equal("import { observable } from 'mobx';", test.ToString()); }
public void TwoImports() { var test = new StaticDependency { ImportPath = "mobx", Exports = { "observable", "decorate" } }; Assert.Equal("import { observable, decorate } from 'mobx';", test.ToString()); }
public void DualImport_Default() { var test = new StaticDependency { ImportPath = "react", DefaultExport = "React", Exports = { "Component" } }; Assert.Equal("import React, { Component } from 'react';", test.ToString()); }
protected void WriteDependencies(IndentedStringBuilder builder, IEnumerable <IHasDependencies> properties) { var allStaticDependencies = new List <StaticDependency>(); var hasDependencies = properties as IHasDependencies[] ?? properties.ToArray(); foreach (var staticDependency in StaticDependencies.Union(hasDependencies.SelectMany(x => x.StaticDependencies))) { var fromAll = allStaticDependencies.FirstOrDefault(s => s.ImportPath == staticDependency.ImportPath); if (fromAll == null) { //haven't seen this dependency var x = new StaticDependency { DefaultExport = staticDependency.DefaultExport, ImportPath = staticDependency.ImportPath, UseStarAs = staticDependency.UseStarAs }; x.Exports.AddRange(staticDependency.Exports); allStaticDependencies.Add(x); continue; } if (string.IsNullOrEmpty(fromAll.DefaultExport)) { if (!string.IsNullOrEmpty(staticDependency.DefaultExport)) { //we can just replace the default export since we don't have one yet fromAll.DefaultExport = staticDependency.DefaultExport; } } else { if (!string.IsNullOrEmpty(staticDependency.DefaultExport)) { allStaticDependencies.Add(new StaticDependency { DefaultExport = staticDependency.DefaultExport, UseStarAs = staticDependency.UseStarAs, ImportPath = staticDependency.ImportPath }); } } fromAll.Exports.AddRange(staticDependency.Exports.Where(e => fromAll.Exports.Contains(e))); } foreach (var staticDependency in allStaticDependencies) { builder.AppendLine(staticDependency); } foreach (var fileDependency in Dependencies.Union(hasDependencies.SelectMany(x => x.Dependencies))) { builder.AppendLine(fileDependency.Import(Directory)); } if (Dependencies.Any() || hasDependencies.Any()) { builder.AppendLine(); } }