Skip to content

Improve flow declarations #682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions flow/declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,61 @@ declare module 'path-to-regexp' {
}
}

declare type Dictionary<T> = { [key: string]: T }

declare type RouterOptions = {
routes?: Array<RouteConfig>;
mode?: string;
base?: string;
linkActiveClass?: string;
scrollBehavior?: Function;
scrollBehavior?: (
to: Route,
from: Route,
savedPosition: ?{ x: number, y: number }
) => { x: number, y: number } | { selector: string } | ?{};
}

declare type RedirectOption = RawLocation | Function
declare type RedirectOption = RawLocation | ((to: Route) => RawLocation)

declare type RouteConfig = {
path: string;
name?: string;
component?: any;
components?: { [name: string]: any };
components?: Dictionary<any>;
redirect?: RedirectOption;
alias?: string | Array<string>;
children?: Array<RouteConfig>;
beforeEnter?: Function;
beforeEnter?: (
route: Route,
redirect: (location: RawLocation) => void,
next: () => void
) => any;
meta?: any;
}

declare type RouteRecord = {
path: string;
components: { [name: string]: any };
instances: { [name: string]: any };
components: Dictionary<any>;
instances: Dictionary<any>;
name: ?string;
parent: ?RouteRecord;
redirect: ?RedirectOption;
matchAs: ?string;
beforeEnter: ?Function;
beforeEnter: ?(
route: Route,
redirect: (location: RawLocation) => void,
next: () => void
) => any;
meta: any;
}

declare type RouteMap = {
[key: string]: RouteRecord;
}

declare type StringHash = { [key: string]: string }

declare type Location = {
_normalized?: boolean;
name?: string;
path?: string;
hash?: string;
query?: StringHash;
params?: StringHash;
query?: Dictionary<string>;
params?: Dictionary<string>;
}

declare type RawLocation = string | Location
Expand All @@ -60,8 +68,8 @@ declare type Route = {
path: string;
name: ?string;
hash: string;
query: StringHash;
params: StringHash;
query: Dictionary<string>;
params: Dictionary<string>;
fullPath: string;
matched: Array<RouteRecord>;
redirectedFrom?: string;
Expand Down
12 changes: 6 additions & 6 deletions src/create-route-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { assert, warn } from './util/warn'
import { cleanPath } from './util/path'

export function createRouteMap (routes: Array<RouteConfig>): {
pathMap: RouteMap,
nameMap: RouteMap
pathMap: Dictionary<RouteRecord>,
nameMap: Dictionary<RouteRecord>
} {
const pathMap: RouteMap = Object.create(null)
const nameMap: RouteMap = Object.create(null)
const pathMap: Dictionary<RouteRecord> = Object.create(null)
const nameMap: Dictionary<RouteRecord> = Object.create(null)

routes.forEach(route => {
addRouteRecord(pathMap, nameMap, route)
Expand All @@ -21,8 +21,8 @@ export function createRouteMap (routes: Array<RouteConfig>): {
}

function addRouteRecord (
pathMap: RouteMap,
nameMap: RouteMap,
pathMap: Dictionary<RouteRecord>,
nameMap: Dictionary<RouteRecord>,
route: RouteConfig,
parent?: RouteRecord,
matchAs?: string
Expand Down
2 changes: 1 addition & 1 deletion src/history/html5.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class HTML5History extends History {
return
}
const isObject = typeof shouldScroll === 'object'
if (isObject && shouldScroll.selector) {
if (isObject && typeof shouldScroll.selector === 'string') {
const el = document.querySelector(shouldScroll.selector)
if (el) {
position = getElementPosition(el)
Expand Down
8 changes: 4 additions & 4 deletions src/util/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const decode = decodeURIComponent

export function resolveQuery (
query: ?string,
extraQuery: StringHash = {}
): StringHash {
extraQuery: Dictionary<string> = {}
): Dictionary<string> {
if (query) {
let parsedQuery
try {
Expand All @@ -26,7 +26,7 @@ export function resolveQuery (
}
}

function parseQuery (query: string): StringHash {
function parseQuery (query: string): Dictionary<string> {
const res = Object.create(null)

query = query.trim().replace(/^(\?|#|&)/, '')
Expand Down Expand Up @@ -54,7 +54,7 @@ function parseQuery (query: string): StringHash {
return res
}

export function stringifyQuery (obj: StringHash): string {
export function stringifyQuery (obj: Dictionary<string>): string {
const res = obj ? Object.keys(obj).sort().map(key => {
const val = obj[key]

Expand Down
2 changes: 1 addition & 1 deletion src/util/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function isIncludedRoute (current: Route, target: Route): boolean {
)
}

function queryIncludes (current: StringHash, target: StringHash): boolean {
function queryIncludes (current: Dictionary<string>, target: Dictionary<string>): boolean {
for (const key in target) {
if (!(key in current)) {
return false
Expand Down