-
-
Notifications
You must be signed in to change notification settings - Fork 691
Add autofix to vue/prefer-use-template-ref
#2632
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
base: master
Are you sure you want to change the base?
Changes from 5 commits
ac5aa92
fe49694
d7331ac
00c9c0e
7db1382
c8bb2e0
f6aefde
e1ea60f
ff2ba97
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,60 @@ function getScriptRefsFromSetupFunction(body) { | |
return refDeclarators.map(convertDeclaratorToScriptRef) | ||
} | ||
|
||
/** @param node {Statement | ModuleDeclaration} */ | ||
function createIndent(node) { | ||
const indentSize = node.loc.start.column | ||
|
||
return ' '.repeat(indentSize) | ||
} | ||
|
||
/** | ||
* @param context {RuleContext} | ||
* @param fixer {RuleFixer} | ||
* */ | ||
function addUseTemplateRefImport(context, fixer) { | ||
const sourceCode = context.getSourceCode() | ||
|
||
if (!sourceCode) { | ||
return | ||
} | ||
|
||
const body = sourceCode.ast.body | ||
|
||
const imports = body.filter((node) => node.type === 'ImportDeclaration') | ||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const vueDestructuredImport = imports.find( | ||
(importStatement) => | ||
importStatement.source.value === 'vue' && | ||
importStatement.specifiers.some( | ||
(specifier) => specifier.type === 'ImportSpecifier' | ||
) | ||
) | ||
|
||
if (vueDestructuredImport) { | ||
const importSpecifiers = vueDestructuredImport.specifiers | ||
const lastImportSpecifier = importSpecifiers[importSpecifiers.length - 1] | ||
|
||
// @ts-ignore | ||
return fixer.insertTextAfter(lastImportSpecifier, `,useTemplateRef`) | ||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
const lastImport = imports[imports.length - 1] | ||
|
||
const importStatement = "import {useTemplateRef} from 'vue';" | ||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (lastImport) { | ||
const indent = createIndent(lastImport) | ||
|
||
return fixer.insertTextAfter(lastImport, `\n${indent}${importStatement}`) | ||
} | ||
|
||
const firstNode = body[0] | ||
const indent = createIndent(firstNode) | ||
|
||
return fixer.insertTextBefore(firstNode, `${importStatement}\n${indent}`) | ||
} | ||
|
||
/** @type {import("eslint").Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
|
@@ -54,6 +108,7 @@ module.exports = { | |
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/prefer-use-template-ref.html' | ||
}, | ||
fixable: 'code', | ||
schema: [], | ||
messages: { | ||
preferUseTemplateRef: "Replace '{{name}}' with 'useTemplateRef'." | ||
|
@@ -93,6 +148,8 @@ module.exports = { | |
}), | ||
{ | ||
'Program:exit'() { | ||
let missingImportChecked = false | ||
|
||
for (const templateRef of templateRefs) { | ||
const scriptRef = scriptRefs.find( | ||
(scriptRef) => scriptRef.ref === templateRef | ||
|
@@ -108,6 +165,32 @@ module.exports = { | |
data: { | ||
// @ts-ignore | ||
name: scriptRef.node?.callee?.name | ||
}, | ||
fix(fixer) { | ||
/** @type {Fix[]} */ | ||
const fixes = [] | ||
|
||
const replaceFunctionFix = fixer.replaceText( | ||
scriptRef.node, | ||
`useTemplateRef('${scriptRef.ref}')` | ||
) | ||
|
||
fixes.push(replaceFunctionFix) | ||
|
||
if (!missingImportChecked) { | ||
missingImportChecked = true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this flag for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Thomasan1999 it would be great to get this merged. Could you please have a look into this? |
||
|
||
const missingImportFix = addUseTemplateRefImport( | ||
context, | ||
fixer | ||
) | ||
|
||
if (missingImportFix) { | ||
fixes.push(missingImportFix) | ||
} | ||
} | ||
|
||
return fixes | ||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}) | ||
} | ||
|
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.