diff --git a/lib/rules/prefer-use-template-ref.js b/lib/rules/prefer-use-template-ref.js index 16e6f866c..e2dcc24f9 100644 --- a/lib/rules/prefer-use-template-ref.js +++ b/lib/rules/prefer-use-template-ref.js @@ -75,13 +75,15 @@ function addUseTemplateRefImport(context, fixer) { ) if (vueDestructuredImport) { - const importSpecifierLast = vueDestructuredImport.specifiers.at(-1) + const importSpecifierLast = utils.getLastArrayElement( + vueDestructuredImport.specifiers + ) // @ts-ignore return fixer.insertTextAfter(importSpecifierLast, `,useTemplateRef`) } - const lastImport = imports.at(-1) + const lastImport = utils.getLastArrayElement(imports) const importStatement = "import {useTemplateRef} from 'vue';" diff --git a/lib/utils/index.js b/lib/utils/index.js index 167edf208..ace71b919 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -2208,6 +2208,16 @@ module.exports = { (token, i) => token.type === tokensR[i].type && token.value === tokensR[i].value ) + }, + + /** + * TODO: replace usages with 'array.at(-1)' once the minimum supported Node.js version is at least 16.6.0 + * @template T + * @param array {T[]} + * @returns {T | undefined} + * */ + getLastArrayElement(array) { + return array[array.length - 1] } }