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