Skip to content

Commit

Permalink
feat(app): code refactoring with Singleton for engines
Browse files Browse the repository at this point in the history
  • Loading branch information
vogloblinsky committed Oct 17, 2018
1 parent bd23ec8 commit e290cca
Show file tree
Hide file tree
Showing 36 changed files with 746 additions and 675 deletions.
593 changes: 285 additions & 308 deletions src/app/application.ts

Large diffs are not rendered by default.

81 changes: 41 additions & 40 deletions src/app/compiler/angular-dependencies.ts
Expand Up @@ -6,19 +6,23 @@ import Ast, { ts, SyntaxKind } from 'ts-simple-ast';
import { logger } from '../../logger';
import { markedtags, mergeTagsAndArgs, cleanLifecycleHooksFromMethods } from '../../utils/utils';
import { kindToType } from '../../utils/kind-to-type';
import { $componentsTreeEngine } from '../engines/components-tree.engine';
import ComponentsTreeEngine from '../engines/components-tree.engine';

import { FrameworkDependencies } from './framework-dependencies';

import ImportsUtil from '../../utils/imports.util';

import {
JsdocParserUtil,
RouterParserUtil,
ImportsUtil,
isModuleWithProviders,
getModuleWithProviders,
isIgnore
} from '../../utils';

import ExtendsMerger from '../../utils/extends-merger.util';

import RouterParserUtil from '../../utils/router-parser.util';

import { CodeGenerator } from './angular/code-generator';

import { DirectiveDepFactory } from './angular/deps/directive-dep.factory';
Expand All @@ -30,7 +34,7 @@ import { ModuleHelper } from './angular/deps/helpers/module-helper';
import { JsDocHelper } from './angular/deps/helpers/js-doc-helper';
import { SymbolHelper } from './angular/deps/helpers/symbol-helper';

import { ConfigurationInterface } from '../interfaces/configuration.interface';
import Configuration from '../configuration';

import {
IInjectableDep,
Expand All @@ -55,15 +59,12 @@ export class AngularDependencies extends FrameworkDependencies {
private jsDocHelper = new JsDocHelper();
private symbolHelper = new SymbolHelper();
private jsdocParserUtil = new JsdocParserUtil();
private importsUtil = new ImportsUtil();

constructor(
files: string[],
options: any,
configuration: ConfigurationInterface,
routerParser: RouterParserUtil
options: any
) {
super(files, options, configuration, routerParser);
super(files, options);
}

public getDependencies() {
Expand Down Expand Up @@ -96,7 +97,7 @@ export class AngularDependencies extends FrameworkDependencies {

if (path.extname(filePath) === '.ts' || path.extname(filePath) === '.tsx') {
if (
!this.configuration.mainData.angularJSProject &&
!Configuration.mainData.angularJSProject &&
path.extname(filePath) === '.js'
) {
logger.info('parsing', filePath);
Expand Down Expand Up @@ -186,16 +187,16 @@ export class AngularDependencies extends FrameworkDependencies {
* - properties
* - methods
*/
deps = this.extendsMerger.merge(deps, this.configuration);
deps = ExtendsMerger.merge(deps, Configuration);

// this.routerParser.printModulesRoutes();
// this.routerParser.printRoutes();
// RouterParserUtil.printModulesRoutes();
// RouterParserUtil.printRoutes();

if (!this.configuration.mainData.disableRoutesGraph) {
this.routerParser.linkModulesAndRoutes();
this.routerParser.constructModulesTree();
if (!Configuration.mainData.disableRoutesGraph) {
RouterParserUtil.linkModulesAndRoutes();
RouterParserUtil.constructModulesTree();

deps.routesTree = this.routerParser.constructRoutesTree();
deps.routesTree = RouterParserUtil.constructRoutesTree();
}

return deps;
Expand Down Expand Up @@ -254,7 +255,7 @@ export class AngularDependencies extends FrameworkDependencies {
if (IO.hostListeners) {
deps.hostListeners = IO.hostListeners;
}
if (this.configuration.mainData.disableLifeCycleHooks) {
if (Configuration.mainData.disableLifeCycleHooks) {
deps.methods = cleanLifecycleHooksFromMethods(deps.methods);
}
if (IO.implements && IO.implements.length > 0) {
Expand Down Expand Up @@ -313,22 +314,22 @@ export class AngularDependencies extends FrameworkDependencies {
});
}

if (hasRoutesStatements && !this.configuration.mainData.disableRoutesGraph) {
if (hasRoutesStatements && !Configuration.mainData.disableRoutesGraph) {
// Clean file for spread and dynamics inside routes definitions
logger.info('Analysing routes definitions and clean them if necessary');

// scannedFile = this.routerParser.cleanFileIdentifiers(astFile).compilerNode;
let firstClean = this.routerParser.cleanFileSpreads(astFile).compilerNode;
scannedFile = this.routerParser.cleanCallExpressions(astFile).compilerNode;
scannedFile = this.routerParser.cleanFileDynamics(astFile).compilerNode;
// scannedFile = RouterParserUtil.cleanFileIdentifiers(astFile).compilerNode;
let firstClean = RouterParserUtil.cleanFileSpreads(astFile).compilerNode;
scannedFile = RouterParserUtil.cleanCallExpressions(astFile).compilerNode;
scannedFile = RouterParserUtil.cleanFileDynamics(astFile).compilerNode;

scannedFile.kind = SyntaxKind.SourceFile;
}

ts.forEachChild(scannedFile, (initialNode: ts.Node) => {
if (
this.jsDocHelper.hasJSDocInternalTag(fileName, scannedFile, initialNode) &&
this.configuration.mainData.disableInternal
Configuration.mainData.disableInternal
) {
return;
}
Expand Down Expand Up @@ -357,16 +358,16 @@ export class AngularDependencies extends FrameworkDependencies {
props,
IO
);
if (this.routerParser.hasRouterModuleInImports(moduleDep.imports)) {
this.routerParser.addModuleWithRoutes(
if (RouterParserUtil.hasRouterModuleInImports(moduleDep.imports)) {
RouterParserUtil.addModuleWithRoutes(
name,
this.moduleHelper.getModuleImportsRaw(props, srcFile),
file
);
}
deps = moduleDep;
if (typeof IO.ignore === 'undefined') {
this.routerParser.addModule(name, moduleDep.imports);
RouterParserUtil.addModule(name, moduleDep.imports);
outputSymbols.modules.push(moduleDep);
outputSymbols.modulesForGraph.push(moduleDep);
}
Expand All @@ -376,11 +377,11 @@ export class AngularDependencies extends FrameworkDependencies {
}
const componentDep = new ComponentDepFactory(
this.componentHelper,
this.configuration
Configuration
).create(file, srcFile, name, props, IO);
deps = componentDep;
if (typeof IO.ignore === 'undefined') {
$componentsTreeEngine.addComponent(componentDep);
ComponentsTreeEngine.addComponent(componentDep);
outputSymbols.components.push(componentDep);
}
} else if (this.isController(visitedDecorator)) {
Expand Down Expand Up @@ -462,7 +463,7 @@ export class AngularDependencies extends FrameworkDependencies {
}
let directiveDeps = new DirectiveDepFactory(
this.componentHelper,
this.configuration
Configuration
).create(file, srcFile, name, props, IO);
deps = directiveDeps;
if (typeof IO.ignore === 'undefined') {
Expand Down Expand Up @@ -614,14 +615,14 @@ export class AngularDependencies extends FrameworkDependencies {
if (IO.routes) {
let newRoutes;
try {
newRoutes = this.routerParser.cleanRawRouteParsed(IO.routes);
newRoutes = RouterParserUtil.cleanRawRouteParsed(IO.routes);
} catch (e) {
// tslint:disable-next-line:max-line-length
logger.error(
'Routes parsing error, maybe a trailing comma or an external variable, trying to fix that later after sources scanning.'
);
newRoutes = IO.routes.replace(/ /gm, '');
this.routerParser.addIncompleteRoute({
RouterParserUtil.addIncompleteRoute({
data: newRoutes,
file: file
});
Expand Down Expand Up @@ -685,12 +686,12 @@ export class AngularDependencies extends FrameworkDependencies {
});
}
if (rootModule) {
this.routerParser.setRootModule(rootModule);
RouterParserUtil.setRootModule(rootModule);
}
}
}
}
if (ts.isVariableStatement(node) && !this.routerParser.isVariableRoutes(node)) {
if (ts.isVariableStatement(node) && !RouterParserUtil.isVariableRoutes(node)) {
let infos: any = this.visitVariableDeclaration(node);
let name = infos.name;
let deps: any = {
Expand All @@ -711,8 +712,8 @@ export class AngularDependencies extends FrameworkDependencies {
}
if (isModuleWithProviders(node)) {
let routingInitializer = getModuleWithProviders(node);
this.routerParser.addModuleWithRoutes(name, [routingInitializer], file);
this.routerParser.addModule(name, [routingInitializer]);
RouterParserUtil.addModuleWithRoutes(name, [routingInitializer], file);
RouterParserUtil.addModule(name, [routingInitializer]);
}
if (!isIgnore(node)) {
outputSymbols.miscellaneous.variables.push(deps);
Expand Down Expand Up @@ -958,7 +959,7 @@ export class AngularDependencies extends FrameworkDependencies {
return [pop];
} else {
logger.warn('Empty metadatas, trying to found it with imports.');
return this.importsUtil.findValueInImportOrLocalVariables(pop.text, sourceFile);
return ImportsUtil.findValueInImportOrLocalVariables(pop.text, sourceFile);
}
}

Expand Down Expand Up @@ -1171,9 +1172,9 @@ export class AngularDependencies extends FrameworkDependencies {
for (i; i < len; i++) {
let routesInitializer = node.declarationList.declarations[i].initializer;
let data = new CodeGenerator().generate(routesInitializer);
this.routerParser.addRoute({
RouterParserUtil.addRoute({
name: node.declarationList.declarations[i].name.text,
data: this.routerParser.cleanRawRoute(data),
data: RouterParserUtil.cleanRawRoute(data),
filename: fileName
});
return [
Expand All @@ -1190,7 +1191,7 @@ export class AngularDependencies extends FrameworkDependencies {
let res;
if (sourceFile.statements) {
res = sourceFile.statements.reduce((directive, statement) => {
if (this.routerParser.isVariableRoutes(statement)) {
if (RouterParserUtil.isVariableRoutes(statement)) {
if (statement.pos === node.pos && statement.end === node.end) {
return directive.concat(
this.visitEnumDeclarationForRoutes(filename, statement)
Expand Down
6 changes: 3 additions & 3 deletions src/app/compiler/angular/deps/component-dep.factory.ts
@@ -1,12 +1,12 @@
import { IDep } from '../dependencies.interfaces';
import { ComponentHelper } from './helpers/component-helper';
import { cleanLifecycleHooksFromMethods } from '../../../../utils';
import { ConfigurationInterface } from '../../../interfaces/configuration.interface';
import Configuration from '../../../configuration';

const crypto = require('crypto');

export class ComponentDepFactory {
constructor(private helper: ComponentHelper, private configuration: ConfigurationInterface) {}
constructor(private helper: ComponentHelper) {}

public create(file: any, srcFile: any, name: any, props: any, IO: any): IComponentDep {
// console.log(util.inspect(props, { showHidden: true, depth: 10 }));
Expand Down Expand Up @@ -65,7 +65,7 @@ export class ComponentDepFactory {
srcFile
);
}
if (this.configuration.mainData.disableLifeCycleHooks) {
if (Configuration.mainData.disableLifeCycleHooks) {
componentDep.methodsClass = cleanLifecycleHooksFromMethods(componentDep.methodsClass);
}
if (IO.jsdoctags && IO.jsdoctags.length > 0) {
Expand Down
1 change: 0 additions & 1 deletion src/app/compiler/angular/deps/controller-dep.factory.ts
@@ -1,6 +1,5 @@
import { IDep } from '../dependencies.interfaces';
import { ts } from 'ts-simple-ast';
import { ConfigurationInterface } from '../../../interfaces/configuration.interface';

const crypto = require('crypto');

Expand Down
9 changes: 3 additions & 6 deletions src/app/compiler/angular/deps/directive-dep.factory.ts
@@ -1,16 +1,13 @@
import { IDep } from '../dependencies.interfaces';
import { ComponentHelper } from './helpers/component-helper';
import { ConfigurationInterface } from '../../../interfaces/configuration.interface';
import Configuration from '../../../configuration';
import { cleanLifecycleHooksFromMethods } from '../../../../utils';

const crypto = require('crypto');

export class DirectiveDepFactory {
constructor(
private helper: ComponentHelper,
private configuration: ConfigurationInterface) {

}
private helper: ComponentHelper) {}

public create(file: any, srcFile: any, name: any, props: any, IO: any): IDirectiveDep {
let sourceCode = srcFile.getText();
Expand All @@ -35,7 +32,7 @@ export class DirectiveDepFactory {
methodsClass: IO.methods,
exampleUrls: this.helper.getComponentExampleUrls(srcFile.getText())
};
if (this.configuration.mainData.disableLifeCycleHooks) {
if (Configuration.mainData.disableLifeCycleHooks) {
directiveDeps.methodsClass = cleanLifecycleHooksFromMethods(directiveDeps.methodsClass);
}
if (IO.jsdoctags && IO.jsdoctags.length > 0) {
Expand Down
36 changes: 16 additions & 20 deletions src/app/compiler/angular/deps/helpers/class-helper.ts
Expand Up @@ -6,27 +6,23 @@ import { ts, SyntaxKind } from 'ts-simple-ast';

import { getNamesCompareFn, mergeTagsAndArgs, markedtags } from '../../../../../utils/utils';
import { kindToType } from '../../../../../utils/kind-to-type';
import { ConfigurationInterface } from '../../../../interfaces/configuration.interface';
import { JsdocParserUtil } from '../../../../../utils/jsdoc-parser.util';
import { ImportsUtil } from '../../../../../utils/imports.util';
import { logger } from '../../../../../logger';
import { isIgnore, AngularVersionUtil, BasicTypeUtil } from '../../../../../utils';
import { isIgnore } from '../../../../../utils';
import AngularVersionUtil from '../../../../..//utils/angular-version.util';
import BasicTypeUtil from '../../../../../utils/basic-type.util';
import { StringifyObjectLiteralExpression } from '../../../../../utils/object-literal-expression.util';

import DependenciesEngine from '../../../../engines/dependencies.engine';
import Configuration from '../../../../configuration';

const crypto = require('crypto');
const marked = require('marked');

export class ClassHelper {
private jsdocParserUtil = new JsdocParserUtil();
private importsUtil = new ImportsUtil();
private angularVersionUtil = new AngularVersionUtil();
private basicTypeUtil = new BasicTypeUtil();

constructor(
private typeChecker: ts.TypeChecker,
private configuration: ConfigurationInterface
private typeChecker: ts.TypeChecker
) {}

/**
Expand Down Expand Up @@ -98,16 +94,16 @@ export class ClassHelper {
_result.data.name
}.html">${argu.type}</a>`;
} else {
let path = this.angularVersionUtil.getApiLink(
let path = AngularVersionUtil.getApiLink(
_result.data,
this.configuration.mainData.angularVersion
Configuration.mainData.angularVersion
);
return `${argu.name}${this.getOptionalString(
arg
)}: <a href="${path}" target="_blank">${argu.type}</a>`;
}
} else if (this.basicTypeUtil.isKnownType(argu.type)) {
let path = this.basicTypeUtil.getTypeUrl(argu.type);
} else if (BasicTypeUtil.isKnownType(argu.type)) {
let path = BasicTypeUtil.getTypeUrl(argu.type);
return `${argu.name}${this.getOptionalString(
arg
)}: <a href="${path}" target="_blank">${argu.type}</a>`;
Expand Down Expand Up @@ -146,9 +142,9 @@ export class ClassHelper {
_result.data.name
}.html">${arg.type}</a>`;
} else {
let path = this.angularVersionUtil.getApiLink(
let path = AngularVersionUtil.getApiLink(
_result.data,
this.configuration.mainData.angularVersion
Configuration.mainData.angularVersion
);
return `${arg.name}${this.getOptionalString(
arg
Expand All @@ -166,8 +162,8 @@ export class ClassHelper {
return `'` + arg.text + `'`;
} else if (arg.kind && arg.kind === SyntaxKind.ObjectLiteralExpression) {
return StringifyObjectLiteralExpression(arg);
} else if (this.basicTypeUtil.isKnownType(arg.type)) {
let path = this.basicTypeUtil.getTypeUrl(arg.type);
} else if (BasicTypeUtil.isKnownType(arg.type)) {
let path = BasicTypeUtil.getTypeUrl(arg.type);
return `${arg.name}${this.getOptionalString(
arg
)}: <a href="${path}" target="_blank">${arg.type}</a>`;
Expand Down Expand Up @@ -592,12 +588,12 @@ export class ClassHelper {
} else if (hostListener) {
hostListeners.push(this.visitHostListener(member, hostListener, sourceFile));
} else if (!this.isHiddenMember(member)) {
if (!(this.isPrivate(member) && this.configuration.mainData.disablePrivate)) {
if (!(this.isInternal(member) && this.configuration.mainData.disableInternal)) {
if (!(this.isPrivate(member) && Configuration.mainData.disablePrivate)) {
if (!(this.isInternal(member) && Configuration.mainData.disableInternal)) {
if (
!(
this.isProtected(member) &&
this.configuration.mainData.disableProtected
Configuration.mainData.disableProtected
)
) {
if (ts.isMethodDeclaration(member) || ts.isMethodSignature(member)) {
Expand Down

0 comments on commit e290cca

Please sign in to comment.