@@ -19,6 +19,17 @@ const MACROS_PROPS = 'defineProps'
1919const ORDER = [ MACROS_EMITS , MACROS_PROPS ]
2020const 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
0 commit comments