Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(prefer-use-template-ref): add support for fix option #2632

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

Thomasan1999
Copy link
Contributor

@Thomasan1999 Thomasan1999 commented Nov 30, 2024

Addresses #2554 (comment)

lib/utils/index.js Outdated Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add an test case using TypeScript?

<template>
  <div ref="root" />
</template>
<script setup lang="ts">
  import { ref } from 'vue';
  const root = ref<HTMLDivElement>();
</script>

should get autofixed to

<template>
  <div ref="root" />
</template>
<script setup lang="ts">
  import { ref,useTemplateRef } from 'vue';
  const root = useTemplateRef<HTMLDivElement>('root');
</script>

Note that you'll need to specify the parser in the test:

languageOptions: {
  parserOptions: {
    parser: require.resolve('@typescript-eslint/parser')
  }
},

const lastImportSpecifier = importSpecifiers[importSpecifiers.length - 1]

// @ts-ignore
return fixer.insertTextAfter(lastImportSpecifier, `,useTemplateRef`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return fixer.insertTextAfter(lastImportSpecifier, `,useTemplateRef`)
return fixer.insertTextAfter(lastImportSpecifier, `, useTemplateRef`)

Comment on lines +65 to +67
const body = sourceCode.ast.body

const imports = body.filter((node) => node.type === 'ImportDeclaration')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const body = sourceCode.ast.body
const imports = body.filter((node) => node.type === 'ImportDeclaration')
const body = sourceCode.ast.body
const imports = body.filter((node) => node.type === 'ImportDeclaration')

Comment on lines +85 to +87
const lastImport = imports[imports.length - 1]

const importStatement = "import {useTemplateRef} from 'vue';"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Let's put the importStatement variable first because it's used in both following branches.
  2. Remove the empty line to make it a little more concise.
  3. Add spaces between the braces in the added code, to conform with the common Prettier style. (If users don't like it, they can and have to change it either way.)
Suggested change
const lastImport = imports[imports.length - 1]
const importStatement = "import {useTemplateRef} from 'vue';"
const importStatement = "import { useTemplateRef } from 'vue';"
const lastImport = imports[imports.length - 1]

Comment on lines +170 to +193
/** @type {Fix[]} */
const fixes = []

const replaceFunctionFix = fixer.replaceText(
scriptRef.node,
`useTemplateRef('${scriptRef.ref}')`
)

fixes.push(replaceFunctionFix)

if (!missingImportChecked) {
missingImportChecked = true

const missingImportFix = addUseTemplateRefImport(
context,
fixer
)

if (missingImportFix) {
fixes.push(missingImportFix)
}
}

return fixes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a little easier to understand (and shorter) without the fixes array:

Suggested change
/** @type {Fix[]} */
const fixes = []
const replaceFunctionFix = fixer.replaceText(
scriptRef.node,
`useTemplateRef('${scriptRef.ref}')`
)
fixes.push(replaceFunctionFix)
if (!missingImportChecked) {
missingImportChecked = true
const missingImportFix = addUseTemplateRefImport(
context,
fixer
)
if (missingImportFix) {
fixes.push(missingImportFix)
}
}
return fixes
const replaceFunctionFix = fixer.replaceText(
scriptRef.node,
`useTemplateRef('${scriptRef.ref}')`
)
if (!missingImportChecked) {
missingImportChecked = true
const missingImportFix = addUseTemplateRefImport(
context,
fixer
)
if (missingImportFix) {
return [replaceFunctionFix, missingImportFix]
}
}
return replaceFunctionFix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants