@@ -7,103 +7,6 @@ import { component_context } from '../context.js';
77import * as w from '../warnings.js' ;
88import { sanitize_location } from '../../../utils.js' ;
99
10- /** @type {Record<string, Array<{ start: Location, end: Location, component: Function }>> } */
11- const boundaries = { } ;
12-
13- const chrome_pattern = / a t (?: .+ \( ) ? ( .+ ) : ( \d + ) : ( \d + ) \) ? $ / ;
14- const firefox_pattern = / @ ( .+ ) : ( \d + ) : ( \d + ) $ / ;
15-
16- function get_stack ( ) {
17- const stack = new Error ( ) . stack ;
18- if ( ! stack ) return null ;
19-
20- const entries = [ ] ;
21-
22- for ( const line of stack . split ( '\n' ) ) {
23- let match = chrome_pattern . exec ( line ) ?? firefox_pattern . exec ( line ) ;
24-
25- if ( match ) {
26- entries . push ( {
27- file : match [ 1 ] ,
28- line : + match [ 2 ] ,
29- column : + match [ 3 ]
30- } ) ;
31- }
32- }
33-
34- return entries ;
35- }
36-
37- /**
38- * Determines which `.svelte` component is responsible for a given state change
39- * @returns {Function | null }
40- */
41- export function get_component ( ) {
42- // first 4 lines are svelte internals; adjust this number if we change the internal call stack
43- const stack = get_stack ( ) ?. slice ( 4 ) ;
44- if ( ! stack ) return null ;
45-
46- for ( let i = 0 ; i < stack . length ; i ++ ) {
47- const entry = stack [ i ] ;
48- const modules = boundaries [ entry . file ] ;
49- if ( ! modules ) {
50- // If the first entry is not a component, that means the modification very likely happened
51- // within a .svelte.js file, possibly triggered by a component. Since these files are not part
52- // of the bondaries/component context heuristic, we need to bail in this case, else we would
53- // have false positives when the .svelte.ts file provides a state creator function, encapsulating
54- // the state and its mutations, and is being called from a component other than the one who
55- // called the state creator function.
56- if ( i === 0 ) return null ;
57- continue ;
58- }
59-
60- for ( const module of modules ) {
61- if ( module . end == null ) {
62- return null ;
63- }
64- if ( module . start . line < entry . line && module . end . line > entry . line ) {
65- return module . component ;
66- }
67- }
68- }
69-
70- return null ;
71- }
72-
73- /**
74- * Together with `mark_module_end`, this function establishes the boundaries of a `.svelte` file,
75- * such that subsequent calls to `get_component` can tell us which component is responsible
76- * for a given state change
77- */
78- export function mark_module_start ( ) {
79- const start = get_stack ( ) ?. [ 2 ] ;
80-
81- if ( start ) {
82- ( boundaries [ start . file ] ??= [ ] ) . push ( {
83- start,
84- // @ts -expect-error
85- end : null ,
86- // @ts -expect-error we add the component at the end, since HMR will overwrite the function
87- component : null
88- } ) ;
89- }
90- }
91-
92- /**
93- * @param {Function } component
94- */
95- export function mark_module_end ( component ) {
96- const end = get_stack ( ) ?. [ 2 ] ;
97-
98- if ( end ) {
99- const boundaries_file = boundaries [ end . file ] ;
100- const boundary = boundaries_file [ boundaries_file . length - 1 ] ;
101-
102- boundary . end = end ;
103- boundary . component = component ;
104- }
105- }
106-
10710/**
10811 * Sets up a validator that
10912 * - traverses the path of a prop to find out if it is allowed to be mutated
0 commit comments