|
21 | 21 | // VARIABLES //
|
22 | 22 |
|
23 | 23 | // Regular expressions for matching TypeScript declarations:
|
| 24 | + |
| 25 | +/** |
| 26 | +* Regular expression to match function declarations like "declare function abs( x: number ): number;" (captures function name). |
| 27 | +* |
| 28 | +* @type {RegExp} |
| 29 | +*/ |
24 | 30 | var RE_DECLARE_FUNCTION = /declare\s+function\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*[<(]/;
|
| 31 | + |
| 32 | +/** |
| 33 | +* Regular expression to match variable declarations like "declare var someVar: SomeType;" (captures variable name). |
| 34 | +* |
| 35 | +* @type {RegExp} |
| 36 | +*/ |
25 | 37 | var RE_DECLARE_VAR = /declare\s+var\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*:/;
|
| 38 | + |
| 39 | +/** |
| 40 | +* Regular expression to match class declarations like "declare class Complex64Array {" (captures class name). |
| 41 | +* |
| 42 | +* @type {RegExp} |
| 43 | +*/ |
26 | 44 | var RE_DECLARE_CLASS = /declare\s+class\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s/;
|
27 |
| -var RE_DECLARE_VAR_NAMESPACE = /declare\s+var\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*:\s*[A-Z][a-zA-Z0-9_$]*/; |
| 45 | + |
| 46 | +/** |
| 47 | +* Regular expression to match const declarations like "declare const PI: number;" (captures constant name). |
| 48 | +* |
| 49 | +* @type {RegExp} |
| 50 | +*/ |
28 | 51 | var RE_DECLARE_CONST = /declare\s+const\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*:/;
|
| 52 | + |
| 53 | +/** |
| 54 | +* Regular expression to match variable declarations with interface types like "declare var ctor: Int32Vector;" (captures variable name and interface name). |
| 55 | +* |
| 56 | +* @type {RegExp} |
| 57 | +*/ |
29 | 58 | var RE_DECLARE_VAR_INTERFACE = /declare\s+var\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*:\s*([A-Z][a-zA-Z0-9_$]*)/;
|
30 | 59 |
|
31 | 60 |
|
32 | 61 | // MAIN //
|
33 | 62 |
|
34 | 63 | /**
|
35 |
| -* Adds package export to scope based on TypeScript declarations. |
| 64 | +* Adds a package export to the scope based on TypeScript declarations. |
36 | 65 | *
|
| 66 | +* @private |
37 | 67 | * @param {Object} scope - VM scope object to add the package export to
|
38 | 68 | * @param {*} pkg - package export value to be added to scope
|
39 | 69 | * @param {string} sourceText - TypeScript declaration source text to parse for identifier names
|
40 | 70 | */
|
41 | 71 | function addPackageToScope( scope, pkg, sourceText ) {
|
42 | 72 | var interfaceMatch;
|
43 | 73 | var namespaceMatch;
|
44 |
| - var funcMatch; |
| 74 | + var pkgType; |
| 75 | + var match; |
45 | 76 |
|
46 |
| - if ( typeof pkg === 'function' ) { |
47 |
| - // Try to match declare function pattern (handles generics with <): |
48 |
| - funcMatch = sourceText.match( RE_DECLARE_FUNCTION ); |
49 |
| - if ( !funcMatch ) { |
50 |
| - // Try to match declare var pattern: |
51 |
| - funcMatch = sourceText.match( RE_DECLARE_VAR ); |
52 |
| - } |
53 |
| - if ( !funcMatch ) { |
54 |
| - // Try to match declare class pattern (for constructor functions): |
55 |
| - funcMatch = sourceText.match( RE_DECLARE_CLASS ); |
56 |
| - } |
57 |
| - if ( funcMatch ) { |
58 |
| - scope[ funcMatch[1] ] = pkg; |
| 77 | + pkgType = typeof pkg; |
| 78 | + if ( pkgType === 'function' ) { |
| 79 | + match = sourceText.match( RE_DECLARE_FUNCTION ) || |
| 80 | + sourceText.match( RE_DECLARE_VAR ) || |
| 81 | + sourceText.match( RE_DECLARE_CLASS ); |
| 82 | + if ( match ) { |
| 83 | + scope[ match[1] ] = pkg; |
59 | 84 | }
|
60 |
| - // Also check for declare var with interface pattern (e.g., declare var ctor: Int32Vector): |
61 | 85 | interfaceMatch = sourceText.match( RE_DECLARE_VAR_INTERFACE );
|
62 | 86 | if ( interfaceMatch ) {
|
63 |
| - // Make the function available under both the variable name and the interface name: |
| 87 | + // Make the function available under both the variable and interface names: |
64 | 88 | scope[ interfaceMatch[1] ] = pkg; // e.g., ctor
|
65 | 89 | scope[ interfaceMatch[2] ] = pkg; // e.g., Int32Vector
|
66 | 90 | }
|
67 |
| - } else if ( typeof pkg === 'object' && pkg !== null ) { |
68 |
| - // Handle namespace objects and other object interfaces: |
69 |
| - namespaceMatch = sourceText.match( RE_DECLARE_VAR_NAMESPACE ); |
70 |
| - if ( namespaceMatch ) { |
71 |
| - scope[ namespaceMatch[1] ] = pkg; |
72 |
| - } |
73 |
| - // Also check for const declarations (e.g., Complex64/Complex128 constants): |
74 |
| - funcMatch = sourceText.match( RE_DECLARE_CONST ); |
75 |
| - if ( funcMatch ) { |
76 |
| - scope[ funcMatch[1] ] = pkg; |
77 |
| - } |
78 | 91 | } else {
|
79 |
| - // Try to match declare const pattern: |
80 |
| - funcMatch = sourceText.match( RE_DECLARE_CONST ); |
81 |
| - if ( funcMatch ) { |
82 |
| - scope[ funcMatch[1] ] = pkg; |
| 92 | + if ( pkgType === 'object' && pkg !== null ) { |
| 93 | + namespaceMatch = sourceText.match( RE_DECLARE_VAR_INTERFACE ); |
| 94 | + if ( namespaceMatch ) { |
| 95 | + scope[ namespaceMatch[1] ] = pkg; |
| 96 | + } |
| 97 | + } |
| 98 | + match = sourceText.match( RE_DECLARE_CONST ); |
| 99 | + if ( match ) { |
| 100 | + scope[ match[1] ] = pkg; |
83 | 101 | }
|
84 | 102 | }
|
85 | 103 | }
|
|
0 commit comments