(filePath: string, context: ResolutionContext)
| 1436 | const pythonStarExportMemos = new WeakMap<ResolutionContext, Map<string, Set<string> | null>>(); |
| 1437 | |
| 1438 | function pythonStarExports(filePath: string, context: ResolutionContext): Set<string> | null { |
| 1439 | let memo = pythonStarExportMemos.get(context); |
| 1440 | if (!memo) { |
| 1441 | memo = new Map(); |
| 1442 | pythonStarExportMemos.set(context, memo); |
| 1443 | } |
| 1444 | if (memo.has(filePath)) return memo.get(filePath)!; |
| 1445 | let names: Set<string> | null = null; |
| 1446 | const content = context.readFile(filePath); |
| 1447 | if (content && content.includes('__all__')) { |
| 1448 | // Locate every statement that builds `__all__` on blanked source (so a |
| 1449 | // docstring mention is ignored), then read the literals from the original |
| 1450 | // at the same offsets. Only a list written out in full is trusted: one |
| 1451 | // assembled from other names (`__all__ = fields_all + [...]`, |
| 1452 | // `__all__.extend(...)`) is unknown, and falls back to the public names. |
| 1453 | const code = stripCommentsForRegex(content, 'python'); |
| 1454 | let literal = true; |
| 1455 | const collected = new Set<string>(); |
| 1456 | for (const m of code.matchAll(/^[ \t]*__all__\b[ \t]*(\.|\+=|=|:[^=\n]*=)?[ \t]*(.?)/gm)) { |
| 1457 | const open = m.index! + m[0].length - 1; |
| 1458 | const bracket = m[2]; |
| 1459 | const close = bracket === '[' ? code.indexOf(']', open) : bracket === '(' ? code.indexOf(')', open) : -1; |
| 1460 | const assigns = m[1] !== undefined && m[1] !== '.'; |
| 1461 | const tail = close < 0 ? '' : code.slice(close + 1, code.indexOf('\n', close) < 0 ? undefined : code.indexOf('\n', close)); |
| 1462 | if (!assigns || close < 0 || tail.trim() !== '') { |
| 1463 | literal = false; |
| 1464 | break; |
| 1465 | } |
| 1466 | for (const lit of content.slice(open, close).matchAll(/["']([A-Za-z_]\w*)["']/g)) collected.add(lit[1]!); |
| 1467 | } |
| 1468 | if (literal && collected.size > 0) names = collected; |
| 1469 | } |
| 1470 | memo.set(filePath, names); |
| 1471 | return names; |
| 1472 | } |
| 1473 | |
| 1474 | function pythonStarAllows(filePath: string, name: string, context: ResolutionContext): boolean { |
| 1475 | const names = pythonStarExports(filePath, context); |
no test coverage detected