(source: string, declarationFile: boolean)
| 324 | * namespace a qualified name reaches, and is left out. |
| 325 | */ |
| 326 | export function scanNamespaceBlocks(source: string, declarationFile: boolean): NamespaceBlock[] { |
| 327 | const topLevelGlobal = declarationFile && scanAmbientModules(source).script; |
| 328 | const blocks: NamespaceBlock[] = []; |
| 329 | // What each open brace opened, innermost last. |
| 330 | const open: Array<NamespaceBlock | 'global' | 'module' | 'other'> = []; |
| 331 | let names: string[] | null = null; // the names after `namespace`, while its header is read |
| 332 | let expectName = false; |
| 333 | let keyword = { line: 0, column: 0 }; |
| 334 | let declare = 0; // 1: `declare`, 2: `declare global` |
| 335 | let ambientModule = false; // just read `module 'x'` |
| 336 | let afterDot = false; |
| 337 | tokenize(source, (kind, text, line, column) => { |
| 338 | if (kind === Kind.Punct && text === '{') { |
| 339 | const header = names && names.length > 0 && !expectName ? names : null; |
| 340 | const outer = open[open.length - 1]; |
| 341 | if (declare === 2) { |
| 342 | open.push('global'); |
| 343 | } else if (ambientModule) { |
| 344 | open.push('module'); |
| 345 | } else if (header && outer !== 'other') { |
| 346 | const parent = typeof outer === 'object' ? outer : null; |
| 347 | const block: NamespaceBlock = { |
| 348 | path: (parent ? `${parent.path}.` : '') + header.join('.'), |
| 349 | startLine: keyword.line, |
| 350 | startColumn: keyword.column, |
| 351 | endLine: line, |
| 352 | endColumn: column, |
| 353 | global: parent ? parent.global : outer === 'global' || (outer === undefined && topLevelGlobal), |
| 354 | }; |
| 355 | blocks.push(block); |
| 356 | open.push(block); |
| 357 | } else { |
| 358 | open.push('other'); |
| 359 | } |
| 360 | names = null; |
| 361 | declare = 0; |
| 362 | ambientModule = false; |
| 363 | afterDot = false; |
| 364 | return; |
| 365 | } |
| 366 | if (kind === Kind.Punct && text === '}') { |
| 367 | const closed = open.pop(); |
| 368 | if (typeof closed === 'object') { |
| 369 | closed.endLine = line; |
| 370 | closed.endColumn = column; |
| 371 | } |
| 372 | names = null; |
| 373 | declare = 0; |
| 374 | ambientModule = false; |
| 375 | afterDot = false; |
| 376 | return; |
| 377 | } |
| 378 | ambientModule = names !== null && expectName && names.length === 0 && kind === Kind.String; |
| 379 | if (names) { |
| 380 | if (expectName && kind === Kind.Word) { |
| 381 | names.push(text); |
| 382 | expectName = false; |
| 383 | return; |
no test coverage detected