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

Improve auto-fix for vue/define-macros-order rule #1863

Merged
merged 3 commits into from May 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 18 additions & 8 deletions lib/rules/define-macros-order.js
Expand Up @@ -232,13 +232,12 @@ function create(context) {
const targetComment = sourceCode.getTokenAfter(beforeTargetToken, {
includeComments: true
})
const textSpace = getTextBetweenTokens(beforeTargetToken, targetComment)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This thing fixes these newlines https://github.com/vuejs/eslint-plugin-vue/pull/1861/files#diff-6ebdd3c21be70e29ce94a2f399884fd220d4fe9bba358ade66b0bceb325d3102L158

Before we took space before the target token and added it after the moving token.
Now we take space after the target token and added it after the moving token.

// make insert text: comments + node + space before target
const textNode = sourceCode.getText(
node,
node.range[0] - nodeComment.range[0]
)
const insertText = textNode + textSpace
const insertText = getInsertText(textNode, target)

return [
fixer.insertTextBefore(targetComment, insertText),
Expand All @@ -247,17 +246,28 @@ function create(context) {
}

/**
* @param {ASTNode} tokenBefore
* @param {ASTNode} tokenAfter
* Get result text to insert
* @param {string} textNode
* @param {ASTNode} target
*/
function getTextBetweenTokens(tokenBefore, tokenAfter) {
return sourceCode.text.slice(tokenBefore.range[1], tokenAfter.range[0])
function getInsertText(textNode, target) {
const afterTargetComment = sourceCode.getTokenAfter(target, {
includeComments: true
})
const afterText = sourceCode.text.slice(
target.range[1],
afterTargetComment.range[0]
)
// handle case when a();b() -> b()a();
const invalidResult = !textNode.endsWith(';') && !afterText.includes('\n')
FloEdelmann marked this conversation as resolved.
Show resolved Hide resolved

return textNode + afterText + (invalidResult ? ';' : '')
}

/**
* Get position of the beginning of the token's line(or prevToken end if no line)
* @param {ASTNode} token
* @param {ASTNode} prevToken
* @param {ASTNode|Token} token
* @param {ASTNode|Token} prevToken
*/
function getLineStartIndex(token, prevToken) {
// if we have next token on the same line - get index right before that token
Expand Down
8 changes: 1 addition & 7 deletions tests/lib/rules/define-macros-order.js
Expand Up @@ -155,9 +155,7 @@ tester.run('define-macros-order', rule, {
defineProps({
test: Boolean
})

defineEmits(['update:test'])

console.log('test1')
console.log('test2')
console.log('test3')
Expand Down Expand Up @@ -192,7 +190,6 @@ tester.run('define-macros-order', rule, {
defineProps({
test: Boolean
})

defineEmits(['update:test'])
console.log('test1')
</script>
Expand Down Expand Up @@ -261,7 +258,6 @@ tester.run('define-macros-order', rule, {
}

const emit = defineEmits<{(e: 'update:test'): void}>()

const props = withDefaults(defineProps<Props>(), {
msg: 'hello',
labels: () => ['one', 'two']
Expand Down Expand Up @@ -377,8 +373,7 @@ tester.run('define-macros-order', rule, {
`,
output: `
<script setup>
defineEmits(['update:test'])
const props = defineProps({ test: Boolean }); </script>
defineEmits(['update:test']);const props = defineProps({ test: Boolean }); </script>
`,
options: optionsEmitsFirst,
errors: [
Expand Down Expand Up @@ -413,7 +408,6 @@ tester.run('define-macros-order', rule, {
import 'test'

const props = defineProps({ test: Boolean });

defineEmits(['update:test'])
</script>
`,
Expand Down