Skip to content

feat(no-this-in-before-router-enter): create rule #1506

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
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
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ For example:
| [vue/no-restricted-v-bind](./no-restricted-v-bind.md) | disallow specific argument in `v-bind` | |
| [vue/no-static-inline-styles](./no-static-inline-styles.md) | disallow static inline `style` attributes | |
| [vue/no-template-target-blank](./no-template-target-blank.md) | disallow target="_blank" attribute without rel="noopener noreferrer" | |
| [vue/no-this-in-before-route-enter](./no-this-in-before-route-enter.md) | disallow this usage in a beforeRouteEnter method | |
| [vue/no-unregistered-components](./no-unregistered-components.md) | disallow using components that are not registered inside templates | |
| [vue/no-unsupported-features](./no-unsupported-features.md) | disallow unsupported Vue.js syntax on the specified version | :wrench: |
| [vue/no-unused-properties](./no-unused-properties.md) | disallow unused properties | |
Expand Down
115 changes: 115 additions & 0 deletions docs/rules/no-this-in-before-route-enter.md
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

Copy link
Member

@ota-meshi ota-meshi Jun 3, 2021

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.

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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use <eslint-code-block> like any other docs to build your docs for online demos. You can try it locally with the npm run docs:watch command.


<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)
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ module.exports = {
'no-template-shadow': require('./rules/no-template-shadow'),
'no-template-target-blank': require('./rules/no-template-target-blank'),
'no-textarea-mustache': require('./rules/no-textarea-mustache'),
'no-this-in-before-route-enter': require('./rules/no-this-in-before-route-enter'),
'no-unregistered-components': require('./rules/no-unregistered-components'),
'no-unsupported-features': require('./rules/no-unsupported-features'),
'no-unused-components': require('./rules/no-unused-components'),
Expand Down
91 changes: 91 additions & 0 deletions lib/rules/no-this-in-before-route-enter.js
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'
})
}
}
})
}
}
Loading