Skip to content

Commit

Permalink
fix typing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Apr 27, 2022
1 parent beda280 commit c20d40b
Show file tree
Hide file tree
Showing 70 changed files with 481 additions and 165 deletions.
1 change: 0 additions & 1 deletion packages/babel-generator/src/generators/methods.ts
Expand Up @@ -146,7 +146,6 @@ function hasTypesOrComments(
return !!(
node.typeParameters ||
node.returnType ||
// @ts-expect-error
node.predicate ||
param.typeAnnotation ||
param.optional ||
Expand Down
13 changes: 5 additions & 8 deletions packages/babel-helper-create-class-features-plugin/src/index.ts
@@ -1,5 +1,5 @@
import { types as t } from "@babel/core";
import type { File } from "@babel/core";
import type { File, PluginAPI, PluginObject } from "@babel/core";
import type { NodePath } from "@babel/traverse";
import nameFunction from "@babel/helper-function-name";
import splitExportDeclaration from "@babel/helper-split-export-declaration";
Expand All @@ -14,7 +14,6 @@ import { buildDecoratedClass, hasDecorators } from "./decorators";
import { injectInitialization, extractComputedKeys } from "./misc";
import { enableFeature, FEATURES, isLoose, shouldTransform } from "./features";
import { assertFieldTransformed } from "./typescript";
import type { ParserOptions } from "@babel/parser";

export { FEATURES, enableFeature, injectInitialization };

Expand All @@ -33,19 +32,17 @@ interface Options {
name: string;
feature: number;
loose?: boolean;
inherits?: (api: any, options: any) => any;
// same as PluginObject.manipulateOptions
manipulateOptions?: (options: unknown, parserOpts: ParserOptions) => void;
// TODO(flow->ts): change to babel api
api?: { assumption: (key?: string) => boolean | undefined };
inherits?: PluginObject["inherits"];
manipulateOptions?: PluginObject["manipulateOptions"];
api?: PluginAPI;
}

export function createClassFeaturePlugin({
name,
feature,
loose,
manipulateOptions,
// TODO(Babel 8): Remove the default value
// @ts-ignore TODO(Babel 8): Remove the default value
api = { assumption: () => void 0 },
inherits,
}: Options) {
Expand Down
Expand Up @@ -3,6 +3,7 @@ import { featuresKey, FEATURES, enableFeature, runtimeKey } from "./features";
import { generateRegexpuOptions, canSkipRegexpu, transformFlags } from "./util";

import { types as t } from "@babel/core";
import type { PluginObject } from "@babel/core";
import annotateAsPure from "@babel/helper-annotate-as-pure";

declare const PACKAGE_JSON: { name: string; version: string };
Expand All @@ -20,8 +21,8 @@ export function createRegExpFeaturePlugin({
name,
feature,
options = {} as any,
manipulateOptions = (() => {}) as (opts: any, parserOpts: any) => void,
}) {
manipulateOptions = (() => {}) as PluginObject["manipulateOptions"],
}): PluginObject {
return {
name,

Expand Down
Expand Up @@ -4,7 +4,7 @@ type RootOptions = {
sourceRoot?: string;
};

type PluginOptions = {
export type PluginOptions = {
moduleId?: string;
moduleIds?: boolean;
getModuleId?: (moduleName: string) => string | null | undefined;
Expand Down
1 change: 1 addition & 0 deletions packages/babel-helper-module-transforms/src/index.ts
Expand Up @@ -34,6 +34,7 @@ import type {
import type { NodePath } from "@babel/traverse";

export { default as getModuleName } from "./get-module-name";
export type { PluginOptions } from "./get-module-name";

export { hasExports, isSideEffectImport, isModule, rewriteThis };

Expand Down
@@ -1,6 +1,4 @@
import { declare } from "@babel/helper-plugin-utils";
import type { PluginPass } from "@babel/core";
import type { Visitor } from "@babel/traverse";
import { shouldTransform } from "./util";

export default declare(api => {
Expand All @@ -20,6 +18,6 @@ export default declare(api => {
scope.rename(name, newParamName);
}
},
} as Visitor<PluginPass>,
},
};
});
@@ -1,6 +1,8 @@
import { declare } from "@babel/helper-plugin-utils";
import { transform } from "@babel/plugin-proposal-optional-chaining";
import { shouldTransform } from "./util";
import type { NodePath } from "@babel/traverse";
import type * as t from "@babel/types";

export default declare(api => {
api.assertVersion(7);
Expand All @@ -12,7 +14,9 @@ export default declare(api => {
name: "bugfix-v8-spread-parameters-in-optional-chaining",

visitor: {
"OptionalCallExpression|OptionalMemberExpression"(path) {
"OptionalCallExpression|OptionalMemberExpression"(
path: NodePath<t.OptionalCallExpression | t.OptionalMemberExpression>,
) {
if (shouldTransform(path)) {
transform(path, { noDocumentAll, pureGetters });
}
Expand Down
7 changes: 6 additions & 1 deletion packages/babel-plugin-external-helpers/src/index.ts
@@ -1,7 +1,12 @@
import { declare } from "@babel/helper-plugin-utils";
import { types as t } from "@babel/core";

export default declare((api, options) => {
export interface Options {
helperVersion?: string;
whitelist?: false | string[];
}

export default declare((api, options: Options) => {
api.assertVersion(7);

const { helperVersion = "7.0.0-beta.0", whitelist = false } = options;
Expand Down
Expand Up @@ -2,7 +2,6 @@ import { declare } from "@babel/helper-plugin-utils";
import syntaxAsyncDoExpressions from "@babel/plugin-syntax-async-do-expressions";
import hoistVariables from "@babel/helper-hoist-variables";
import type * as t from "@babel/types";
import type { NodePath } from "@babel/traverse";

export default declare(({ types: t, assertVersion }) => {
assertVersion("^7.13.0");
Expand All @@ -12,7 +11,7 @@ export default declare(({ types: t, assertVersion }) => {
inherits: syntaxAsyncDoExpressions,
visitor: {
DoExpression: {
exit(path: NodePath<t.DoExpression>) {
exit(path) {
if (!path.is("async")) {
// non-async do expressions are handled by proposal-do-expressions
return;
Expand Down
Expand Up @@ -62,11 +62,11 @@ export default function (path, { getAsyncIterator }) {

// remove async function wrapper
// @ts-expect-error todo(flow->ts) improve type annotation for buildForAwait
template = template.body.body;
template = template.body.body as t.Statement[];

const isLabeledParent = t.isLabeledStatement(parent);
const tryBody = template[3].block.body;
const loop = tryBody[0];
const tryBody = (template[3] as t.TryStatement).block.body;
const loop = tryBody[0] as t.ForStatement;

if (isLabeledParent) {
tryBody[0] = t.labeledStatement(parent.label, loop);
Expand Down
Expand Up @@ -2,12 +2,14 @@ import { declare } from "@babel/helper-plugin-utils";
import remapAsyncToGenerator from "@babel/helper-remap-async-to-generator";
import syntaxAsyncGenerators from "@babel/plugin-syntax-async-generators";
import { types as t } from "@babel/core";
import type { PluginPass } from "@babel/core";
import type { Visitor } from "@babel/traverse";
import rewriteForAwait from "./for-await";

export default declare(api => {
api.assertVersion(7);

const yieldStarVisitor = {
const yieldStarVisitor: Visitor<PluginPass> = {
Function(path) {
path.skip();
},
Expand All @@ -22,7 +24,7 @@ export default declare(api => {
},
};

const forAwaitVisitor = {
const forAwaitVisitor: Visitor<PluginPass> = {
Function(path) {
path.skip();
},
Expand All @@ -36,7 +38,7 @@ export default declare(api => {
});

const { declar, loop } = build;
const block = loop.body;
const block = loop.body as t.BlockStatement;

// ensure that it's a block so we can take all its statements
path.ensureBlock();
Expand All @@ -47,7 +49,7 @@ export default declare(api => {
}

// push the rest of the original loop body onto our new body
block.body.push(...node.body.body);
block.body.push(...(node.body as t.BlockStatement).body);

t.inherits(loop, node);
t.inherits(loop.body, node.body);
Expand All @@ -60,7 +62,7 @@ export default declare(api => {
},
};

const visitor = {
const visitor: Visitor<PluginPass> = {
Function(path, state) {
if (!path.node.async) return;

Expand Down
6 changes: 5 additions & 1 deletion packages/babel-plugin-proposal-class-properties/src/index.ts
Expand Up @@ -6,7 +6,11 @@ import {
FEATURES,
} from "@babel/helper-create-class-features-plugin";

export default declare((api, options) => {
export interface Options {
loose?: boolean;
}

export default declare((api, options: Options) => {
api.assertVersion(7);

return createClassFeaturePlugin({
Expand Down
Expand Up @@ -7,7 +7,6 @@ import {
} from "@babel/helper-create-class-features-plugin";

import type * as t from "@babel/types";
import type { NodePath } from "@babel/traverse";
/**
* Generate a uid that is not in `denyList`
*
Expand Down Expand Up @@ -43,7 +42,7 @@ export default declare(({ types: t, template, assertVersion }) => {
// Run on ClassBody and not on class so that if @babel/helper-create-class-features-plugin
// is enabled it can generte optimized output without passing from the intermediate
// private fields representation.
ClassBody(classBody: NodePath<t.ClassBody>) {
ClassBody(classBody) {
const { scope } = classBody;
const privateNames = new Set<string>();
const body = classBody.get("body");
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-plugin-proposal-decorators/src/index.ts
Expand Up @@ -8,8 +8,9 @@ import {
} from "@babel/helper-create-class-features-plugin";
import legacyVisitor from "./transformer-legacy";
import transformer2021_12 from "./transformer-2021-12";
import type { Options } from "@babel/plugin-syntax-decorators";

export default declare((api, options) => {
export default declare((api, options: Options) => {
api.assertVersion(7);

// Options are validated in @babel/plugin-syntax-decorators
Expand Down
Expand Up @@ -4,6 +4,8 @@ import syntaxDecorators from "@babel/plugin-syntax-decorators";
import ReplaceSupers from "@babel/helper-replace-supers";
import splitExportDeclaration from "@babel/helper-split-export-declaration";
import * as charCodes from "charcodes";
import type { PluginAPI } from "@babel/core";
import type { PluginOptions } from "..";

type ClassDecoratableElement =
| t.ClassMethod
Expand Down Expand Up @@ -999,7 +1001,10 @@ function transformClass(
return path;
}

export default function ({ assertVersion, assumption }, { loose }) {
export default function (
{ assertVersion, assumption }: PluginAPI,
{ loose }: PluginOptions,
) {
assertVersion("^7.16.0");

const VISITED = new WeakSet<NodePath>();
Expand Down
Expand Up @@ -17,7 +17,10 @@ export default declare(api => {

const specifier = specifiers.shift();
const { exported } = specifier;
const uid = scope.generateUidIdentifier(exported.name);
const uid = scope.generateUidIdentifier(
// @ts-ignore Identifier ?? StringLiteral
exported.name ?? exported.value,
);

const nodes = [
t.importDeclaration(
Expand Down
Expand Up @@ -28,6 +28,7 @@ export default declare(api => {
const specifier = specifiers.shift();
const { exported } = specifier;
const uid = scope.generateUidIdentifier(
// @ts-ignore Identifier ?? StringLiteral
exported.name ?? exported.value,
);

Expand Down
1 change: 1 addition & 0 deletions packages/babel-plugin-proposal-function-sent/src/index.ts
Expand Up @@ -51,6 +51,7 @@ export default declare(api => {
const sentId = path.scope.generateUid("function.sent");

fnPath.traverse(yieldVisitor, { sentId });
// @ts-expect-error A generator must not be an arrow function
fnPath.node.body.body.unshift(
t.variableDeclaration("let", [
t.variableDeclarator(t.identifier(sentId), t.yieldExpression()),
Expand Down
8 changes: 6 additions & 2 deletions packages/babel-plugin-proposal-json-strings/src/index.ts
@@ -1,5 +1,7 @@
import { declare } from "@babel/helper-plugin-utils";
import syntaxJsonStrings from "@babel/plugin-syntax-json-strings";
import type * as t from "@babel/types";
import type { NodePath } from "@babel/traverse";

export default declare(api => {
api.assertVersion(7);
Expand All @@ -19,11 +21,13 @@ export default declare(api => {
inherits: syntaxJsonStrings.default,

visitor: {
"DirectiveLiteral|StringLiteral"({ node }) {
"DirectiveLiteral|StringLiteral"({
node,
}: NodePath<t.DirectiveLiteral | t.StringLiteral>) {
const { extra } = node;
if (!extra?.raw) return;

extra.raw = extra.raw.replace(regex, replace);
extra.raw = (extra.raw as string).replace(regex, replace);
},
},
};
Expand Down
Expand Up @@ -18,20 +18,24 @@ export default declare(api => {
return;
}

const lhs = t.cloneNode(left);
const lhs = t.cloneNode(left) as t.Identifier | t.MemberExpression;
if (t.isMemberExpression(left)) {
const { object, property, computed } = left;
const memo = scope.maybeGenerateMemoised(object);
if (memo) {
left.object = memo;
lhs.object = t.assignmentExpression("=", t.cloneNode(memo), object);
(lhs as t.MemberExpression).object = t.assignmentExpression(
"=",
t.cloneNode(memo),
object,
);
}

if (computed) {
const memo = scope.maybeGenerateMemoised(property);
if (memo) {
left.property = memo;
lhs.property = t.assignmentExpression(
(lhs as t.MemberExpression).property = t.assignmentExpression(
"=",
t.cloneNode(memo),
// @ts-expect-error todo(flow->ts): property can be t.PrivateName
Expand All @@ -43,6 +47,7 @@ export default declare(api => {

path.replaceWith(
t.logicalExpression(
// @ts-ignore operatorTrunc has been tested by t.LOGICAL_OPERATORS
operatorTrunc,
lhs,
t.assignmentExpression("=", left, right),
Expand Down
Expand Up @@ -2,7 +2,11 @@ import { declare } from "@babel/helper-plugin-utils";
import syntaxNullishCoalescingOperator from "@babel/plugin-syntax-nullish-coalescing-operator";
import { types as t, template } from "@babel/core";

export default declare((api, { loose = false }) => {
export interface Options {
loose?: boolean;
}

export default declare((api, { loose = false }: Options) => {
api.assertVersion(7);
const noDocumentAll = api.assumption("noDocumentAll") ?? loose;

Expand All @@ -26,7 +30,7 @@ export default declare((api, { loose = false }) => {
} else if (scope.path.isPattern()) {
// Replace `function (a, x = a.b ?? c) {}` to `function (a, x = (() => a.b ?? c)() ){}`
// so the temporary variable can be injected in correct scope
path.replaceWith(template.ast`(() => ${path.node})()`);
path.replaceWith(template.ast`(() => ${path.node})()` as t.Statement);
// The injected nullish expression will be queued and eventually transformed when visited
return;
} else {
Expand Down

0 comments on commit c20d40b

Please sign in to comment.