-
-
Notifications
You must be signed in to change notification settings - Fork 680
Add new vue/prefer-define-component
rule
#2738
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
Open
thesheppard
wants to merge
5
commits into
vuejs:master
Choose a base branch
from
thesheppard:prefer-define-component
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fd75893
feat: add prefer-define-component rule
thesheppard 58343cb
docs: update docs
thesheppard 94b6915
Merge branch 'master' into prefer-define-component
thesheppard 2a6be72
Merge branch 'master' into prefer-define-component
thesheppard 7ac6c2c
Revert formatting changes for markdown
thesheppard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,96 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/prefer-define-component | ||
description: require components to be defined using `defineComponent` | ||
--- | ||
|
||
# vue/prefer-define-component | ||
|
||
> require components to be defined using `defineComponent` | ||
|
||
## :book: Rule Details | ||
|
||
This rule enforces the use of `defineComponent` when defining Vue components. Using `defineComponent` provides proper typing in Vue 3 and IDE support for object properties. | ||
|
||
<eslint-code-block :rules="{'vue/prefer-define-component': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
import { defineComponent } from 'vue' | ||
|
||
/* ✓ GOOD */ | ||
export default defineComponent({ | ||
name: 'ComponentA', | ||
props: { | ||
message: String | ||
} | ||
}) | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
<eslint-code-block :rules="{'vue/prefer-define-component': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
/* ✗ BAD */ | ||
export default { | ||
name: 'ComponentA', | ||
props: { | ||
message: String | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
<eslint-code-block :rules="{'vue/prefer-define-component': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
/* ✗ BAD */ | ||
export default Vue.extend({ | ||
name: 'ComponentA', | ||
props: { | ||
message: String | ||
} | ||
}) | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
This rule doesn't report components using `<script setup>` without a normal `<script>` tag, as those don't require `defineComponent`. | ||
|
||
<eslint-code-block :rules="{'vue/prefer-define-component': ['error']}"> | ||
|
||
```vue | ||
<script setup> | ||
/* ✓ GOOD - script setup doesn't need defineComponent */ | ||
import { ref } from 'vue' | ||
const count = ref(0) | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :couple: Related Rules | ||
|
||
- [vue/require-default-export](./require-default-export.md) | ||
- [vue/require-direct-export](./require-direct-export.md) | ||
|
||
## :books: Further Reading | ||
|
||
- [Vue.js Guide - TypeScript with Composition API](https://vuejs.org/guide/typescript/composition-api.html#typing-component-props) | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/prefer-define-component.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/prefer-define-component.js) |
This file contains hidden or 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 hidden or 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,114 @@ | ||
/** | ||
* @author Kamogelo Moalusi <github.com/thesheppard> | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
// @ts-nocheck | ||
const utils = require('../utils') | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'enforce components to be defined using `defineComponent`', | ||
categories: ['vue3-recommended', 'vue2-recommended'], | ||
url: 'https://eslint.vuejs.org/rules/prefer-define-component.html' | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
'prefer-define-component': 'Use `defineComponent` to define a component.' | ||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
const filePath = context.getFilename() | ||
if (!utils.isVueFile(filePath)) return {} | ||
|
||
const sourceCode = context.getSourceCode() | ||
const documentFragment = sourceCode.parserServices.getDocumentFragment?.() | ||
|
||
// Check if there's a non-setup script tag | ||
const hasNormalScript = | ||
documentFragment && | ||
documentFragment.children.some( | ||
(e) => | ||
utils.isVElement(e) && | ||
e.name === 'script' && | ||
(!e.startTag.attributes || | ||
!e.startTag.attributes.some((attr) => attr.key.name === 'setup')) | ||
) | ||
|
||
// If no regular script tag, we don't need to check | ||
if (!hasNormalScript) return {} | ||
|
||
// Skip checking if there's only a setup script (no normal script) | ||
if (utils.isScriptSetup(context) && !hasNormalScript) return {} | ||
|
||
let hasDefineComponent = false | ||
/** @type {ExportDefaultDeclaration | null} */ | ||
let exportDefaultNode = null | ||
let hasVueExtend = false | ||
|
||
return utils.compositingVisitors(utils.defineVueVisitor(context, {}), { | ||
/** @param {ExportDefaultDeclaration} node */ | ||
'Program > ExportDefaultDeclaration'(node) { | ||
exportDefaultNode = node | ||
}, | ||
|
||
/** @param {CallExpression} node */ | ||
'Program > ExportDefaultDeclaration > CallExpression'(node) { | ||
if ( | ||
node.callee.type === 'Identifier' && | ||
node.callee.name === 'defineComponent' | ||
) { | ||
hasDefineComponent = true | ||
return | ||
} | ||
|
||
// Support aliased imports | ||
if (node.callee.type === 'Identifier') { | ||
const variable = utils.findVariableByIdentifier(context, node.callee) | ||
if ( | ||
variable && | ||
variable.defs && | ||
variable.defs.length > 0 && | ||
variable.defs[0].node.type === 'ImportSpecifier' && | ||
variable.defs[0].node.imported && | ||
variable.defs[0].node.imported.name === 'defineComponent' | ||
) { | ||
hasDefineComponent = true | ||
return | ||
} | ||
} | ||
|
||
// Check for Vue.extend case | ||
if ( | ||
node.callee.type === 'MemberExpression' && | ||
node.callee.object && | ||
node.callee.object.type === 'Identifier' && | ||
node.callee.object.name === 'Vue' && | ||
node.callee.property && | ||
node.callee.property.type === 'Identifier' && | ||
node.callee.property.name === 'extend' | ||
) { | ||
hasVueExtend = true | ||
} | ||
}, | ||
|
||
'Program > ExportDefaultDeclaration > ObjectExpression'() { | ||
hasDefineComponent = false | ||
}, | ||
|
||
'Program:exit'() { | ||
if (exportDefaultNode && (hasVueExtend || !hasDefineComponent)) { | ||
context.report({ | ||
node: exportDefaultNode, | ||
messageId: 'prefer-define-component' | ||
}) | ||
} | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
This link definition is now unused. Please run
npm run update
to regenerate the docs, then both Markdownlint errors should be fixed.