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
feat(no-this-in-before-router-enter): create rule #1506
Merged
ota-meshi
merged 7 commits into
vuejs:master
from
przemyslawjanpietrzak:feat/no-this-in-before-route-enter
Jun 9, 2021
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2870d51
feat(no-this-in-before-router-enter): create rule
przemyslawjanpietrzak 7ef747a
Update lib/rules/no-this-in-before-route-enter.js
przemyslawjanpietrzak 792dc1d
Update docs/rules/no-this-in-before-route-enter.md
przemyslawjanpietrzak df6b1ae
feat(no-this-in-before-router-enter): create rule
przemyslawjanpietrzak 9c8e458
Merge branch 'feat/no-this-in-before-route-enter' of github.com:przem…
przemyslawjanpietrzak 188c81a
(feat): update test assertions for no-this-in-before-route-enter rule
przemyslawjanpietrzak 0c81b90
(feat): update docs for no-this-in-before-route-enter
przemyslawjanpietrzak File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-this-in-before-route-enter | ||
description: disallow this usage in a beforeRouteEnter method | ||
--- | ||
# vue/no-this-in-before-route-enter | ||
|
||
> disallow this usage in a beforeRouteEnter method | ||
|
||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> | ||
|
||
## Rule Details | ||
|
||
Because lack of `this` in the `beforeRouteEnter` [(docs)](https://router.vuejs.org/guide/advanced/navigation-guards.html#in-component-guards). This behavior isn't obvious, so it's pretty easy to make a `TypeError`. Especially during some refactor. | ||
|
||
Bad: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
|
||
<eslint-code-block :rules="{'vue/no-this-in-before-route-enter': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
export default { | ||
beforeRouteEnter() { | ||
this.method(); // Uncaught TypeError: Cannot read property 'method' of undefined | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
Bad: | ||
|
||
<eslint-code-block :rules="{'vue/no-this-in-before-route-enter': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
export default { | ||
beforeRouteEnter() { | ||
this.attribute = 42; | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
Bad: | ||
|
||
<eslint-code-block :rules="{'vue/no-this-in-before-route-enter': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
export default { | ||
beforeRouteEnter() { | ||
if (this.value === 42) { | ||
|
||
} | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
|
||
Bad: | ||
|
||
<eslint-code-block :rules="{'vue/no-this-in-before-route-enter': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
export default { | ||
beforeRouteEnter() { | ||
this.attribute = this.method(); | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
Good: | ||
|
||
<eslint-code-block :rules="{'vue/no-this-in-before-route-enter': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
export default { | ||
beforeRouteEnter() { | ||
// anything without this | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
### Options | ||
|
||
Nothing. | ||
|
||
## When Not To Use It | ||
|
||
When [vue-router](https://router.vuejs.org/) is not installed. | ||
|
||
## Further Reading | ||
|
||
[vue-router - in-component-guards](https://router.vuejs.org/guide/advanced/navigation-guards.html#in-component-guards) | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-this-in-before-route-enter.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-this-in-before-route-enter.js) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* @fileoverview Don't use this in a beforeRouteEnter method | ||
* @author Przemyslaw Jan Beigert | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'disallow this usage in a beforeRouteEnter method', | ||
categories: null, | ||
url: 'https://eslint.vuejs.org/rules/no-this-in-before-route-enter.html' | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
disallow: | ||
"'beforeRouteEnter' does NOT have access to `this` component instance. https://router.vuejs.org/guide/advanced/navigation-guards.html#in-component-guards." | ||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
/** | ||
* @typedef {object} ScopeStack | ||
* @property {ScopeStack | null} upper | ||
* @property {FunctionExpression | FunctionDeclaration} node | ||
* @property {boolean} beforeRouteEnter | ||
*/ | ||
/** @type {Set<FunctionExpression>} */ | ||
const beforeRouteEnterFunctions = new Set() | ||
/** @type {ScopeStack | null} */ | ||
let scopeStack = null | ||
|
||
/** | ||
* @param {FunctionExpression | FunctionDeclaration | ArrowFunctionExpression} node | ||
*/ | ||
function onFunctionEnter(node) { | ||
if (node.type === 'ArrowFunctionExpression') { | ||
return | ||
} | ||
scopeStack = { | ||
upper: scopeStack, | ||
node, | ||
beforeRouteEnter: beforeRouteEnterFunctions.has( | ||
/** @type {never} */ (node) | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* @param {FunctionExpression | FunctionDeclaration | ArrowFunctionExpression} node | ||
*/ | ||
function onFunctionExit(node) { | ||
if (scopeStack && scopeStack.node === node) { | ||
scopeStack = scopeStack.upper | ||
} | ||
} | ||
return utils.defineVueVisitor(context, { | ||
onVueObjectEnter(node) { | ||
const beforeRouteEnter = utils.findProperty(node, 'beforeRouteEnter') | ||
if ( | ||
beforeRouteEnter && | ||
beforeRouteEnter.value.type === 'FunctionExpression' | ||
) { | ||
beforeRouteEnterFunctions.add(beforeRouteEnter.value) | ||
} | ||
}, | ||
':function': onFunctionEnter, | ||
':function:exit': onFunctionExit, | ||
ThisExpression(node) { | ||
if (scopeStack && scopeStack.beforeRouteEnter) { | ||
context.report({ | ||
node, | ||
messageId: 'disallow' | ||
}) | ||
} | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you write a description of the rule here? Documents should automatically update their headers and footers with the
npm run update
command.