babel-plugin-syntax-no-paren
v1.2.1
Published
Allows parentheses to be omitted in javascript 'if', 'while' and 'for'(each) statements
Downloads
13
Maintainers
Readme
babel-plugin-syntax-no-paren
Adds the possibility to omit:
- Parentheses in
if
,while
andfor
(each) statements. - Brackets in
try
,catch
andfinally
. catch
intry
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