babel-plugin-syntax-no-paren

1.2.1 • Public • Published

babel-plugin-syntax-no-paren

Adds the possibility to omit:

  • Parentheses in if, while and for(each) statements.
  • Brackets in try, catch and finally.
  • catch in try statements (it will result in an empty catch).

Always update to the latest version to have more features and bug fixes (A looot of bug fixes!)

npm r babel-plugin-syntax-no-paren & npm i babel-plugin-syntax-no-paren

Warning

This module uses register-babel-syntax, which modifies the source code of @babel/parser, so ensure that register-babel-syntax loads before you require @babel/core. Since this is basically an hack it may stop working, it has been tested with @babel/parser@7.18.0.

Allowed Syntax

// Normal 'if' with block
if (cond) {
    statement();
}

// Normal 'if' with statement
if (cond)
    statement();

// Paren-free 'if' with block
if cond {
    statement();
}

// Paren-free 'if' with statement
if cond
    statement();

// Single-line paren-free 'if' with statement
if cond; statement();

The same is valid for the while, do-while and for(each) statements

while cond
    statement();

do
    statement();
while cond

for const e of list
    statement();

Now allows the following syntaxes for try, catch, and finally

try
    statement();
catch
    statement();
finally
    statement();

and allows to put try statements without a catch

try
    statement();

try {
    statement();
}

// Both transpile to
try {
    statement();
}
catch {}

Not Allowed Syntax

Number range for

for let i = 0; i < 10; i++ {
    statement();
}

Empty statements

The following code is ambiguous:

const a = [ [ 1, 2, 3 ] ];

if a
    [0].forEach(console.log);

console.log(a);

It could be transpiled in both

const a = [ [ 1, 2, 3 ] ];

if (a)
    [0].forEach(console.log);

console.log(a);

and

const a = [ [ 1, 2, 3 ] ];

if (a[0].forEach(console.log))
    console.log(a);

So I made it so that when an empty statement (;) is inside a paren-free if it gets skipped.
Practically this

const a = [ [ 1, 2, 3 ] ];

if a; // ← Empty statement
    [0].forEach(console.log);

console.log(a);

doesn't get transpiled to this

const a = [ [ 1, 2, 3 ] ];

if (a)
    ; // ← Empty statement

[0].forEach(console.log);
console.log(a);

but to this

const a = [ [ 1, 2, 3 ] ];

if (a)
    [0].forEach(console.log);

console.log(a);

so basically, the following is not allowed.

if cond
    ;

The same is valid for the while and for(each) statements.

No parentheses in catch parameter

If the parameter where allowed to not have parentheses it would be impossible to distinguish from a body

// Not allowed
try
    statement();
catch e
    statement();

// Allowed
try
    statement();
catch (e)
    statement();

because it could have meaned this

try {
    statement();
}
catch {
    e
}
    
statement();

I will probably fix this, because the param must actually be a simple identifier, while a body must not

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.2.1
    4
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.2.1
    4
  • 1.2.0
    0
  • 1.0.0
    0

Package Sidebar

Install

npm i babel-plugin-syntax-no-paren

Weekly Downloads

4

Version

1.2.1

License

ISC

Unpacked Size

10.4 kB

Total Files

4

Last publish

Collaborators

  • seanalunni