convert @babel/helper-create-class-features-plugin to TS (#13214)
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
parent
342fec1a78
commit
4ee6a27780
@ -59,21 +59,15 @@ function extractElementDescriptor(/* this: File, */ classRef, superRef, path) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
new ReplaceSupers(
|
new ReplaceSupers({
|
||||||
{
|
|
||||||
methodPath: path,
|
methodPath: path,
|
||||||
methodNode: node,
|
|
||||||
objectRef: classRef,
|
objectRef: classRef,
|
||||||
isStatic: node.static,
|
|
||||||
superRef,
|
superRef,
|
||||||
scope,
|
|
||||||
file: this,
|
file: this,
|
||||||
refToPreserve: classRef,
|
refToPreserve: classRef,
|
||||||
},
|
}).replace();
|
||||||
true,
|
|
||||||
).replace();
|
|
||||||
|
|
||||||
const properties = [
|
const properties: t.ObjectExpression["properties"] = [
|
||||||
prop("kind", t.stringLiteral(isMethod ? node.kind : "field")),
|
prop("kind", t.stringLiteral(isMethod ? node.kind : "field")),
|
||||||
prop("decorators", takeDecorators(node)),
|
prop("decorators", takeDecorators(node)),
|
||||||
prop("static", node.static && t.booleanLiteral(true)),
|
prop("static", node.static && t.booleanLiteral(true)),
|
||||||
@ -135,7 +129,7 @@ export function buildDecoratedClass(ref, path, elements, file) {
|
|||||||
.map(extractElementDescriptor.bind(file, node.id, superId)),
|
.map(extractElementDescriptor.bind(file, node.id, superId)),
|
||||||
);
|
);
|
||||||
|
|
||||||
let replacement = template.expression.ast`
|
const wrapperCall = template.expression.ast`
|
||||||
${addDecorateHelper(file)}(
|
${addDecorateHelper(file)}(
|
||||||
${classDecorators || t.nullLiteral()},
|
${classDecorators || t.nullLiteral()},
|
||||||
function (${initializeId}, ${superClass ? t.cloneNode(superId) : null}) {
|
function (${initializeId}, ${superClass ? t.cloneNode(superId) : null}) {
|
||||||
@ -144,17 +138,18 @@ export function buildDecoratedClass(ref, path, elements, file) {
|
|||||||
},
|
},
|
||||||
${superClass}
|
${superClass}
|
||||||
)
|
)
|
||||||
`;
|
` as t.CallExpression & { arguments: [unknown, t.FunctionExpression] };
|
||||||
let classPathDesc = "arguments.1.body.body.0";
|
|
||||||
|
|
||||||
if (!isStrict) {
|
if (!isStrict) {
|
||||||
replacement.arguments[1].body.directives.push(
|
wrapperCall.arguments[1].body.directives.push(
|
||||||
t.directive(t.directiveLiteral("use strict")),
|
t.directive(t.directiveLiteral("use strict")),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let replacement: t.Node = wrapperCall;
|
||||||
|
let classPathDesc = "arguments.1.body.body.0";
|
||||||
if (isDeclaration) {
|
if (isDeclaration) {
|
||||||
replacement = template.ast`let ${ref} = ${replacement}`;
|
replacement = template.statement.ast`let ${ref} = ${wrapperCall}`;
|
||||||
classPathDesc = "declarations.0.init." + classPathDesc;
|
classPathDesc = "declarations.0.init." + classPathDesc;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -22,6 +22,8 @@ import {
|
|||||||
|
|
||||||
export { FEATURES, enableFeature, injectInitialization };
|
export { FEATURES, enableFeature, injectInitialization };
|
||||||
|
|
||||||
|
declare const PACKAGE_JSON: { name: string; version: string };
|
||||||
|
|
||||||
// Note: Versions are represented as an integer. e.g. 7.1.5 is represented
|
// Note: Versions are represented as an integer. e.g. 7.1.5 is represented
|
||||||
// as 70000100005. This method is easier than using a semver-parsing
|
// as 70000100005. This method is easier than using a semver-parsing
|
||||||
// package, but it breaks if we release x.y.z where x, y or z are
|
// package, but it breaks if we release x.y.z where x, y or z are
|
||||||
@ -38,6 +40,12 @@ export function createClassFeaturePlugin({
|
|||||||
manipulateOptions,
|
manipulateOptions,
|
||||||
// TODO(Babel 8): Remove the default falue
|
// TODO(Babel 8): Remove the default falue
|
||||||
api = { assumption: () => {} },
|
api = { assumption: () => {} },
|
||||||
|
}: {
|
||||||
|
name;
|
||||||
|
feature;
|
||||||
|
loose?;
|
||||||
|
manipulateOptions;
|
||||||
|
api?;
|
||||||
}) {
|
}) {
|
||||||
const setPublicClassFields = api.assumption("setPublicClassFields");
|
const setPublicClassFields = api.assumption("setPublicClassFields");
|
||||||
const privateFieldsAsProperties = api.assumption("privateFieldsAsProperties");
|
const privateFieldsAsProperties = api.assumption("privateFieldsAsProperties");
|
||||||
@ -44,7 +44,7 @@ const classFieldDefinitionEvaluationTDZVisitor = {
|
|||||||
ReferencedIdentifier: handleClassTDZ,
|
ReferencedIdentifier: handleClassTDZ,
|
||||||
};
|
};
|
||||||
|
|
||||||
export function injectInitialization(path, constructor, nodes, renamer) {
|
export function injectInitialization(path, constructor, nodes, renamer?) {
|
||||||
if (!nodes.length) return;
|
if (!nodes.length) return;
|
||||||
|
|
||||||
const isDerived = !!path.node.superClass;
|
const isDerived = !!path.node.superClass;
|
||||||
@ -1,8 +1,7 @@
|
|||||||
// @flow
|
|
||||||
|
|
||||||
import type { NodePath } from "@babel/traverse";
|
import type { NodePath } from "@babel/traverse";
|
||||||
|
import type * as t from "@babel/types";
|
||||||
|
|
||||||
export function assertFieldTransformed(path: NodePath) {
|
export function assertFieldTransformed(path: NodePath<t.ClassProperty>) {
|
||||||
// TODO (Babel 8): Also check path.node.definite
|
// TODO (Babel 8): Also check path.node.definite
|
||||||
|
|
||||||
if (path.node.declare) {
|
if (path.node.declare) {
|
||||||
@ -254,7 +254,6 @@ const looseHandlers = {
|
|||||||
|
|
||||||
type ReplaceSupersOptionsBase = {
|
type ReplaceSupersOptionsBase = {
|
||||||
methodPath: NodePath<any>;
|
methodPath: NodePath<any>;
|
||||||
superRef: any;
|
|
||||||
constantSuper?: boolean;
|
constantSuper?: boolean;
|
||||||
file: any;
|
file: any;
|
||||||
// objectRef might have been shadowed in child scopes,
|
// objectRef might have been shadowed in child scopes,
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
"./packages/babel-generator/src/**/*.ts",
|
"./packages/babel-generator/src/**/*.ts",
|
||||||
"./packages/babel-helper-annotate-as-pure/src/**/*.ts",
|
"./packages/babel-helper-annotate-as-pure/src/**/*.ts",
|
||||||
"./packages/babel-helper-compilation-targets/src/**/*.ts",
|
"./packages/babel-helper-compilation-targets/src/**/*.ts",
|
||||||
|
"./packages/babel-helper-create-class-features-plugin/src/**/*.ts",
|
||||||
"./packages/babel-helper-create-regexp-features-plugin/src/**/*.ts",
|
"./packages/babel-helper-create-regexp-features-plugin/src/**/*.ts",
|
||||||
"./packages/babel-helper-explode-assignable-expression/src/**/*.ts",
|
"./packages/babel-helper-explode-assignable-expression/src/**/*.ts",
|
||||||
"./packages/babel-helper-fixtures/src/**/*.ts",
|
"./packages/babel-helper-fixtures/src/**/*.ts",
|
||||||
@ -53,6 +54,9 @@
|
|||||||
"@babel/helper-compilation-targets": [
|
"@babel/helper-compilation-targets": [
|
||||||
"./packages/babel-helper-compilation-targets/src"
|
"./packages/babel-helper-compilation-targets/src"
|
||||||
],
|
],
|
||||||
|
"@babel/helper-create-class-features-plugin": [
|
||||||
|
"./packages/babel-helper-create-class-features-plugin/src"
|
||||||
|
],
|
||||||
"@babel/helper-create-regexp-features-plugin": [
|
"@babel/helper-create-regexp-features-plugin": [
|
||||||
"./packages/babel-helper-create-regexp-features-plugin/src"
|
"./packages/babel-helper-create-regexp-features-plugin/src"
|
||||||
],
|
],
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user