Giter Site home page Giter Site logo

babel-plugin-syntax-no-paren's Introduction

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/[email protected].

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

babel-plugin-syntax-no-paren's People

Contributors

afatnibba avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.