|
1 | 1 | import { JspmError } from "../common/err.js";
|
2 | 2 | import { getIntegrity } from "../common/integrity.js";
|
3 | 3 |
|
4 |
| -export type Analysis = AnalysisData | { |
5 |
| - parseError: JspmError | Error |
6 |
| -}; |
| 4 | +export type Analysis = |
| 5 | + | AnalysisData |
| 6 | + | { |
| 7 | + parseError: JspmError | Error; |
| 8 | + }; |
7 | 9 |
|
8 | 10 | export interface AnalysisData {
|
9 | 11 | deps: string[];
|
@@ -31,8 +33,9 @@ export async function createEsmAnalysis(
|
31 | 33 | imports: any[],
|
32 | 34 | source: string,
|
33 | 35 | url: string
|
34 |
| -): Promise<Analysis> { // Change the return type to Promise<Analysis> |
35 |
| - if (!imports.length && registerRegEx.test(source)) |
| 36 | +): Promise<Analysis> { |
| 37 | + // Change the return type to Promise<Analysis> |
| 38 | + if (!imports.length && systemMatch(source)) |
36 | 39 | return createSystemAnalysis(source, imports, url);
|
37 | 40 | const deps: string[] = [];
|
38 | 41 | const dynamicDeps: string[] = [];
|
@@ -67,15 +70,21 @@ export async function createEsmAnalysis(
|
67 | 70 | integrity: await getIntegrity(source),
|
68 | 71 | };
|
69 | 72 | }
|
| 73 | +const leadingCommentRegex = /^\s*(\/\*[\s\S]*?\*\/|\s*\/\/[^\n]*)*/; |
| 74 | +const registerRegex = /^\s*System\s*\.\s*register\s*\(\s*(\[[^\]]*\])\s*,\s*\(?function\s*\(\s*([^\),\s]+\s*(,\s*([^\),\s]+)\s*)?\s*)?\)/; |
| 75 | + |
| 76 | +function systemMatch(code) { |
| 77 | + const commentMatch = code.match(leadingCommentRegex); |
| 78 | + const offset = commentMatch ? commentMatch[0].length : 0; |
| 79 | + return code.slice(offset).match(registerRegex); |
| 80 | +} |
70 | 81 |
|
71 |
| -const registerRegEx = |
72 |
| - /^\s*(\/\*[^\*]*(\*(?!\/)[^\*]*)*\*\/|\s*\/\/[^\n]*)*\s*System\s*\.\s*register\s*\(\s*(\[[^\]]*\])\s*,\s*\(?function\s*\(\s*([^\),\s]+\s*(,\s*([^\),\s]+)\s*)?\s*)?\)/; |
73 | 82 | export async function createSystemAnalysis(
|
74 | 83 | source: string,
|
75 | 84 | imports: string[],
|
76 | 85 | url: string
|
77 | 86 | ): Promise<Analysis> {
|
78 |
| - const [, , , rawDeps, , , contextId] = source.match(registerRegEx) || []; |
| 87 | + const [, rawDeps, contextId] = systemMatch(source) || []; |
79 | 88 | if (!rawDeps) return createEsmAnalysis(imports, source, url);
|
80 | 89 | const deps = JSON.parse(rawDeps.replace(/'/g, '"'));
|
81 | 90 | const dynamicDeps: string[] = [];
|
|
0 commit comments