question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Control of semicolon behaviour

See original GitHub issue

Motivation

I’m using astring as part of an atom plugin to help with code generation. It would be great to be able to control whether semicolons are emitted, so that I can match the current style directives of the current project. Right now, semicolons are implemented as a whole bunch of state.write(';') instances.

It would be awesome to have a config option to control semicolon emission. I’m thinking that you could replace all those writes with a semantic state.terminate(), which can check if it should print a semicolon or not.

The only alternative to control this is to basically fork the entire baseGenerator, so you can remove all those writes.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
davidbonnetcommented, Jun 23, 2017

This could be handled through a custom output stream, that removes semicolons:

const output = {
  code: '',
  write(code) {
    switch (code) {
      case ';':
        break
      case ');':
        this.code += ')'
        break
      case 'debugger;':
        this.code += 'debugger'
        break
      default:
        this.code += code
        break
    }
  },
}

const { code } = astring.generate(ast, { output })

console.log('Code without semicolons:', code)

There are however two known issues to this:

  1. Not all semicolons can be removed, in particular, when the next expression starts with a parenthesis.
  2. Using a custom output stream has some performance hits.

Thus, a more precise handling is required. This could be a first step towards version 2. Pull requests are welcome.

0reactions
Mouradifcommented, Oct 1, 2020

@KFlash did you check my PR above ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Semicolons - Grammar and Mechanics - Academic Guides
The theory helps explain an individual's ability to exert self-control over his or her behavior. INCORRECT (The second clause here is dependent and...
Read more >
Semicolon: The 2 Ways to Use a ; - The Write Practice
1. Use a semicolon to connect two independent clauses. You know what an independent clause is, right? A clause has a subject and...
Read more >
How to Use a Semicolon - Herzing University
Let's look at the second reason to use a semicolon: separating items in a complicated list. ... Generally, you separate items in a...
Read more >
Why is semicolon changing JavaScript behavior in the ...
The behaviour you experience comes from the special handling of inputs that begin with { and end with } in the Chrome devtools....
Read more >
C Break and Continue Statements – Loop Control Statements ...
As with all statements in C, the break statement should terminate with a semicolon ( ; ). Let's take an example to understand...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found