@babel/helper-create-class-features-plugin
Advanced tools
Comparing version
@@ -53,4 +53,4 @@ "use strict"; | ||
if (path.node.static) { | ||
throw path.buildCodeFrameError("@babel/plugin-class-features doesn't support class static private methods yet."); | ||
if (path.node.static && path.node.kind !== "method") { | ||
throw path.buildCodeFrameError("@babel/plugin-class-features doesn't support class static private accessors yet."); | ||
} | ||
@@ -57,0 +57,0 @@ } |
@@ -77,3 +77,3 @@ "use strict"; | ||
update.setId = prop.scope.generateUidIdentifier(`set_${name}`); | ||
} else if (prop.node.kind === "method" && isMethod && isInstance) { | ||
} else if (prop.node.kind === "method") { | ||
update.methodId = prop.scope.generateUidIdentifier(name); | ||
@@ -206,4 +206,5 @@ } | ||
if (isStatic && !isMethod) { | ||
return _core().types.callExpression(file.addHelper("classStaticPrivateFieldSpecGet"), [this.receiver(member), _core().types.cloneNode(classRef), _core().types.cloneNode(id)]); | ||
if (isStatic) { | ||
const helperName = isMethod ? "classStaticPrivateMethodGet" : "classStaticPrivateFieldSpecGet"; | ||
return _core().types.callExpression(file.addHelper(helperName), [this.receiver(member), _core().types.cloneNode(classRef), _core().types.cloneNode(id)]); | ||
} | ||
@@ -238,4 +239,5 @@ | ||
if (isStatic && !isMethod) { | ||
return _core().types.callExpression(file.addHelper("classStaticPrivateFieldSpecSet"), [this.receiver(member), _core().types.cloneNode(classRef), _core().types.cloneNode(id), value]); | ||
if (isStatic) { | ||
const helperName = isMethod ? "classStaticPrivateMethodSet" : "classStaticPrivateFieldSpecSet"; | ||
return _core().types.callExpression(file.addHelper(helperName), [this.receiver(member), _core().types.cloneNode(classRef), _core().types.cloneNode(id), value]); | ||
} | ||
@@ -458,5 +460,21 @@ | ||
function buildPrivateInstanceMethodDeclaration(prop, privateNamesMap) { | ||
function buildPrivateStaticMethodInitLoose(ref, prop, state, privateNamesMap) { | ||
const { | ||
id, | ||
methodId | ||
} = privateNamesMap.get(prop.node.key.id.name); | ||
return _core().template.statement.ast` | ||
Object.defineProperty(${ref}, ${id}, { | ||
// configurable is false by default | ||
// enumerable is false by default | ||
// writable is false by default | ||
value: ${methodId.name} | ||
}); | ||
`; | ||
} | ||
function buildPrivateMethodDeclaration(prop, privateNamesMap, loose = false) { | ||
const privateName = privateNamesMap.get(prop.node.key.id.name); | ||
const { | ||
id, | ||
methodId, | ||
@@ -466,3 +484,4 @@ getId, | ||
getterDeclared, | ||
setterDeclared | ||
setterDeclared, | ||
static: isStatic | ||
} = privateName; | ||
@@ -495,2 +514,6 @@ const { | ||
if (isStatic && !loose) { | ||
return _core().types.variableDeclaration("var", [_core().types.variableDeclarator(id, _core().types.functionExpression(id, params, body, generator, async))]); | ||
} | ||
return _core().types.variableDeclaration("var", [_core().types.variableDeclarator(methodId, methodValue)]); | ||
@@ -520,9 +543,12 @@ } | ||
state.needsClassRef = true; | ||
return ref; | ||
return path.node.static ? ref : _core().types.thisExpression(); | ||
} | ||
}); | ||
replacer.isStatic = true; | ||
replacer.replace(); | ||
path.traverse(thisContextVisitor, state); | ||
if (path.isProperty()) { | ||
path.traverse(thisContextVisitor, state); | ||
} | ||
return state.needsClassRef; | ||
@@ -544,3 +570,3 @@ } | ||
if (isStatic && isField) { | ||
if (isStatic || isMethod && isPrivate) { | ||
const replaced = replaceThisContext(prop, ref, superRef, state, loose); | ||
@@ -581,3 +607,3 @@ needsClassRef = needsClassRef || replaced; | ||
instanceNodes.unshift(buildPrivateMethodInitLoose(_core().types.thisExpression(), prop, privateNamesMap)); | ||
staticNodes.push(buildPrivateInstanceMethodDeclaration(prop, privateNamesMap)); | ||
staticNodes.push(buildPrivateMethodDeclaration(prop, privateNamesMap, loose)); | ||
break; | ||
@@ -587,5 +613,16 @@ | ||
instanceNodes.unshift(buildPrivateInstanceMethodInitSpec(_core().types.thisExpression(), prop, privateNamesMap)); | ||
staticNodes.push(buildPrivateInstanceMethodDeclaration(prop, privateNamesMap)); | ||
staticNodes.push(buildPrivateMethodDeclaration(prop, privateNamesMap, loose)); | ||
break; | ||
case isStatic && isPrivate && isMethod && !loose: | ||
needsClassRef = true; | ||
staticNodes.push(buildPrivateMethodDeclaration(prop, privateNamesMap, loose)); | ||
break; | ||
case isStatic && isPrivate && isMethod && loose: | ||
needsClassRef = true; | ||
staticNodes.push(buildPrivateMethodDeclaration(prop, privateNamesMap, loose)); | ||
staticNodes.push(buildPrivateStaticMethodInitLoose(_core().types.cloneNode(ref), prop, state, privateNamesMap)); | ||
break; | ||
case isInstance && isPublic && isField && loose: | ||
@@ -592,0 +629,0 @@ instanceNodes.push(buildPublicFieldInitLoose(_core().types.thisExpression(), prop)); |
{ | ||
"name": "@babel/helper-create-class-features-plugin", | ||
"version": "7.3.4", | ||
"version": "7.4.0", | ||
"author": "The Babel Team (https://babeljs.io/team)", | ||
@@ -21,4 +21,4 @@ "license": "MIT", | ||
"@babel/helper-plugin-utils": "^7.0.0", | ||
"@babel/helper-replace-supers": "^7.3.4", | ||
"@babel/helper-split-export-declaration": "^7.0.0" | ||
"@babel/helper-replace-supers": "^7.4.0", | ||
"@babel/helper-split-export-declaration": "^7.4.0" | ||
}, | ||
@@ -29,6 +29,6 @@ "peerDependencies": { | ||
"devDependencies": { | ||
"@babel/core": "^7.3.4", | ||
"@babel/core": "^7.4.0", | ||
"@babel/helper-plugin-test-runner": "^7.0.0" | ||
}, | ||
"gitHead": "1f6454cc90fe33e0a32260871212e2f719f35741" | ||
"gitHead": "f1328fb913b5a93d54dfc6e3728b1f56c8f4a804" | ||
} |
38269
3.67%1008
3.17%