Implement assumptions defined in the babel/rfcs#5 RFC

- `mutableTemplateObject` and `ignoreToPrimitiveHint` (#12408)
- `setClassMethods` (#12407)
- `setComputedProperties` (#12490)
- `ignoreFunctionLength` (#12491)
- `noDocumentAll` (#12481)
- `iterableIsArray` and `arrayLikeIsIterable` (#12489)
- `pureGetters` (#12504)
- `skipForOfIteratorClosing` (#12496)
- `objectRestNoSymbols`, `setSpreadProperties` and `pureGetters` (#12505)
- `noNewArrows` (#12613, #12793)
- `setPublicClassFields` and `privateFieldsAsProperties` (#12497)
- `constantReexports` and `enumerableModuleMeta` (#12618)
- `constantSuper`, `superIsCallableConstructor` and `noClassCalls` (#12726)

Co-authored-by: Justin Ridgewell <justin@ridgewell.name>
Co-authored-by: Huáng Jùnliàng <JLHwung@users.noreply.github.com>
This commit is contained in:
Nicolò Ribaudo
2020-12-11 20:28:38 +01:00
parent 7965c15557
commit 6ef7b51a11
586 changed files with 5747 additions and 201 deletions

View File

@@ -21,6 +21,12 @@ export default declare((api, options) => {
const { loose } = options;
const setClassMethods = api.assumption("setClassMethods") ?? options.loose;
const constantSuper = api.assumption("constantSuper") ?? options.loose;
const superIsCallableConstructor =
api.assumption("superIsCallableConstructor") ?? options.loose;
const noClassCalls = api.assumption("noClassCalls") ?? options.loose;
// todo: investigate traversal requeueing
const VISITED = Symbol();
@@ -58,12 +64,18 @@ export default declare((api, options) => {
node[VISITED] = true;
path.replaceWith(
transformClass(path, state.file, builtinClasses, loose),
transformClass(path, state.file, builtinClasses, loose, {
setClassMethods,
constantSuper,
superIsCallableConstructor,
noClassCalls,
}),
);
if (path.isCallExpression()) {
annotateAsPure(path);
if (path.get("callee").isArrowFunctionExpression()) {
// This is an IIFE, so we don't need to worry about the noNewArrows assumption
path.get("callee").arrowFunctionToExpression();
}
}

View File

@@ -11,6 +11,13 @@ import addCreateSuperHelper from "./inline-createSuper-helpers";
type ReadonlySet<T> = Set<T> | { has(val: T): boolean };
type ClassAssumptions = {
setClassMethods: boolean,
constantSuper: boolean,
superIsCallableConstructor: boolean,
noClassCalls: boolean,
};
function buildConstructor(classRef, constructorBody, node) {
const func = t.functionDeclaration(
t.cloneNode(classRef),
@@ -26,6 +33,7 @@ export default function transformClass(
file: any,
builtinClasses: ReadonlySet<string>,
isLoose: boolean,
assumptions: ClassAssumptions,
) {
const classState = {
parent: undefined,
@@ -161,7 +169,7 @@ export default function transformClass(
methodPath: path,
objectRef: classState.classRef,
superRef: classState.superName,
isLoose: classState.isLoose,
constantSuper: assumptions.constantSuper,
file: classState.file,
refToPreserve: classState.classRef,
});
@@ -246,7 +254,7 @@ export default function transformClass(
const bareSuperNode = bareSuper.node;
let call;
if (classState.isLoose) {
if (assumptions.superIsCallableConstructor) {
bareSuperNode.arguments.unshift(t.thisExpression());
if (
bareSuperNode.arguments.length === 2 &&
@@ -452,7 +460,7 @@ export default function transformClass(
}
function processMethod(node, scope) {
if (classState.isLoose && !node.decorators) {
if (assumptions.setClassMethods && !node.decorators) {
// use assignments instead of define properties for loose classes
let { classRef } = classState;
if (!node.static) {
@@ -563,7 +571,7 @@ export default function transformClass(
// Unshift to ensure that the constructor inheritance is set up before
// any properties can be assigned to the prototype.
if (!classState.isLoose) {
if (!assumptions.superIsCallableConstructor) {
classState.body.unshift(
t.variableDeclaration("var", [
t.variableDeclarator(
@@ -663,7 +671,7 @@ export default function transformClass(
buildBody();
// make sure this class isn't directly called (with A() instead new A())
if (!classState.isLoose) {
if (!assumptions.noClassCalls) {
constructorBody.body.unshift(
t.expressionStatement(
t.callExpression(classState.file.addHelper("classCallCheck"), [