-
-
Notifications
You must be signed in to change notification settings - Fork 649
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
vue/require-typed-ref
rule (#2204)
Co-authored-by: Flo Edelmann <git@flo-edelmann.de>
- Loading branch information
1 parent
11f3f9f
commit 81ce0ce
Showing
7 changed files
with
400 additions
and
0 deletions.
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,47 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/require-typed-ref | ||
description: require `ref` and `shallowRef` functions to be strongly typed | ||
--- | ||
# vue/require-typed-ref | ||
|
||
> require `ref` and `shallowRef` functions to be strongly typed | ||
- :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 disallows calling `ref()` or `shallowRef()` functions without generic type parameter or an argument when using TypeScript. | ||
|
||
With TypeScript it is easy to prevent usage of `any` by using [`noImplicitAny`](https://www.typescriptlang.org/tsconfig#noImplicitAny). Unfortunately this rule is easily bypassed with Vue `ref()` function. Calling `ref()` function without a generic parameter or an initial value leads to ref having `Ref<any>` type. | ||
|
||
<eslint-code-block :rules="{'vue/require-typed-ref': ['error']}"> | ||
|
||
```vue | ||
<script setup lang="ts"> | ||
import { ref, shallowRef, type Ref } from 'vue' | ||
/* ✗ BAD */ | ||
const count = ref() // Returns Ref<any> that is not type checked | ||
count.value = '50' // Should be a type error, but it is not | ||
const count = shallowRef() | ||
/* ✓ GOOD */ | ||
const count = ref<number>() | ||
const count = ref(0) | ||
const count: Ref<number | undefined> = ref() | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/require-typed-ref.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/require-typed-ref.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,102 @@ | ||
/** | ||
* @author Ivan Demchuk <https://github.com/Demivan> | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const { iterateDefineRefs } = require('../utils/ref-object-references') | ||
const utils = require('../utils') | ||
|
||
/** | ||
* @param {Expression|SpreadElement} node | ||
*/ | ||
function isNullOrUndefined(node) { | ||
return ( | ||
(node.type === 'Literal' && node.value === null) || | ||
(node.type === 'Identifier' && node.name === 'undefined') | ||
) | ||
} | ||
|
||
/** | ||
* @typedef {import('../utils/ref-object-references').RefObjectReferences} RefObjectReferences | ||
*/ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: | ||
'require `ref` and `shallowRef` functions to be strongly typed', | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/require-typed-ref.html' | ||
}, | ||
fixable: null, | ||
messages: { | ||
noType: | ||
'Specify type parameter for `{{name}}` function, otherwise created variable will not by typechecked.' | ||
}, | ||
schema: [] | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
const filename = context.getFilename() | ||
if (!utils.isVueFile(filename) && !utils.isTypeScriptFile(filename)) { | ||
return {} | ||
} | ||
|
||
const scriptSetup = utils.getScriptSetupElement(context) | ||
if ( | ||
scriptSetup && | ||
!utils.hasAttribute(scriptSetup, 'lang', 'ts') && | ||
!utils.hasAttribute(scriptSetup, 'lang', 'typescript') | ||
) { | ||
return {} | ||
} | ||
|
||
const defines = iterateDefineRefs(context.getScope()) | ||
|
||
/** | ||
* @param {string} name | ||
* @param {CallExpression} node | ||
*/ | ||
function report(name, node) { | ||
context.report({ | ||
node, | ||
messageId: 'noType', | ||
data: { | ||
name | ||
} | ||
}) | ||
} | ||
|
||
return { | ||
Program() { | ||
for (const ref of defines) { | ||
if (ref.name !== 'ref' && ref.name !== 'shallowRef') { | ||
continue | ||
} | ||
|
||
if ( | ||
ref.node.arguments.length > 0 && | ||
!isNullOrUndefined(ref.node.arguments[0]) | ||
) { | ||
continue | ||
} | ||
|
||
if (ref.node.typeParameters == null) { | ||
if ( | ||
ref.node.parent.type === 'VariableDeclarator' && | ||
ref.node.parent.id.type === 'Identifier' | ||
) { | ||
if (ref.node.parent.id.typeAnnotation == null) { | ||
report(ref.name, ref.node) | ||
} | ||
} else { | ||
report(ref.name, ref.node) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.