Skip to content

Commit c43e04f

Browse files
authored
vue/define-macros-order bug (#1861)
* define-macros-order bugs * define-macros-order bugs revert
1 parent bd13174 commit c43e04f

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

lib/rules/define-macros-order.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ const MACROS_PROPS = 'defineProps'
1919
const ORDER = [MACROS_EMITS, MACROS_PROPS]
2020
const DEFAULT_ORDER = [MACROS_PROPS, MACROS_EMITS]
2121

22+
/**
23+
* @param {VElement} scriptSetup
24+
* @param {ASTNode} node
25+
*/
26+
function inScriptSetup(scriptSetup, node) {
27+
return (
28+
scriptSetup.range[0] <= node.range[0] &&
29+
node.range[1] <= scriptSetup.range[1]
30+
)
31+
}
32+
2233
/**
2334
* @param {ASTNode} node
2435
*/
@@ -33,9 +44,10 @@ function isUseStrictStatement(node) {
3344
/**
3445
* Get an index of the first statement after imports and interfaces in order
3546
* to place defineEmits and defineProps before this statement
47+
* @param {VElement} scriptSetup
3648
* @param {Program} program
3749
*/
38-
function getTargetStatementPosition(program) {
50+
function getTargetStatementPosition(scriptSetup, program) {
3951
const skipStatements = new Set([
4052
'ImportDeclaration',
4153
'TSInterfaceDeclaration',
@@ -45,7 +57,11 @@ function getTargetStatementPosition(program) {
4557
])
4658

4759
for (const [index, item] of program.body.entries()) {
48-
if (!skipStatements.has(item.type) && !isUseStrictStatement(item)) {
60+
if (
61+
inScriptSetup(scriptSetup, item) &&
62+
!skipStatements.has(item.type) &&
63+
!isUseStrictStatement(item)
64+
) {
4965
return index
5066
}
5167
}
@@ -104,7 +120,10 @@ function create(context) {
104120
'Program:exit'(program) {
105121
const shouldFirstNode = macrosNodes.get(order[0])
106122
const shouldSecondNode = macrosNodes.get(order[1])
107-
const firstStatementIndex = getTargetStatementPosition(program)
123+
const firstStatementIndex = getTargetStatementPosition(
124+
scriptSetup,
125+
program
126+
)
108127
const firstStatement = program.body[firstStatementIndex]
109128

110129
// have both defineEmits and defineProps

tests/lib/rules/define-macros-order.js

+36
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,42 @@ tester.run('define-macros-order', rule, {
387387
line: 3
388388
}
389389
]
390+
},
391+
{
392+
filename: 'test.vue',
393+
code: `
394+
<script>
395+
import 'test2'
396+
export default { inheritAttrs: false };
397+
</script>
398+
399+
<script setup>
400+
import 'test'
401+
402+
defineEmits(['update:test'])
403+
const props = defineProps({ test: Boolean });
404+
</script>
405+
`,
406+
output: `
407+
<script>
408+
import 'test2'
409+
export default { inheritAttrs: false };
410+
</script>
411+
412+
<script setup>
413+
import 'test'
414+
415+
const props = defineProps({ test: Boolean });
416+
417+
defineEmits(['update:test'])
418+
</script>
419+
`,
420+
errors: [
421+
{
422+
message: message('defineProps'),
423+
line: 11
424+
}
425+
]
390426
}
391427
]
392428
})

0 commit comments

Comments
 (0)