PR comments

This commit is contained in:
Justin Ridgewell
2017-06-05 22:53:24 -04:00
parent faa6c9f708
commit d92309f0db
4 changed files with 22 additions and 5 deletions

View File

@@ -51,11 +51,16 @@ export function NewExpression(node: Object, parent: Object) {
this.word("new");
this.space();
this.print(node.callee, node);
if (node.arguments.length === 0 && this.format.minified &&
if (this.format.minified &&
node.arguments.length === 0 &&
!node.optional &&
!t.isCallExpression(parent, { callee: node }) &&
!t.isMemberExpression(parent) &&
!t.isNewExpression(parent)) return;
if (node.optional) {
this.token("?.");
}
this.token("(");
this.printList(node.arguments, node);
this.token(")");
@@ -89,6 +94,9 @@ function commaSeparatorNewline() {
export function CallExpression(node: Object) {
this.print(node.callee, node);
if (node.optional) {
this.token("?.");
}
this.token("(");
const isPrettyCall = node._prettyCall;

View File

@@ -43,6 +43,8 @@ const baz = obj?.foo?.bar?.baz(); // 42
const safe = obj?.qux?.baz(); // undefined
const safe2 = obj?.foo.bar.qux?.(); // undefined
const willThrow = obj?.foo.bar.qux(); // Error: not a function
// Top function can be called directly, too.
function test() {
return 42;
@@ -69,6 +71,8 @@ const baz = new obj?.foo?.bar?.baz(); // baz instance
const safe = new obj?.qux?.baz(); // undefined
const safe2 = new obj?.foo.bar.qux?.(); // undefined
const willThrow = new obj?.foo.bar.qux(); // Error: not a constructor
// Top classes can be called directly, too.
class Test {
}

View File

@@ -6,14 +6,11 @@ export default function ({ types: t }) {
const optionals = [path.node];
const nil = scope.buildUndefinedNode();
let objectPath = path.get(key);
while (objectPath.isMemberExpression()) {
for (let objectPath = path.get(key); objectPath.isMemberExpression(); objectPath = objectPath.get("object")) {
const { node } = objectPath;
if (node.optional) {
optionals.push(node);
}
objectPath = objectPath.get("object");
}
for (let i = optionals.length - 1; i >= 0; i--) {

View File

@@ -119,6 +119,10 @@ defineType("CallExpression", {
arguments: {
validate: chain(assertValueType("array"), assertEach(assertNodeType("Expression", "SpreadElement"))),
},
optional: {
validate: assertOneOf(true, false),
optional: true,
},
},
aliases: ["Expression"],
});
@@ -454,6 +458,10 @@ defineType("NewExpression", {
arguments: {
validate: chain(assertValueType("array"), assertEach(assertNodeType("Expression", "SpreadElement"))),
},
optional: {
validate: assertOneOf(true, false),
optional: true,
},
},
});