File tree 2 files changed +36
-4
lines changed
packages/vite/src/node/ssr
2 files changed +36
-4
lines changed Original file line number Diff line number Diff line change @@ -812,3 +812,33 @@ function test() {
812
812
}"
813
813
` )
814
814
} )
815
+
816
+ // #11806
817
+ test ( 'track scope by blocks' , async ( ) => {
818
+ expect (
819
+ await ssrTransformSimpleCode ( `
820
+ import { foo, bar, baz } from 'foobar'
821
+ function test() {
822
+ [foo];
823
+ {
824
+ let foo = 10;
825
+ let bar = 10;
826
+ }
827
+ try {} catch (baz){ baz };
828
+ return bar;
829
+ }` ) ,
830
+ ) . toMatchInlineSnapshot ( `
831
+ "
832
+ const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foobar\\");
833
+
834
+ function test() {
835
+ [__vite_ssr_import_0__.foo];
836
+ {
837
+ let foo = 10;
838
+ let bar = 10;
839
+ }
840
+ try {} catch (baz){ baz };
841
+ return __vite_ssr_import_0__.bar;
842
+ }"
843
+ ` )
844
+ } )
Original file line number Diff line number Diff line change @@ -448,6 +448,8 @@ function walk(
448
448
if ( parentFunction ) {
449
449
handlePattern ( node . id , parentFunction )
450
450
}
451
+ } else if ( node . type === 'CatchClause' && node . param ) {
452
+ handlePattern ( node . param , node )
451
453
}
452
454
} ,
453
455
@@ -550,14 +552,14 @@ function isFunction(node: _Node): node is FunctionNode {
550
552
return functionNodeTypeRE . test ( node . type )
551
553
}
552
554
553
- const scopeNodeTypeRE =
554
- / (?: F u n c t i o n | C l a s s ) (?: E x p r e s s i o n | D e c l a r a t i o n ) $ | M e t h o d $ | ^ I f S t a t e m e n t $ /
555
555
function findParentScope (
556
556
parentStack : _Node [ ] ,
557
557
isVar = false ,
558
558
) : _Node | undefined {
559
- const regex = isVar ? functionNodeTypeRE : scopeNodeTypeRE
560
- return parentStack . find ( ( i ) => regex . test ( i . type ) )
559
+ const predicate = isVar
560
+ ? isFunction
561
+ : ( node : _Node ) => node . type === 'BlockStatement'
562
+ return parentStack . find ( predicate )
561
563
}
562
564
563
565
function isInDestructuringAssignment (
You can’t perform that action at this time.
0 commit comments