-
-
Notifications
You must be signed in to change notification settings - Fork 669
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
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`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return fixer.insertTextAfter(lastImportSpecifier, `,useTemplateRef`) | |
return fixer.insertTextAfter(lastImportSpecifier, `, useTemplateRef`) |
const body = sourceCode.ast.body | ||
|
||
const imports = body.filter((node) => node.type === 'ImportDeclaration') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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') |
const lastImport = imports[imports.length - 1] | ||
|
||
const importStatement = "import {useTemplateRef} from 'vue';" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Let's put the
importStatement
variable first because it's used in both following branches. - Remove the empty line to make it a little more concise.
- 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.)
const lastImport = imports[imports.length - 1] | |
const importStatement = "import {useTemplateRef} from 'vue';" | |
const importStatement = "import { useTemplateRef } from 'vue';" | |
const lastImport = imports[imports.length - 1] |
/** @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 |
There was a problem hiding this comment.
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:
/** @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 |
Addresses #2554 (comment)