Skip to content

Commit

Permalink
Extract isAccessor variable
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-mc committed Aug 23, 2019
1 parent 3e6c955 commit fc24d84
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions packages/babel-helper-create-class-features-plugin/src/fields.js
Expand Up @@ -44,14 +44,15 @@ export function buildPrivateNamesNodes(privateNamesMap, loose, state) {
// because static fields are directly assigned to a variable in the
// buildPrivateStaticFieldInitSpec function.
const { id, static: isStatic, method: isMethod, getId, setId } = value;
const isAccessor = getId || setId;
if (loose) {
initNodes.push(
template.statement.ast`
var ${id} = ${state.addHelper("classPrivateFieldLooseKey")}("${name}")
`,
);
} else if (isMethod && !isStatic) {
if (getId || setId) {
if (isAccessor) {
initNodes.push(template.statement.ast`var ${id} = new WeakMap();`);
} else {
initNodes.push(template.statement.ast`var ${id} = new WeakSet();`);
Expand Down Expand Up @@ -140,10 +141,11 @@ const privateNameHandlerSpec = {
getId,
setId,
} = privateNamesMap.get(name);
const isAccessor = getId || setId;

if (isStatic) {
const helperName =
isMethod && !(getId || setId)
isMethod && !isAccessor
? "classStaticPrivateMethodGet"
: "classStaticPrivateFieldSpecGet";

Expand All @@ -155,7 +157,7 @@ const privateNameHandlerSpec = {
}

if (isMethod) {
if (getId || setId) {
if (isAccessor) {
return t.callExpression(file.addHelper("classPrivateFieldGet"), [
this.receiver(member),
t.cloneNode(id),
Expand Down Expand Up @@ -183,10 +185,11 @@ const privateNameHandlerSpec = {
setId,
getId,
} = privateNamesMap.get(name);
const isAccessor = getId || setId;

if (isStatic) {
const helperName =
isMethod && !(getId || setId)
isMethod && !isAccessor
? "classStaticPrivateMethodSet"
: "classStaticPrivateFieldSpecSet";

Expand Down Expand Up @@ -305,10 +308,11 @@ function buildPrivateInstanceFieldInitSpec(ref, prop, privateNamesMap) {
function buildPrivateStaticFieldInitSpec(prop, privateNamesMap) {
const privateName = privateNamesMap.get(prop.node.key.id.name);
const { id, getId, setId, initAdded } = privateName;
const isAccessor = getId || setId;

if (!prop.isProperty() && (initAdded || !(getId || setId))) return;
if (!prop.isProperty() && (initAdded || !isAccessor)) return;

if (getId || setId) {
if (isAccessor) {
privateNamesMap.set(prop.node.key.id.name, {
...privateName,
initAdded: true,
Expand Down Expand Up @@ -351,8 +355,8 @@ function buildPrivateMethodInitLoose(ref, prop, privateNamesMap) {
});
`;
}

if (getId || setId) {
const isAccessor = getId || setId;
if (isAccessor) {
privateNamesMap.set(prop.node.key.id.name, {
...privateName,
initAdded: true,
Expand All @@ -373,9 +377,11 @@ function buildPrivateMethodInitLoose(ref, prop, privateNamesMap) {
function buildPrivateInstanceMethodInitSpec(ref, prop, privateNamesMap) {
const privateName = privateNamesMap.get(prop.node.key.id.name);
const { id, getId, setId, initAdded } = privateName;

if (initAdded) return;

if (getId || setId) {
const isAccessor = getId || setId;
if (isAccessor) {
privateNamesMap.set(prop.node.key.id.name, {
...privateName,
initAdded: true,
Expand Down Expand Up @@ -420,9 +426,11 @@ function buildPublicFieldInitSpec(ref, prop, state) {
function buildPrivateStaticMethodInitLoose(ref, prop, state, privateNamesMap) {
const privateName = privateNamesMap.get(prop.node.key.id.name);
const { id, methodId, getId, setId, initAdded } = privateName;

if (initAdded) return;

if (getId || setId) {
const isAccessor = getId || setId;
if (isAccessor) {
privateNamesMap.set(prop.node.key.id.name, {
...privateName,
initAdded: true,
Expand Down

0 comments on commit fc24d84

Please sign in to comment.