(source: string, filePath?: string)
| 58 | * pragma — the ones `detectLanguage` sends here from `.js`. |
| 59 | */ |
| 60 | export function blankFlowSyntax(source: string, filePath?: string): string { |
| 61 | if (!filePath || !/\.(?:jsx?|mjs|cjs)$/.test(filePath) || !hasFlowPragma(source)) return source; |
| 62 | return source |
| 63 | .replace(/\{\|/g, '{ ') |
| 64 | .replace(/\|\}/g, ' }') |
| 65 | .replace(/([:<,]\s*)\?(?=[\w$({[])/g, '$1 ') |
| 66 | .replace(/\bimport\s+typeof\b/g, (m) => 'import' + ' '.repeat(m.length - 'import'.length)) |
| 67 | .replace(/\bopaque(?=\s+type\b)/g, ' ') |
| 68 | // An inexact object type's bare `...` (`{a: T, ...}`) — never a value spread. |
| 69 | .replace(/\.\.\.(?=\s*[},])/g, ' '); |
| 70 | } |
| 71 | |
| 72 | /** Whether the file writes `export type *` anywhere: most files don't, and skip the rest. */ |
| 73 | const EXPORT_TYPE_STAR_HINT = /export\s+type\s*\*/; |
no test coverage detected