Skip to content

Commit c4f67b4

Browse files
committed
refactor: refactor warn function
1 parent e4fe767 commit c4f67b4

File tree

11 files changed

+75
-97
lines changed

11 files changed

+75
-97
lines changed

src/components/link.js

+18-25
Original file line numberDiff line numberDiff line change
@@ -112,40 +112,33 @@ export default {
112112
})
113113

114114
if (scopedSlot) {
115-
if (process.env.NODE_ENV !== 'production' && !this.custom) {
116-
!warnedCustomSlot && warn(false, 'In Vue Router 4, the v-slot API will by default wrap its content with an <a> element. Use the custom prop to remove this warning:\n<router-link v-slot="{ navigate, href }" custom></router-link>\n')
115+
if (!this.custom) {
116+
!warnedCustomSlot && warn('In Vue Router 4, the v-slot API will by default wrap its content with an <a> element. Use the custom prop to remove this warning:\n<router-link v-slot="{ navigate, href }" custom></router-link>\n')
117117
warnedCustomSlot = true
118118
}
119119
if (scopedSlot.length === 1) {
120120
return scopedSlot[0]
121121
} else if (scopedSlot.length > 1 || !scopedSlot.length) {
122-
if (process.env.NODE_ENV !== 'production') {
123-
warn(
124-
false,
125-
`<router-link> with to="${
126-
this.to
127-
}" is trying to use a scoped slot but it didn't provide exactly one child. Wrapping the content with a span element.`
128-
)
129-
}
122+
warn(
123+
`<router-link> with to="${
124+
this.to
125+
}" is trying to use a scoped slot but it didn't provide exactly one child. Wrapping the content with a span element.`
126+
)
130127
return scopedSlot.length === 0 ? h() : h('span', {}, scopedSlot)
131128
}
132129
}
133130

134-
if (process.env.NODE_ENV !== 'production') {
135-
if ('tag' in this.$options.propsData && !warnedTagProp) {
136-
warn(
137-
false,
138-
`<router-link>'s tag prop is deprecated and has been removed in Vue Router 4. Use the v-slot API to remove this warning: https://next.router.vuejs.org/guide/migration/#removal-of-event-and-tag-props-in-router-link.`
139-
)
140-
warnedTagProp = true
141-
}
142-
if ('event' in this.$options.propsData && !warnedEventProp) {
143-
warn(
144-
false,
145-
`<router-link>'s event prop is deprecated and has been removed in Vue Router 4. Use the v-slot API to remove this warning: https://next.router.vuejs.org/guide/migration/#removal-of-event-and-tag-props-in-router-link.`
146-
)
147-
warnedEventProp = true
148-
}
131+
if ('tag' in this.$options.propsData && !warnedTagProp) {
132+
warn(
133+
`<router-link>'s tag prop is deprecated and has been removed in Vue Router 4. Use the v-slot API to remove this warning: https://next.router.vuejs.org/guide/migration/#removal-of-event-and-tag-props-in-router-link.`
134+
)
135+
warnedTagProp = true
136+
}
137+
if ('event' in this.$options.propsData && !warnedEventProp) {
138+
warn(
139+
`<router-link>'s event prop is deprecated and has been removed in Vue Router 4. Use the v-slot API to remove this warning: https://next.router.vuejs.org/guide/migration/#removal-of-event-and-tag-props-in-router-link.`
140+
)
141+
warnedEventProp = true
149142
}
150143

151144
if (this.tag === 'a') {

src/components/view.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,9 @@ function resolveProps (route, config) {
144144
case 'boolean':
145145
return config ? route.params : undefined
146146
default:
147-
if (process.env.NODE_ENV !== 'production') {
148-
warn(
149-
false,
150-
`props in "${route.path}" is a ${typeof config}, ` +
151-
`expecting an object, function or boolean.`
152-
)
153-
}
147+
warn(
148+
`props in "${route.path}" is a ${typeof config}, ` +
149+
`expecting an object, function or boolean.`
150+
)
154151
}
155152
}

src/create-matcher.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ export function createMatcher (
5858

5959
if (name) {
6060
const record = nameMap[name]
61-
if (process.env.NODE_ENV !== 'production') {
62-
warn(record, `Route with name '${name}' does not exist`)
61+
if (!record) {
62+
warn(`Route with name '${name}' does not exist`)
6363
}
6464
if (!record) return _createRoute(null, location)
6565
const paramNames = record.regex.keys
@@ -108,11 +108,9 @@ export function createMatcher (
108108
}
109109

110110
if (!redirect || typeof redirect !== 'object') {
111-
if (process.env.NODE_ENV !== 'production') {
112-
warn(
113-
false, `invalid redirect option: ${JSON.stringify(redirect)}`
114-
)
115-
}
111+
warn(
112+
`invalid redirect option: ${JSON.stringify(redirect)}`
113+
)
116114
return _createRoute(null, location)
117115
}
118116

@@ -149,9 +147,7 @@ export function createMatcher (
149147
hash
150148
}, undefined, location)
151149
} else {
152-
if (process.env.NODE_ENV !== 'production') {
153-
warn(false, `invalid redirect option: ${JSON.stringify(redirect)}`)
154-
}
150+
warn(`invalid redirect option: ${JSON.stringify(redirect)}`)
155151
return _createRoute(null, location)
156152
}
157153
}

src/create-route-map.js

+33-41
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,14 @@ export function createRouteMap (
3535
}
3636
}
3737

38-
if (process.env.NODE_ENV === 'development') {
39-
// warn if routes do not include leading slashes
40-
const found = pathList
41-
// check for missing leading slash
42-
.filter(path => path && path.charAt(0) !== '*' && path.charAt(0) !== '/')
43-
44-
if (found.length > 0) {
45-
const pathNames = found.map(path => `- ${path}`).join('\n')
46-
warn(false, `Non-nested routes must include a leading slash character. Fix the following routes: \n${pathNames}`)
47-
}
38+
// warn if routes do not include leading slashes
39+
const found = pathList
40+
// check for missing leading slash
41+
.filter(path => path && path.charAt(0) !== '*' && path.charAt(0) !== '/')
42+
43+
if (found.length > 0) {
44+
const pathNames = found.map(path => `- ${path}`).join('\n')
45+
warn(`Non-nested routes must include a leading slash character. Fix the following routes: \n${pathNames}`)
4846
}
4947

5048
return {
@@ -71,10 +69,10 @@ function addRouteRecord (
7169
path || name
7270
)} cannot be a ` + `string id. Use an actual component instead.`
7371
)
74-
72+
}
73+
// eslint-disable-next-line no-control-regex
74+
if (/[^\u0000-\u007F]+/.test(path)) {
7575
warn(
76-
// eslint-disable-next-line no-control-regex
77-
!/[^\u0000-\u007F]+/.test(path),
7876
`Route with path "${path}" contains unencoded characters, make sure ` +
7977
`your path is correctly encoded before passing it to the router. Use ` +
8078
`encodeURI to encode static segments of your path.`
@@ -118,23 +116,20 @@ function addRouteRecord (
118116
// Warn if route is named, does not redirect and has a default child route.
119117
// If users navigate to this route by name, the default child will
120118
// not be rendered (GH Issue #629)
121-
if (process.env.NODE_ENV !== 'production') {
122-
if (
123-
route.name &&
124-
!route.redirect &&
125-
route.children.some(child => /^\/?$/.test(child.path))
126-
) {
127-
warn(
128-
false,
129-
`Named Route '${route.name}' has a default child route. ` +
130-
`When navigating to this named route (:to="{name: '${
131-
route.name
132-
}'"), ` +
133-
`the default child route will not be rendered. Remove the name from ` +
134-
`this route and use the name of the default child route for named ` +
135-
`links instead.`
136-
)
137-
}
119+
if (
120+
route.name &&
121+
!route.redirect &&
122+
route.children.some(child => /^\/?$/.test(child.path))
123+
) {
124+
warn(
125+
`Named Route '${route.name}' has a default child route. ` +
126+
`When navigating to this named route (:to="{name: '${
127+
route.name
128+
}'"), ` +
129+
`the default child route will not be rendered. Remove the name from ` +
130+
`this route and use the name of the default child route for named ` +
131+
`links instead.`
132+
)
138133
}
139134
route.children.forEach(child => {
140135
const childMatchAs = matchAs
@@ -153,9 +148,8 @@ function addRouteRecord (
153148
const aliases = Array.isArray(route.alias) ? route.alias : [route.alias]
154149
for (let i = 0; i < aliases.length; ++i) {
155150
const alias = aliases[i]
156-
if (process.env.NODE_ENV !== 'production' && alias === path) {
151+
if (alias === path) {
157152
warn(
158-
false,
159153
`Found an alias with the same value as the path: "${path}". You have to remove that alias. It will be ignored in development.`
160154
)
161155
// skip in dev to make it work
@@ -180,9 +174,8 @@ function addRouteRecord (
180174
if (name) {
181175
if (!nameMap[name]) {
182176
nameMap[name] = record
183-
} else if (process.env.NODE_ENV !== 'production' && !matchAs) {
177+
} else if (!matchAs) {
184178
warn(
185-
false,
186179
`Duplicate named routes definition: ` +
187180
`{ name: "${name}", path: "${record.path}" }`
188181
)
@@ -195,16 +188,15 @@ function compileRouteRegex (
195188
pathToRegexpOptions: PathToRegexpOptions
196189
): RouteRegExp {
197190
const regex = Regexp(path, [], pathToRegexpOptions)
198-
if (process.env.NODE_ENV !== 'production') {
199-
const keys: any = Object.create(null)
200-
regex.keys.forEach(key => {
191+
const keys: any = Object.create(null)
192+
regex.keys.forEach(key => {
193+
if (keys[key.name]) {
201194
warn(
202-
!keys[key.name],
203195
`Duplicate param keys in route with path: "${path}"`
204196
)
205-
keys[key.name] = true
206-
})
207-
}
197+
}
198+
keys[key.name] = true
199+
})
208200
return regex
209201
}
210202

src/history/base.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export class History {
146146
cb(err)
147147
})
148148
} else {
149-
warn(false, 'uncaught error during route navigation:')
149+
warn('uncaught error during route navigation:')
150150
console.error(err)
151151
}
152152
}

src/index.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ export default class VueRouter {
3939
afterHooks: Array<?AfterNavigationHook>
4040

4141
constructor (options: RouterOptions = {}) {
42+
if (!(this instanceof VueRouter)) {
43+
warn('Router must be called with the new operator.')
44+
}
45+
4246
this.app = null
4347
this.apps = []
4448
this.options = options
@@ -257,9 +261,7 @@ export default class VueRouter {
257261
}
258262

259263
addRoutes (routes: Array<RouteConfig>) {
260-
if (process.env.NODE_ENV !== 'production') {
261-
warn(false, 'router.addRoutes() is deprecated and has been removed in Vue Router 4. Use router.addRoute() instead.')
262-
}
264+
warn('router.addRoutes() is deprecated and has been removed in Vue Router 4. Use router.addRoute() instead.')
263265
this.matcher.addRoutes(routes)
264266
if (this.history.current !== START) {
265267
this.history.transitionTo(this.history.getCurrentLocation())

src/util/location.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function normalizeLocation (
3838
const rawPath = current.matched[current.matched.length - 1].path
3939
next.path = fillParams(rawPath, params, `path ${current.path}`)
4040
} else if (process.env.NODE_ENV !== 'production') {
41-
warn(false, `relative params navigation requires a current route.`)
41+
warn(`relative params navigation requires a current route.`)
4242
}
4343
return next
4444
}

src/util/params.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ export function fillParams (
2525

2626
return filler(params, { pretty: true })
2727
} catch (e) {
28-
if (process.env.NODE_ENV !== 'production') {
28+
if (!typeof params.pathMatch === 'string') {
2929
// Fix #3072 no warn if `pathMatch` is string
30-
warn(typeof params.pathMatch === 'string', `missing param for ${routeMsg}: ${e.message}`)
30+
warn(`missing param for ${routeMsg}: ${e.message}`)
3131
}
3232
return ''
3333
} finally {

src/util/query.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ export function decode (str: string) {
1818
try {
1919
return decodeURIComponent(str)
2020
} catch (err) {
21-
if (process.env.NODE_ENV !== 'production') {
22-
warn(false, `Error decoding "${str}". Leaving it intact.`)
23-
}
21+
warn(`Error decoding "${str}". Leaving it intact.`)
2422
}
2523
return str
2624
}
@@ -35,7 +33,7 @@ export function resolveQuery (
3533
try {
3634
parsedQuery = parse(query || '')
3735
} catch (e) {
38-
process.env.NODE_ENV !== 'production' && warn(false, e.message)
36+
warn(e.message)
3937
parsedQuery = {}
4038
}
4139
for (const key in extraQuery) {

src/util/resolve-components.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function resolveAsyncComponents (matched: Array<RouteRecord>): Function {
3737

3838
const reject = once(reason => {
3939
const msg = `Failed to resolve async component ${key}: ${reason}`
40-
process.env.NODE_ENV !== 'production' && warn(false, msg)
40+
warn(msg)
4141
if (!error) {
4242
error = isError(reason)
4343
? reason

src/util/warn.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ export function assert (condition: any, message: string) {
66
}
77
}
88

9-
export function warn (condition: any, message: string) {
10-
if (process.env.NODE_ENV !== 'production' && !condition) {
9+
export function warn (message: string) {
10+
if (process.env.NODE_ENV !== 'production') {
1111
typeof console !== 'undefined' && console.warn(`[vue-router] ${message}`)
1212
}
1313
}

0 commit comments

Comments
 (0)