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

trailing comma creates non-descript error #2848

Open
gotjoshua opened this issue Feb 23, 2024 · 11 comments · Fixed by #2849
Open

trailing comma creates non-descript error #2848

gotjoshua opened this issue Feb 23, 2024 · 11 comments · Fixed by #2849

Comments

@gotjoshua
Copy link

Describe the bug
when using format=js if there is a trailing comma, an error is thrown.
Can happen when the commented //NAMESERVER line is after a normal record with a trailing comma

executing dnsconfig.js: (anonymous): Line 52:1 Unexpected token ) (and 1 more errors)

To Reproduce

  1. dnsconfig.js:
D("foobar.me", REG_CHANGEME,
	DnsProvider(DSP_WHATEVER),
	CNAME("foo", "bar.xyz."),
	//NAMESERVER("bar.")
)
  1. dnscontrol preview

Expected behavior
Tolerate trailing commas (especially if they are produced by get-zones

DNS Provider

  • CLOUDNS (but not relevant)
@imlonghao
Copy link
Contributor

Maybe you should try -format=djs?

   --format=js        dnsconfig.js format (not perfect, just a decent first draft)
   --format=djs       js with disco commas (leading commas)

@tlimoncelli
Copy link
Contributor

Another trick is to close your D() with END):

D("foo.com", ...
    A(...),
    A(...),
    A(...),
END)

END is just an alias for {}, which is ignored by DNSControl. However it makes a comma on the previous line required, like all other lines.

I think END should be the default. I'm fixing this here: #2849

@cafferata
Copy link
Collaborator

Another trick is to close your D() with END):

D("foo.com", ...
    A(...),
    A(...),
    A(...),
END)

END is just an alias for {}, which is ignored by DNSControl. However it makes a comma on the previous line required, like all other lines.

Cool! I didn't know this and it was always a small frustration. Maybe something to document as tips and tricks?

@gotjoshua
Copy link
Author

Maybe you should try -format=djs?

yes, i did that and it works, but i find it rather ugly.
and still the bug is legit in js mode.

@gotjoshua
Copy link
Author

I think END should be the default. I'm fixing this here: #2849

thanks for the fix, will it also work if a commented line is just above the END ?

@tlimoncelli
Copy link
Contributor

Hmmm... a comment the line before the END should work, assuming the line above it ends in a comma.

   A(),
   // comment
   END);

@alanpearce
Copy link

I think this issue should still be open: the issue is still present and is rather frustrating when the trailing comma is added by an editor's on-save formatting. Both prettier and deno fmt will split long lines adding trailing commas by default, including after END, thereby negating its usefulness [for users where a formatter will be applied].

Fortunately it seems as though a possible fix has been merged in otto, so after its next release and a dependency update here, this won't be an issue any more.

I worked around it in the meantime by reconfiguring prettier:

/**
 * @see https://prettier.io/docs/en/configuration.html
 * @type {import("prettier").Config}
 */
const config = {
  trailingComma: 'es5',
  tabWidth: 2,
  semi: false,
  singleQuote: true,
}

export default config

@tlimoncelli tlimoncelli reopened this Jun 4, 2024
@tlimoncelli
Copy link
Contributor

Hi there!

I've reopened the bug as requested.

Can you give an example of how END is affected? Maybe a "before and after" would help.

@alanpearce
Copy link

source: commands/test_data/ds.com.js

before:

var DSP_BIND = NewDnsProvider("bind", "BIND");
var REG_CHANGEME = NewRegistrar("none");

D("ds.com", REG_CHANGEME,
	DnsProvider(DSP_BIND),
	//SOA("@", "ns3.serverfault.com.", "sysadmin.stackoverflow.com.", 2020022300, 3600, 600, 604800, 1440),
	DS("geo", 14480, 13, 2, "BB1C4B615CDED2B34347CF23710471934D972F1E34F53B54ED8D5F786202C73B"),
END);

after deno fmt or prettier

var DSP_BIND = NewDnsProvider("bind", "BIND");
var REG_CHANGEME = NewRegistrar("none");

D(
  "ds.com",
  REG_CHANGEME,
  DnsProvider(DSP_BIND),
  //SOA("@", "ns3.serverfault.com.", "sysadmin.stackoverflow.com.", 2020022300, 3600, 600, 604800, 1
440),
  DS(
    "geo",
    14480,
    13,
    2,
    "BB1C4B615CDED2B34347CF23710471934D972F1E34F53B54ED8D5F786202C73B",
  ),
  END,
);

@tlimoncelli
Copy link
Contributor

Oh! I see!

The editor is adding a , after END -- the result being END,); which fmt rewrites as END,\n);

Yeah, that's not good.

Which editor does this? Can that feature be disabled?

@alanpearce
Copy link

alanpearce commented Jun 4, 2024

It's not the editor itself (in my case Emacs), it's any editor or language server that uses prettier or deno (possibly others?) to format code automatically on save.

Format on save can be disabled, but I think the better workaround is to reconfigure prettier so that it understands that otto can only parse es5-style trailing commas (as of 0.4.0), which is the workaround in my previous comment and even this repository.

Deno doesn't appear to have a setting for trailing commas, because it is both an interpreter and formatter (so the formatter knows what the interpreter supports) and new enough that trailing commas were always supported everywhere. Fortunately, prettier is the common choice.

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

Successfully merging a pull request may close this issue.

5 participants