feat(router): make it work with TypeScript 1.8
This commit is contained in:
parent
3683fb6886
commit
b3e801ed9e
|
@ -63,7 +63,7 @@
|
|||
"systemjs-builder": "^0.15.7",
|
||||
"traceur": "0.0.96",
|
||||
"tsd": "^0.6.5",
|
||||
"typescript": "^1.9.0-dev.20160409",
|
||||
"typescript": "^1.8.10",
|
||||
"typings": "^1.0.4",
|
||||
"zone.js": "^0.6.6",
|
||||
"clang-format": "^1.0.35",
|
||||
|
|
|
@ -94,7 +94,8 @@ function expandRegularPathWithParamsAgainstRouteUsingRedirect(
|
|||
segment: UrlSegment, routes: Route[], route: Route, paths: UrlPathWithParams[],
|
||||
outlet: string): UrlSegment {
|
||||
const {consumedPaths, lastChild, positionalParamSegments} = match(segment, route, paths);
|
||||
const newPaths = applyRedirectCommands(consumedPaths, route.redirectTo, positionalParamSegments);
|
||||
const newPaths =
|
||||
applyRedirectCommands(consumedPaths, route.redirectTo, <any>positionalParamSegments);
|
||||
if (route.redirectTo.startsWith('/')) {
|
||||
throw new GlobalRedirect(newPaths);
|
||||
} else {
|
||||
|
|
|
@ -4,8 +4,8 @@ import {UrlPathWithParams, UrlSegment, UrlTree} from './url_tree';
|
|||
import {forEach, shallowEqual} from './utils/collection';
|
||||
|
||||
export function createUrlTree(
|
||||
route: ActivatedRoute, urlTree: UrlTree, commands: any[], queryParams: Params | undefined,
|
||||
fragment: string | undefined): UrlTree {
|
||||
route: ActivatedRoute, urlTree: UrlTree, commands: any[], queryParams: Params,
|
||||
fragment: string): UrlTree {
|
||||
if (commands.length === 0) {
|
||||
return tree(urlTree.root, urlTree.root, urlTree, queryParams, fragment);
|
||||
}
|
||||
|
@ -24,8 +24,8 @@ export function createUrlTree(
|
|||
}
|
||||
|
||||
function tree(
|
||||
oldSegment: UrlSegment, newSegment: UrlSegment, urlTree: UrlTree,
|
||||
queryParams: Params | undefined, fragment: string | undefined): UrlTree {
|
||||
oldSegment: UrlSegment, newSegment: UrlSegment, urlTree: UrlTree, queryParams: Params,
|
||||
fragment: string): UrlTree {
|
||||
const q = queryParams ? stringify(queryParams) : urlTree.queryParams;
|
||||
const f = fragment ? fragment : urlTree.fragment;
|
||||
|
||||
|
@ -38,7 +38,7 @@ function tree(
|
|||
|
||||
function replaceSegment(
|
||||
current: UrlSegment, oldSegment: UrlSegment, newSegment: UrlSegment): UrlSegment {
|
||||
const children = {};
|
||||
const children: {[key: string]: UrlSegment} = {};
|
||||
forEach(current.children, (c, k) => {
|
||||
if (c === oldSegment) {
|
||||
children[k] = newSegment;
|
||||
|
@ -162,7 +162,7 @@ function updateSegmentChildren(
|
|||
return new UrlSegment(segment.pathsWithParams, {});
|
||||
} else {
|
||||
const outlet = getOutlet(commands);
|
||||
const children = {};
|
||||
const children: {[key: string]: UrlSegment} = {};
|
||||
children[outlet] = updateSegment(segment.children[outlet], startIndex, commands);
|
||||
forEach(segment.children, (child, childOutlet) => {
|
||||
if (childOutlet !== outlet) {
|
||||
|
@ -223,7 +223,7 @@ function createNewSegment(segment: UrlSegment, startIndex: number, commands: any
|
|||
}
|
||||
|
||||
function stringify(params: {[key: string]: any}): {[key: string]: string} {
|
||||
const res = {};
|
||||
const res: {[key: string]: string} = {};
|
||||
forEach(params, (v, k) => res[k] = `${v}`);
|
||||
return res;
|
||||
}
|
||||
|
|
|
@ -5,8 +5,8 @@ import {PRIMARY_OUTLET} from '../shared';
|
|||
|
||||
@Directive({selector: 'router-outlet'})
|
||||
export class RouterOutlet {
|
||||
private activated: ComponentRef<any>|null;
|
||||
private _activatedRoute: ActivatedRoute|null;
|
||||
private activated: ComponentRef<any>;
|
||||
private _activatedRoute: ActivatedRoute;
|
||||
public outletMap: RouterOutletMap;
|
||||
|
||||
/**
|
||||
|
|
|
@ -118,7 +118,7 @@ function match(segment: UrlSegment, route: Route, paths: UrlPathWithParams[]) {
|
|||
|
||||
const path = route.path.startsWith('/') ? route.path.substring(1) : route.path;
|
||||
const parts = path.split('/');
|
||||
const posParameters = {};
|
||||
const posParameters: {[key: string]: any} = {};
|
||||
const consumedPaths = [];
|
||||
|
||||
let currentIndex = 0;
|
||||
|
@ -142,7 +142,7 @@ function match(segment: UrlSegment, route: Route, paths: UrlPathWithParams[]) {
|
|||
throw new NoMatch();
|
||||
}
|
||||
|
||||
const parameters = <any>merge(posParameters, consumedPaths[consumedPaths.length - 1].parameters);
|
||||
const parameters = merge(posParameters, consumedPaths[consumedPaths.length - 1].parameters);
|
||||
return {consumedPaths, lastChild: currentIndex, parameters};
|
||||
}
|
||||
|
||||
|
|
|
@ -352,9 +352,9 @@ class GuardChecks {
|
|||
}
|
||||
|
||||
private traverseChildRoutes(
|
||||
futureNode: TreeNode<ActivatedRouteSnapshot>, currNode: TreeNode<ActivatedRouteSnapshot>|null,
|
||||
outletMap: RouterOutletMap|null): void {
|
||||
const prevChildren = nodeChildrenAsMap(currNode);
|
||||
futureNode: TreeNode<ActivatedRouteSnapshot>, currNode: TreeNode<ActivatedRouteSnapshot>,
|
||||
outletMap: RouterOutletMap): void {
|
||||
const prevChildren: {[key: string]: any} = nodeChildrenAsMap(currNode);
|
||||
futureNode.children.forEach(c => {
|
||||
this.traverseRoutes(c, prevChildren[c.value.outlet], outletMap);
|
||||
delete prevChildren[c.value.outlet];
|
||||
|
@ -363,8 +363,8 @@ class GuardChecks {
|
|||
}
|
||||
|
||||
traverseRoutes(
|
||||
futureNode: TreeNode<ActivatedRouteSnapshot>, currNode: TreeNode<ActivatedRouteSnapshot>|null,
|
||||
parentOutletMap: RouterOutletMap|null): void {
|
||||
futureNode: TreeNode<ActivatedRouteSnapshot>, currNode: TreeNode<ActivatedRouteSnapshot>,
|
||||
parentOutletMap: RouterOutletMap): void {
|
||||
const future = futureNode.value;
|
||||
const curr = currNode ? currNode.value : null;
|
||||
const outlet = parentOutletMap ? parentOutletMap._outlets[futureNode.value.outlet] : null;
|
||||
|
@ -445,9 +445,9 @@ class ActivateRoutes {
|
|||
}
|
||||
|
||||
private activateChildRoutes(
|
||||
futureNode: TreeNode<ActivatedRoute>, currNode: TreeNode<ActivatedRoute>|null,
|
||||
futureNode: TreeNode<ActivatedRoute>, currNode: TreeNode<ActivatedRoute>,
|
||||
outletMap: RouterOutletMap): void {
|
||||
const prevChildren = nodeChildrenAsMap(currNode);
|
||||
const prevChildren: {[key: string]: any} = nodeChildrenAsMap(currNode);
|
||||
futureNode.children.forEach(c => {
|
||||
this.activateRoutes(c, prevChildren[c.value.outlet], outletMap);
|
||||
delete prevChildren[c.value.outlet];
|
||||
|
@ -456,7 +456,7 @@ class ActivateRoutes {
|
|||
}
|
||||
|
||||
activateRoutes(
|
||||
futureNode: TreeNode<ActivatedRoute>, currNode: TreeNode<ActivatedRoute>|null,
|
||||
futureNode: TreeNode<ActivatedRoute>, currNode: TreeNode<ActivatedRoute>,
|
||||
parentOutletMap: RouterOutletMap): void {
|
||||
const future = futureNode.value;
|
||||
const curr = currNode ? currNode.value : null;
|
||||
|
@ -502,7 +502,7 @@ function pushQueryParamsAndFragment(state: RouterState): void {
|
|||
}
|
||||
}
|
||||
|
||||
function nodeChildrenAsMap(node: TreeNode<any>| null) {
|
||||
function nodeChildrenAsMap(node: TreeNode<any>) {
|
||||
return node ? node.children.reduce((m, c) => {
|
||||
m[c.value.outlet] = c;
|
||||
return m;
|
||||
|
|
|
@ -115,7 +115,7 @@ export class ActivatedRouteSnapshot {
|
|||
_resolvedComponentFactory: ComponentFactory<any>;
|
||||
|
||||
/** @internal **/
|
||||
_routeConfig: Route|null;
|
||||
_routeConfig: Route;
|
||||
|
||||
/** @internal **/
|
||||
_urlSegment: UrlSegment;
|
||||
|
@ -127,7 +127,7 @@ export class ActivatedRouteSnapshot {
|
|||
*/
|
||||
constructor(
|
||||
public url: UrlPathWithParams[], public params: Params, public outlet: string,
|
||||
public component: Type|string, routeConfig: Route|null, urlSegment: UrlSegment,
|
||||
public component: Type|string, routeConfig: Route, urlSegment: UrlSegment,
|
||||
lastPathIndex: number) {
|
||||
this._routeConfig = routeConfig;
|
||||
this._urlSegment = urlSegment;
|
||||
|
@ -160,7 +160,7 @@ export class RouterStateSnapshot extends Tree<ActivatedRouteSnapshot> {
|
|||
*/
|
||||
constructor(
|
||||
public url: string, root: TreeNode<ActivatedRouteSnapshot>, public queryParams: Params,
|
||||
public fragment: string|null) {
|
||||
public fragment: string) {
|
||||
super(root);
|
||||
}
|
||||
|
||||
|
|
|
@ -162,7 +162,7 @@ class UrlParser {
|
|||
paths.push(this.parsePathWithParams());
|
||||
}
|
||||
|
||||
let children = {};
|
||||
let children: {[key: string]: UrlSegment} = {};
|
||||
if (this.peekStartsWith('/(')) {
|
||||
this.capture('/');
|
||||
children = this.parseParens(true);
|
||||
|
@ -200,7 +200,7 @@ class UrlParser {
|
|||
return params;
|
||||
}
|
||||
|
||||
parseFragment(): string|null {
|
||||
parseFragment(): string {
|
||||
if (this.peekStartsWith('#')) {
|
||||
return this.remaining.substring(1);
|
||||
} else {
|
||||
|
@ -255,7 +255,7 @@ class UrlParser {
|
|||
}
|
||||
|
||||
parseParens(allowPrimary: boolean): {[key: string]: UrlSegment} {
|
||||
const segments = {};
|
||||
const segments: {[key: string]: UrlSegment} = {};
|
||||
this.capture('(');
|
||||
|
||||
while (!this.peekStartsWith(')') && this.remaining.length > 0) {
|
||||
|
|
|
@ -15,13 +15,13 @@ export class UrlTree {
|
|||
*/
|
||||
constructor(
|
||||
public root: UrlSegment, public queryParams: {[key: string]: string},
|
||||
public fragment: string|null) {}
|
||||
public fragment: string) {}
|
||||
|
||||
toString(): string { return new DefaultUrlSerializer().serialize(this); }
|
||||
}
|
||||
|
||||
export class UrlSegment {
|
||||
public parent: UrlSegment|null = null;
|
||||
public parent: UrlSegment = null;
|
||||
constructor(
|
||||
public pathsWithParams: UrlPathWithParams[], public children: {[key: string]: UrlSegment}) {
|
||||
forEach(children, (v, k) => v.parent = this);
|
||||
|
@ -46,7 +46,7 @@ export function equalPathsWithParams(a: UrlPathWithParams[], b: UrlPathWithParam
|
|||
|
||||
export function mapChildren(segment: UrlSegment, fn: (v: UrlSegment, k: string) => UrlSegment):
|
||||
{[name: string]: UrlSegment} {
|
||||
const newChildren = {};
|
||||
const newChildren: {[name: string]: UrlSegment} = {};
|
||||
forEach(segment.children, (child, childOutlet) => {
|
||||
if (childOutlet === PRIMARY_OUTLET) {
|
||||
newChildren[childOutlet] = fn(child, childOutlet);
|
||||
|
|
|
@ -24,11 +24,11 @@ export function flatten<T>(a: T[][]): T[] {
|
|||
return target;
|
||||
}
|
||||
|
||||
export function first<T>(a: T[]): T|null {
|
||||
export function first<T>(a: T[]): T {
|
||||
return a.length > 0 ? a[0] : null;
|
||||
}
|
||||
|
||||
export function last<T>(a: T[]): T|null {
|
||||
export function last<T>(a: T[]): T {
|
||||
return a.length > 0 ? a[a.length - 1] : null;
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ export class Tree<T> {
|
|||
|
||||
get root(): T { return this._root.value; }
|
||||
|
||||
parent(t: T): T|null {
|
||||
parent(t: T): T {
|
||||
const p = this.pathFromRoot(t);
|
||||
return p.length > 1 ? p[p.length - 2] : null;
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ export class Tree<T> {
|
|||
return n ? n.children.map(t => t.value) : [];
|
||||
}
|
||||
|
||||
firstChild(t: T): T|null {
|
||||
firstChild(t: T): T {
|
||||
const n = findNode(t, this._root);
|
||||
return n && n.children.length > 0 ? n.children[0].value : null;
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ export class Tree<T> {
|
|||
contains(tree: Tree<T>): boolean { return contains(this._root, tree._root); }
|
||||
}
|
||||
|
||||
function findNode<T>(expected: T, c: TreeNode<T>): TreeNode<T>|null {
|
||||
function findNode<T>(expected: T, c: TreeNode<T>): TreeNode<T> {
|
||||
if (expected === c.value) return c;
|
||||
for (let cc of c.children) {
|
||||
const r = findNode(expected, cc);
|
||||
|
|
|
@ -61,7 +61,7 @@ function createState(config: RouterConfig, url: string): RouterStateSnapshot {
|
|||
return res;
|
||||
}
|
||||
|
||||
function checkActivatedRoute(actual: ActivatedRoute | null, cmp: Function, outlet: string = PRIMARY_OUTLET):void {
|
||||
function checkActivatedRoute(actual: ActivatedRoute, cmp: Function, outlet: string = PRIMARY_OUTLET):void {
|
||||
if (actual === null) {
|
||||
expect(actual).toBeDefined();
|
||||
} else {
|
||||
|
|
|
@ -353,7 +353,7 @@ function checkRecognize(config: RouterConfig, url: string, callback: any): void
|
|||
});
|
||||
}
|
||||
|
||||
function checkActivatedRoute(actual: ActivatedRouteSnapshot | null, url: string, params: Params, cmp: Function, outlet: string = PRIMARY_OUTLET):void {
|
||||
function checkActivatedRoute(actual: ActivatedRouteSnapshot, url: string, params: Params, cmp: Function, outlet: string = PRIMARY_OUTLET):void {
|
||||
if (actual === null) {
|
||||
expect(actual).not.toBeNull();
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue