|
| 1 | +/** |
| 2 | + * @author Ivan Demchuk <https://github.com/Demivan> |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const { iterateDefineRefs } = require('../utils/ref-object-references') |
| 8 | +const utils = require('../utils') |
| 9 | + |
| 10 | +/** |
| 11 | + * @param {Expression|SpreadElement} node |
| 12 | + */ |
| 13 | +function isNullOrUndefined(node) { |
| 14 | + return ( |
| 15 | + (node.type === 'Literal' && node.value === null) || |
| 16 | + (node.type === 'Identifier' && node.name === 'undefined') |
| 17 | + ) |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * @typedef {import('../utils/ref-object-references').RefObjectReferences} RefObjectReferences |
| 22 | + */ |
| 23 | + |
| 24 | +module.exports = { |
| 25 | + meta: { |
| 26 | + type: 'suggestion', |
| 27 | + docs: { |
| 28 | + description: |
| 29 | + 'require `ref` and `shallowRef` functions to be strongly typed', |
| 30 | + categories: undefined, |
| 31 | + url: 'https://eslint.vuejs.org/rules/require-typed-ref.html' |
| 32 | + }, |
| 33 | + fixable: null, |
| 34 | + messages: { |
| 35 | + noType: |
| 36 | + 'Specify type parameter for `{{name}}` function, otherwise created variable will not by typechecked.' |
| 37 | + }, |
| 38 | + schema: [] |
| 39 | + }, |
| 40 | + /** @param {RuleContext} context */ |
| 41 | + create(context) { |
| 42 | + const filename = context.getFilename() |
| 43 | + if (!utils.isVueFile(filename) && !utils.isTypeScriptFile(filename)) { |
| 44 | + return {} |
| 45 | + } |
| 46 | + |
| 47 | + const scriptSetup = utils.getScriptSetupElement(context) |
| 48 | + if ( |
| 49 | + scriptSetup && |
| 50 | + !utils.hasAttribute(scriptSetup, 'lang', 'ts') && |
| 51 | + !utils.hasAttribute(scriptSetup, 'lang', 'typescript') |
| 52 | + ) { |
| 53 | + return {} |
| 54 | + } |
| 55 | + |
| 56 | + const defines = iterateDefineRefs(context.getScope()) |
| 57 | + |
| 58 | + /** |
| 59 | + * @param {string} name |
| 60 | + * @param {CallExpression} node |
| 61 | + */ |
| 62 | + function report(name, node) { |
| 63 | + context.report({ |
| 64 | + node, |
| 65 | + messageId: 'noType', |
| 66 | + data: { |
| 67 | + name |
| 68 | + } |
| 69 | + }) |
| 70 | + } |
| 71 | + |
| 72 | + return { |
| 73 | + Program() { |
| 74 | + for (const ref of defines) { |
| 75 | + if (ref.name !== 'ref' && ref.name !== 'shallowRef') { |
| 76 | + continue |
| 77 | + } |
| 78 | + |
| 79 | + if ( |
| 80 | + ref.node.arguments.length > 0 && |
| 81 | + !isNullOrUndefined(ref.node.arguments[0]) |
| 82 | + ) { |
| 83 | + continue |
| 84 | + } |
| 85 | + |
| 86 | + if (ref.node.typeParameters == null) { |
| 87 | + if ( |
| 88 | + ref.node.parent.type === 'VariableDeclarator' && |
| 89 | + ref.node.parent.id.type === 'Identifier' |
| 90 | + ) { |
| 91 | + if (ref.node.parent.id.typeAnnotation == null) { |
| 92 | + report(ref.name, ref.node) |
| 93 | + } |
| 94 | + } else { |
| 95 | + report(ref.name, ref.node) |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments