public ChangeAST MakeRelativeImports(int maxNumberOfRelativeLevels = Int32.MaxValue) { ChangeAST change = new ChangeAST(); foreach (var import in Imports.Where(i => i.IsLocalImport)) { import.MakeRelativeImports(change, maxNumberOfRelativeLevels); } return(change); }
public ChangeAST MakeAbsoluteImports(bool skipSelfAndSubdirectories = false) { ChangeAST change = new ChangeAST(); foreach (var import in Imports.Where(i => i.IsLocalImport)) { import.MakeAbsoluteImport(change, skipSelfAndSubdirectories); } return(change); }
public void MakeRelativeImports(ChangeAST change, int maxNumberOfRelativeLevels) { try { if (Regex.Matches(RelativeImportPath, @"\.\.").Count <= maxNumberOfRelativeLevels) { change.ChangeNode(_pathNode, $" '{RelativeImportPath}'"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public ChangeAST AddImports(ChangeAST change, IEnumerable <ImportedModule> imports) { var groupedByPath = imports.GroupBy(i => i.Path); foreach (var byPath in groupedByPath) { var existingImportsForPath = Imports.SingleOrDefault(i => i.FilePath == byPath.Key); if (existingImportsForPath != null) { var newImports = byPath.Where(imp => !existingImportsForPath.ImportedModules.Any(i => i.Name.Equals(imp.Name))); existingImportsForPath.Add(newImports); change.ChangeNode(existingImportsForPath.ImportDeclaration, existingImportsForPath.Serialize()); } } return(change); }
public ChangeAST RemoveImports(ChangeAST change, IEnumerable <ImportedModule> imports) { var groupedByPath = imports.GroupBy(i => i.Path); foreach (var byPath in groupedByPath) { var existingImportsForPath = Imports.SingleOrDefault(i => i.FilePath == byPath.Key); if (existingImportsForPath != null) { existingImportsForPath.Remove(imports); change.Delete(existingImportsForPath.ImportDeclaration); change.ChangeNode(existingImportsForPath.ImportDeclaration, existingImportsForPath.Serialize()); } } return(change); }
private void Button_Click_7(object sender, RoutedEventArgs e) { _nodeChangeItems.Clear(); _currentChangeAst = null; _currentAst = new TypeScriptAST(); _currentSource = tbSource.Text; _currentSourceFileName = tbFileName.Text; if (string.IsNullOrWhiteSpace(_currentSourceFileName)) { _currentAst.MakeAST(_currentSource); } else { _currentAst.MakeAST(_currentSource, _currentSourceFileName); } tbTreeString.Text = _currentAst.GetTreeString(); lbNodes.ItemsSource = _currentAst.GetDescendants(); }
private void Button_Click_2(object sender, RoutedEventArgs e) { var node = lbNodes.SelectedItem as Node; if (node != null) { try { if (_currentChangeAst == null) { _currentChangeAst = new ChangeAST(_nodeChangeItems); } _currentChangeAst.InsertBefore(node, tbNodeText.Text); } catch (Exception exception) { MessageBox.Show(exception.ToString()); } } }
public void MakeAbsoluteImport(ChangeAST change, bool skipSelfAndSubdirectories) { try { if (skipSelfAndSubdirectories && RelativeImportPath.StartsWith("./") && !RelativeImportPath.Contains("..")) { return; } if (skipSelfAndSubdirectories && !RelativeImportPath.Contains("../")) { return; } change.ChangeNode(_pathNode, $" '{GetAbsoluteImport()}'"); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private void Button_Click_8(object sender, RoutedEventArgs e) { var fileName = "parser.ts"; if (!File.Exists(fileName)) { var openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() == true) { fileName = openFileDialog.FileName; } else { return; } } var source = File.ReadAllText(fileName); var ast = new TypeScriptAST(source, fileName); var change = new ChangeAST(); foreach (var module in ast.GetDescendants().OfType <ModuleDeclaration>()) { var funcs = module.Body.Children.OfType <FunctionDeclaration>().ToList(); var enums = module.Body.Children.OfType <EnumDeclaration>(); var moduleInfoFunc = $@" export function getModuleInfo() {{ return ""Module {module.IdentifierStr} contains {funcs.Count()} functions ({ funcs.Count(v => v.IdentifierStr.StartsWith("parse")) } starts with parse), {enums.Count()} enums ...""; }} "; change.InsertBefore(module.Body.Children.First(), moduleInfoFunc); } var newSource = change.GetChangedSource(ast.SourceStr); tbExample2Res.Text = newSource; File.WriteAllText("parser2.ts", newSource); }
public ChangeAST AddRemoveImports(ChangeAST change, IEnumerable <ImportedModule> toAdd, IEnumerable <ImportedModule> removedUsagesImports) { List <Imports> toChange = new List <Imports>(); var allImports = new List <ImportedModule>(); allImports.AddRange(toAdd.Select(n => new ImportedModule(n.Name, n.IsLocalImport ? n.Path.ToRelativeImportPath(this.FileName) : n.Path))); allImports.AddRange(removedUsagesImports); foreach (var byPath in allImports.GroupBy(i => i.Path)) { var existingImportsForPath = Imports.SingleOrDefault(i => i.FilePath == byPath.Key && !i.ImportedModules.Any(m => m.IsNamespaceImport)); if (existingImportsForPath != null) { var newImports = toAdd.Where(imp => !existingImportsForPath.ImportedModules.Any(i => i.Name.Equals(imp.Name))); var removedImports = removedUsagesImports.Where(imp => existingImportsForPath.ImportedModules.Any(i => i.Name.Equals(imp.Name))); existingImportsForPath.Add(newImports); existingImportsForPath.Remove(removedImports); toChange.Add(existingImportsForPath); } else { toChange.Add(new Imports(this.FileName, this.Root, byPath.ToArray())); } } var lastImport = Imports.Last(i => i.ImportDeclaration != null); foreach (var import in toChange.Where(i => i.ImportDeclaration != null)) { change.ChangeNode(import.ImportDeclaration, import.Serialize()); } foreach (var import in toChange.Where(i => i.ImportDeclaration == null)) { change.InsertAfter(lastImport.ImportDeclaration, $"\n{import.Serialize()}"); } return(change); }
public Change(TypescriptCompilation compilation) { Compilation = compilation; ChangeAst = new ChangeAST(); }