Skip to content

Commit

Permalink
supress or fix typing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jul 12, 2022
1 parent 6a23ccc commit 8b59890
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 25 deletions.
4 changes: 2 additions & 2 deletions packages/babel-generator/src/generators/expressions.ts
Expand Up @@ -113,7 +113,7 @@ export function Super(this: Printer) {
}

function isDecoratorMemberExpression(
node: t.Expression | t.V8IntrinsicIdentifier,
node: t.Expression | t.Super | t.V8IntrinsicIdentifier,
): boolean {
switch (node.type) {
case "Identifier":
Expand All @@ -129,7 +129,7 @@ function isDecoratorMemberExpression(
}
}
function shouldParenthesizeDecoratorExpression(
node: t.Expression | t.V8IntrinsicIdentifier,
node: t.Expression | t.Super | t.V8IntrinsicIdentifier,
) {
if (node.type === "CallExpression") {
node = node.callee;
Expand Down
Expand Up @@ -5,7 +5,7 @@ import type * as t from "@babel/types";

export default function (opts: {
build: (
left: t.Expression | t.PrivateName,
left: t.Expression | t.PrivateName | t.Super,
right: t.Expression,
) => t.Expression;
operator: t.BinaryExpression["operator"];
Expand Down
Expand Up @@ -258,7 +258,7 @@ const privateNameHandlerSpec: Handler<PrivateNameState & Receiver> & Receiver =
{
memoise(member, count) {
const { scope } = member;
const { object } = member.node;
const { object } = member.node as { object: t.Expression };

const memo = scope.maybeGenerateMemoised(object);
if (!memo) {
Expand All @@ -269,10 +269,10 @@ const privateNameHandlerSpec: Handler<PrivateNameState & Receiver> & Receiver =
},

receiver(member) {
const { object } = member.node;
const { object } = member.node as { object: t.Expression };

if (this.memoiser.has(object)) {
return t.cloneNode(this.memoiser.get(object) as t.Expression);
return t.cloneNode(this.memoiser.get(object));
}

return t.cloneNode(object);
Expand Down Expand Up @@ -466,7 +466,7 @@ const privateNameHandlerLoose: Handler<PrivateNameState> = {
boundGet(member) {
return t.callExpression(
t.memberExpression(this.get(member), t.identifier("bind")),
[t.cloneNode(member.node.object)],
[t.cloneNode(member.node.object as t.Expression)],
);
},

Expand Down
Expand Up @@ -279,7 +279,12 @@ const handle = {
const { object } = regular;
context = member.scope.maybeGenerateMemoised(object);
if (context) {
regular.object = assignmentExpression("=", context, object);
regular.object = assignmentExpression(
"=",
context,
// object must not be Super when `context` is an identifier
object as t.Expression,
);
}
}

Expand Down
Expand Up @@ -43,8 +43,8 @@ export function skipTransparentExprWrappers(
}

export function skipTransparentExprWrapperNodes(
node: t.Expression,
): t.Expression {
node: t.Expression | t.Super,
): t.Expression | t.Super {
while (isTransparentExprWrapper(node)) {
node = node.expression;
}
Expand Down
1 change: 1 addition & 0 deletions packages/babel-plugin-proposal-function-bind/src/index.ts
Expand Up @@ -44,6 +44,7 @@ export default declare(api => {
bind.callee.object = t.assignmentExpression(
"=",
tempId,
// @ts-ignore(Babel 7 vs Babel 8) Fixme: support `super.foo(?)`
bind.callee.object,
);
}
Expand Down
Expand Up @@ -27,7 +27,8 @@ export default declare(api => {
(lhs as t.MemberExpression).object = t.assignmentExpression(
"=",
t.cloneNode(memo),
object,
// object must not be Super when `memo` is an identifier
object as t.Expression,
);
}

Expand Down
25 changes: 16 additions & 9 deletions packages/babel-plugin-proposal-optional-chaining/src/transform.ts
Expand Up @@ -9,7 +9,7 @@ import { willPathCastToBoolean, findOutermostTransparentParent } from "./util";
const { ast } = template.expression;

function isSimpleMemberExpression(
expression: t.Expression,
expression: t.Expression | t.Super,
): expression is t.Identifier | t.Super | t.MemberExpression {
expression = skipTransparentExprWrapperNodes(expression);
return (
Expand Down Expand Up @@ -102,6 +102,7 @@ export function transform(
}
}

// todo: Improve replacementPath typings
let replacementPath: NodePath<any> = path;
if (parentPath.isUnaryExpression({ operator: "delete" })) {
replacementPath = parentPath;
Expand Down Expand Up @@ -139,8 +140,8 @@ export function transform(
t.cloneNode(ref),
// Here `chainWithTypes` MUST NOT be cloned because it could be
// updated when generating the memoised context of a call
// expression
chainWithTypes,
// expression. It must be an Expression when `ref` is an identifier
chainWithTypes as t.Expression,
);

isCall ? (node.callee = ref) : (node.object = ref);
Expand All @@ -160,13 +161,16 @@ export function transform(
// Otherwise, we need to memoize the context object, and change the call into a Function#call.
// `a.?b.?()` translates roughly to `(_b = _a.b) != null && _b.call(_a)`
const { object } = chain;
let context: t.Expression = scope.maybeGenerateMemoised(object);
if (context) {
chain.object = t.assignmentExpression("=", context, object);
} else if (t.isSuper(object)) {
let context: t.Expression;
if (t.isSuper(object)) {
context = t.thisExpression();
} else {
context = object;
const memoized = scope.maybeGenerateMemoised(object);
if (memoized) {
chain.object = t.assignmentExpression("=", memoized, object);
} else {
context = object;
}
}

node.arguments.unshift(t.cloneNode(context));
Expand All @@ -181,7 +185,10 @@ export function transform(
// i.e. `?.b` in `(a?.b.c)()`
if (i === 0 && parentIsCall) {
// `(a?.b)()` to `(a == null ? undefined : a.b.bind(a))()`
const object = skipTransparentExprWrapperNodes(replacement.object);
// object must not be Super as super?.foo is invalid
const object = skipTransparentExprWrapperNodes(
replacement.object,
) as t.Expression;
let baseRef;
if (!pureGetters || !isSimpleMemberExpression(object)) {
// memoize the context object when getters are not always pure
Expand Down
Expand Up @@ -101,7 +101,12 @@ export default declare(api => {
scope.push({ id: receiverLVal });

sequenceParts.push(
t.assignmentExpression("=", t.cloneNode(receiverLVal), receiver),
t.assignmentExpression(
"=",
t.cloneNode(receiverLVal),
// @ts-ignore(Babel 7 vs Babel 8) Fixme: support `super.foo(?)`
receiver,
),
t.assignmentExpression(
"=",
t.cloneNode(functionLVal),
Expand Down
15 changes: 13 additions & 2 deletions packages/babel-plugin-transform-proto-to-assign/src/index.ts
Expand Up @@ -31,7 +31,11 @@ export default declare(api => {
file: File,
) {
return t.expressionStatement(
t.callExpression(file.addHelper("defaults"), [ref, expr.right]),
t.callExpression(file.addHelper("defaults"), [
// @ts-ignore(Babel 7 vs Babel 8) Fixme: support `super.__proto__ = ...`
ref,
expr.right,
]),
);
}

Expand All @@ -48,7 +52,14 @@ export default declare(api => {

if (temp) {
nodes.push(
t.expressionStatement(t.assignmentExpression("=", temp, left)),
t.expressionStatement(
t.assignmentExpression(
"=",
temp,
// left must not be Super when `temp` is an identifier
left as t.Expression,
),
),
);
}
nodes.push(
Expand Down
9 changes: 7 additions & 2 deletions packages/babel-plugin-transform-spread/src/index.ts
Expand Up @@ -143,7 +143,7 @@ export default declare((api, options: Options) => {
"Please add '@babel/plugin-transform-classes' to your Babel configuration.",
);
}
let contextLiteral: t.Expression = scope.buildUndefinedNode();
let contextLiteral: t.Expression | t.Super = scope.buildUndefinedNode();
node.arguments = [];

let nodes: t.Expression[];
Expand Down Expand Up @@ -175,7 +175,12 @@ export default declare((api, options: Options) => {
if (t.isMemberExpression(callee)) {
const temp = scope.maybeGenerateMemoised(callee.object);
if (temp) {
callee.object = t.assignmentExpression("=", temp, callee.object);
callee.object = t.assignmentExpression(
"=",
temp,
// object must not be Super when `temp` is an identifier
callee.object as t.Expression,
);
contextLiteral = temp;
} else {
contextLiteral = t.cloneNode(callee.object);
Expand Down

0 comments on commit 8b59890

Please sign in to comment.