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
Handle cases when ??
and ?.
is in binding initializers
#12032
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c8d8644
test: add test for nullish coalescing
JLHwung 000c924
test: add control group
JLHwung babc8f7
test: add tests for optional chaining
JLHwung 9481ae0
test: add tests on optional chaining mixed with private class elements
JLHwung 833f6aa
fix: wrap member chains to IIFE when it is in parameter default
JLHwung 99ad236
chore: add more testcases
JLHwung 47b51c4
chore: update test fixtures
JLHwung dc257bf
fix: NodePath.get is always non nullish
JLHwung File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...rties/test/fixtures/private-loose/optional-chain-in-function-param-with-transform/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
class Foo { | ||
static #x = 1; | ||
static #m = function() { return this.#x; }; | ||
static #self = Foo; | ||
static self = Foo; | ||
static getSelf() { return this } | ||
|
||
static test() { | ||
const o = { Foo: Foo }; | ||
const deep = { very: { o } }; | ||
function fn() { | ||
return o; | ||
} | ||
function fnDeep() { | ||
return deep; | ||
} | ||
|
||
function f(o, r = o?.Foo.#m()) { | ||
return r; | ||
} | ||
|
||
function g(o, r = o?.Foo.#self.getSelf().#m()) { | ||
return r; | ||
} | ||
|
||
function h(fnDeep, r = fnDeep?.().very.o?.Foo?.#m()) { | ||
return r; | ||
} | ||
|
||
function i(fn, r = fn?.().Foo.#self?.getSelf()?.self.#m()) { | ||
return r; | ||
} | ||
|
||
function j(fn, r = (fn().Foo.#self.getSelf().self.#m)?.()) { | ||
return r; | ||
} | ||
|
||
expect(f(o)).toBe(1); | ||
expect(g(o)).toBe(1); | ||
expect(h(fnDeep)).toBe(1); | ||
expect(i(fn)).toBe(1); | ||
expect(j(fn)).toBe(1); | ||
} | ||
} | ||
|
||
Foo.test(); |
46 changes: 46 additions & 0 deletions
46
...ties/test/fixtures/private-loose/optional-chain-in-function-param-with-transform/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
class Foo { | ||
static #x = 1; | ||
static #m = function() { return this.#x; }; | ||
static #self = Foo; | ||
static self = Foo; | ||
static getSelf() { return this } | ||
|
||
static test() { | ||
const o = { Foo: Foo }; | ||
const deep = { very: { o } }; | ||
function fn() { | ||
return o; | ||
} | ||
function fnDeep() { | ||
return deep; | ||
} | ||
|
||
function f(o, r = o?.Foo.#m()) { | ||
return r; | ||
} | ||
|
||
function g(o, r = o?.Foo.#self.getSelf().#m?.()) { | ||
return r; | ||
} | ||
|
||
function h(fnDeep, r = fnDeep?.().very.o?.Foo?.#m()) { | ||
return r; | ||
} | ||
|
||
function i(fn, r = fn?.().Foo.#self?.getSelf()?.self.#m()) { | ||
return r; | ||
} | ||
|
||
function j(fn, r = (fn().Foo.#self.getSelf().self.#m)?.()) { | ||
return r; | ||
} | ||
|
||
f(o); | ||
g(o); | ||
h(fnDeep); | ||
i(fn); | ||
j(fn); | ||
} | ||
} | ||
|
||
Foo.test(); |
6 changes: 6 additions & 0 deletions
6
.../test/fixtures/private-loose/optional-chain-in-function-param-with-transform/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"plugins": [ | ||
["proposal-optional-chaining", { "loose": true }], | ||
["proposal-class-properties", { "loose": true }] | ||
] | ||
} |
96 changes: 96 additions & 0 deletions
96
...ies/test/fixtures/private-loose/optional-chain-in-function-param-with-transform/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
function _classPrivateFieldLooseBase(receiver, privateKey) { if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) { throw new TypeError("attempted to use private field on non-instance"); } return receiver; } | ||
|
||
var id = 0; | ||
|
||
function _classPrivateFieldLooseKey(name) { return "__private_" + id++ + "_" + name; } | ||
|
||
var _x = _classPrivateFieldLooseKey("x"); | ||
|
||
var _m = _classPrivateFieldLooseKey("m"); | ||
|
||
var _self = _classPrivateFieldLooseKey("self"); | ||
|
||
class Foo { | ||
static getSelf() { | ||
return this; | ||
} | ||
|
||
static test() { | ||
const o = { | ||
Foo: Foo | ||
}; | ||
const deep = { | ||
very: { | ||
o | ||
} | ||
}; | ||
|
||
function fn() { | ||
return o; | ||
} | ||
|
||
function fnDeep() { | ||
return deep; | ||
} | ||
|
||
function f(o, r = (() => o === null || o === void 0 ? void 0 : _classPrivateFieldLooseBase(o.Foo, _m)[_m]())()) { | ||
return r; | ||
} | ||
|
||
function g(o, r = (() => { | ||
var _ref; | ||
|
||
return (_ref = (() => o === null || o === void 0 ? void 0 : _classPrivateFieldLooseBase(_classPrivateFieldLooseBase(o.Foo, _self)[_self].getSelf(), _m)[_m])()) == null ? void 0 : _ref(); | ||
})()) { | ||
return r; | ||
} | ||
|
||
function h(fnDeep, r = (() => { | ||
var _fnDeep$very$o$Foo, _fnDeep$very$o; | ||
|
||
return (_fnDeep$very$o$Foo = fnDeep == null ? void 0 : (_fnDeep$very$o = fnDeep().very.o) == null ? void 0 : _fnDeep$very$o.Foo) === null || _fnDeep$very$o$Foo === void 0 ? void 0 : _classPrivateFieldLooseBase(_fnDeep$very$o$Foo, _m)[_m](); | ||
})()) { | ||
return r; | ||
} | ||
|
||
function i(fn, r = (() => { | ||
var _getSelf, _ref2; | ||
|
||
return (_getSelf = (_ref2 = (() => fn === null || fn === void 0 ? void 0 : _classPrivateFieldLooseBase(fn().Foo, _self)[_self])()) == null ? void 0 : _ref2.getSelf()) === null || _getSelf === void 0 ? void 0 : _classPrivateFieldLooseBase(_getSelf.self, _m)[_m](); | ||
})()) { | ||
return r; | ||
} | ||
|
||
function j(fn, r = (() => { | ||
var _classPrivateFieldLoo, _classPrivateFieldLoo2; | ||
|
||
return (_classPrivateFieldLoo = (_classPrivateFieldLoo2 = _classPrivateFieldLooseBase(_classPrivateFieldLooseBase(fn().Foo, _self)[_self].getSelf().self, _m))[_m]) == null ? void 0 : _classPrivateFieldLoo.call(_classPrivateFieldLoo2); | ||
})()) { | ||
return r; | ||
} | ||
|
||
f(o); | ||
g(o); | ||
h(fnDeep); | ||
i(fn); | ||
j(fn); | ||
} | ||
|
||
} | ||
|
||
Object.defineProperty(Foo, _x, { | ||
writable: true, | ||
value: 1 | ||
}); | ||
Object.defineProperty(Foo, _m, { | ||
writable: true, | ||
value: function () { | ||
return _classPrivateFieldLooseBase(this, _x)[_x]; | ||
} | ||
}); | ||
Object.defineProperty(Foo, _self, { | ||
writable: true, | ||
value: Foo | ||
}); | ||
Foo.self = Foo; | ||
Foo.test(); |
46 changes: 46 additions & 0 deletions
46
...sal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
class Foo { | ||
static #x = 1; | ||
static #m = function() { return this.#x; }; | ||
static #self = Foo; | ||
static self = Foo; | ||
static getSelf() { return this } | ||
|
||
static test() { | ||
const o = { Foo: Foo }; | ||
const deep = { very: { o } }; | ||
function fn() { | ||
return o; | ||
} | ||
function fnDeep() { | ||
return deep; | ||
} | ||
|
||
function f(o, r = o?.Foo.#m()) { | ||
return r; | ||
} | ||
|
||
function g(o, r = o?.Foo.#self.getSelf().#m()) { | ||
return r; | ||
} | ||
|
||
function h(fnDeep, r = fnDeep?.().very.o?.Foo?.#m()) { | ||
return r; | ||
} | ||
|
||
function i(fn, r = fn?.().Foo.#self?.getSelf()?.self.#m()) { | ||
return r; | ||
} | ||
|
||
function j(fn, r = (fn().Foo.#self.getSelf().self.#m)?.()) { | ||
return r; | ||
} | ||
|
||
expect(f(o)).toBe(1); | ||
expect(g(o)).toBe(1); | ||
expect(h(fnDeep)).toBe(1); | ||
expect(i(fn)).toBe(1); | ||
expect(j(fn)).toBe(1); | ||
} | ||
} | ||
|
||
Foo.test(); |
46 changes: 46 additions & 0 deletions
46
...al-class-properties/test/fixtures/private-loose/optional-chain-in-function-param/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
class Foo { | ||
static #x = 1; | ||
static #m = function() { return this.#x; }; | ||
static #self = Foo; | ||
static self = Foo; | ||
static getSelf() { return this } | ||
|
||
static test() { | ||
const o = { Foo: Foo }; | ||
const deep = { very: { o } }; | ||
function fn() { | ||
return o; | ||
} | ||
function fnDeep() { | ||
return deep; | ||
} | ||
|
||
function f(o, r = o?.Foo.#m()) { | ||
return r; | ||
} | ||
|
||
function g(o, r = o?.Foo.#self.getSelf().#m?.()) { | ||
return r; | ||
} | ||
|
||
function h(fnDeep, r = fnDeep?.().very.o?.Foo?.#m()) { | ||
return r; | ||
} | ||
|
||
function i(fn, r = fn?.().Foo.#self?.getSelf()?.self.#m()) { | ||
return r; | ||
} | ||
|
||
function j(fn, r = (fn().Foo.#self.getSelf().self.#m)?.()) { | ||
return r; | ||
} | ||
|
||
f(o); | ||
g(o); | ||
h(fnDeep); | ||
i(fn); | ||
j(fn); | ||
} | ||
} | ||
|
||
Foo.test(); |
4 changes: 4 additions & 0 deletions
4
...lass-properties/test/fixtures/private-loose/optional-chain-in-function-param/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"plugins": [["proposal-class-properties", { "loose": true }]], | ||
"minNodeVersion": "14.0.0" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this only apply in parameters? I'm not sure when a scope'a path would be the pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, if a Pattern is a
sopce.path
, its parent must be aFunction
:babel/packages/babel-types/src/validators/isScope.js
Line 22 in 3628c52
because a
Pattern
is not Scopeable.However when reviewing what is authored by past me in #10912, I do realize that current
isScope
definition does not cover pattern in catch clause:I will fix that in a separate PR.