What expression `text` evaluates to in a loader, or null when it is not one this reads.
(text: string, scope: LoaderScope)
| 1808 | |
| 1809 | /** What expression `text` evaluates to in a loader, or null when it is not one this reads. */ |
| 1810 | function valueOf(text: string, scope: LoaderScope): Loaded | null { |
| 1811 | let s = text.trim(); |
| 1812 | // `await`, and parentheses around the whole expression, change nothing here. |
| 1813 | for (let before = ''; before !== s;) { |
| 1814 | before = s; |
| 1815 | s = s.replace(/^await\b\s*/, ''); |
| 1816 | if (s.startsWith('(') && matchBracket(s, 0) === s.length - 1) s = s.slice(1, -1).trim(); |
| 1817 | } |
| 1818 | if (s.startsWith('{') || s.startsWith('[')) { |
| 1819 | if (matchBracket(s, 0) !== s.length - 1) return null; |
| 1820 | const inside = s.slice(1, -1); |
| 1821 | return s[0] === '{' |
| 1822 | ? { kind: 'object', text: inside, scope } |
| 1823 | : { kind: 'array', items: topLevelItems(inside).map((item) => (item ? valueOf(item, scope) : null)) }; |
| 1824 | } |
| 1825 | // The head: `import('./x')`, `Promise.all([…])`, a name, or a parenthesized expression. |
| 1826 | const head = /^(?:(import)\s*\(|(Promise)\s*\.\s*all\s*\(|([A-Za-z_$][\w$]*)|\()/.exec(s); |
| 1827 | if (!head) return null; |
| 1828 | let value: Loaded | null; |
| 1829 | let i: number; |
| 1830 | if (head[3]) { |
| 1831 | value = nameValue(head[3], scope); |
| 1832 | i = head[0].length; |
| 1833 | } else { |
| 1834 | const open = head[0].length - 1; |
| 1835 | const close = matchBracket(s, open); |
| 1836 | if (close < 0) return null; |
| 1837 | const inside = s.slice(open + 1, close); |
| 1838 | if (head[1]) { |
| 1839 | const spec = /^\s*(["'])([^"'`]+)\1\s*$/.exec(inside)?.[2]; |
| 1840 | value = spec ? { kind: 'module', spec } : null; |
| 1841 | } else if (head[2]) { |
| 1842 | const all = valueOf(inside, scope); |
| 1843 | value = all?.kind === 'array' ? all : null; |
| 1844 | } else value = valueOf(inside, scope); |
| 1845 | i = close + 1; |
| 1846 | } |
| 1847 | // Then each `.name`, `?.name`, `['name']` and `.then(…)` on it. |
| 1848 | while (value && i < s.length) { |
| 1849 | const rest = s.slice(i); |
| 1850 | const then = /^\s*\??\.\s*then\s*\(/.exec(rest); |
| 1851 | if (then) { |
| 1852 | const open = i + then[0].length - 1; |
| 1853 | const close = matchBracket(s, open); |
| 1854 | if (close < 0 || value.kind !== 'module') return null; |
| 1855 | const callback = topLevelItems(s.slice(open + 1, close))[0]; |
| 1856 | value = callback ? loaderValue(callback, scope, value) : null; |
| 1857 | i = close + 1; |
| 1858 | continue; |
| 1859 | } |
| 1860 | const dot = /^\s*\??\.\s*([A-Za-z_$][\w$]*)/.exec(rest); |
| 1861 | const bracket = dot ? null : /^\s*\[\s*(["'])([A-Za-z_$][\w$]*)\1\s*\]/.exec(rest); |
| 1862 | const name = dot?.[1] ?? bracket?.[2]; |
| 1863 | if (name === undefined) return null; |
| 1864 | value = memberOf(value, name); |
| 1865 | i += (dot ?? bracket)![0].length; |
| 1866 | } |
| 1867 | return value; |
no test coverage detected