From d7331ac63ea9f0bb0580001570e580ee60f6859e Mon Sep 17 00:00:00 2001 From: Thomasan1999 Date: Sat, 30 Nov 2024 12:27:28 +0100 Subject: [PATCH] make it compatible with Node.js v14 --- lib/rules/prefer-use-template-ref.js | 6 ++++-- lib/utils/index.js | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) 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] } }