(filePath: string, context: ResolutionContext)
| 1172 | const pythonStarExportMemos = new WeakMap<ResolutionContext, Map<string, Set<string> | null>>(); |
| 1173 | |
| 1174 | function pythonStarExports(filePath: string, context: ResolutionContext): Set<string> | null { |
| 1175 | let memo = pythonStarExportMemos.get(context); |
| 1176 | if (!memo) { |
| 1177 | memo = new Map(); |
| 1178 | pythonStarExportMemos.set(context, memo); |
| 1179 | } |
| 1180 | if (memo.has(filePath)) return memo.get(filePath)!; |
| 1181 | let names: Set<string> | null = null; |
| 1182 | const content = context.readFile(filePath); |
| 1183 | if (content && content.includes('__all__')) { |
| 1184 | // Locate every statement that builds `__all__` on blanked source (so a |
| 1185 | // docstring mention is ignored), then read the literals from the original |
| 1186 | // at the same offsets. Only a list written out in full is trusted: one |
| 1187 | // assembled from other names (`__all__ = fields_all + [...]`, |
| 1188 | // `__all__.extend(...)`) is unknown, and falls back to the public names. |
| 1189 | const code = stripCommentsForRegex(content, 'python'); |
| 1190 | let literal = true; |
| 1191 | const collected = new Set<string>(); |
| 1192 | for (const m of code.matchAll(/^[ \t]*__all__\b[ \t]*(\.|\+=|=|:[^=\n]*=)?[ \t]*(.?)/gm)) { |
| 1193 | const open = m.index! + m[0].length - 1; |
| 1194 | const bracket = m[2]; |
| 1195 | const close = bracket === '[' ? code.indexOf(']', open) : bracket === '(' ? code.indexOf(')', open) : -1; |
| 1196 | const assigns = m[1] !== undefined && m[1] !== '.'; |
| 1197 | const tail = close < 0 ? '' : code.slice(close + 1, code.indexOf('\n', close) < 0 ? undefined : code.indexOf('\n', close)); |
| 1198 | if (!assigns || close < 0 || tail.trim() !== '') { |
| 1199 | literal = false; |
| 1200 | break; |
| 1201 | } |
| 1202 | for (const lit of content.slice(open, close).matchAll(/["']([A-Za-z_]\w*)["']/g)) collected.add(lit[1]!); |
| 1203 | } |
| 1204 | if (literal && collected.size > 0) names = collected; |
| 1205 | } |
| 1206 | memo.set(filePath, names); |
| 1207 | return names; |
| 1208 | } |
| 1209 | |
| 1210 | function pythonStarAllows(filePath: string, name: string, context: ResolutionContext): boolean { |
| 1211 | const names = pythonStarExports(filePath, context); |
no test coverage detected