-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Class private properties #6120
Class private properties #6120
Changes from all commits
75e8129
3335638
824eb88
81577eb
65cb056
ed60ccf
ec364a2
4b73b44
2974eb2
a3376a8
f59d7e3
5cceab0
a593905
4f1817d
9886bac
2d4ea85
a3cd4d0
59d3b9a
74dd906
f462604
396f35e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{ "plugins": ["classProperties"] } | ||
{ "plugins": ["classProperties", "classPrivateProperties"] } |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -495,6 +495,42 @@ helpers.possibleConstructorReturn = defineHelper(` | |
} | ||
`); | ||
|
||
helpers.classPrivateFieldKey = defineHelper(` | ||
var id = 0; | ||
export default function _classPrivateFieldKey(name) { | ||
// Can we use a middle finger emoji? | ||
return "__private_" + (id++) + "_" + name; | ||
} | ||
`); | ||
|
||
helpers.classPrivateFieldBase = defineHelper(` | ||
export default function _classPrivateFieldBase(receiver, privateKey) { | ||
if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In loose mode, do you even want to bother with these hasOwnProperty checks, using defining non-configurable properties rather than using = when setting it up, etc? Seems like public class fields use =. |
||
throw new TypeError("attempted to use private field on non-instance"); | ||
} | ||
return receiver; | ||
} | ||
`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like you use classPrivateFieldBase/classPrivateFieldKey in the loose mode transform, and classPrivateFieldGet/classPrivateFieldSet in the normal mode. Is that right? Consider adding comments or putting "loose" in the names of the first two to make this more locally apparent. |
||
|
||
helpers.classPrivateFieldGet = defineHelper(` | ||
export default function _classPrivateFieldGet(receiver, privateMap) { | ||
if (!privateMap.has(receiver)) { | ||
throw new TypeError("attempted to get private field on non-instance"); | ||
} | ||
return privateMap.get(receiver); | ||
} | ||
`); | ||
|
||
helpers.classPrivateFieldPut = defineHelper(` | ||
export default function _classPrivateFieldPut(receiver, privateMap, value) { | ||
if (!privateMap.has(receiver)) { | ||
throw new TypeError("attempted to set private field on non-instance"); | ||
} | ||
privateMap.set(receiver, value); | ||
return value; | ||
} | ||
`); | ||
|
||
helpers.set = defineHelper(` | ||
export default function _set(object, property, value, receiver) { | ||
var desc = Object.getOwnPropertyDescriptor(object, property); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
export default function() { | ||
return { | ||
manipulateOptions(opts, parserOpts) { | ||
parserOpts.plugins.push("classProperties"); | ||
parserOpts.plugins.push("classProperties", "classPrivateProperties"); | ||
}, | ||
}; | ||
} |
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.
😄