@@ -19,6 +19,17 @@ const MACROS_PROPS = 'defineProps'
19
19
const ORDER = [ MACROS_EMITS , MACROS_PROPS ]
20
20
const DEFAULT_ORDER = [ MACROS_PROPS , MACROS_EMITS ]
21
21
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
+
22
33
/**
23
34
* @param {ASTNode } node
24
35
*/
@@ -33,9 +44,10 @@ function isUseStrictStatement(node) {
33
44
/**
34
45
* Get an index of the first statement after imports and interfaces in order
35
46
* to place defineEmits and defineProps before this statement
47
+ * @param {VElement } scriptSetup
36
48
* @param {Program } program
37
49
*/
38
- function getTargetStatementPosition ( program ) {
50
+ function getTargetStatementPosition ( scriptSetup , program ) {
39
51
const skipStatements = new Set ( [
40
52
'ImportDeclaration' ,
41
53
'TSInterfaceDeclaration' ,
@@ -45,7 +57,11 @@ function getTargetStatementPosition(program) {
45
57
] )
46
58
47
59
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
+ ) {
49
65
return index
50
66
}
51
67
}
@@ -104,7 +120,10 @@ function create(context) {
104
120
'Program:exit' ( program ) {
105
121
const shouldFirstNode = macrosNodes . get ( order [ 0 ] )
106
122
const shouldSecondNode = macrosNodes . get ( order [ 1 ] )
107
- const firstStatementIndex = getTargetStatementPosition ( program )
123
+ const firstStatementIndex = getTargetStatementPosition (
124
+ scriptSetup ,
125
+ program
126
+ )
108
127
const firstStatement = program . body [ firstStatementIndex ]
109
128
110
129
// have both defineEmits and defineProps
0 commit comments