Convert import assertions to query parameters

This commit is contained in:
Jonathan Neal 2021-12-07 11:59:11 -05:00
parent 0ef682c924
commit bec8c4e4e9

View file

@ -0,0 +1,11 @@
export const convertAssertionsToQueryParams = (code: string) => code.replace(
/import\s+(?:[\W\w]*?\sfrom\s+(?:"(?:[^"]|\\")*"|'(?:[^']|\\')*')(\s+assert\s+([^}]+\})))/gm,
(statement, assertions, assertionType) => {
return assertions
? statement.slice(0, -assertions.length - 1) +
'?' +
new URLSearchParams(JSON.parse(assertionType.replace(/(['"])?([a-z0-9A-Z_]+)(['"])?\s*:/g, '"$2": '))).toString() +
statement[statement.length - assertions.length - 1]
: statement;
}
)