Skip to content

Commit

Permalink
vue/define-macros-order bug (#1861)
Browse files Browse the repository at this point in the history
* define-macros-order bugs

* define-macros-order bugs revert
  • Loading branch information
edikdeisling committed Apr 22, 2022
1 parent bd13174 commit c43e04f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
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) &&
!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
}
]
}
]
})

0 comments on commit c43e04f

Please sign in to comment.