Permalink
Browse files

chore: linting and stricter type checking

  • Loading branch information...
mgechev committed Apr 24, 2018
1 parent 45713a6 commit c1516d875e78572e529890c54f6f0ac14335bb5c
View
@@ -1,4 +1,5 @@
.DS_Store
.vscode
build
node_modules
npm-debug.log
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -37,6 +37,7 @@
"raw-loader": "^0.5.1",
"ts-jest": "^22.4.1",
"ts-loader": "^3.5.0",
"tslint": "^5.9.1",
"typescript": "^2.8.1",
"webpack": "^3.11.0",
"webpack-cli": "^2.0.11"
@@ -1,4 +1,6 @@
export type Neighbors = { [key: string]: number };
export interface Neighbors {
[key: string]: number;
}
export interface Graph {
[key: string]: Neighbors;
@@ -12,7 +14,7 @@ export interface Module {
export interface RoutingModule {
path: string;
modulePath: string;
parentModulePath: string;
parentModulePath: string | null;
lazy: boolean;
}
View
@@ -1,7 +1,7 @@
import { Period } from '../common/interfaces';
interface PageConfig {
pageToken: number;
pageToken: number | undefined;
pageSize: number;
}
View
@@ -25,7 +25,7 @@ export const matchRoute = (route: string, declaration: string): boolean => {
// TODO optimize
const findRoute = (d: string[], r: string) => {
const res = d.filter(d => d.indexOf(':') < 0).find(c => matchRoute(r, c)) || d.find(c => matchRoute(r, c));
const res = d.filter(def => def.indexOf(':') < 0).find(c => matchRoute(r, c)) || d.find(c => matchRoute(r, c));
if (!res && r && r !== '(entrance)' && d.length) {
console.warn(`No declaration for ${r}`);
}
@@ -42,7 +42,7 @@ export const normalize = (data: any, formatter: (s: string) => string, declarati
return {
from: processRoute(declarations, formatter(r.dimensions[0])),
to: processRoute(declarations, formatter(r.dimensions[1])),
weight: parseInt(r.metrics[0].values[0])
weight: parseInt(r.metrics[0].values[0], 10)
};
})
.filter((node: Connection) => node.from !== '(entrance)' && node.from !== node.to);
@@ -5,7 +5,7 @@
"sourceMap": true,
"declaration": true,
"experimentalDecorators": true,
"noImplicitAny": true,
"strict": true,
"outDir": "./dist",
"downlevelIteration": true,
"lib": ["es2015", "esnext", "dom"]
@@ -1,4 +1,4 @@
import { ProjectSymbols } from 'ngast';
import { ProjectSymbols, ModuleSymbol } from 'ngast';
import { readFileSync, readFile, writeFileSync } from 'fs';
import { CompileIdentifierMetadata, CompileProviderMetadata } from '@angular/compiler';
import { Route } from '@angular/compiler/src/core';
@@ -43,8 +43,11 @@ export const parseRoutes = (tsconfig: string): RoutingModule[] => {
get(name: string) {
return new Promise((resolve: any, reject: any) => {
readFile(name, (e, data) => {
if (e) reject(e);
else resolve(data.toString());
if (e) {
reject(e);
} else {
resolve(data.toString());
}
});
});
},
@@ -55,29 +58,34 @@ export const parseRoutes = (tsconfig: string): RoutingModule[] => {
e => console.error(e)
);
const m = s.getModules().map(m => {
return m.getModuleSummary().providers.filter(p => {
return p.provider.token.identifier.reference.name === 'ROUTES';
});
const m = s.getModules().map((module: ModuleSymbol) => {
const summary = module.getModuleSummary();
if (summary) {
return summary.providers.filter(p => {
return p.provider.token.identifier && p.provider.token.identifier.reference.name === 'ROUTES';
});
}
return [];
});
const flattened = m.concat.apply([], m) as RawModuleData[];
const rawMap: { [key: string]: RawModuleData } = {};
flattened.forEach(m => {
rawMap[key(m.module.reference)] = m;
flattened.forEach(module => {
rawMap[key(module.module.reference)] = module;
});
const root = flattened.find(m => m.module.reference.name === 'AppModule');
// Relying on a convention established in https://angular.io/styleguide
const root = flattened.find(module => module.module.reference.name === 'AppModule');
const result: RoutingModule[] = [];
const findRoutes = (
modulePath: string,
routes: Route[],
parentRoute: string,
rawMap: { [key: string]: RawModuleData },
result: RoutingModule[],
rawModuleMap: { [key: string]: RawModuleData },
routingModules: RoutingModule[],
parentModule: string | null
) => {
const parts = modulePath.split('/');
@@ -89,42 +97,42 @@ export const parseRoutes = (tsconfig: string): RoutingModule[] => {
parent: string;
}
const r: RouteWithParent[] = routes.map(r => ({
route: r,
const r: RouteWithParent[] = routes.map(currentRoute => ({
route: currentRoute,
parent: parentRoute
}));
while (r.length) {
const c = r.pop();
const c = r.pop() as RouteWithParent;
const path = (c.route as any).path;
let module = modulePath;
if (c.route.loadChildren) {
const path = c.route.loadChildren.split('#')[0] + '.ts';
const routePath = c.route.loadChildren.split('#')[0] + '.ts';
const parentParts = modulePath.split('/');
parentParts.pop();
const parentPath = parentParts.join('/');
module = join(parentPath, path);
module = join(parentPath, routePath);
}
const currentPath = normalize('/' + join(c.parent, path));
result.push({
routingModules.push({
path: currentPath,
modulePath: module,
lazy: !!c.route.loadChildren,
parentModulePath: currentPath === '/' ? null : modulePath
});
if (c.route.loadChildren) {
const parts = c.route.loadChildren.split('#');
const childModule = join(filePath, parts[0]) + '.ts';
const key = childModule + '#' + parts[1];
const routeParts = c.route.loadChildren.split('#');
const childModule = join(filePath, routeParts[0]) + '.ts';
const moduleKey = childModule + '#' + routeParts[1];
findRoutes(
childModule,
rawMap[key].provider.useValue as Route[],
rawModuleMap[moduleKey].provider.useValue as Route[],
join(c.parent, (c.route as any).path),
rawMap,
result,
rawModuleMap,
routingModules,
modulePath
);
}
@@ -137,7 +145,9 @@ export const parseRoutes = (tsconfig: string): RoutingModule[] => {
}
};
findRoutes(root.module.reference.filePath, root.provider.useValue as Route[], '', rawMap, result, null);
if (root) {
findRoutes(root.module.reference.filePath, root.provider.useValue as Route[], '', rawMap, result, null);
}
return result;
};
@@ -49,6 +49,9 @@ const extractRoutes = (file: ts.SourceFile): RoutingModule[] => {
while (stack.length) {
const c = stack.pop();
if (!c) {
return result;
}
if (c.kind === ts.SyntaxKind.JsxElement || c.kind === ts.SyntaxKind.JsxSelfClosingElement) {
let el: ts.JsxSelfClosingElement | ts.JsxOpeningElement = (c as ts.JsxElement).openingElement;
if (c.kind === ts.SyntaxKind.JsxSelfClosingElement) {
@@ -93,20 +96,22 @@ export const parseRoutes = (tsconfig: string) => {
const parsed = ts.parseJsonConfigFileContent(config, parseConfigHost, basePath);
const program = ts.createProgram(parsed.fileNames, parsed.options);
const jsxFiles = program.getSourceFiles().filter(f => f.fileName.endsWith('.tsx') || f.fileName.endsWith('.jsx'));
const routes = jsxFiles.reduce((a, f) => a.concat(extractRoutes(f)), []);
const routes = jsxFiles.reduce((a, f) => a.concat(extractRoutes(f)), [] as RoutingModule[]);
const modules = routes.reduce(
(a, r) => {
a[r.modulePath] = true;
return a;
},
{} as { [key: string]: boolean }
);
const rootModulePath = routes.filter(r => !modules[r.parentModulePath]).pop().parentModulePath;
routes.push({
path: '/',
parentModulePath: null,
modulePath: rootModulePath,
lazy: false
});
const rootModule = routes.filter(r => r.parentModulePath && !modules[r.parentModulePath]).pop();
if (rootModule) {
routes.push({
path: '/',
parentModulePath: null,
modulePath: rootModule.parentModulePath,
lazy: false
} as RoutingModule);
}
return routes;
};
@@ -5,7 +5,7 @@
"sourceMap": true,
"declaration": true,
"experimentalDecorators": true,
"noImplicitAny": true,
"strict": true,
"outDir": "./dist",
"downlevelIteration": true,
"lib": ["es2015", "esnext", "dom"]
@@ -7,16 +7,12 @@ export enum Mode {
Auto = 'auto'
}
export interface RouteProvider {
(): RoutingModule[];
}
export type RouteProvider = () => RoutingModule[];
export type Cluster = string[];
export type Clusters = Cluster[];
export interface ClusteringAlgorithm {
(graph: Graph, modules: Module[], totalClusters: number): Clusters;
}
export type ClusteringAlgorithm = (graph: Graph, modules: Module[], totalClusters: number) => Clusters;
export interface Module {
modulePath: string;
@@ -13,7 +13,7 @@ const defaultParsers: RoutingStrategies = {
return parseRoutes('tsconfig.json', ProjectType.React);
},
[Mode.Gatsby](): RoutingModule[] {
throw 'Not supported';
throw new Error('Not supported');
},
[Mode.Auto]() {
const path = ['package.json', '../package.json'].filter(existsSync).pop();
@@ -1,4 +1,3 @@
import { ProjectType, parseRoutes } from 'guess-parser';
import { fetch } from 'guess-ga';
import { existsSync, readFileSync } from 'fs';
@@ -52,10 +51,10 @@ export class GuessPlugin {
clientId,
clientSecret,
scope
}).then((auth: any) => {
}).then((token: any) => {
const { google } = require('googleapis');
const oauth2Client = new google.auth.OAuth2();
oauth2Client.setCredentials(auth);
oauth2Client.setCredentials(token);
const routes = (this._config.routeProvider || defaultRouteProvider(this._config.mode || Mode.Auto))();
@@ -119,7 +119,7 @@ export const initialize = (
const pushState = history.pushState;
history.pushState = function(state) {
if (typeof (history as any).onpushstate == 'function') {
if (typeof (history as any).onpushstate === 'function') {
(history as any).onpushstate({ state: state });
}
handleNavigationChange(graph, basePath, thresholds, arguments[2]);
@@ -5,8 +5,8 @@
"module": "commonjs",
"sourceMap": true,
"declaration": true,
"strict": true,
"experimentalDecorators": true,
"noImplicitAny": true,
"outDir": "./dist",
"downlevelIteration": true,
"lib": ["es2015", "esnext", "dom"],
View
@@ -5,7 +5,7 @@
"sourceMap": true,
"declaration": true,
"experimentalDecorators": true,
"noImplicitAny": true,
"strict": true,
"outDir": "./dist",
"downlevelIteration": true,
"lib": ["es2015", "esnext", "dom"]
Oops, something went wrong.

0 comments on commit c1516d8

Please sign in to comment.