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

Fix #365 #437

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0d54237
add tests
lucivpav Oct 7, 2020
d5a507d
improve the "throw" fix
lucivpav Oct 7, 2020
2cc7937
add failing arrow function test
lucivpav Oct 13, 2020
3394a26
replace the tests with a single thorough test
lucivpav Oct 13, 2020
a80ae88
add another test - ()expression inside of a return expression
lucivpav Oct 13, 2020
a5790be
more thought out fix; WIP
lucivpav Oct 15, 2020
2a1ed4e
proper fix
lucivpav Oct 19, 2020
4fa78f0
cleanup
lucivpav Oct 19, 2020
6bb44bb
cleanup
lucivpav Oct 19, 2020
b44cff1
cleanup
lucivpav Oct 19, 2020
6e566cf
prettify
lucivpav Oct 19, 2020
0ff6beb
fix problem with with default function parameters
Nov 26, 2020
ce9e1ce
fix problem with unwanted parentheses in imports
Nov 26, 2020
acc9cd0
revert no-op change
Dec 14, 2020
da7d71c
dont parenthesize variable declaration expressions
Dec 17, 2020
873307f
revert unwanted new files
Dec 17, 2020
bb30704
revert logic to simplify PR; only parenthesize at a level where it is…
Dec 17, 2020
3e73ef3
Merge branch 'master' into github/fix-365
Dec 21, 2020
34cd8c6
nits
Jan 6, 2021
fae4df2
nit
Jan 6, 2021
91c5ee0
improve performance
Jan 6, 2021
2cfc090
fix isParenthesized zero length input behavior
Jan 6, 2021
9bc0f12
use a single isParenthesized() fn instead of additional isParenthesiz…
Jan 6, 2021
ca68677
use constant for newline instead of a literal
Jan 6, 2021
d669661
handle edge case in addMultiLineIndent()
Jan 6, 2021
fb1ab86
add test, where parenthesis should not be added
Jan 13, 2021
3f8ae47
remove unused function parameter
Jan 14, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
631 changes: 398 additions & 233 deletions escodegen.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions test/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
'use strict';

var fs = require('fs'),
esprima = require('./3rdparty/esprima-harmony.original'),
esprima = require('esprima'),
escodegen = require('./loader'),
chai = require('chai'),
expect = chai.expect;
Expand All @@ -41,7 +41,8 @@ function test(code, expected) {
range: true,
loc: false,
tokens: true,
raw: false
raw: false,
sourceType: 'module'
};

tree = esprima.parse(code, options);
Expand Down
12 changes: 12 additions & 0 deletions test/comment/array-elements.expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var test = [
/**
* Test 2
*/
a,
/*
* Test 1
*/
2,
// Test 3
3 + 3
];
12 changes: 12 additions & 0 deletions test/comment/array-elements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var test = [
/**
* Test 2
*/
a,
/*
* Test 1
*/
2,
// Test 3
3+3
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function foo(x) {
return (
/* comment 1 */
x > 0 || // comment 2
x < 5
);
}
function foo(x) {
return (
/* comment 1 */
x > 0 || // comment 2
x < 5
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// this test should pass, but it is not passing due to a bug
// https://github.com/estools/escodegen/issues/336
// https://github.com/estools/escodegen/issues/435

function foo(x) {
return /* comment 1 */ x > 0 || // comment 2
x < 5;
}

function foo(x) {
return /* comment 1 */ x > 0 ||
// comment 2
x < 5;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function foo(x) {
return /* comment 1 */ x > 0 || // comment 2
x < 5;
}

function foo(x) {
return /* comment 1 */ x > 0 ||
// comment 2
x < 5;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// this test should pass, but it is not passing due to a bug
// https://github.com/estools/escodegen/issues/336
// https://github.com/estools/escodegen/issues/435

function foo(x) {
return /* comment 1 */ x > 0 || // comment 2
x < 5;
}

function foo(x) {
return /* comment 1 */ x > 0 ||
// comment 2
x < 5;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
function foo() {
return (
// comment
3 + 3
);
}
function foo() {
return (
// comment
3 + 3
);
}
function foo() {
return (
/* comment */
3 + 3
);
}
function foo() {
return (
/* comment
comment
comment
*/
3 + 3
);
}
function foo() {
return (
/* comment
comment
comment
*/
3 + 3
);
}
function foo() {
return (
// comment
/* comment */
// comment
3 + 3
);
}
function foo() {
return (
// one
3 + 3 - // two
(1 + 1)
);
}
function foo(a, b, c) {
return (
// comment
a >= b && a <= c || a === 42 || a === 666
);
}
function foo(a, b, c) {
return (
// comment
a >= b && a <= c || a === 42 || a === 666
);
}
function foo(a, b, c) {
throw (
// comment
a >= b && a <= c || a === 42 || a === 666
);
}
let arrowFn = () => (
// comment
{
a: 1,
b: 2
}
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// this test should pass, but it is not passing due to a bug
// https://github.com/estools/escodegen/issues/433

function foo() {
return /* comment */ (
// comment
3 + 3
);
}
91 changes: 91 additions & 0 deletions test/comment/comment-as-first-element-in-parenthesis-expression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
function foo() {
return (
// comment
3+3
);
}

function foo() {
return ( // comment
3+3
);
}

function foo() {
return ( /* comment */
3+3
);
}

function foo() {
return ( /* comment
comment
comment
*/
3+3
);
}

function foo() {
return (
/* comment
comment
comment
*/
3+3
);
}

function foo() {
return (
// comment
/* comment */
// comment
3+3
);
}

function foo() {
return (
(
// one
3+3
) -
(
// two
1+1
)
);
}

function foo(a, b, c) {
return (
// comment
(a >= b && a <= c)
|| a === 42 || a === 666
);
}

function foo(a, b, c) {
return (
( // comment
a >= b &&
a <= c)
|| a === 42 || a === 666
);
}

function foo(a, b, c) {
throw (
// comment
(a >= b && a <= c)
|| a === 42 || a === 666
);
}

let arrowFn = () => (
// comment
{
a: 1, b: 2
}
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// this test should pass, but it is not passing due to a bug
// https://github.com/estools/escodegen/issues/433

function foo() {
return /* comment */ (
// comment
3+3
);
}
11 changes: 11 additions & 0 deletions test/comment/imports.expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// this import is important
import 'foo';
import { foo } from 'foo';
import { foo as bar } from 'foo';
// this import is important too
import {
foo as bar,
// alias needed because of ...
test as testing,
logging
} from 'foo';
11 changes: 11 additions & 0 deletions test/comment/imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// this import is important
import 'foo';
import {foo} from 'foo';
import {foo as bar} from 'foo';
// this import is important too
import {
foo as bar,
// alias needed because of ...
test as testing,
logging
} from 'foo';
15 changes: 15 additions & 0 deletions test/comment/method-description.expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class MyClass {
/**
* description
*/
foo(a, b) {
a.bar(b);
}
}
class MyClass2 {
// description
foo(a, b, c, // NOTE: ...
d = false) {
return 42;
}
}
17 changes: 17 additions & 0 deletions test/comment/method-description.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class MyClass {
/**
* description
*/
foo(a, b) {
a.bar(b);
}
}

class MyClass2 {
// description
foo(a, b, c,
// NOTE: ...
d = false) {
return 42;
}
}
6 changes: 4 additions & 2 deletions test/comment/try-block-line-comment.expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ finally {
}
try {
} catch (e) {
} finally {
} //
finally {
}
{
try {
} catch (e) {
} finally {
} //
finally {
}
}
27 changes: 27 additions & 0 deletions test/comment/variable-declaration.expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
let variable = // comment
3 + 3;
let variable = // comment
3 + 3;
let variable = /* comment */
3 + 3;
let variable = /* comment
comment
comment
*/
3 + 3;
let variable = // comment
/* comment */
// comment
3 + 3;
let variable = /* comment */
// comment
3 + 3;
let variable = // one
3 + 3 - // two
(1 + 1);
let age = // comment
42,
// comment
height = 165;
let variable = // comment
a >= b && a <= c || a === 42 || a === 666;
Loading