Convert proposal-object-rest-spread to TS (#13948)

* chore: add typings to object-rest-spread

* chore: bundle object-rest-spread package

* improve type inference

* address review comments
This commit is contained in:
Huáng Jùnliàng 2021-11-24 10:48:31 -05:00 committed by GitHub
parent ad1798ed48
commit 55f020e02d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 105 additions and 56 deletions

View File

@ -461,6 +461,7 @@ function copyDts(packages) {
const libBundles = [ const libBundles = [
"packages/babel-parser", "packages/babel-parser",
"packages/babel-plugin-proposal-object-rest-spread",
"packages/babel-plugin-proposal-optional-chaining", "packages/babel-plugin-proposal-optional-chaining",
"packages/babel-preset-react", "packages/babel-preset-react",
"packages/babel-preset-typescript", "packages/babel-preset-typescript",

View File

@ -1,21 +1,24 @@
import { declare } from "@babel/helper-plugin-utils"; import { declare } from "@babel/helper-plugin-utils";
import syntaxObjectRestSpread from "@babel/plugin-syntax-object-rest-spread"; import syntaxObjectRestSpread from "@babel/plugin-syntax-object-rest-spread";
import { types as t } from "@babel/core"; import { types as t } from "@babel/core";
import type { PluginPass } from "@babel/core";
import type { NodePath, Visitor, Scope } from "@babel/traverse";
import { convertFunctionParams } from "@babel/plugin-transform-parameters"; import { convertFunctionParams } from "@babel/plugin-transform-parameters";
import { isRequired } from "@babel/helper-compilation-targets"; import { isRequired } from "@babel/helper-compilation-targets";
import compatData from "@babel/compat-data/corejs2-built-ins"; import compatData from "@babel/compat-data/corejs2-built-ins";
import shouldStoreRHSInTemporaryVariable from "./shouldStoreRHSInTemporaryVariable"; import shouldStoreRHSInTemporaryVariable from "./shouldStoreRHSInTemporaryVariable";
// TODO: Remove in Babel 8 const { isAssignmentPattern, isObjectProperty } = t;
// @babel/types <=7.3.3 counts FOO as referenced in var { x: FOO }. // @babel/types <=7.3.3 counts FOO as referenced in var { x: FOO }.
// We need to detect this bug to know if "unused" means 0 or 1 references. // We need to detect this bug to know if "unused" means 0 or 1 references.
const ZERO_REFS = (() => { if (!process.env.BABEL_8_BREAKING) {
const node = t.identifier("a"); const node = t.identifier("a");
const property = t.objectProperty(t.identifier("key"), node); const property = t.objectProperty(t.identifier("key"), node);
const pattern = t.objectPattern([property]); const pattern = t.objectPattern([property]);
return t.isReferenced(node, property, pattern) ? 1 : 0; // eslint-disable-next-line no-var
})(); var ZERO_REFS = t.isReferenced(node, property, pattern) ? 1 : 0;
}
export default declare((api, opts) => { export default declare((api, opts) => {
api.assertVersion(7); api.assertVersion(7);
@ -36,7 +39,9 @@ export default declare((api, opts) => {
const pureGetters = api.assumption("pureGetters") ?? loose; const pureGetters = api.assumption("pureGetters") ?? loose;
const setSpreadProperties = api.assumption("setSpreadProperties") ?? loose; const setSpreadProperties = api.assumption("setSpreadProperties") ?? loose;
function getExtendsHelper(file) { function getExtendsHelper(
file: PluginPass,
): t.MemberExpression | t.Identifier {
return useBuiltIns return useBuiltIns
? t.memberExpression(t.identifier("Object"), t.identifier("assign")) ? t.memberExpression(t.identifier("Object"), t.identifier("assign"))
: file.addHelper("extends"); : file.addHelper("extends");
@ -51,7 +56,7 @@ export default declare((api, opts) => {
return foundRestElement; return foundRestElement;
} }
function hasObjectPatternRestElement(path) { function hasObjectPatternRestElement(path: NodePath): boolean {
let foundRestElement = false; let foundRestElement = false;
visitRestElements(path, restElement => { visitRestElements(path, restElement => {
if (restElement.parentPath.isObjectPattern()) { if (restElement.parentPath.isObjectPattern()) {
@ -62,15 +67,16 @@ export default declare((api, opts) => {
return foundRestElement; return foundRestElement;
} }
function visitRestElements(path, visitor) { function visitRestElements(
path: NodePath,
visitor: (path: NodePath<t.RestElement>) => any,
) {
path.traverse({ path.traverse({
Expression(path) { Expression(path) {
const parentType = path.parent.type; const { parent, key } = path;
if ( if (
(parentType === "AssignmentPattern" && path.key === "right") || (isAssignmentPattern(parent) && key === "right") ||
(parentType === "ObjectProperty" && (isObjectProperty(parent) && parent.computed && key === "key")
path.parent.computed &&
path.key === "key")
) { ) {
path.skip(); path.skip();
} }
@ -79,7 +85,7 @@ export default declare((api, opts) => {
}); });
} }
function hasSpread(node) { function hasSpread(node: t.ObjectExpression): boolean {
for (const prop of node.properties) { for (const prop of node.properties) {
if (t.isSpreadElement(prop)) { if (t.isSpreadElement(prop)) {
return true; return true;
@ -92,9 +98,10 @@ export default declare((api, opts) => {
// were converted to stringLiterals or not // were converted to stringLiterals or not
// e.g. extracts {keys: ["a", "b", "3", ++x], allLiteral: false } // e.g. extracts {keys: ["a", "b", "3", ++x], allLiteral: false }
// from ast of {a: "foo", b, 3: "bar", [++x]: "baz"} // from ast of {a: "foo", b, 3: "bar", [++x]: "baz"}
function extractNormalizedKeys(path) { function extractNormalizedKeys(node: t.ObjectPattern) {
const props = path.node.properties; // RestElement has been removed in createObjectRest
const keys = []; const props = node.properties as t.ObjectProperty[];
const keys: t.Expression[] = [];
let allLiteral = true; let allLiteral = true;
let hasTemplateLiteral = false; let hasTemplateLiteral = false;
@ -106,7 +113,14 @@ export default declare((api, opts) => {
keys.push(t.cloneNode(prop.key)); keys.push(t.cloneNode(prop.key));
hasTemplateLiteral = true; hasTemplateLiteral = true;
} else if (t.isLiteral(prop.key)) { } else if (t.isLiteral(prop.key)) {
keys.push(t.stringLiteral(String(prop.key.value))); keys.push(
t.stringLiteral(
String(
//@ts-ignore prop.key can not be a NullLiteral
prop.key.value,
),
),
);
} else { } else {
keys.push(t.cloneNode(prop.key)); keys.push(t.cloneNode(prop.key));
allLiteral = false; allLiteral = false;
@ -118,8 +132,11 @@ export default declare((api, opts) => {
// replaces impure computed keys with new identifiers // replaces impure computed keys with new identifiers
// and returns variable declarators of these new identifiers // and returns variable declarators of these new identifiers
function replaceImpureComputedKeys(properties, scope) { function replaceImpureComputedKeys(
const impureComputedPropertyDeclarators = []; properties: NodePath<t.ObjectProperty>[],
scope: Scope,
) {
const impureComputedPropertyDeclarators: t.VariableDeclarator[] = [];
for (const propPath of properties) { for (const propPath of properties) {
const key = propPath.get("key"); const key = propPath.get("key");
if (propPath.node.computed && !key.isPure()) { if (propPath.node.computed && !key.isPure()) {
@ -132,13 +149,14 @@ export default declare((api, opts) => {
return impureComputedPropertyDeclarators; return impureComputedPropertyDeclarators;
} }
function removeUnusedExcludedKeys(path) { function removeUnusedExcludedKeys(path: NodePath<t.ObjectPattern>): void {
const bindings = path.getOuterBindingIdentifierPaths(); const bindings = path.getOuterBindingIdentifierPaths();
Object.keys(bindings).forEach(bindingName => { Object.keys(bindings).forEach(bindingName => {
const bindingParentPath = bindings[bindingName].parentPath; const bindingParentPath = bindings[bindingName].parentPath;
if ( if (
path.scope.getBinding(bindingName).references > ZERO_REFS || path.scope.getBinding(bindingName).references >
(process.env.BABEL_8_BREAKING ? 0 : ZERO_REFS) ||
!bindingParentPath.isObjectProperty() !bindingParentPath.isObjectProperty()
) { ) {
return; return;
@ -148,7 +166,11 @@ export default declare((api, opts) => {
} }
//expects path to an object pattern //expects path to an object pattern
function createObjectRest(path, file, objRef) { function createObjectRest(
path: NodePath<t.ObjectPattern>,
file: PluginPass,
objRef: t.Identifier | t.MemberExpression,
): [t.VariableDeclarator[], t.LVal, t.CallExpression] {
const props = path.get("properties"); const props = path.get("properties");
const last = props[props.length - 1]; const last = props[props.length - 1];
t.assertRestElement(last.node); t.assertRestElement(last.node);
@ -156,11 +178,12 @@ export default declare((api, opts) => {
last.remove(); last.remove();
const impureComputedPropertyDeclarators = replaceImpureComputedKeys( const impureComputedPropertyDeclarators = replaceImpureComputedKeys(
path.get("properties"), path.get("properties") as NodePath<t.ObjectProperty>[],
path.scope, path.scope,
); );
const { keys, allLiteral, hasTemplateLiteral } = const { keys, allLiteral, hasTemplateLiteral } = extractNormalizedKeys(
extractNormalizedKeys(path); path.node,
);
if (keys.length === 0) { if (keys.length === 0) {
return [ return [
@ -210,7 +233,11 @@ export default declare((api, opts) => {
]; ];
} }
function replaceRestElement(parentPath, paramPath, container) { function replaceRestElement(
parentPath: NodePath<t.Function | t.CatchClause>,
paramPath: NodePath,
container?: t.VariableDeclaration[],
): void {
if (paramPath.isAssignmentPattern()) { if (paramPath.isAssignmentPattern()) {
replaceRestElement(parentPath, paramPath.get("left"), container); replaceRestElement(parentPath, paramPath.get("left"), container);
return; return;
@ -299,7 +326,7 @@ export default declare((api, opts) => {
for (let i = 0; i < params.length; ++i) { for (let i = 0; i < params.length; ++i) {
const param = params[i]; const param = params[i];
if (paramsWithRestElement.has(i)) { if (paramsWithRestElement.has(i)) {
replaceRestElement(param.parentPath, param); replaceRestElement(path, param);
} }
} }
} else { } else {
@ -360,14 +387,15 @@ export default declare((api, opts) => {
} }
let ref = originalPath.node.init; let ref = originalPath.node.init;
const refPropertyPath = []; const refPropertyPath: NodePath<t.ObjectProperty>[] = [];
let kind; let kind;
path.findParent(path => { path.findParent((path: NodePath): boolean => {
if (path.isObjectProperty()) { if (path.isObjectProperty()) {
refPropertyPath.unshift(path); refPropertyPath.unshift(path);
} else if (path.isVariableDeclarator()) { } else if (path.isVariableDeclarator()) {
kind = path.parentPath.node.kind; kind = (path.parentPath as NodePath<t.VariableDeclaration>).node
.kind;
return true; return true;
} }
}); });
@ -385,12 +413,17 @@ export default declare((api, opts) => {
); );
}); });
const objectPatternPath = path.findParent(path => //@ts-expect-error: findParent can not apply assertions on result shape
path.isObjectPattern(), const objectPatternPath: NodePath<t.ObjectPattern> = path.findParent(
path => path.isObjectPattern(),
); );
const [impureComputedPropertyDeclarators, argument, callExpression] = const [impureComputedPropertyDeclarators, argument, callExpression] =
createObjectRest(objectPatternPath, file, ref); createObjectRest(
objectPatternPath,
file,
ref as t.MemberExpression,
);
if (pureGetters) { if (pureGetters) {
removeUnusedExcludedKeys(objectPatternPath); removeUnusedExcludedKeys(objectPatternPath);
@ -402,11 +435,9 @@ export default declare((api, opts) => {
insertionPath.insertBefore(impureObjRefComputedDeclarators); insertionPath.insertBefore(impureObjRefComputedDeclarators);
insertionPath.insertAfter( insertionPath = insertionPath.insertAfter(
t.variableDeclarator(argument, callExpression), t.variableDeclarator(argument, callExpression),
); )[0] as NodePath<t.VariableDeclarator>;
insertionPath = insertionPath.getSibling(insertionPath.key + 1);
path.scope.registerBinding(kind, insertionPath); path.scope.registerBinding(kind, insertionPath);
@ -433,7 +464,7 @@ export default declare((api, opts) => {
const specifiers = []; const specifiers = [];
for (const name of Object.keys(path.getOuterBindingIdentifiers(path))) { for (const name of Object.keys(path.getOuterBindingIdentifiers(true))) {
specifiers.push( specifiers.push(
t.exportSpecifier(t.identifier(name), t.identifier(name)), t.exportSpecifier(t.identifier(name), t.identifier(name)),
); );
@ -449,7 +480,7 @@ export default declare((api, opts) => {
// try {} catch ({a, ...b}) {} // try {} catch ({a, ...b}) {}
CatchClause(path) { CatchClause(path) {
const paramPath = path.get("param"); const paramPath = path.get("param");
replaceRestElement(paramPath.parentPath, paramPath); replaceRestElement(path, paramPath);
}, },
// ({a, ...b} = c); // ({a, ...b} = c);
@ -511,14 +542,15 @@ export default declare((api, opts) => {
]); ]);
path.ensureBlock(); path.ensureBlock();
const body = node.body as t.BlockStatement;
if (node.body.body.length === 0 && path.isCompletionRecord()) { if (body.body.length === 0 && path.isCompletionRecord()) {
node.body.body.unshift( body.body.unshift(
t.expressionStatement(scope.buildUndefinedNode()), t.expressionStatement(scope.buildUndefinedNode()),
); );
} }
node.body.body.unshift( body.body.unshift(
t.expressionStatement( t.expressionStatement(
t.assignmentExpression("=", left, t.cloneNode(temp)), t.assignmentExpression("=", left, t.cloneNode(temp)),
), ),
@ -533,8 +565,9 @@ export default declare((api, opts) => {
]); ]);
path.ensureBlock(); path.ensureBlock();
const body = node.body as t.BlockStatement;
node.body.body.unshift( body.body.unshift(
t.variableDeclaration(node.left.kind, [ t.variableDeclaration(node.left.kind, [
t.variableDeclarator(pattern, t.cloneNode(key)), t.variableDeclarator(pattern, t.cloneNode(key)),
]), ]),
@ -565,11 +598,13 @@ export default declare((api, opts) => {
if (objectPatterns.length > 0) { if (objectPatterns.length > 0) {
const statementPath = path.getStatementParent(); const statementPath = path.getStatementParent();
const statementNode = statementPath.node;
const kind =
statementNode.type === "VariableDeclaration"
? statementNode.kind
: "var";
statementPath.insertAfter( statementPath.insertAfter(
t.variableDeclaration( t.variableDeclaration(kind, objectPatterns),
statementPath.node.kind || "var",
objectPatterns,
),
); );
} }
}, },
@ -627,7 +662,7 @@ export default declare((api, opts) => {
]); ]);
} }
for (const prop of (path.node.properties: Array)) { for (const prop of path.node.properties) {
if (t.isSpreadElement(prop)) { if (t.isSpreadElement(prop)) {
make(); make();
exp.arguments.push(prop.argument); exp.arguments.push(prop.argument);
@ -640,6 +675,6 @@ export default declare((api, opts) => {
path.replaceWith(exp); path.replaceWith(exp);
}, },
}, } as Visitor<PluginPass>,
}; };
}); });

View File

@ -1,5 +1,6 @@
import { types as t } from "@babel/core"; import { types as t } from "@babel/core";
const { isObjectProperty } = t;
/** /**
* This is a helper function to determine if we should create an intermediate variable * This is a helper function to determine if we should create an intermediate variable
* such that the RHS of an assignment is not duplicated. * such that the RHS of an assignment is not duplicated.
@ -7,17 +8,24 @@ import { types as t } from "@babel/core";
* See https://github.com/babel/babel/pull/13711#issuecomment-914388382 for discussion * See https://github.com/babel/babel/pull/13711#issuecomment-914388382 for discussion
* on further optimizations. * on further optimizations.
*/ */
export default function shouldStoreRHSInTemporaryVariable(node) { export default function shouldStoreRHSInTemporaryVariable(node: t.LVal) {
if (t.isArrayPattern(node)) { if (t.isArrayPattern(node)) {
const nonNullElements = node.elements.filter(element => element !== null); const nonNullElements = node.elements.filter(element => element !== null);
if (nonNullElements.length > 1) return true; if (nonNullElements.length > 1) return true;
else return shouldStoreRHSInTemporaryVariable(nonNullElements[0]); else return shouldStoreRHSInTemporaryVariable(nonNullElements[0]);
} else if (t.isObjectPattern(node)) { } else if (t.isObjectPattern(node)) {
if (node.properties.length > 1) return true; const { properties } = node;
else if (node.properties.length === 0) return false; if (properties.length > 1) return true;
else return shouldStoreRHSInTemporaryVariable(node.properties[0]); else if (properties.length === 0) return false;
} else if (t.isObjectProperty(node)) { else {
return shouldStoreRHSInTemporaryVariable(node.value); const firstProperty = properties[0];
if (isObjectProperty(firstProperty)) {
// the value of the property must be an LVal
return shouldStoreRHSInTemporaryVariable(firstProperty.value as t.LVal);
} else {
return shouldStoreRHSInTemporaryVariable(firstProperty);
}
}
} else if (t.isAssignmentPattern(node)) { } else if (t.isAssignmentPattern(node)) {
return shouldStoreRHSInTemporaryVariable(node.left); return shouldStoreRHSInTemporaryVariable(node.left);
} else if (t.isRestElement(node)) { } else if (t.isRestElement(node)) {

View File

@ -12,6 +12,7 @@ import NodePath from "./index";
*/ */
export function findParent( export function findParent(
this: NodePath,
callback: (path: NodePath) => boolean, callback: (path: NodePath) => boolean,
): NodePath | null { ): NodePath | null {
let path = this; let path = this;

View File

@ -983,7 +983,7 @@ export default class Scope {
init?: t.Expression; init?: t.Expression;
unique?: boolean; unique?: boolean;
_blockHoist?: number | undefined; _blockHoist?: number | undefined;
kind?: "var" | "let"; kind?: "var" | "let" | "const";
}) { }) {
let path = this.path; let path = this.path;

View File

@ -30,6 +30,7 @@
"./packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/src/**/*.ts", "./packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/src/**/*.ts",
"./packages/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining/src/**/*.ts", "./packages/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining/src/**/*.ts",
"./packages/babel-plugin-proposal-async-do-expressions/src/**/*.ts", "./packages/babel-plugin-proposal-async-do-expressions/src/**/*.ts",
"./packages/babel-plugin-proposal-object-rest-spread/src/**/*.ts",
"./packages/babel-plugin-syntax-async-do-expressions/src/**/*.ts", "./packages/babel-plugin-syntax-async-do-expressions/src/**/*.ts",
"./packages/babel-plugin-transform-block-scoping/src/**/*.ts", "./packages/babel-plugin-transform-block-scoping/src/**/*.ts",
"./packages/babel-plugin-transform-classes/src/**/*.ts", "./packages/babel-plugin-transform-classes/src/**/*.ts",
@ -129,6 +130,9 @@
"@babel/plugin-proposal-async-do-expressions": [ "@babel/plugin-proposal-async-do-expressions": [
"./packages/babel-plugin-proposal-async-do-expressions/src" "./packages/babel-plugin-proposal-async-do-expressions/src"
], ],
"@babel/plugin-proposal-object-rest-spread": [
"./packages/babel-plugin-proposal-object-rest-spread/src"
],
"@babel/plugin-syntax-async-do-expressions": [ "@babel/plugin-syntax-async-do-expressions": [
"./packages/babel-plugin-syntax-async-do-expressions/src" "./packages/babel-plugin-syntax-async-do-expressions/src"
], ],