Skip to content

Commit fbaba5f

Browse files
committed
Refactoring: Only one Component model now. Renaming of variables.
Detecting router outlets in component templates.
1 parent 3f02f3b commit fbaba5f

8 files changed

+65
-59
lines changed

src/commands/componentHierarchyMarkdown.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { CommandBase } from '@commands';
2-
import { Config, Component, ComponentManager, FileSystemUtils } from '@src';
2+
import { Component } from '@model';
3+
import { Config, ComponentManager, FileSystemUtils } from '@src';
34
import * as path from 'path';
45

56
export class ComponentHierarchyMarkdown extends CommandBase {

src/commands/generateDependencyInjectionGraph.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,20 @@ export class GenerateDependencyInjectionGraph extends ShowHierarchyBase {
9696
const outputs = component.outputs.map(i => i.name).join(", ");
9797
nodeContent += `\\n<b>Outputs: </b> ${outputs}`;
9898
}
99-
if (component.viewchilds.length > 0) {
100-
const viewchilds = component.viewchilds.map(i => i.name).join(", ");
99+
if (component.viewChilds.length > 0) {
100+
const viewchilds = component.viewChilds.map(i => i.name).join(", ");
101101
nodeContent += `\\n<b>Viewchilds: </b> ${viewchilds}`;
102102
}
103-
if (component.viewchildren.length > 0) {
104-
const viewchildren = component.viewchildren.map(i => i.name).join(", ");
103+
if (component.viewChildren.length > 0) {
104+
const viewchildren = component.viewChildren.map(i => i.name).join(", ");
105105
nodeContent += `\\n<b>Viewchildren: </b> ${viewchildren}`;
106106
}
107-
if (component.contentchilds.length > 0) {
108-
const contentchilds = component.contentchilds.map(i => i.name).join(", ");
107+
if (component.contentChilds.length > 0) {
108+
const contentchilds = component.contentChilds.map(i => i.name).join(", ");
109109
nodeContent += `\\n<b>Contentchilds: </b> ${contentchilds}`;
110110
}
111-
if (component.contentchildren.length > 0) {
112-
const contentchildren = component.contentchildren.map(i => i.name).join(", ");
111+
if (component.contentChildren.length > 0) {
112+
const contentchildren = component.contentChildren.map(i => i.name).join(", ");
113113
nodeContent += `\\n<b>Contentchildren: </b> ${contentchildren}`;
114114
}
115115
return nodeContent;

src/commands/modulesToMarkdown.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as vscode from 'vscode';
22
import * as path from 'path';
3-
import { ArrayUtils, Config, FileSystemUtils, ModuleManager, NgModule } from '@src';
3+
import { ArrayUtils, Config, FileSystemUtils, ModuleManager } from '@src';
44
import { CommandBase } from '@commands';
5-
import { Project } from '@model';
5+
import { NgModule, Project } from '@model';
66

77
export class ModulesToMarkdown extends CommandBase {
88
private config = new Config();

src/commands/showComponentHierarchy.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ShowHierarchyBase } from './showHierarchyBase';
2-
import { Component, ComponentManager } from '@src';
3-
import { ArrowType, Edge, GraphState, Node, NodeType } from '@model';
2+
import { ComponentManager } from '@src';
3+
import { ArrowType, Component, Edge, GraphState, Node, NodeType } from '@model';
44
import * as fs from 'fs';
55
import * as path from 'path';
66
import * as vscode from 'vscode';
@@ -99,10 +99,10 @@ export class ShowComponentHierarchy extends ShowHierarchyBase {
9999
}
100100

101101
private generateDirectedGraphNodes(components: Component[], component: Component, isRoot: boolean, parentSelector: string, appendNodes: (nodeList: Node[]) => void) {
102-
let componentFilename = component.tsFilename.replace(this.directoryPath, '.');
102+
let componentFilename = component.filename.replace(this.directoryPath, '.');
103103
componentFilename = componentFilename.split('\\').join('/');
104104
const componentPosition = this.graphState.nodePositions[component.selector];
105-
appendNodes([new Node(component.selector, component.selector, componentFilename, component.tsFilename, isRoot, isRoot ? NodeType.rootNode : NodeType.component, componentPosition)]);
105+
appendNodes([new Node(component.selector, component.selector, componentFilename, component.filename, isRoot, isRoot ? NodeType.rootNode : NodeType.component, componentPosition)]);
106106
if (components.length > 0) {
107107
components.forEach((subComponent) => {
108108
if (parentSelector !== subComponent.selector) {

src/componentManager.ts

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
import * as fs from 'fs';
22
import path = require('path');
33

4-
import { Config , FileSystemUtils } from '@src';
5-
6-
export class Component {
7-
8-
constructor(tsFilename: string, templateFilename: string, selector: string, subComponents: Component[], isRoot: boolean) {
9-
this.tsFilename = tsFilename;
10-
this.templateFilename = templateFilename;
11-
this.selector = selector;
12-
this.subComponents = subComponents;
13-
this.isRoot = isRoot;
14-
}
15-
16-
public tsFilename: string;
17-
public templateFilename: string;
18-
public selector: string;
19-
public subComponents: Component[];
20-
public isRoot: boolean;
21-
}
4+
import { Config, FileSystemUtils } from '@src';
5+
import { Component } from '@model';
226

237
export class ComponentManager {
8+
private static componentRegex = /@Component\({/ig;
9+
private static templateUrlRegex = /.*templateUrl:.+\/(.+)'/i;
10+
private static selectorRegex = /.*selector:.+'(.+)'/i;
11+
private static endBracketRegex = /}\)/i;
12+
private static routerOutletRegex = /<router-outlet.*?>.*?<\/router-outlet>/ims;
2413

2514
public static findComponents(directoryPath: string): { [selector: string]: Component; } {
2615
const fsUtils = new FileSystemUtils();
@@ -37,34 +26,30 @@ export class ComponentManager {
3726

3827
private static scanWorkspaceForComponents(componentFilenames: string[]): { [selector: string]: Component; } {
3928
const compHash: { [selector: string]: Component; } = {};
40-
const componentRegex = /@Component\({/ig;
41-
const templateUrlRegex = /.*templateUrl:.+\/(.+)'/i;
42-
const selectorRegex = /.*selector:.+'(.+)'/i;
43-
const endBracketRegex = /}\)/i;
4429
componentFilenames.forEach((componentFilename) => {
4530
let componentDefinitionFound = false;
46-
let currentComponent = new Component(componentFilename, "", "", [], true);
31+
let currentComponent = new Component('', componentFilename, "", "", [], true);
4732
const content = fs.readFileSync(componentFilename, 'utf8');
4833
const lines: string[] = content.split('\n');
4934
for (let i: number = 0; i < lines.length; i++) {
5035
let line = lines[i];
51-
let match = componentRegex.exec(line);
36+
let match = this.componentRegex.exec(line);
5237
if (match) {
5338
componentDefinitionFound = true;
5439
}
5540
if (componentDefinitionFound) {
56-
match = templateUrlRegex.exec(line);
41+
match = this.templateUrlRegex.exec(line);
5742
if (match) {
5843
currentComponent.templateFilename = path.join(path.dirname(componentFilename), match[1]);
5944
}
60-
match = selectorRegex.exec(line);
45+
match = this.selectorRegex.exec(line);
6146
if (match) {
6247
let currentSelector = match[1];
6348
currentSelector = currentSelector.replace("[", "");
6449
currentSelector = currentSelector.replace("]", "");
6550
currentComponent.selector = currentSelector;
6651
}
67-
match = endBracketRegex.exec(line);
52+
match = this.endBracketRegex.exec(line);
6853
if (match) {
6954
break;
7055
}
@@ -77,27 +62,33 @@ export class ComponentManager {
7762

7863
private static enrichComponentsFromComponentTemplates(componentHash: { [selector: string]: Component; }) {
7964
for (let selector1 in componentHash) {
80-
if (fs.existsSync(componentHash[selector1].templateFilename)) {
81-
const template = fs.readFileSync(componentHash[selector1].templateFilename); // We read the entire template file
65+
const component = componentHash[selector1];
66+
if (fs.existsSync(component.templateFilename)) {
67+
const template = fs.readFileSync(component.templateFilename); // We read the entire template file
68+
component.isRouterOutlet = this.isComponentRouterOutlet(template);
8269
for (let selector2 in componentHash) { // then we check if the template contains each of the selectors we found in the components
8370
let pattern = `</${selector2}>`;
8471
let index = template.indexOf(pattern);
8572
if (index >= 0) {
86-
componentHash[selector1].subComponents = componentHash[selector1].subComponents.concat(componentHash[selector2]);
73+
component.subComponents = component.subComponents.concat(componentHash[selector2]);
8774
// If selector2 has been found in a template then it is not root
8875
componentHash[selector2].isRoot = false;
8976
}
9077
else {
9178
pattern = ` ${selector2}`;
9279
index = template.indexOf(pattern);
9380
if (index >= 0) {
94-
componentHash[selector1].subComponents = componentHash[selector1].subComponents.concat(componentHash[selector2]);
81+
component.subComponents = component.subComponents.concat(componentHash[selector2]);
9582
// If selector2 has been found in a template then it is not root
9683
componentHash[selector2].isRoot = false;
9784
}
9885
}
9986
}
10087
}
10188
}
102-
}
89+
}
90+
private static isComponentRouterOutlet(template: Buffer): boolean {
91+
const match = this.routerOutletRegex.exec(template.toString());
92+
return match !== null;
93+
}
10394
}

src/model/Component.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,21 @@ export class Component extends NamedEntity {
44
public dependencyInjections: NamedEntity[] = [];
55
public inputs: NamedEntity[] = [];
66
public outputs: NamedEntity[] = [];
7-
public viewchilds: NamedEntity[] = [];
8-
public viewchildren: NamedEntity[] = [];
9-
public contentchilds: NamedEntity[] = [];
10-
public contentchildren: NamedEntity[] = [];
7+
public viewChilds: NamedEntity[] = [];
8+
public viewChildren: NamedEntity[] = [];
9+
public contentChilds: NamedEntity[] = [];
10+
public contentChildren: NamedEntity[] = [];
11+
public templateFilename: string = '';
12+
public selector: string = '';
13+
public subComponents: Component[] = [];
14+
public isRoot: boolean = false;
15+
public isRouterOutlet: boolean = false;
16+
17+
constructor(className: string, filename: string, templateFilename: string = '', selector: string = '', subComponents: Component[] = [], isRoot: boolean = false) {
18+
super(className, filename);
19+
this.templateFilename = templateFilename;
20+
this.selector = selector;
21+
this.subComponents = subComponents;
22+
this.isRoot = isRoot;
23+
}
1124
}

src/model/Project.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NgModule } from "@src";
1+
import { NgModule } from "@model";
22
import { Component } from './Component';
33
import { NamedEntity } from "./NamedEntity";
44

src/moduleManager.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class ModuleManager {
4343
module.filename = filename;
4444
module.moduleName = moduleName;
4545

46-
regex = /:\s+routes\s+=\s+\[.*?\]/ims;
46+
regex = /:\s+Routes\s+=\s+\[.*?\]/ims;
4747
match = regex.exec(fileContents.toString());
4848
if (match !== null) {
4949
module.isRoutingModule = true;
@@ -58,10 +58,11 @@ export class ModuleManager {
5858
regex = /@Component\s*\(\s*(\{.+?\})\s*\)\s*export\s+class\s+(\w+)\s+(.*)/ims;
5959
match = regex.exec(fileContents.toString());
6060
if (match !== null) {
61+
const componentBody = match[1];
6162
const className = match[2];
6263
const component = new Component(className, filename);
6364
const classBody = match[3];
64-
this.enrichComponent(component, classBody);
65+
this.enrichComponent(component, classBody, componentBody);
6566
return component;
6667
}
6768
regex = /@Directive\s*\(\s*(\{.+?\})\s*\)\s*export\s+class\s+(\w+)\s+/ims;
@@ -81,7 +82,7 @@ export class ModuleManager {
8182
}
8283
}
8384

84-
private static enrichComponent(component: Component, classBody: string): void {
85+
private static enrichComponent(component: Component, classBody: string, componentBody: string): void {
8586
let regex = /constructor\s*\((.*?)\)/ims;
8687
let match = regex.exec(classBody);
8788
if (match !== null) {
@@ -95,10 +96,10 @@ export class ModuleManager {
9596
}
9697
this.matchMultipleSpecificDecorator(classBody, '@Input', component.filename, component.inputs);
9798
this.matchMultipleSpecificDecorator(classBody, '@output', component.filename, component.outputs);
98-
this.matchSpecificDecorator(classBody, '@ViewChild', component.filename, component.viewchilds);
99-
this.matchSpecificDecorator(classBody, '@ViewChildren', component.filename, component.viewchildren);
100-
this.matchSpecificDecorator(classBody, '@ContentChild', component.filename, component.contentchilds);
101-
this.matchSpecificDecorator(classBody, '@ContentChildren', component.filename, component.contentchildren);
99+
this.matchSpecificDecorator(classBody, '@ViewChild', component.filename, component.viewChilds);
100+
this.matchSpecificDecorator(classBody, '@ViewChildren', component.filename, component.viewChildren);
101+
this.matchSpecificDecorator(classBody, '@ContentChild', component.filename, component.contentChilds);
102+
this.matchSpecificDecorator(classBody, '@ContentChildren', component.filename, component.contentChildren);
102103
}
103104

104105
private static matchMultipleSpecificDecorator(classBody: string, decorator: string, filename: string, decoratorArray: NamedEntity[]) {

0 commit comments

Comments
 (0)