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

vue/define-macros-order bug #1861

Merged
merged 2 commits into from Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 22 additions & 3 deletions lib/rules/define-macros-order.js
Expand Up @@ -19,6 +19,17 @@ const MACROS_PROPS = 'defineProps'
const ORDER = [MACROS_EMITS, MACROS_PROPS]
const DEFAULT_ORDER = [MACROS_PROPS, MACROS_EMITS]

/**
* @param {VElement} scriptSetup
* @param {ASTNode} node
*/
function inScriptSetup(scriptSetup, node) {
return (
scriptSetup.range[0] <= node.range[0] &&
node.range[1] <= scriptSetup.range[1]
)
}

/**
* @param {ASTNode} node
*/
Expand All @@ -33,9 +44,10 @@ function isUseStrictStatement(node) {
/**
* Get an index of the first statement after imports and interfaces in order
* to place defineEmits and defineProps before this statement
* @param {VElement} scriptSetup
* @param {Program} program
*/
function getTargetStatementPosition(program) {
function getTargetStatementPosition(scriptSetup, program) {
const skipStatements = new Set([
'ImportDeclaration',
'TSInterfaceDeclaration',
Expand All @@ -45,7 +57,11 @@ function getTargetStatementPosition(program) {
])

for (const [index, item] of program.body.entries()) {
if (!skipStatements.has(item.type) && !isUseStrictStatement(item)) {
if (
inScriptSetup(scriptSetup, item) &&
Copy link
Contributor Author

Choose a reason for hiding this comment

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

!skipStatements.has(item.type) &&
!isUseStrictStatement(item)
) {
return index
}
}
Expand Down Expand Up @@ -104,7 +120,10 @@ function create(context) {
'Program:exit'(program) {
const shouldFirstNode = macrosNodes.get(order[0])
const shouldSecondNode = macrosNodes.get(order[1])
const firstStatementIndex = getTargetStatementPosition(program)
const firstStatementIndex = getTargetStatementPosition(
scriptSetup,
program
)
const firstStatement = program.body[firstStatementIndex]

// have both defineEmits and defineProps
Expand Down
36 changes: 36 additions & 0 deletions tests/lib/rules/define-macros-order.js
Expand Up @@ -387,6 +387,42 @@ tester.run('define-macros-order', rule, {
line: 3
}
]
},
{
filename: 'test.vue',
code: `
<script>
import 'test2'
export default { inheritAttrs: false };
</script>

<script setup>
import 'test'

defineEmits(['update:test'])
const props = defineProps({ test: Boolean });
</script>
`,
output: `
<script>
import 'test2'
export default { inheritAttrs: false };
</script>

<script setup>
import 'test'

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

defineEmits(['update:test'])
</script>
`,
errors: [
{
message: message('defineProps'),
line: 11
}
]
}
]
})