feat(router): make it work with TypeScript 1.8

This commit is contained in:
vsavkin 2016-06-15 09:14:41 -07:00
parent 3683fb6886
commit b3e801ed9e
13 changed files with 39 additions and 38 deletions

View File

@ -63,7 +63,7 @@
"systemjs-builder": "^0.15.7", "systemjs-builder": "^0.15.7",
"traceur": "0.0.96", "traceur": "0.0.96",
"tsd": "^0.6.5", "tsd": "^0.6.5",
"typescript": "^1.9.0-dev.20160409", "typescript": "^1.8.10",
"typings": "^1.0.4", "typings": "^1.0.4",
"zone.js": "^0.6.6", "zone.js": "^0.6.6",
"clang-format": "^1.0.35", "clang-format": "^1.0.35",

View File

@ -94,7 +94,8 @@ function expandRegularPathWithParamsAgainstRouteUsingRedirect(
segment: UrlSegment, routes: Route[], route: Route, paths: UrlPathWithParams[], segment: UrlSegment, routes: Route[], route: Route, paths: UrlPathWithParams[],
outlet: string): UrlSegment { outlet: string): UrlSegment {
const {consumedPaths, lastChild, positionalParamSegments} = match(segment, route, paths); 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('/')) { if (route.redirectTo.startsWith('/')) {
throw new GlobalRedirect(newPaths); throw new GlobalRedirect(newPaths);
} else { } else {

View File

@ -4,8 +4,8 @@ import {UrlPathWithParams, UrlSegment, UrlTree} from './url_tree';
import {forEach, shallowEqual} from './utils/collection'; import {forEach, shallowEqual} from './utils/collection';
export function createUrlTree( export function createUrlTree(
route: ActivatedRoute, urlTree: UrlTree, commands: any[], queryParams: Params | undefined, route: ActivatedRoute, urlTree: UrlTree, commands: any[], queryParams: Params,
fragment: string | undefined): UrlTree { fragment: string): UrlTree {
if (commands.length === 0) { if (commands.length === 0) {
return tree(urlTree.root, urlTree.root, urlTree, queryParams, fragment); return tree(urlTree.root, urlTree.root, urlTree, queryParams, fragment);
} }
@ -24,8 +24,8 @@ export function createUrlTree(
} }
function tree( function tree(
oldSegment: UrlSegment, newSegment: UrlSegment, urlTree: UrlTree, oldSegment: UrlSegment, newSegment: UrlSegment, urlTree: UrlTree, queryParams: Params,
queryParams: Params | undefined, fragment: string | undefined): UrlTree { fragment: string): UrlTree {
const q = queryParams ? stringify(queryParams) : urlTree.queryParams; const q = queryParams ? stringify(queryParams) : urlTree.queryParams;
const f = fragment ? fragment : urlTree.fragment; const f = fragment ? fragment : urlTree.fragment;
@ -38,7 +38,7 @@ function tree(
function replaceSegment( function replaceSegment(
current: UrlSegment, oldSegment: UrlSegment, newSegment: UrlSegment): UrlSegment { current: UrlSegment, oldSegment: UrlSegment, newSegment: UrlSegment): UrlSegment {
const children = {}; const children: {[key: string]: UrlSegment} = {};
forEach(current.children, (c, k) => { forEach(current.children, (c, k) => {
if (c === oldSegment) { if (c === oldSegment) {
children[k] = newSegment; children[k] = newSegment;
@ -162,7 +162,7 @@ function updateSegmentChildren(
return new UrlSegment(segment.pathsWithParams, {}); return new UrlSegment(segment.pathsWithParams, {});
} else { } else {
const outlet = getOutlet(commands); const outlet = getOutlet(commands);
const children = {}; const children: {[key: string]: UrlSegment} = {};
children[outlet] = updateSegment(segment.children[outlet], startIndex, commands); children[outlet] = updateSegment(segment.children[outlet], startIndex, commands);
forEach(segment.children, (child, childOutlet) => { forEach(segment.children, (child, childOutlet) => {
if (childOutlet !== outlet) { if (childOutlet !== outlet) {
@ -223,7 +223,7 @@ function createNewSegment(segment: UrlSegment, startIndex: number, commands: any
} }
function stringify(params: {[key: string]: any}): {[key: string]: string} { function stringify(params: {[key: string]: any}): {[key: string]: string} {
const res = {}; const res: {[key: string]: string} = {};
forEach(params, (v, k) => res[k] = `${v}`); forEach(params, (v, k) => res[k] = `${v}`);
return res; return res;
} }

View File

@ -5,8 +5,8 @@ import {PRIMARY_OUTLET} from '../shared';
@Directive({selector: 'router-outlet'}) @Directive({selector: 'router-outlet'})
export class RouterOutlet { export class RouterOutlet {
private activated: ComponentRef<any>|null; private activated: ComponentRef<any>;
private _activatedRoute: ActivatedRoute|null; private _activatedRoute: ActivatedRoute;
public outletMap: RouterOutletMap; public outletMap: RouterOutletMap;
/** /**

View File

@ -118,7 +118,7 @@ function match(segment: UrlSegment, route: Route, paths: UrlPathWithParams[]) {
const path = route.path.startsWith('/') ? route.path.substring(1) : route.path; const path = route.path.startsWith('/') ? route.path.substring(1) : route.path;
const parts = path.split('/'); const parts = path.split('/');
const posParameters = {}; const posParameters: {[key: string]: any} = {};
const consumedPaths = []; const consumedPaths = [];
let currentIndex = 0; let currentIndex = 0;
@ -142,7 +142,7 @@ function match(segment: UrlSegment, route: Route, paths: UrlPathWithParams[]) {
throw new NoMatch(); 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}; return {consumedPaths, lastChild: currentIndex, parameters};
} }

View File

@ -352,9 +352,9 @@ class GuardChecks {
} }
private traverseChildRoutes( private traverseChildRoutes(
futureNode: TreeNode<ActivatedRouteSnapshot>, currNode: TreeNode<ActivatedRouteSnapshot>|null, futureNode: TreeNode<ActivatedRouteSnapshot>, currNode: TreeNode<ActivatedRouteSnapshot>,
outletMap: RouterOutletMap|null): void { outletMap: RouterOutletMap): void {
const prevChildren = nodeChildrenAsMap(currNode); const prevChildren: {[key: string]: any} = nodeChildrenAsMap(currNode);
futureNode.children.forEach(c => { futureNode.children.forEach(c => {
this.traverseRoutes(c, prevChildren[c.value.outlet], outletMap); this.traverseRoutes(c, prevChildren[c.value.outlet], outletMap);
delete prevChildren[c.value.outlet]; delete prevChildren[c.value.outlet];
@ -363,8 +363,8 @@ class GuardChecks {
} }
traverseRoutes( traverseRoutes(
futureNode: TreeNode<ActivatedRouteSnapshot>, currNode: TreeNode<ActivatedRouteSnapshot>|null, futureNode: TreeNode<ActivatedRouteSnapshot>, currNode: TreeNode<ActivatedRouteSnapshot>,
parentOutletMap: RouterOutletMap|null): void { parentOutletMap: RouterOutletMap): void {
const future = futureNode.value; const future = futureNode.value;
const curr = currNode ? currNode.value : null; const curr = currNode ? currNode.value : null;
const outlet = parentOutletMap ? parentOutletMap._outlets[futureNode.value.outlet] : null; const outlet = parentOutletMap ? parentOutletMap._outlets[futureNode.value.outlet] : null;
@ -445,9 +445,9 @@ class ActivateRoutes {
} }
private activateChildRoutes( private activateChildRoutes(
futureNode: TreeNode<ActivatedRoute>, currNode: TreeNode<ActivatedRoute>|null, futureNode: TreeNode<ActivatedRoute>, currNode: TreeNode<ActivatedRoute>,
outletMap: RouterOutletMap): void { outletMap: RouterOutletMap): void {
const prevChildren = nodeChildrenAsMap(currNode); const prevChildren: {[key: string]: any} = nodeChildrenAsMap(currNode);
futureNode.children.forEach(c => { futureNode.children.forEach(c => {
this.activateRoutes(c, prevChildren[c.value.outlet], outletMap); this.activateRoutes(c, prevChildren[c.value.outlet], outletMap);
delete prevChildren[c.value.outlet]; delete prevChildren[c.value.outlet];
@ -456,7 +456,7 @@ class ActivateRoutes {
} }
activateRoutes( activateRoutes(
futureNode: TreeNode<ActivatedRoute>, currNode: TreeNode<ActivatedRoute>|null, futureNode: TreeNode<ActivatedRoute>, currNode: TreeNode<ActivatedRoute>,
parentOutletMap: RouterOutletMap): void { parentOutletMap: RouterOutletMap): void {
const future = futureNode.value; const future = futureNode.value;
const curr = currNode ? currNode.value : null; 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) => { return node ? node.children.reduce((m, c) => {
m[c.value.outlet] = c; m[c.value.outlet] = c;
return m; return m;

View File

@ -115,7 +115,7 @@ export class ActivatedRouteSnapshot {
_resolvedComponentFactory: ComponentFactory<any>; _resolvedComponentFactory: ComponentFactory<any>;
/** @internal **/ /** @internal **/
_routeConfig: Route|null; _routeConfig: Route;
/** @internal **/ /** @internal **/
_urlSegment: UrlSegment; _urlSegment: UrlSegment;
@ -127,7 +127,7 @@ export class ActivatedRouteSnapshot {
*/ */
constructor( constructor(
public url: UrlPathWithParams[], public params: Params, public outlet: string, 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) { lastPathIndex: number) {
this._routeConfig = routeConfig; this._routeConfig = routeConfig;
this._urlSegment = urlSegment; this._urlSegment = urlSegment;
@ -160,7 +160,7 @@ export class RouterStateSnapshot extends Tree<ActivatedRouteSnapshot> {
*/ */
constructor( constructor(
public url: string, root: TreeNode<ActivatedRouteSnapshot>, public queryParams: Params, public url: string, root: TreeNode<ActivatedRouteSnapshot>, public queryParams: Params,
public fragment: string|null) { public fragment: string) {
super(root); super(root);
} }

View File

@ -162,7 +162,7 @@ class UrlParser {
paths.push(this.parsePathWithParams()); paths.push(this.parsePathWithParams());
} }
let children = {}; let children: {[key: string]: UrlSegment} = {};
if (this.peekStartsWith('/(')) { if (this.peekStartsWith('/(')) {
this.capture('/'); this.capture('/');
children = this.parseParens(true); children = this.parseParens(true);
@ -200,7 +200,7 @@ class UrlParser {
return params; return params;
} }
parseFragment(): string|null { parseFragment(): string {
if (this.peekStartsWith('#')) { if (this.peekStartsWith('#')) {
return this.remaining.substring(1); return this.remaining.substring(1);
} else { } else {
@ -255,7 +255,7 @@ class UrlParser {
} }
parseParens(allowPrimary: boolean): {[key: string]: UrlSegment} { parseParens(allowPrimary: boolean): {[key: string]: UrlSegment} {
const segments = {}; const segments: {[key: string]: UrlSegment} = {};
this.capture('('); this.capture('(');
while (!this.peekStartsWith(')') && this.remaining.length > 0) { while (!this.peekStartsWith(')') && this.remaining.length > 0) {

View File

@ -15,13 +15,13 @@ export class UrlTree {
*/ */
constructor( constructor(
public root: UrlSegment, public queryParams: {[key: string]: string}, public root: UrlSegment, public queryParams: {[key: string]: string},
public fragment: string|null) {} public fragment: string) {}
toString(): string { return new DefaultUrlSerializer().serialize(this); } toString(): string { return new DefaultUrlSerializer().serialize(this); }
} }
export class UrlSegment { export class UrlSegment {
public parent: UrlSegment|null = null; public parent: UrlSegment = null;
constructor( constructor(
public pathsWithParams: UrlPathWithParams[], public children: {[key: string]: UrlSegment}) { public pathsWithParams: UrlPathWithParams[], public children: {[key: string]: UrlSegment}) {
forEach(children, (v, k) => v.parent = this); 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): export function mapChildren(segment: UrlSegment, fn: (v: UrlSegment, k: string) => UrlSegment):
{[name: string]: UrlSegment} { {[name: string]: UrlSegment} {
const newChildren = {}; const newChildren: {[name: string]: UrlSegment} = {};
forEach(segment.children, (child, childOutlet) => { forEach(segment.children, (child, childOutlet) => {
if (childOutlet === PRIMARY_OUTLET) { if (childOutlet === PRIMARY_OUTLET) {
newChildren[childOutlet] = fn(child, childOutlet); newChildren[childOutlet] = fn(child, childOutlet);

View File

@ -24,11 +24,11 @@ export function flatten<T>(a: T[][]): T[] {
return target; return target;
} }
export function first<T>(a: T[]): T|null { export function first<T>(a: T[]): T {
return a.length > 0 ? a[0] : null; 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; return a.length > 0 ? a[a.length - 1] : null;
} }

View File

@ -6,7 +6,7 @@ export class Tree<T> {
get root(): T { return this._root.value; } get root(): T { return this._root.value; }
parent(t: T): T|null { parent(t: T): T {
const p = this.pathFromRoot(t); const p = this.pathFromRoot(t);
return p.length > 1 ? p[p.length - 2] : null; 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) : []; return n ? n.children.map(t => t.value) : [];
} }
firstChild(t: T): T|null { firstChild(t: T): T {
const n = findNode(t, this._root); const n = findNode(t, this._root);
return n && n.children.length > 0 ? n.children[0].value : null; 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); } 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; if (expected === c.value) return c;
for (let cc of c.children) { for (let cc of c.children) {
const r = findNode(expected, cc); const r = findNode(expected, cc);

View File

@ -61,7 +61,7 @@ function createState(config: RouterConfig, url: string): RouterStateSnapshot {
return res; 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) { if (actual === null) {
expect(actual).toBeDefined(); expect(actual).toBeDefined();
} else { } else {

View File

@ -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) { if (actual === null) {
expect(actual).not.toBeNull(); expect(actual).not.toBeNull();
} else { } else {