Skip to content

Commit

Permalink
Add type-based declaration rule of defineProps and defineEmits (#…
Browse files Browse the repository at this point in the history
…1968)

* Add `vue/prefer-type-props-decl` rule

* Add  rule

* chore: Related Rules

* Update lib/rules/prefer-type-emits-decl.js

Co-authored-by: Yosuke Ota <otameshiyo23@gmail.com>

* Update lib/rules/prefer-type-props-decl.js

Co-authored-by: Yosuke Ota <otameshiyo23@gmail.com>

* fix: remove ts eslint-code-block & improve related rules link

* Update lib/rules/prefer-type-emits-decl.js

Co-authored-by: Flo Edelmann <florian-edelmann@online.de>

* Update tests/lib/rules/prefer-type-emits-decl.js

Co-authored-by: Flo Edelmann <florian-edelmann@online.de>

* chore: rename rule name

* feat: implement renamed rules

* update rule docs

* Add eslint-code-block

* fix lint issues

* Update docs/rules/define-emits-declaration.md

Co-authored-by: Flo Edelmann <florian-edelmann@online.de>

* Update docs/rules/define-props-declaration.md

Co-authored-by: Flo Edelmann <florian-edelmann@online.de>

* Update docs/rules/define-emits-declaration.md

Co-authored-by: Flo Edelmann <florian-edelmann@online.de>

* Update docs/rules/define-props-declaration.md

Co-authored-by: Flo Edelmann <florian-edelmann@online.de>

* add runtime option example

* Fix order in docs

Co-authored-by: Yosuke Ota <otameshiyo23@gmail.com>
Co-authored-by: Flo Edelmann <florian-edelmann@online.de>
  • Loading branch information
3 people authored Sep 16, 2022
1 parent bf9b95c commit 4383a86
Show file tree
Hide file tree
Showing 10 changed files with 663 additions and 48 deletions.
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
78 changes: 78 additions & 0 deletions docs/rules/define-emits-declaration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
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
}>()
/* ✗ BAD */
const emit = defineEmits(['change', 'update'])
</script>
```

</eslint-code-block>

## :wrench: Options

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

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

### `runtime`

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

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

</eslint-code-block>

## :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)
79 changes: 79 additions & 0 deletions docs/rules/define-props-declaration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
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
}>()
/* ✗ BAD */
const props = defineProps({
kind: { type: String }
})
</script>
```

</eslint-code-block>

## :wrench: Options

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

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

### `"runtime"`

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

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

</eslint-code-block>

## :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
Loading

0 comments on commit 4383a86

Please sign in to comment.