Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
00f58b9
feat: optional chaing
xtuc Apr 24, 2017
72259ca
feat: cleanup
xtuc Apr 25, 2017
64ff5a0
refactor: improved transformation logic
xtuc Apr 29, 2017
ed15443
fix: use undefined instead of null
xtuc Apr 29, 2017
6bd3bf4
feat: optional chaining with function call
xtuc Apr 29, 2017
cc6959e
feat: WIP assignements
xtuc May 4, 2017
3faca62
wip
xtuc May 7, 2017
a9d8040
refactor: change undefined to void 0
xtuc May 29, 2017
98487b5
fix: typo
xtuc May 29, 2017
750b03a
refactor: use WeakSet to keep track of transformed nodes
xtuc May 29, 2017
3fae121
Implement Null Propagation Operator
jridgewell Jun 2, 2017
2a49689
Use a better nested syntax
jridgewell Jun 3, 2017
30ee871
Split syntax and transform into two plugins
jridgewell Jun 3, 2017
5fe4803
Simplify NewExpression|CallExpression visitor
jridgewell Jun 3, 2017
85b6b4b
Print optional chain operator
jridgewell Jun 4, 2017
acdd363
Refactor
jridgewell Jun 4, 2017
9ce797d
Reduce context memoization when possible
jridgewell Jun 4, 2017
b048bff
Add optional to MemberExpression
jridgewell Jun 4, 2017
899634d
Add exec tests
jridgewell Jun 4, 2017
0e5f597
Readme
jridgewell Jun 4, 2017
1f22ac3
Lint
jridgewell Jun 4, 2017
ef87acc
Test not-top-level optional chaining
jridgewell Jun 4, 2017
a62cb92
Delete unnecessary check
jridgewell Jun 4, 2017
6cc2f5d
Consider any unary expression
jridgewell Jun 4, 2017
faa6c9f
Use nil
jridgewell Jun 5, 2017
d92309f
PR comments
jridgewell Jun 6, 2017
54d9732
Test Update and Unary expressions
jridgewell Jun 6, 2017
9e91ac5
Optional call expressions short circuit later member expressions
jridgewell Jun 7, 2017
462825b
Simplify transform
jridgewell Jun 7, 2017
f537fc7
Add babel-generator tests
jridgewell Jun 7, 2017
97d0ab7
Update babylon
jridgewell Jun 7, 2017
0740e61
Use strict
jridgewell Jun 8, 2017
f363ec6
Add to stage-1 preset
jridgewell Jun 27, 2017
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
2 changes: 1 addition & 1 deletion packages/babel-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"babel-template": "7.0.0-alpha.12",
"babel-traverse": "7.0.0-alpha.12",
"babel-types": "7.0.0-alpha.12",
"babylon": "7.0.0-beta.12",
"babylon": "7.0.0-beta.13",
"convert-source-map": "^1.1.0",
"debug": "^2.1.1",
"json5": "^0.5.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
},
"devDependencies": {
"babel-helper-fixtures": "7.0.0-alpha.12",
"babylon": "^7.0.0-beta.12"
"babylon": "^7.0.0-beta.13"
}
}
17 changes: 15 additions & 2 deletions packages/babel-generator/src/generators/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(")");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -203,12 +211,17 @@ export function MemberExpression(node: Object) {
computed = true;
}

if (node.optional) {
this.token("?.");
}
if (computed) {
this.token("[");
this.print(node.property, node);
this.token("]");
} else {
this.token(".");
if (!node.optional) {
this.token(".");
}
this.print(node.property, node);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
foo?.();
foo?.("foo");
foo?.("foo", "bar");
foo?.(bar());
foo?.(bar("test"));
foo(bar?.());
foo(bar?.("test"));

a.foo?.();
a.foo?.("foo");
a.foo?.("foo", "bar");
a.foo?.(bar());
a.foo?.(bar("test"));
a.foo(bar?.());
a.foo(bar?.("test"));

a?.foo?.();
a?.foo?.("foo");
a?.foo?.("foo", "bar");
a?.foo?.(bar());
a?.foo?.(bar("test"));
a?.foo(bar?.());
a?.foo(bar?.("test"));

a.foo?.().baz;
a.foo?.("foo").baz;
a.foo?.("foo", "bar").baz;
a.foo?.(bar()).baz;
a.foo?.(bar("test")).baz;
a.foo(bar?.()).baz;
a.foo(bar?.("test")).baz;

a.foo?.()?.baz;
a.foo?.("foo")?.baz;
a.foo?.("foo", "bar")?.baz;
a.foo?.(bar())?.baz;
a.foo?.(bar("test"))?.baz;
a.foo(bar?.())?.baz;
a.foo(bar?.("test"))?.baz;
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
foo?.();
foo?.("foo");
foo?.("foo", "bar");
foo?.(bar());
foo?.(bar("test"));
foo(bar?.());
foo(bar?.("test"));

a.foo?.();
a.foo?.("foo");
a.foo?.("foo", "bar");
a.foo?.(bar());
a.foo?.(bar("test"));
a.foo(bar?.());
a.foo(bar?.("test"));

a?.foo?.();
a?.foo?.("foo");
a?.foo?.("foo", "bar");
a?.foo?.(bar());
a?.foo?.(bar("test"));
a?.foo(bar?.());
a?.foo(bar?.("test"));

a.foo?.().baz;
a.foo?.("foo").baz;
a.foo?.("foo", "bar").baz;
a.foo?.(bar()).baz;
a.foo?.(bar("test")).baz;
a.foo(bar?.()).baz;
a.foo(bar?.("test")).baz;

a.foo?.()?.baz;
a.foo?.("foo")?.baz;
a.foo?.("foo", "bar")?.baz;
a.foo?.(bar())?.baz;
a.foo?.(bar("test"))?.baz;
a.foo(bar?.())?.baz;
a.foo(bar?.("test"))?.baz;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
foo?.["bar"];
foo?.bar;

foo.bar?.foo;
foo?.bar.foo;
foo?.bar?.foo;
foo.bar?.["foo"];
foo?.bar["foo"];
foo?.bar?.["foo"];
foo["bar"]?.foo;
foo?.["bar"].foo;
foo?.["bar"]?.foo;

0.?.toString();
0.5?.toString();
1.000?.toString();
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
foo?.["bar"];
foo?.bar;

foo.bar?.foo;
foo?.bar.foo;
foo?.bar?.foo;
foo.bar?.["foo"];
foo?.bar["foo"];
foo?.bar?.["foo"];
foo["bar"]?.foo;
foo?.["bar"].foo;
foo?.["bar"]?.foo;

0.?.toString();
0.5?.toString();
1.000?.toString();
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
new foo?.();
new foo?.("foo");
new foo?.("foo", "bar");
new foo?.(bar());
new foo?.(bar("test"));
foo(new bar?.());
foo(new bar?.("test"));

new a.foo?.();
new a.foo?.("foo");
new a.foo?.("foo", "bar");
new a.foo?.(bar());
new a.foo?.(bar("test"));
a.foo(new bar?.());
a.foo(new bar?.("test"));

new a?.foo?.();
new a?.foo?.("foo");
new a?.foo?.("foo", "bar");
new a?.foo?.(bar());
new a?.foo?.(bar("test"));
a?.foo(new bar?.());
a?.foo(new bar?.("test"));

new a.foo?.().baz;
new a.foo?.("foo").baz;
new a.foo?.("foo", "bar").baz;
new a.foo?.(bar()).baz;
new a.foo?.(bar("test")).baz;
a.foo(new bar?.()).baz;
a.foo(new bar?.("test")).baz;

new a.foo?.()?.baz;
new a.foo?.("foo")?.baz;
new a.foo?.("foo", "bar")?.baz;
new a.foo?.(bar())?.baz;
new a.foo?.(bar("test"))?.baz;
a.foo(new bar?.())?.baz;
a.foo(new bar?.("test"))?.baz;
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
new foo?.();
new foo?.("foo");
new foo?.("foo", "bar");
new foo?.(bar());
new foo?.(bar("test"));
foo(new bar?.());
foo(new bar?.("test"));

new a.foo?.();
new a.foo?.("foo");
new a.foo?.("foo", "bar");
new a.foo?.(bar());
new a.foo?.(bar("test"));
a.foo(new bar?.());
a.foo(new bar?.("test"));

new a?.foo?.();
new a?.foo?.("foo");
new a?.foo?.("foo", "bar");
new a?.foo?.(bar());
new a?.foo?.(bar("test"));
a?.foo(new bar?.());
a?.foo(new bar?.("test"));

new a.foo?.().baz;
new a.foo?.("foo").baz;
new a.foo?.("foo", "bar").baz;
new a.foo?.(bar()).baz;
new a.foo?.(bar("test")).baz;
a.foo(new bar?.()).baz;
a.foo(new bar?.("test")).baz;

new a.foo?.()?.baz;
new a.foo?.("foo")?.baz;
new a.foo?.("foo", "bar")?.baz;
new a.foo?.(bar())?.baz;
new a.foo?.(bar("test"))?.baz;
a.foo(new bar?.())?.baz;
a.foo(new bar?.("test"))?.baz;
1 change: 1 addition & 0 deletions packages/babel-generator/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ suites.forEach(function (testSuite) {
"functionSent",
"jsx",
"objectRestSpread",
"optionalChaining",
],
strictMode: false,
sourceType: "module",
Expand Down
3 changes: 3 additions & 0 deletions packages/babel-plugin-syntax-optional-chaining/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
*.log
src
3 changes: 3 additions & 0 deletions packages/babel-plugin-syntax-optional-chaining/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
src
test
*.log
35 changes: 35 additions & 0 deletions packages/babel-plugin-syntax-optional-chaining/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# babel-plugin-syntax-optional-chaining

Allow parsing of optional properties.

## Installation

```sh
npm install --save-dev babel-plugin-syntax-optional-chaining
```

## Usage

### Via `.babelrc` (Recommended)

**.babelrc**

```json
{
"plugins": ["syntax-optional-chaining"]
}
```

### Via CLI

```sh
babel --plugins syntax-optional-chaining script.js
```

### Via Node API

```javascript
require("babel-core").transform("code", {
plugins: ["syntax-optional-chaining"]
});
```
7 changes: 7 additions & 0 deletions packages/babel-plugin-syntax-optional-chaining/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function () {
return {
manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("optionalChaining");
},
};
}
13 changes: 13 additions & 0 deletions packages/babel-plugin-syntax-optional-chaining/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "babel-plugin-syntax-optional-chaining",
"version": "7.0.0-alpha.13",
"description": "Allow parsing of optional properties",
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-optional-chaining",
"license": "MIT",
"main": "lib/index.js",
"keywords": [
"babel-plugin"
],
"dependencies": {},
"devDependencies": {}
}
3 changes: 3 additions & 0 deletions packages/babel-plugin-transform-optional-chaining/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
src
test
*.log
Loading