improve code generation for jsImports

This commit is contained in:
Vyacheslav Matyukhin 2022-09-01 18:55:15 +04:00
parent 64ed32a17c
commit e2abc53200
No known key found for this signature in database
GPG Key ID: 3D2A774C5489F96C

View File

@ -24,7 +24,7 @@ const jsImportsValueToSquiggleCode = (v: JsImportsValue): string => {
return ( return (
"{" + "{" +
Object.entries(v) Object.entries(v)
.map(([k, v]) => `${k}:${jsImportsValueToSquiggleCode(v)},`) .map(([k, v]) => `${quote(k)}:${jsImportsValueToSquiggleCode(v)},`)
.join("") + .join("") +
"}" "}"
); );
@ -35,7 +35,17 @@ const jsImportsValueToSquiggleCode = (v: JsImportsValue): string => {
}; };
export const jsImportsToSquiggleCode = (v: JsImports) => { export const jsImportsToSquiggleCode = (v: JsImports) => {
return Object.entries(v) const validId = new RegExp("[a-zA-Z][[a-zA-Z0-9]*");
.map(([k, v]) => `$${k} = ${jsImportsValueToSquiggleCode(v)}\n`) let result = Object.entries(v)
.map(([k, v]) => {
if (!k.match(validId)) {
return ""; // skipping without warnings; can be improved
}
return `$${k} = ${jsImportsValueToSquiggleCode(v)}\n`;
})
.join(""); .join("");
if (!result) {
result = "$__no_valid_imports__ = 1"; // without this generated squiggle code can be invalid
}
return result;
}; };