mbloch / mapshaper

Tools for editing Shapefile, GeoJSON, TopoJSON and CSV files
http://mapshaper.org
Other
3.67k stars 529 forks source link

Template expressions and object literals #645

Open indus opened 2 weeks ago

indus commented 2 weeks ago

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 commented 1 week ago

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 commented 1 week ago

Thanks - this works.

indus commented 1 week ago

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