Skip to content

Commit

Permalink
refactor(types): improve of type assertions (#1064)
Browse files Browse the repository at this point in the history
Co-authored-by: webfansplz <>
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com>
  • Loading branch information
webfansplz and posva committed Aug 6, 2021
1 parent 21ea143 commit 4c4c347
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/devtools.ts
Expand Up @@ -533,7 +533,7 @@ function omit<T extends object, K extends [...(keyof T)[]]>(obj: T, keys: K) {
}

for (const key in obj) {
if (!keys.includes(key as any)) {
if (!keys.includes(key)) {
// @ts-expect-error
ret[key] = obj[key]
}
Expand Down
4 changes: 2 additions & 2 deletions src/history/html5.ts
Expand Up @@ -177,10 +177,10 @@ function useHistoryStateNavigation(base: string) {
const { history, location } = window

// private variables
let currentLocation: ValueContainer<HistoryLocation> = {
const currentLocation: ValueContainer<HistoryLocation> = {
value: createCurrentLocation(base, location),
}
let historyState: ValueContainer<StateEntry> = { value: history.state }
const historyState: ValueContainer<StateEntry> = { value: history.state }
// build current history entry as this is a fresh navigation
if (!historyState.value) {
changeLocation(
Expand Down
2 changes: 1 addition & 1 deletion src/history/memory.ts
Expand Up @@ -44,7 +44,7 @@ export function createMemoryHistory(base: string = ''): RouterHistory {
delta,
type: NavigationType.pop,
}
for (let callback of listeners) {
for (const callback of listeners) {
callback(to, from, info)
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/location.ts
Expand Up @@ -93,7 +93,7 @@ export function stringifyURL(
stringifyQuery: (query: LocationQueryRaw) => string,
location: LocationPartial
): string {
let query: string = location.query ? stringifyQuery(location.query) : ''
const query: string = location.query ? stringifyQuery(location.query) : ''
return location.path + (query && '?') + query + (location.hash || '')
}

Expand Down Expand Up @@ -124,8 +124,8 @@ export function isSameRouteLocation(
a: RouteLocation,
b: RouteLocation
): boolean {
let aLastIndex = a.matched.length - 1
let bLastIndex = b.matched.length - 1
const aLastIndex = a.matched.length - 1
const bLastIndex = b.matched.length - 1

return (
aLastIndex > -1 &&
Expand Down Expand Up @@ -157,7 +157,7 @@ export function isSameRouteLocationParams(
): boolean {
if (Object.keys(a).length !== Object.keys(b).length) return false

for (let key in a) {
for (const key in a) {
if (!isSameRouteLocationParamsValue(a[key], b[key])) return false
}

Expand Down
37 changes: 19 additions & 18 deletions src/matcher/index.ts
Expand Up @@ -5,6 +5,7 @@ import {
isRouteName,
RouteRecordName,
_RouteRecordProps,
RouteRecordRedirect,
} from '../types'
import { createRouterError, ErrorTypes, MatcherError } from '../errors'
import { createRouteRecordMatcher, RouteRecordMatcher } from './pathMatcher'
Expand Down Expand Up @@ -76,8 +77,8 @@ export function createRouterMatcher(
originalRecord?: RouteRecordMatcher
) {
// used later on to remove by name
let isRootAdd = !originalRecord
let mainNormalizedRecord = normalizeRouteRecord(record)
const isRootAdd = !originalRecord
const mainNormalizedRecord = normalizeRouteRecord(record)
// we might be the child of an alias
mainNormalizedRecord.aliasOf = originalRecord && originalRecord.record
const options: PathParserOptions = mergeOptions(globalOptions, record)
Expand Down Expand Up @@ -112,13 +113,13 @@ export function createRouterMatcher(
let originalMatcher: RouteRecordMatcher | undefined

for (const normalizedRecord of normalizedRecords) {
let { path } = normalizedRecord
const { path } = normalizedRecord
// Build up the path for nested routes if the child isn't an absolute
// route. Only add the / delimiter if the child path isn't empty and if the
// parent path doesn't have a trailing slash
if (parent && path[0] !== '/') {
let parentPath = parent.record.path
let connectingSlash =
const parentPath = parent.record.path
const connectingSlash =
parentPath[parentPath.length - 1] === '/' ? '' : '/'
normalizedRecord.path =
parent.record.path + (path && connectingSlash + path)
Expand Down Expand Up @@ -156,7 +157,7 @@ export function createRouterMatcher(
}

if ('children' in mainNormalizedRecord) {
let children = mainNormalizedRecord.children
const children = mainNormalizedRecord.children
for (let i = 0; i < children.length; i++) {
addRoute(
children[i],
Expand Down Expand Up @@ -196,7 +197,7 @@ export function createRouterMatcher(
matcher.alias.forEach(removeRoute)
}
} else {
let index = matchers.indexOf(matcherRef)
const index = matchers.indexOf(matcherRef)
if (index > -1) {
matchers.splice(index, 1)
if (matcherRef.record.name) matcherMap.delete(matcherRef.record.name)
Expand Down Expand Up @@ -322,9 +323,9 @@ function paramsFromLocation(
params: MatcherLocation['params'],
keys: string[]
): MatcherLocation['params'] {
let newParams = {} as MatcherLocation['params']
const newParams = {} as MatcherLocation['params']

for (let key of keys) {
for (const key of keys) {
if (key in params) newParams[key] = params[key]
}

Expand Down Expand Up @@ -370,13 +371,14 @@ function normalizeRecordProps(
): Record<string, _RouteRecordProps> {
const propsObject = {} as Record<string, _RouteRecordProps>
// props does not exist on redirect records but we can set false directly
const props = (record as any).props || false
const props =
(record as Exclude<RouteRecordRaw, RouteRecordRedirect>).props || false
if ('component' in record) {
propsObject.default = props
} else {
// NOTE: we could also allow a function to be applied to every component.
// Would need user feedback for use cases
for (let name in record.components)
for (const name in record.components)
propsObject[name] = typeof props === 'boolean' ? props : props[name]
}

Expand Down Expand Up @@ -409,10 +411,9 @@ function mergeMetaFields(matched: MatcherLocation['matched']) {
}

function mergeOptions<T>(defaults: T, partialOptions: Partial<T>): T {
let options = {} as T
for (let key in defaults) {
options[key] =
key in partialOptions ? partialOptions[key] : (defaults[key] as any)
const options = {} as T
for (const key in defaults) {
options[key] = key in partialOptions ? partialOptions[key]! : defaults[key]
}

return options
Expand All @@ -435,13 +436,13 @@ function isSameParam(a: ParamKey, b: ParamKey): boolean {
* @param b - alias record
*/
function checkSameParams(a: RouteRecordMatcher, b: RouteRecordMatcher) {
for (let key of a.keys) {
for (const key of a.keys) {
if (!key.optional && !b.keys.find(isSameParam.bind(null, key)))
return warn(
`Alias "${b.record.path}" and the original record: "${a.record.path}" should have the exact same param named "${key.name}"`
)
}
for (let key of b.keys) {
for (const key of b.keys) {
if (!key.optional && !a.keys.find(isSameParam.bind(null, key)))
return warn(
`Alias "${b.record.path}" and the original record: "${a.record.path}" should have the exact same param named "${key.name}"`
Expand All @@ -453,7 +454,7 @@ function checkMissingParamsInAbsolutePath(
record: RouteRecordMatcher,
parent: RouteRecordMatcher
) {
for (let key of parent.keys) {
for (const key of parent.keys) {
if (!record.keys.find(isSameParam.bind(null, key)))
return warn(
`Absolute path "${record.record.path}" should have the exact same param named "${key.name}" as its parent "${parent.record.path}".`
Expand Down
2 changes: 1 addition & 1 deletion src/matcher/pathParserRanker.ts
Expand Up @@ -122,7 +122,7 @@ export function tokensToParser(
const options = assign({}, BASE_PATH_PARSER_OPTIONS, extraOptions)

// the amount of scores is the same as the length of segments except for the root segment "/"
let score: Array<number[]> = []
const score: Array<number[]> = []
// the regexp as a string
let pattern = options.start ? '^' : ''
// extracted keys
Expand Down
2 changes: 1 addition & 1 deletion src/router.ts
Expand Up @@ -827,7 +827,7 @@ export function createRouter(options: RouterOptions): Router {
guards = []
for (const record of to.matched) {
// do not trigger beforeEnter on reused views
if (record.beforeEnter && !from.matched.includes(record as any)) {
if (record.beforeEnter && !from.matched.includes(record)) {
if (Array.isArray(record.beforeEnter)) {
for (const beforeEnter of record.beforeEnter)
guards.push(guardToPromiseFn(beforeEnter, to, from))
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.ts
Expand Up @@ -23,4 +23,4 @@ export function applyToParams(
return newParams
}

export let noop = () => {}
export const noop = () => {}

0 comments on commit 4c4c347

Please sign in to comment.