Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template expressions and object literals #645

Open
indus opened this issue Jun 20, 2024 · 3 comments
Open

Template expressions and object literals #645

indus opened this issue Jun 20, 2024 · 3 comments

Comments

@indus
Copy link

indus commented Jun 20, 2024

I'm facing a problem with this commands:

-require myFn.js
-run "myFn('foo',{optA:true,optB:false})" 

The curly braces of the options-property are interpreted as template identifiers and result in an error.

Maybe the template identifier should be changed to double curly braces in the parseTemplateParts function. It is not uncommon to use {{template}} but it would break existing code.

related: #608

@mbloch
Copy link
Owner

mbloch commented Jun 23, 2024

Hi!

Originally, the -run command did not support embedded template expressions, so there was no ambiguity around curly braces. I went with single braces because the double-brace syntax felt clunky after working with single-brace frameworks like Svelte. You're using the original -run command syntax, which I'm supporting for backwards compatibility. You can work around the problem by enclosing your entire expression in curly braces, like this:

-require myFn.js
-run "{myFn('foo',{optA:true,optB:false})}" 

Maybe I should have gone with double-braces, but at least you have a workaround.

@indus
Copy link
Author

indus commented Jun 23, 2024

Thanks - this works.

@indus
Copy link
Author

indus commented Jun 23, 2024

I found a strange quirk:

// test.js
module.exports = {
    fnA: function (opt) {
        return "-each this.geojson=fnB()";
    },
    fnB: function () {
        return null;
    }
}
-require "./test.js" -run "fnA()" 

... works as expected

-require "./test.js" -run "{fnA()}" 

... results in an error SyntaxError in JS source: Unexpected token 'this'

I think this was why I avoided the template literal in the first place. But now I need (read "want" 😁 ) it to provide options as object and not as positional function parameters.

please tell me if I'm being a pain...
i can also get a huge amount of use out of mapshaper as it is

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants