( input: string, )
| 462 | * @param input - The stringified object |
| 463 | */ |
| 464 | const parseJSObject = ( |
| 465 | input: string, |
| 466 | ): { |
| 467 | parsed: Record<string, string>; |
| 468 | unparsed?: string; |
| 469 | } => { |
| 470 | const unparsed: string[] = []; |
| 471 | let parsed: Record<string, string> = {}; |
| 472 | |
| 473 | try { |
| 474 | const ast = parseExpression(`(${input})`, { |
| 475 | plugins: ['jsx', 'typescript'], |
| 476 | sourceType: 'module', |
| 477 | }); |
| 478 | |
| 479 | if (ast.type !== 'ObjectExpression') { |
| 480 | return { parsed, unparsed: input }; |
| 481 | } |
| 482 | |
| 483 | for (const prop of ast.properties) { |
| 484 | /** |
| 485 | * If the object includes spread or method, we stop. We can't really break the component into Key/Value |
| 486 | * and the whole expression is considered dynamic. We return `false` to signify that. |
| 487 | */ |
| 488 | if (prop.type === 'ObjectMethod' || prop.type === 'SpreadElement') { |
| 489 | if (!!prop.start && !!prop.end) { |
| 490 | if (typeof input === 'string') { |
| 491 | unparsed.push(input.slice(prop.start - 1, prop.end - 1)); |
| 492 | } |
| 493 | } |
| 494 | continue; |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Ignore shorthand objects when processing incomplete objects. Otherwise we may |
| 499 | * create identifiers unintentionally. |
| 500 | * Example: When accounting for shorthand objects, "{ color" would become |
| 501 | * { color: color } thus creating a "color" identifier that does not exist. |
| 502 | */ |
| 503 | if (prop.type === 'ObjectProperty') { |
| 504 | if (prop.extra?.shorthand) { |
| 505 | if (typeof input === 'string') { |
| 506 | unparsed.push(input.slice(prop.start! - 1, prop.end! - 1)); |
| 507 | } |
| 508 | continue; |
| 509 | } |
| 510 | |
| 511 | let key = ''; |
| 512 | if (prop.key.type === 'Identifier') { |
| 513 | key = prop.key.name; |
| 514 | } else if (prop.key.type === 'StringLiteral') { |
| 515 | key = prop.key.value; |
| 516 | } else { |
| 517 | continue; |
| 518 | } |
| 519 | |
| 520 | if (typeof input === 'string') { |
| 521 | const [val, err] = extractValue(input, prop.value); |
no test coverage detected
searching dependent graphs…