@@ -354,6 +354,25 @@ export function compileScript(
354
354
)
355
355
}
356
356
357
+ function hoistNode ( node : Statement ) {
358
+ const start = node . start ! + startOffset
359
+ let end = node . end ! + startOffset
360
+ // locate comment
361
+ if ( node . trailingComments && node . trailingComments . length > 0 ) {
362
+ const lastCommentNode =
363
+ node . trailingComments [ node . trailingComments . length - 1 ]
364
+ end = lastCommentNode . end + startOffset
365
+ }
366
+ // locate the end of whitespace between this statement and the next
367
+ while ( end <= source . length ) {
368
+ if ( ! / \s / . test ( source . charAt ( end ) ) ) {
369
+ break
370
+ }
371
+ end ++
372
+ }
373
+ s . move ( start , end , 0 )
374
+ }
375
+
357
376
function registerUserImport (
358
377
source : string ,
359
378
local : string ,
@@ -891,6 +910,7 @@ export function compileScript(
891
910
scriptStartOffset !
892
911
)
893
912
913
+ // walk import declarations first
894
914
for ( const node of scriptAst . body ) {
895
915
if ( node . type === 'ImportDeclaration' ) {
896
916
// record imports for dedupe
@@ -910,7 +930,11 @@ export function compileScript(
910
930
! options . inlineTemplate
911
931
)
912
932
}
913
- } else if ( node . type === 'ExportDefaultDeclaration' ) {
933
+ }
934
+ }
935
+
936
+ for ( const node of scriptAst . body ) {
937
+ if ( node . type === 'ExportDefaultDeclaration' ) {
914
938
// export default
915
939
defaultExport = node
916
940
@@ -1040,39 +1064,9 @@ export function compileScript(
1040
1064
)
1041
1065
1042
1066
for ( const node of scriptSetupAst . body ) {
1043
- const start = node . start ! + startOffset
1044
- let end = node . end ! + startOffset
1045
- // locate comment
1046
- if ( node . trailingComments && node . trailingComments . length > 0 ) {
1047
- const lastCommentNode =
1048
- node . trailingComments [ node . trailingComments . length - 1 ]
1049
- end = lastCommentNode . end + startOffset
1050
- }
1051
- // locate the end of whitespace between this statement and the next
1052
- while ( end <= source . length ) {
1053
- if ( ! / \s / . test ( source . charAt ( end ) ) ) {
1054
- break
1055
- }
1056
- end ++
1057
- }
1058
-
1059
- // (Dropped) `ref: x` bindings
1060
- if (
1061
- node . type === 'LabeledStatement' &&
1062
- node . label . name === 'ref' &&
1063
- node . body . type === 'ExpressionStatement'
1064
- ) {
1065
- error (
1066
- `ref sugar using the label syntax was an experimental proposal and ` +
1067
- `has been dropped based on community feedback. Please check out ` +
1068
- `the new proposal at https://github.com/vuejs/rfcs/discussions/369` ,
1069
- node
1070
- )
1071
- }
1072
-
1073
1067
if ( node . type === 'ImportDeclaration' ) {
1074
1068
// import declarations are moved to top
1075
- s . move ( start , end , 0 )
1069
+ hoistNode ( node )
1076
1070
1077
1071
// dedupe imports
1078
1072
let removed = 0
@@ -1137,6 +1131,26 @@ export function compileScript(
1137
1131
s . remove ( node . start ! + startOffset , node . end ! + startOffset )
1138
1132
}
1139
1133
}
1134
+ }
1135
+
1136
+ for ( const node of scriptSetupAst . body ) {
1137
+ // already processed
1138
+ if ( node . type === 'ImportDeclaration' ) continue
1139
+
1140
+ // (Dropped) `ref: x` bindings
1141
+ // TODO remove when out of experimental
1142
+ if (
1143
+ node . type === 'LabeledStatement' &&
1144
+ node . label . name === 'ref' &&
1145
+ node . body . type === 'ExpressionStatement'
1146
+ ) {
1147
+ error (
1148
+ `ref sugar using the label syntax was an experimental proposal and ` +
1149
+ `has been dropped based on community feedback. Please check out ` +
1150
+ `the new proposal at https://github.com/vuejs/rfcs/discussions/369` ,
1151
+ node
1152
+ )
1153
+ }
1140
1154
1141
1155
if ( node . type === 'ExpressionStatement' ) {
1142
1156
// process `defineProps` and `defineEmit(s)` calls
@@ -1268,7 +1282,7 @@ export function compileScript(
1268
1282
( node . type === 'VariableDeclaration' && node . declare )
1269
1283
) {
1270
1284
recordType ( node , declaredTypes )
1271
- s . move ( start , end , 0 )
1285
+ hoistNode ( node )
1272
1286
}
1273
1287
}
1274
1288
}
0 commit comments