Skip to content
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

Add type-based declaration rule of defineProps and defineEmits #1968

Merged
merged 20 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ For example:
| [vue/component-name-in-template-casing](./component-name-in-template-casing.md) | enforce specific casing for the component naming style in template | :wrench: | :hammer: |
| [vue/component-options-name-casing](./component-options-name-casing.md) | enforce the casing of component name in `components` options | :wrench::bulb: | :hammer: |
| [vue/custom-event-name-casing](./custom-event-name-casing.md) | enforce specific casing for custom event name | | :hammer: |
| [vue/define-emits-declaration](./define-emits-declaration.md) | enforce declaration style of `defineEmits` | | :hammer: |
| [vue/define-macros-order](./define-macros-order.md) | enforce order of `defineEmits` and `defineProps` compiler macros | :wrench: | :lipstick: |
| [vue/define-props-declaration](./define-props-declaration.md) | enforce declaration style of `defineProps` | | :hammer: |
| [vue/html-button-has-type](./html-button-has-type.md) | disallow usage of button without an explicit type attribute | | :hammer: |
| [vue/html-comment-content-newline](./html-comment-content-newline.md) | enforce unified line brake in HTML comments | :wrench: | :lipstick: |
| [vue/html-comment-content-spacing](./html-comment-content-spacing.md) | enforce unified spacing in HTML comments | :wrench: | :lipstick: |
Expand Down
67 changes: 67 additions & 0 deletions docs/rules/define-emits-declaration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/define-emits-declaration
description: enforce declaration style of `defineEmits`
---
# vue/define-emits-declaration

> enforce declaration style of `defineEmits`

- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>

## :book: Rule Details

This rule enforces `defineEmits` typing style which you should use `type-based` or `runtime` declaration.

This rule only works in setup script and `lang="ts"`.

<eslint-code-block :rules="{'vue/define-emits-declaration': ['error']}">

```vue
<script setup lang="ts">
/* ✓ GOOD */
const emit = defineEmits<{
(e: 'change', id: number): void
(e: 'update', value: string): void
}>()
</script>
```

</eslint-code-block>

<eslint-code-block :rules="{'vue/define-emits-declaration': ['error']}">

```vue
<script setup lang="ts">
/* ✗ BAD */
const emit = defineEmits(['change', 'update'])
</script>
```

</eslint-code-block>

## :wrench: Options

- `type-based` (default) enforces type-based declaration
- `runtime` enforces runtime declaration

```json
"vue/define-emits-declaration": ["error", "type-based" | "runtime"]
FloEdelmann marked this conversation as resolved.
Show resolved Hide resolved
```

## :couple: Related Rules

- [vue/define-props-declaration](./define-props-declaration.md)
- [vue/valid-define-emits](./valid-define-emits.md)

## :books: Further Reading

- [`defineEmits`](https://vuejs.org/api/sfc-script-setup.html#defineprops-defineemits)
- [Typescript-only-features of `defineEmits`](https://vuejs.org/api/sfc-script-setup.html#typescript-only-features)
- [Guide - Typing-component-emits](https://vuejs.org/guide/typescript/composition-api.html#typing-component-emits)

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/define-emits-declaration.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/define-emits-declaration.js)
68 changes: 68 additions & 0 deletions docs/rules/define-props-declaration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/define-props-declaration
description: enforce declaration style of `defineProps`
---
# vue/define-props-declaration

> enforce declaration style of `defineProps`

- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>

## :book: Rule Details

This rule enforces `defineProps` typing style which you should use `type-based` or `runtime` declaration.

This rule only works in setup script and `lang="ts"`.

<eslint-code-block :rules="{'vue/define-props-declaration': ['error']}">

```vue
<script setup lang="ts">
/* ✓ GOOD */
const props = defineProps<{
kind: string
}>()
</script>
```

</eslint-code-block>

<eslint-code-block :rules="{'vue/define-props-declaration': ['error']}">

```vue
<script setup lang="ts">
/* ✗ BAD */
const props = defineProps({
kind: { type: String }
})
</script>
```

</eslint-code-block>

## :wrench: Options

- `type-based` (default) enforces type-based declaration
- `runtime` enforces runtime declaration

```json
"vue/define-props-declaration": ["error", "type-based" | "runtime"]
```

## :couple: Related Rules

- [vue/define-emits-declaration](./define-emits-declaration.md)
- [vue/valid-define-props](./valid-define-props.md)

## :books: Further Reading

- [`defineProps`](https://vuejs.org/api/sfc-script-setup.html#defineprops-defineemits)
- [Typescript-only-features of `defineProps`](https://vuejs.org/api/sfc-script-setup.html#typescript-only-features)
- [Guide - Typing-component-props](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/define-props-declaration.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/define-props-declaration.js)
53 changes: 29 additions & 24 deletions docs/rules/valid-define-emits.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ This rule reports `defineEmits` compiler macros in the following cases:

```vue
<script setup>
/* ✓ GOOD */
defineEmits({ notify: null })
/* ✓ GOOD */
defineEmits({ notify: null })
</script>
```

Expand All @@ -38,8 +38,8 @@ This rule reports `defineEmits` compiler macros in the following cases:

```vue
<script setup>
/* ✓ GOOD */
defineEmits(['notify'])
/* ✓ GOOD */
defineEmits(['notify'])
</script>
```

Expand All @@ -49,8 +49,8 @@ This rule reports `defineEmits` compiler macros in the following cases:

```vue
<script setup lang="ts">
/* ✓ GOOD */
defineEmits<(e: 'notify')=>void>()
/* ✓ GOOD */
defineEmits<(e: 'notify') => void>()
</script>
```

Expand All @@ -60,11 +60,11 @@ This rule reports `defineEmits` compiler macros in the following cases:

```vue
<script>
const def = { notify: null }
const def = { notify: null }
</script>
<script setup>
/* ✓ GOOD */
defineEmits(def)
/* ✓ GOOD */
defineEmits(def)
</script>
```

Expand All @@ -74,9 +74,9 @@ This rule reports `defineEmits` compiler macros in the following cases:

```vue
<script setup>
/* ✗ BAD */
const def = { notify: null }
defineEmits(def)
/* ✗ BAD */
const def = { notify: null }
defineEmits(def)
</script>
```

Expand All @@ -86,8 +86,8 @@ This rule reports `defineEmits` compiler macros in the following cases:

```vue
<script setup lang="ts">
/* ✗ BAD */
defineEmits<(e: 'notify')=>void>({ submit: null })
/* ✗ BAD */
defineEmits<(e: 'notify') => void>({ submit: null })
</script>
```

Expand All @@ -97,9 +97,9 @@ This rule reports `defineEmits` compiler macros in the following cases:

```vue
<script setup>
/* ✗ BAD */
defineEmits({ notify: null })
defineEmits({ submit: null })
/* ✗ BAD */
defineEmits({ notify: null })
defineEmits({ submit: null })
</script>
```

Expand All @@ -109,13 +109,13 @@ This rule reports `defineEmits` compiler macros in the following cases:

```vue
<script>
export default {
emits: { notify: null }
}
export default {
emits: { notify: null }
}
</script>
<script setup>
/* ✗ BAD */
defineEmits({ submit: null })
/* ✗ BAD */
defineEmits({ submit: null })
</script>
```

Expand All @@ -125,8 +125,8 @@ This rule reports `defineEmits` compiler macros in the following cases:

```vue
<script setup>
/* ✗ BAD */
defineEmits()
/* ✗ BAD */
defineEmits()
</script>
```

Expand All @@ -136,6 +136,11 @@ This rule reports `defineEmits` compiler macros in the following cases:

Nothing.

## :couple: Related Rules

- [vue/define-emits-declaration](./define-emits-declaration.md)
- [vue/valid-define-props](./valid-define-props.md)

## :rocket: Version

This rule was introduced in eslint-plugin-vue v7.13.0
Expand Down