diff --git a/packages/babel-generator/src/generators/methods.ts b/packages/babel-generator/src/generators/methods.ts index 8a5d7ad9aa98..42c6d3d5b20f 100644 --- a/packages/babel-generator/src/generators/methods.ts +++ b/packages/babel-generator/src/generators/methods.ts @@ -146,7 +146,6 @@ function hasTypesOrComments( return !!( node.typeParameters || node.returnType || - // @ts-expect-error node.predicate || param.typeAnnotation || param.optional || diff --git a/packages/babel-helper-create-class-features-plugin/src/index.ts b/packages/babel-helper-create-class-features-plugin/src/index.ts index dbc8b30a0f72..267d9e47c57d 100644 --- a/packages/babel-helper-create-class-features-plugin/src/index.ts +++ b/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"; @@ -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 }; @@ -33,11 +32,9 @@ 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({ @@ -45,7 +42,7 @@ export function createClassFeaturePlugin({ 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) { diff --git a/packages/babel-helper-create-regexp-features-plugin/src/index.ts b/packages/babel-helper-create-regexp-features-plugin/src/index.ts index 997175c320cd..34bd564024ed 100644 --- a/packages/babel-helper-create-regexp-features-plugin/src/index.ts +++ b/packages/babel-helper-create-regexp-features-plugin/src/index.ts @@ -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 }; @@ -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, diff --git a/packages/babel-helper-module-transforms/src/get-module-name.ts b/packages/babel-helper-module-transforms/src/get-module-name.ts index 0df7ae602255..6be4e971d5b9 100644 --- a/packages/babel-helper-module-transforms/src/get-module-name.ts +++ b/packages/babel-helper-module-transforms/src/get-module-name.ts @@ -4,7 +4,7 @@ type RootOptions = { sourceRoot?: string; }; -type PluginOptions = { +export type PluginOptions = { moduleId?: string; moduleIds?: boolean; getModuleId?: (moduleName: string) => string | null | undefined; diff --git a/packages/babel-helper-module-transforms/src/index.ts b/packages/babel-helper-module-transforms/src/index.ts index 64b18ae96d8e..a33656117948 100644 --- a/packages/babel-helper-module-transforms/src/index.ts +++ b/packages/babel-helper-module-transforms/src/index.ts @@ -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 }; diff --git a/packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/src/index.ts b/packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/src/index.ts index cb3179edfa18..ec03242ff46c 100644 --- a/packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/src/index.ts +++ b/packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/src/index.ts @@ -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 => { @@ -20,6 +18,6 @@ export default declare(api => { scope.rename(name, newParamName); } }, - } as Visitor, + }, }; }); diff --git a/packages/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining/src/index.ts b/packages/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining/src/index.ts index 23a3299ea652..0ba4b19954f5 100644 --- a/packages/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining/src/index.ts +++ b/packages/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining/src/index.ts @@ -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); @@ -12,7 +14,9 @@ export default declare(api => { name: "bugfix-v8-spread-parameters-in-optional-chaining", visitor: { - "OptionalCallExpression|OptionalMemberExpression"(path) { + "OptionalCallExpression|OptionalMemberExpression"( + path: NodePath, + ) { if (shouldTransform(path)) { transform(path, { noDocumentAll, pureGetters }); } diff --git a/packages/babel-plugin-external-helpers/src/index.ts b/packages/babel-plugin-external-helpers/src/index.ts index 8ca1526bca9b..53da9423c4eb 100644 --- a/packages/babel-plugin-external-helpers/src/index.ts +++ b/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; diff --git a/packages/babel-plugin-proposal-async-do-expressions/src/index.ts b/packages/babel-plugin-proposal-async-do-expressions/src/index.ts index 828630f5758a..ef2db9fb71b0 100644 --- a/packages/babel-plugin-proposal-async-do-expressions/src/index.ts +++ b/packages/babel-plugin-proposal-async-do-expressions/src/index.ts @@ -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"); @@ -12,7 +11,7 @@ export default declare(({ types: t, assertVersion }) => { inherits: syntaxAsyncDoExpressions, visitor: { DoExpression: { - exit(path: NodePath) { + exit(path) { if (!path.is("async")) { // non-async do expressions are handled by proposal-do-expressions return; diff --git a/packages/babel-plugin-proposal-async-generator-functions/src/for-await.ts b/packages/babel-plugin-proposal-async-generator-functions/src/for-await.ts index 163eb5f6f3d3..c3df45b3f604 100644 --- a/packages/babel-plugin-proposal-async-generator-functions/src/for-await.ts +++ b/packages/babel-plugin-proposal-async-generator-functions/src/for-await.ts @@ -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); diff --git a/packages/babel-plugin-proposal-async-generator-functions/src/index.ts b/packages/babel-plugin-proposal-async-generator-functions/src/index.ts index 0d4d4492faa5..717f7ec18b96 100644 --- a/packages/babel-plugin-proposal-async-generator-functions/src/index.ts +++ b/packages/babel-plugin-proposal-async-generator-functions/src/index.ts @@ -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 = { Function(path) { path.skip(); }, @@ -22,7 +24,7 @@ export default declare(api => { }, }; - const forAwaitVisitor = { + const forAwaitVisitor: Visitor = { Function(path) { path.skip(); }, @@ -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(); @@ -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); @@ -60,7 +62,7 @@ export default declare(api => { }, }; - const visitor = { + const visitor: Visitor = { Function(path, state) { if (!path.node.async) return; diff --git a/packages/babel-plugin-proposal-class-properties/src/index.ts b/packages/babel-plugin-proposal-class-properties/src/index.ts index f8a6058a181f..b89fbae65c59 100644 --- a/packages/babel-plugin-proposal-class-properties/src/index.ts +++ b/packages/babel-plugin-proposal-class-properties/src/index.ts @@ -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({ diff --git a/packages/babel-plugin-proposal-class-static-block/src/index.ts b/packages/babel-plugin-proposal-class-static-block/src/index.ts index 52ce4cfcbf20..a31a0288c078 100644 --- a/packages/babel-plugin-proposal-class-static-block/src/index.ts +++ b/packages/babel-plugin-proposal-class-static-block/src/index.ts @@ -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` * @@ -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) { + ClassBody(classBody) { const { scope } = classBody; const privateNames = new Set(); const body = classBody.get("body"); diff --git a/packages/babel-plugin-proposal-decorators/src/index.ts b/packages/babel-plugin-proposal-decorators/src/index.ts index 3dbe21945ba8..b3e661cdc003 100644 --- a/packages/babel-plugin-proposal-decorators/src/index.ts +++ b/packages/babel-plugin-proposal-decorators/src/index.ts @@ -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 diff --git a/packages/babel-plugin-proposal-decorators/src/transformer-2021-12.ts b/packages/babel-plugin-proposal-decorators/src/transformer-2021-12.ts index 75d7f7d71470..02a2b01fcb91 100644 --- a/packages/babel-plugin-proposal-decorators/src/transformer-2021-12.ts +++ b/packages/babel-plugin-proposal-decorators/src/transformer-2021-12.ts @@ -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 @@ -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(); diff --git a/packages/babel-plugin-proposal-export-default-from/src/index.ts b/packages/babel-plugin-proposal-export-default-from/src/index.ts index 5ddc0ff3f203..8cda2dfa4240 100644 --- a/packages/babel-plugin-proposal-export-default-from/src/index.ts +++ b/packages/babel-plugin-proposal-export-default-from/src/index.ts @@ -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( diff --git a/packages/babel-plugin-proposal-export-namespace-from/src/index.ts b/packages/babel-plugin-proposal-export-namespace-from/src/index.ts index dff6aebd161c..91218640dacc 100644 --- a/packages/babel-plugin-proposal-export-namespace-from/src/index.ts +++ b/packages/babel-plugin-proposal-export-namespace-from/src/index.ts @@ -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, ); diff --git a/packages/babel-plugin-proposal-function-sent/src/index.ts b/packages/babel-plugin-proposal-function-sent/src/index.ts index 959b86c41a53..a3e2c7b2c15f 100644 --- a/packages/babel-plugin-proposal-function-sent/src/index.ts +++ b/packages/babel-plugin-proposal-function-sent/src/index.ts @@ -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()), diff --git a/packages/babel-plugin-proposal-json-strings/src/index.ts b/packages/babel-plugin-proposal-json-strings/src/index.ts index eeaaada8a70d..10e1c7bc9df0 100644 --- a/packages/babel-plugin-proposal-json-strings/src/index.ts +++ b/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); @@ -19,11 +21,13 @@ export default declare(api => { inherits: syntaxJsonStrings.default, visitor: { - "DirectiveLiteral|StringLiteral"({ node }) { + "DirectiveLiteral|StringLiteral"({ + node, + }: NodePath) { const { extra } = node; if (!extra?.raw) return; - extra.raw = extra.raw.replace(regex, replace); + extra.raw = (extra.raw as string).replace(regex, replace); }, }, }; diff --git a/packages/babel-plugin-proposal-logical-assignment-operators/src/index.ts b/packages/babel-plugin-proposal-logical-assignment-operators/src/index.ts index da1eeaea35ee..0a2a85af5b4a 100644 --- a/packages/babel-plugin-proposal-logical-assignment-operators/src/index.ts +++ b/packages/babel-plugin-proposal-logical-assignment-operators/src/index.ts @@ -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 @@ -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), diff --git a/packages/babel-plugin-proposal-nullish-coalescing-operator/src/index.ts b/packages/babel-plugin-proposal-nullish-coalescing-operator/src/index.ts index 63b8d2925e00..1c7a895fc022 100644 --- a/packages/babel-plugin-proposal-nullish-coalescing-operator/src/index.ts +++ b/packages/babel-plugin-proposal-nullish-coalescing-operator/src/index.ts @@ -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; @@ -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 { diff --git a/packages/babel-plugin-proposal-object-rest-spread/src/index.ts b/packages/babel-plugin-proposal-object-rest-spread/src/index.ts index 34602f18ba7b..caf1c6baa1e6 100644 --- a/packages/babel-plugin-proposal-object-rest-spread/src/index.ts +++ b/packages/babel-plugin-proposal-object-rest-spread/src/index.ts @@ -2,7 +2,7 @@ import { declare } from "@babel/helper-plugin-utils"; import syntaxObjectRestSpread from "@babel/plugin-syntax-object-rest-spread"; import { types as t } from "@babel/core"; import type { PluginPass } from "@babel/core"; -import type { NodePath, Visitor, Scope } from "@babel/traverse"; +import type { NodePath, Scope } from "@babel/traverse"; import { convertFunctionParams } from "@babel/plugin-transform-parameters"; import { isRequired } from "@babel/helper-compilation-targets"; import compatData from "@babel/compat-data/corejs2-built-ins"; @@ -20,7 +20,12 @@ if (!process.env.BABEL_8_BREAKING) { var ZERO_REFS = t.isReferenced(node, property, pattern) ? 1 : 0; } -export default declare((api, opts) => { +export interface Options { + useBuiltIns?: boolean; + loose?: boolean; +} + +export default declare((api, opts: Options) => { api.assertVersion(7); const targets = api.targets(); @@ -675,6 +680,6 @@ export default declare((api, opts) => { path.replaceWith(exp); }, - } as Visitor, + }, }; }); diff --git a/packages/babel-plugin-proposal-optional-chaining/src/index.ts b/packages/babel-plugin-proposal-optional-chaining/src/index.ts index c9f3c2e8b368..f7339e0767cd 100644 --- a/packages/babel-plugin-proposal-optional-chaining/src/index.ts +++ b/packages/babel-plugin-proposal-optional-chaining/src/index.ts @@ -1,8 +1,13 @@ import { declare } from "@babel/helper-plugin-utils"; import syntaxOptionalChaining from "@babel/plugin-syntax-optional-chaining"; import { transform } from "./transform"; +import type { NodePath } from "@babel/traverse"; +import type * as t from "@babel/types"; -export default declare((api, options) => { +export interface Options { + loose?: boolean; +} +export default declare((api, options: Options) => { api.assertVersion(7); const { loose = false } = options; @@ -14,7 +19,9 @@ export default declare((api, options) => { inherits: syntaxOptionalChaining.default, visitor: { - "OptionalCallExpression|OptionalMemberExpression"(path) { + "OptionalCallExpression|OptionalMemberExpression"( + path: NodePath, + ) { transform(path, { noDocumentAll, pureGetters }); }, }, diff --git a/packages/babel-plugin-proposal-pipeline-operator/src/hackVisitor.ts b/packages/babel-plugin-proposal-pipeline-operator/src/hackVisitor.ts index ab8be5de7947..8ef6e97bbd0b 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/src/hackVisitor.ts +++ b/packages/babel-plugin-proposal-pipeline-operator/src/hackVisitor.ts @@ -1,10 +1,13 @@ import { types as t } from "@babel/core"; import type { NodePath, Visitor } from "@babel/traverse"; +import type { PluginPass } from "@babel/core"; -const topicReferenceVisitor: Visitor<{ +interface State { topicReferences: NodePath[]; sideEffectsBeforeFirstTopicReference: boolean; -}> = { +} + +const topicReferenceVisitor: Visitor = { exit(path, state) { if (path.isTopicReference()) { state.topicReferences.push(path); @@ -79,10 +82,15 @@ export default { // Replace the pipe expression itself with an assignment expression. path.replaceWith( t.sequenceExpression([ - t.assignmentExpression("=", t.cloneNode(topicVariable), node.left), + t.assignmentExpression( + "=", + t.cloneNode(topicVariable), + // @ts-ignore node.left must not be a PrivateName when operator is |> + node.left, + ), node.right, ]), ); }, }, -}; +} as Visitor; diff --git a/packages/babel-plugin-proposal-pipeline-operator/src/index.ts b/packages/babel-plugin-proposal-pipeline-operator/src/index.ts index 6d895db28d16..ceb1caae07ec 100644 --- a/packages/babel-plugin-proposal-pipeline-operator/src/index.ts +++ b/packages/babel-plugin-proposal-pipeline-operator/src/index.ts @@ -4,6 +4,7 @@ import minimalVisitor from "./minimalVisitor"; import hackVisitor from "./hackVisitor"; import fsharpVisitor from "./fsharpVisitor"; import smartVisitor from "./smartVisitor"; +import type { Options } from "@babel/plugin-syntax-pipeline-operator"; const visitorsPerProposal = { minimal: minimalVisitor, @@ -12,7 +13,7 @@ const visitorsPerProposal = { smart: smartVisitor, }; -export default declare((api, options) => { +export default declare((api, options: Options) => { api.assertVersion(7); const { proposal } = options; diff --git a/packages/babel-plugin-proposal-private-methods/src/index.ts b/packages/babel-plugin-proposal-private-methods/src/index.ts index 31652dfcfacb..1db8d5717917 100644 --- a/packages/babel-plugin-proposal-private-methods/src/index.ts +++ b/packages/babel-plugin-proposal-private-methods/src/index.ts @@ -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({ diff --git a/packages/babel-plugin-proposal-private-property-in-object/src/index.ts b/packages/babel-plugin-proposal-private-property-in-object/src/index.ts index 094e054c669c..fb6384e05232 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/src/index.ts +++ b/packages/babel-plugin-proposal-private-property-in-object/src/index.ts @@ -6,9 +6,16 @@ import { injectInitialization as injectConstructorInit, } from "@babel/helper-create-class-features-plugin"; import annotateAsPure from "@babel/helper-annotate-as-pure"; +import type * as t from "@babel/types"; +import type { NodePath } from "@babel/traverse"; -export default declare(({ assertVersion, types: t, template }, { loose }) => { - assertVersion(7); +export interface Options { + loose?: boolean; +} +export default declare((api, opt: Options) => { + api.assertVersion(7); + const { types: t, template } = api; + const { loose } = opt; // NOTE: When using the class fields or private methods plugins, // they will also take care of '#priv in obj' checks when visiting @@ -107,10 +114,12 @@ export default declare(({ assertVersion, types: t, template }, { loose }) => { .find(({ node }) => t.isPrivate(node) && node.key.id.name === name); return !!privateElement; - }); + }) as NodePath; if (outerClass.parentPath.scope.path.isPattern()) { - outerClass.replaceWith(template.ast`(() => ${outerClass.node})()`); + outerClass.replaceWith( + template.ast`(() => ${outerClass.node})()` as t.Statement, + ); // The injected class will be queued and eventually transformed when visited return; } diff --git a/packages/babel-plugin-proposal-record-and-tuple/src/index.ts b/packages/babel-plugin-proposal-record-and-tuple/src/index.ts index ac57361166c0..551e223707f2 100644 --- a/packages/babel-plugin-proposal-record-and-tuple/src/index.ts +++ b/packages/babel-plugin-proposal-record-and-tuple/src/index.ts @@ -16,14 +16,25 @@ import { declare } from "@babel/helper-plugin-utils"; import syntaxRecordAndTuple from "@babel/plugin-syntax-record-and-tuple"; +import type { Options as SyntaxOptions } from "@babel/plugin-syntax-record-and-tuple"; import { types as t } from "@babel/core"; import { addNamed, isModule } from "@babel/helper-module-imports"; import { OptionValidator } from "@babel/helper-validator-option"; +import type { NodePath } from "@babel/traverse"; declare const PACKAGE_JSON: { name: string; version: string }; const v = new OptionValidator(PACKAGE_JSON.name); -export default declare((api, options) => { +export interface Options extends SyntaxOptions { + polyfillModuleName?: string; + importPolyfill?: boolean; +} + +export interface State { + programPath: NodePath; +} + +export default declare((api, options: Options) => { api.assertVersion(7); const polyfillModuleName = v.validateStringOption( @@ -46,7 +57,7 @@ export default declare((api, options) => { return value; } - function getBuiltIn(name, path, programPath) { + function getBuiltIn(name, programPath) { if (!shouldImportPolyfill) return t.identifier(name); if (!programPath) { throw new Error("Internal error: unable to find the Program node."); @@ -72,14 +83,14 @@ export default declare((api, options) => { state.programPath = path; }, RecordExpression(path, state) { - const record = getBuiltIn("Record", path, state.programPath); + const record = getBuiltIn("Record", state.programPath); const object = t.objectExpression(path.node.properties); const wrapped = t.callExpression(record, [object]); path.replaceWith(wrapped); }, TupleExpression(path, state) { - const tuple = getBuiltIn("Tuple", path, state.programPath); + const tuple = getBuiltIn("Tuple", state.programPath); const wrapped = t.callExpression(tuple, path.node.elements); path.replaceWith(wrapped); diff --git a/packages/babel-plugin-proposal-unicode-property-regex/src/index.ts b/packages/babel-plugin-proposal-unicode-property-regex/src/index.ts index bf76031cbf12..447e08acc3af 100644 --- a/packages/babel-plugin-proposal-unicode-property-regex/src/index.ts +++ b/packages/babel-plugin-proposal-unicode-property-regex/src/index.ts @@ -2,7 +2,11 @@ import { createRegExpFeaturePlugin } from "@babel/helper-create-regexp-features-plugin"; import { declare } from "@babel/helper-plugin-utils"; -export default declare((api, options) => { +export interface Options { + useUnicodeFlag?: boolean; +} + +export default declare((api, options: Options) => { api.assertVersion(7); const { useUnicodeFlag = true } = options; diff --git a/packages/babel-plugin-syntax-decorators/src/index.ts b/packages/babel-plugin-syntax-decorators/src/index.ts index 93c8c9431574..83dd7f8f0b7c 100644 --- a/packages/babel-plugin-syntax-decorators/src/index.ts +++ b/packages/babel-plugin-syntax-decorators/src/index.ts @@ -1,6 +1,12 @@ import { declare } from "@babel/helper-plugin-utils"; -export default declare((api, options) => { +export interface Options { + legacy?: boolean; + version?: "legacy" | "2018-09" | "2021-12"; + decoratorsBeforeExport?: boolean; +} + +export default declare((api, options: Options) => { api.assertVersion(7); const { diff --git a/packages/babel-plugin-syntax-destructuring-private/src/index.ts b/packages/babel-plugin-syntax-destructuring-private/src/index.ts index 71169f24f853..021673205f8f 100644 --- a/packages/babel-plugin-syntax-destructuring-private/src/index.ts +++ b/packages/babel-plugin-syntax-destructuring-private/src/index.ts @@ -6,7 +6,7 @@ export default declare(api => { return { name: "syntax-destructuring-private", - manipulateOptions(_: any, parserOpts: { plugins: string[] }) { + manipulateOptions(_, parserOpts) { parserOpts.plugins.push("destructuringPrivate"); }, }; diff --git a/packages/babel-plugin-syntax-flow/src/index.ts b/packages/babel-plugin-syntax-flow/src/index.ts index 72fb7dc60b95..89dfaead87b1 100644 --- a/packages/babel-plugin-syntax-flow/src/index.ts +++ b/packages/babel-plugin-syntax-flow/src/index.ts @@ -1,6 +1,7 @@ import { declare } from "@babel/helper-plugin-utils"; +import type { FlowPluginOptions } from "@babel/parser"; -export default declare((api, options) => { +export default declare((api, options: FlowPluginOptions) => { api.assertVersion(7); // When enabled and plugins includes flow, all files should be parsed as if diff --git a/packages/babel-plugin-syntax-import-assertions/src/index.ts b/packages/babel-plugin-syntax-import-assertions/src/index.ts index e99359e0372d..e26d5101a46a 100644 --- a/packages/babel-plugin-syntax-import-assertions/src/index.ts +++ b/packages/babel-plugin-syntax-import-assertions/src/index.ts @@ -7,7 +7,7 @@ export default declare(api => { name: "syntax-import-assertions", manipulateOptions(opts, parserOpts) { - parserOpts.plugins.push(["importAssertions"]); + parserOpts.plugins.push("importAssertions"); }, }; }); diff --git a/packages/babel-plugin-syntax-pipeline-operator/src/index.ts b/packages/babel-plugin-syntax-pipeline-operator/src/index.ts index 4a9013ed8965..9072fcd24acc 100644 --- a/packages/babel-plugin-syntax-pipeline-operator/src/index.ts +++ b/packages/babel-plugin-syntax-pipeline-operator/src/index.ts @@ -1,11 +1,16 @@ import { declare } from "@babel/helper-plugin-utils"; -const PIPELINE_PROPOSALS = ["minimal", "fsharp", "hack", "smart"]; -const TOPIC_TOKENS = ["^^", "@@", "^", "%", "#"]; +const PIPELINE_PROPOSALS = ["minimal", "fsharp", "hack", "smart"] as const; +const TOPIC_TOKENS = ["^^", "@@", "^", "%", "#"] as const; const documentationURL = "https://babeljs.io/docs/en/babel-plugin-proposal-pipeline-operator"; -export default declare((api, { proposal, topicToken }) => { +export interface Options { + proposal: typeof PIPELINE_PROPOSALS[number]; + topicToken?: typeof TOPIC_TOKENS[number]; +} + +export default declare((api, { proposal, topicToken }: Options) => { api.assertVersion(7); if (typeof proposal !== "string" || !PIPELINE_PROPOSALS.includes(proposal)) { diff --git a/packages/babel-plugin-syntax-record-and-tuple/src/index.ts b/packages/babel-plugin-syntax-record-and-tuple/src/index.ts index 12f50f60c952..2f97eea173d0 100644 --- a/packages/babel-plugin-syntax-record-and-tuple/src/index.ts +++ b/packages/babel-plugin-syntax-record-and-tuple/src/index.ts @@ -1,6 +1,10 @@ import { declare } from "@babel/helper-plugin-utils"; -export default declare((api, options) => { +export interface Options { + syntaxType: "hash" | "bar"; +} + +export default declare((api, options: Options) => { api.assertVersion(7); return { diff --git a/packages/babel-plugin-syntax-typescript/src/index.ts b/packages/babel-plugin-syntax-typescript/src/index.ts index b98998b388ca..933b8e8c5d8a 100644 --- a/packages/babel-plugin-syntax-typescript/src/index.ts +++ b/packages/babel-plugin-syntax-typescript/src/index.ts @@ -15,7 +15,12 @@ function removePlugin(plugins, name) { } } -export default declare((api, { isTSX, disallowAmbiguousJSXLike }) => { +export interface Options { + disallowAmbiguousJSXLike?: boolean; + isTSX?: boolean; +} + +export default declare((api, { isTSX, disallowAmbiguousJSXLike }: Options) => { api.assertVersion(7); return { diff --git a/packages/babel-plugin-transform-arrow-functions/src/index.ts b/packages/babel-plugin-transform-arrow-functions/src/index.ts index 41384423378a..5050127a87a7 100644 --- a/packages/babel-plugin-transform-arrow-functions/src/index.ts +++ b/packages/babel-plugin-transform-arrow-functions/src/index.ts @@ -1,8 +1,10 @@ import { declare } from "@babel/helper-plugin-utils"; -import type { NodePath } from "@babel/traverse"; -import type * as t from "@babel/types"; -export default declare((api, options) => { +export interface Options { + spec?: boolean; +} + +export default declare((api, options: Options) => { api.assertVersion(7); const noNewArrows = api.assumption("noNewArrows") ?? !options.spec; @@ -11,7 +13,7 @@ export default declare((api, options) => { name: "transform-arrow-functions", visitor: { - ArrowFunctionExpression(path: NodePath) { + ArrowFunctionExpression(path) { // In some conversion cases, it may have already been converted to a function while this callback // was queued up. if (!path.isArrowFunctionExpression()) return; diff --git a/packages/babel-plugin-transform-async-to-generator/src/index.ts b/packages/babel-plugin-transform-async-to-generator/src/index.ts index 0a7f67abdb33..2868ccbe7f26 100644 --- a/packages/babel-plugin-transform-async-to-generator/src/index.ts +++ b/packages/babel-plugin-transform-async-to-generator/src/index.ts @@ -3,7 +3,16 @@ import remapAsyncToGenerator from "@babel/helper-remap-async-to-generator"; import { addNamed } from "@babel/helper-module-imports"; import { types as t } from "@babel/core"; -export default declare((api, options) => { +export interface Options { + method?: string; + module?: string; +} + +export interface State { + methodWrapper?: t.Identifier | t.SequenceExpression; +} + +export default declare((api, options: Options) => { api.assertVersion(7); const { method, module } = options; diff --git a/packages/babel-plugin-transform-block-scoping/src/index.ts b/packages/babel-plugin-transform-block-scoping/src/index.ts index a57ad115c117..f50d5fbf1f48 100644 --- a/packages/babel-plugin-transform-block-scoping/src/index.ts +++ b/packages/babel-plugin-transform-block-scoping/src/index.ts @@ -7,7 +7,12 @@ import type { File } from "@babel/core"; const DONE = new WeakSet(); -export default declare((api, opts) => { +export interface Options { + tdz?: boolean; + throwIfClosureRequired?: boolean; +} + +export default declare((api, opts: Options) => { api.assertVersion(7); const { throwIfClosureRequired = false, tdz: tdzEnabled = false } = opts; diff --git a/packages/babel-plugin-transform-classes/src/index.ts b/packages/babel-plugin-transform-classes/src/index.ts index 60fd7d978f08..ca3b86b38da7 100644 --- a/packages/babel-plugin-transform-classes/src/index.ts +++ b/packages/babel-plugin-transform-classes/src/index.ts @@ -15,7 +15,11 @@ const builtinClasses = new Set([ ...getBuiltinClasses("browser"), ]); -export default declare((api, options) => { +export interface Options { + loose?: boolean; +} + +export default declare((api, options: Options) => { api.assertVersion(7); const { loose } = options; diff --git a/packages/babel-plugin-transform-computed-properties/src/index.ts b/packages/babel-plugin-transform-computed-properties/src/index.ts index e523cd5e884a..efab06d1fbe2 100644 --- a/packages/babel-plugin-transform-computed-properties/src/index.ts +++ b/packages/babel-plugin-transform-computed-properties/src/index.ts @@ -1,7 +1,11 @@ import { declare } from "@babel/helper-plugin-utils"; import { template, types as t } from "@babel/core"; -export default declare((api, options) => { +export interface Options { + loose?: boolean; +} + +export default declare((api, options: Options) => { api.assertVersion(7); const setComputedProperties = @@ -124,6 +128,7 @@ export default declare((api, options) => { const { node, parent, scope } = path; let hasComputed = false; for (const prop of node.properties) { + // @ts-ignore SpreadElement must not have computed property hasComputed = prop.computed === true; if (hasComputed) break; } @@ -137,6 +142,7 @@ export default declare((api, options) => { let foundComputed = false; for (const prop of node.properties) { + // @ts-ignore SpreadElement must not have computed property if (prop.computed) { foundComputed = true; } diff --git a/packages/babel-plugin-transform-destructuring/src/index.ts b/packages/babel-plugin-transform-destructuring/src/index.ts index d70f7011f914..df9292d48ad4 100644 --- a/packages/babel-plugin-transform-destructuring/src/index.ts +++ b/packages/babel-plugin-transform-destructuring/src/index.ts @@ -5,8 +5,6 @@ import { convertVariableDeclaration, convertAssignmentExpression, } from "./util"; -import type { PluginPass } from "@babel/core"; -import type { Visitor } from "@babel/traverse"; /** * Test if a VariableDeclaration's declarations contains any Patterns. @@ -21,7 +19,13 @@ function variableDeclarationHasPattern(node: t.VariableDeclaration) { return false; } -export default declare((api, options) => { +export interface Options { + allowArrayLike?: boolean; + loose?: boolean; + useBuiltIns?: boolean; +} + +export default declare((api, options: Options) => { api.assertVersion(7); const { useBuiltIns = false } = options; @@ -167,6 +171,6 @@ export default declare((api, options) => { useBuiltIns, ); }, - } as Visitor, + }, }; }); diff --git a/packages/babel-plugin-transform-duplicate-keys/src/index.ts b/packages/babel-plugin-transform-duplicate-keys/src/index.ts index 6765c4895de3..654c825ef497 100644 --- a/packages/babel-plugin-transform-duplicate-keys/src/index.ts +++ b/packages/babel-plugin-transform-duplicate-keys/src/index.ts @@ -19,7 +19,7 @@ export default declare(api => { const { node } = path; const plainProps = node.properties.filter( prop => !t.isSpreadElement(prop) && !prop.computed, - ); + ) as (t.ObjectMethod | t.ObjectProperty)[]; // A property is a duplicate key if: // * the property is a data property, and is preceded by a data, @@ -36,6 +36,7 @@ export default declare(api => { for (const prop of plainProps) { const name = getName(prop.key); let isDuplicate = false; + // @ts-ignore prop.kind is not defined in ObjectProperty switch (prop.kind) { case "get": if (alreadySeenData[name] || alreadySeenGetters[name]) { diff --git a/packages/babel-plugin-transform-flow-comments/src/index.ts b/packages/babel-plugin-transform-flow-comments/src/index.ts index d1190fefe078..666e2c8f0e11 100644 --- a/packages/babel-plugin-transform-flow-comments/src/index.ts +++ b/packages/babel-plugin-transform-flow-comments/src/index.ts @@ -2,6 +2,7 @@ import { declare } from "@babel/helper-plugin-utils"; import syntaxFlow from "@babel/plugin-syntax-flow"; import { types as t } from "@babel/core"; import generateCode from "@babel/generator"; +import type { NodePath } from "@babel/traverse"; export default declare(api => { api.assertVersion(7); @@ -111,7 +112,11 @@ export default declare(api => { attachComment({ ofPath: path.get("typeAnnotation"), toPath: path, - optional: node.optional || node.typeAnnotation.optional, + optional: + node.optional || + // @ts-expect-error Fixme: optional is not in t.TypeAnnotation, + // maybe we can remove it + node.typeAnnotation.optional, }); if (node.optional) { node.optional = false; @@ -128,7 +133,9 @@ export default declare(api => { AssignmentPattern: { exit({ node }) { const { left } = node; + // @ts-ignore optional is not in ObjectPattern if (left.optional) { + // @ts-ignore optional is not in ObjectPattern left.optional = false; } }, @@ -142,6 +149,7 @@ export default declare(api => { attachComment({ ofPath: path.get("typeParameters"), toPath: path.get("id"), + // @ts-ignore Fixme: optional is not in t.TypeParameterDeclaration optional: node.typeParameters.optional, }); } @@ -150,6 +158,7 @@ export default declare(api => { ofPath: path.get("returnType"), toPath: path.get("body"), where: "leading", + // @ts-ignore Fixme: optional is not in t.TypeAnnotation optional: node.returnType.typeAnnotation.optional, }); } @@ -164,6 +173,7 @@ export default declare(api => { attachComment({ ofPath: path.get("typeAnnotation"), toPath: path.get("key"), + // @ts-ignore Fixme: optional is not in t.TypeAnnotation optional: node.typeAnnotation.optional, }); } @@ -186,12 +196,16 @@ export default declare(api => { return; } - const typeSpecifiers = node.specifiers.filter(specifier => - isTypeImport(specifier.importKind), + const typeSpecifiers = node.specifiers.filter( + specifier => + specifier.type === "ImportSpecifier" && + isTypeImport(specifier.importKind), ); const nonTypeSpecifiers = node.specifiers.filter( - specifier => !isTypeImport(specifier.importKind), + specifier => + specifier.type !== "ImportSpecifier" || + !isTypeImport(specifier.importKind), ); node.specifiers = nonTypeSpecifiers; @@ -213,7 +227,11 @@ export default declare(api => { attachComment({ ofPath: path.get("typeAnnotation"), toPath: path, - optional: node.optional || node.typeAnnotation.optional, + optional: + // @ts-ignore optional is not in ObjectPattern + node.optional || + // @ts-ignore Fixme: optional is not in t.TypeAnnotation + node.typeAnnotation.optional, }); } }, @@ -228,6 +246,7 @@ export default declare(api => { if (node.typeParameters) { const typeParameters = path.get("typeParameters"); comments.push( + // @ts-ignore optional is not in TypeParameterDeclaration generateComment(typeParameters, node.typeParameters.optional), ); const trailingComments = node.typeParameters.trailingComments; @@ -247,10 +266,13 @@ export default declare(api => { } if (node.superTypeParameters) { - const superTypeParameters = path.get("superTypeParameters"); + const superTypeParameters = path.get( + "superTypeParameters", + ) as NodePath; comments.push( generateComment( superTypeParameters, + // @ts-ignore optional is not in TypeParameterInstantiation superTypeParameters.node.optional, ), ); diff --git a/packages/babel-plugin-transform-flow-strip-types/src/index.ts b/packages/babel-plugin-transform-flow-strip-types/src/index.ts index f5b873c88507..8030a99f9c77 100644 --- a/packages/babel-plugin-transform-flow-strip-types/src/index.ts +++ b/packages/babel-plugin-transform-flow-strip-types/src/index.ts @@ -2,7 +2,12 @@ import { declare } from "@babel/helper-plugin-utils"; import syntaxFlow from "@babel/plugin-syntax-flow"; import { types as t } from "@babel/core"; -export default declare((api, opts) => { +export interface Options { + requireDirective?: boolean; + allowDeclareFields?: boolean; +} + +export default declare((api, opts: Options) => { api.assertVersion(7); const FLOW_DIRECTIVE = /(@flow(\s+(strict(-local)?|weak))?|@noflow)/; @@ -58,6 +63,7 @@ export default declare((api, opts) => { let typeCount = 0; + // @ts-ignore importKind is only in importSpecifier path.node.specifiers.forEach(({ importKind }) => { if (importKind === "type" || importKind === "typeof") { typeCount++; @@ -125,7 +131,11 @@ export default declare((api, opts) => { AssignmentPattern({ node }) { if (skipStrip) return; - node.left.optional = false; + // @ts-expect-error optional is not in ObjectPattern + if (node.left.optional) { + // @ts-expect-error optional is not in ObjectPattern + node.left.optional = false; + } }, Function({ node }) { @@ -138,10 +148,15 @@ export default declare((api, opts) => { node.params.shift(); } for (let i = 0; i < node.params.length; i++) { - const param = node.params[i]; - param.optional = false; + let param = node.params[i]; if (param.type === "AssignmentPattern") { - param.left.optional = false; + // @ts-ignore + param = param.left; + } + // @ts-expect-error optional is not in ObjectPattern + if (param.optional) { + // @ts-expect-error optional is not in ObjectPattern + param.optional = false; } } @@ -154,6 +169,7 @@ export default declare((api, opts) => { if (skipStrip) return; let { node } = path; do { + // @ts-ignore node is a search pointer node = node.expression; } while (t.isTypeCastExpression(node)); path.replaceWith(node); diff --git a/packages/babel-plugin-transform-for-of/src/index.ts b/packages/babel-plugin-transform-for-of/src/index.ts index 91d99c4880a6..732d5d2091f0 100644 --- a/packages/babel-plugin-transform-for-of/src/index.ts +++ b/packages/babel-plugin-transform-for-of/src/index.ts @@ -3,7 +3,13 @@ import { template, types as t } from "@babel/core"; import transformWithoutHelper from "./no-helper-implementation"; -export default declare((api, options) => { +export interface Options { + allowArrayLike?: boolean; + assumeArray?: boolean; + loose?: boolean; +} + +export default declare((api, options: Options) => { api.assertVersion(7); { @@ -59,13 +65,14 @@ export default declare((api, options) => { return; } const i = scope.generateUidIdentifier("i"); - let array = scope.maybeGenerateMemoised(right, true); + let array: t.Identifier | t.ThisExpression = + scope.maybeGenerateMemoised(right, true); const inits = [t.variableDeclarator(i, t.numericLiteral(0))]; if (array) { inits.push(t.variableDeclarator(array, right)); } else { - array = right; + array = right as t.Identifier | t.ThisExpression; } const item = t.memberExpression( @@ -230,7 +237,7 @@ export default declare((api, options) => { // ensure that it's a block so we can take all its statements path.ensureBlock(); - node.body.body.unshift(declar); + (node.body as t.BlockStatement).body.unshift(declar); const nodes = builder.build({ CREATE_ITERATOR_HELPER: state.addHelper(builder.helper), diff --git a/packages/babel-plugin-transform-instanceof/src/index.ts b/packages/babel-plugin-transform-instanceof/src/index.ts index 442bd3f8bd5d..828d92eaac2c 100644 --- a/packages/babel-plugin-transform-instanceof/src/index.ts +++ b/packages/babel-plugin-transform-instanceof/src/index.ts @@ -24,7 +24,13 @@ export default declare(api => { if (isUnderHelper) { return; } else { - path.replaceWith(t.callExpression(helper, [node.left, node.right])); + path.replaceWith( + t.callExpression(helper, [ + // @ts-ignore node.left can be PrivateName only when node.operator is "in" + node.left, + node.right, + ]), + ); } } }, diff --git a/packages/babel-plugin-transform-jscript/src/index.ts b/packages/babel-plugin-transform-jscript/src/index.ts index 06bbad6d3606..75a646a219c5 100644 --- a/packages/babel-plugin-transform-jscript/src/index.ts +++ b/packages/babel-plugin-transform-jscript/src/index.ts @@ -19,6 +19,7 @@ export default declare(api => { null, [], t.blockStatement([ + // @ts-ignore fixme: t.toStatement may return false t.toStatement(node), t.returnStatement(t.cloneNode(node.id)), ]), diff --git a/packages/babel-plugin-transform-literals/src/index.ts b/packages/babel-plugin-transform-literals/src/index.ts index 879a37c49f08..6fc5011fbb0d 100644 --- a/packages/babel-plugin-transform-literals/src/index.ts +++ b/packages/babel-plugin-transform-literals/src/index.ts @@ -9,6 +9,7 @@ export default declare(api => { visitor: { NumericLiteral({ node }) { // number octal like 0b10 or 0o70 + // @ts-ignore Add node.extra typings if (node.extra && /^0[ob]/i.test(node.extra.raw)) { node.extra = undefined; } @@ -16,6 +17,7 @@ export default declare(api => { StringLiteral({ node }) { // unicode escape + // @ts-ignore Add node.extra typings if (node.extra && /\\[u]/gi.test(node.extra.raw)) { node.extra = undefined; } diff --git a/packages/babel-plugin-transform-modules-amd/src/index.ts b/packages/babel-plugin-transform-modules-amd/src/index.ts index 71b0b7585c86..e6715dbe9389 100644 --- a/packages/babel-plugin-transform-modules-amd/src/index.ts +++ b/packages/babel-plugin-transform-modules-amd/src/index.ts @@ -11,6 +11,7 @@ import { } from "@babel/helper-module-transforms"; import { template, types as t } from "@babel/core"; import { getImportSource } from "babel-plugin-dynamic-import-node/utils"; +import type { PluginOptions } from "@babel/helper-module-transforms"; const buildWrapper = template(` define(MODULE_NAME, AMD_ARGUMENTS, function(IMPORT_NAMES) { @@ -35,7 +36,22 @@ function injectWrapper(path, wrapper) { amdFactory.pushContainer("body", body); } -export default declare((api, options) => { +export interface Options extends PluginOptions { + allowTopLevelThis?: boolean; + importInterop?: "babel" | "node"; + loose?: boolean; + noInterop?: boolean; + strict?: boolean; + strictMode?: boolean; +} + +export interface State { + requireId?: t.Identifier; + resolveId?: t.Identifier; + rejectId?: t.Identifier; +} + +export default declare((api, options: Options) => { api.assertVersion(7); const { allowTopLevelThis, strict, strictMode, importInterop, noInterop } = diff --git a/packages/babel-plugin-transform-modules-commonjs/src/index.ts b/packages/babel-plugin-transform-modules-commonjs/src/index.ts index f89866366534..be46970166b3 100644 --- a/packages/babel-plugin-transform-modules-commonjs/src/index.ts +++ b/packages/babel-plugin-transform-modules-commonjs/src/index.ts @@ -10,10 +10,24 @@ import { } from "@babel/helper-module-transforms"; import simplifyAccess from "@babel/helper-simple-access"; import { template, types as t } from "@babel/core"; +import type { PluginOptions } from "@babel/helper-module-transforms"; import { createDynamicImportTransform } from "babel-plugin-dynamic-import-node/utils"; -export default declare((api, options) => { +export interface Options extends PluginOptions { + allowCommonJSExports?: boolean; + allowTopLevelThis?: boolean; + importInterop?: "babel" | "node"; + lazy?: boolean | string[] | ((string) => boolean); + loose?: boolean; + mjsStrictNamespace?: boolean; + noInterop?: boolean; + strict?: boolean; + strictMode?: boolean; + strictNamespace?: boolean; +} + +export default declare((api, options: Options) => { api.assertVersion(7); const transformImportCall = createDynamicImportTransform(api); diff --git a/packages/babel-plugin-transform-modules-systemjs/src/index.ts b/packages/babel-plugin-transform-modules-systemjs/src/index.ts index 3841e1421b6e..1a4657f16ef3 100644 --- a/packages/babel-plugin-transform-modules-systemjs/src/index.ts +++ b/packages/babel-plugin-transform-modules-systemjs/src/index.ts @@ -3,6 +3,7 @@ import hoistVariables from "@babel/helper-hoist-variables"; import { template, types as t } from "@babel/core"; import { getImportSource } from "babel-plugin-dynamic-import-node/utils"; import { rewriteThis, getModuleName } from "@babel/helper-module-transforms"; +import type { PluginOptions } from "@babel/helper-module-transforms"; import { isIdentifierName } from "@babel/helper-validator-identifier"; import type { NodePath } from "@babel/traverse"; @@ -154,7 +155,12 @@ function constructExportCall( return statements; } -export default declare((api, options) => { +export interface Options extends PluginOptions { + allowTopLevelThis?: boolean; + systemGlobal?: string; +} + +export default declare((api, options: Options) => { api.assertVersion(7); const { systemGlobal = "System", allowTopLevelThis = false } = options; diff --git a/packages/babel-plugin-transform-modules-umd/src/index.ts b/packages/babel-plugin-transform-modules-umd/src/index.ts index ff8ef645f398..456772e8563b 100644 --- a/packages/babel-plugin-transform-modules-umd/src/index.ts +++ b/packages/babel-plugin-transform-modules-umd/src/index.ts @@ -10,7 +10,9 @@ import { wrapInterop, getModuleName, } from "@babel/helper-module-transforms"; +import type { PluginOptions } from "@babel/helper-module-transforms"; import { types as t, template } from "@babel/core"; +import type { NodePath } from "@babel/traverse"; const buildPrerequisiteAssignment = template(` GLOBAL_REFERENCE = GLOBAL_REFERENCE || {} @@ -38,7 +40,18 @@ const buildWrapper = template(` }) `); -export default declare((api, options) => { +export interface Options extends PluginOptions { + allowTopLevelThis?: boolean; + exactGlobals?: boolean; + globals?: Record; + importInterop?: "babel" | "node"; + loose?: boolean; + noInterop?: boolean; + strict?: boolean; + strictMode?: boolean; +} + +export default declare((api, options: Options) => { api.assertVersion(7); const { @@ -239,11 +252,11 @@ export default declare((api, options) => { this.filename || "unknown", moduleName, ), - }), - ])[0]; - const umdFactory = umdWrapper - .get("expression.arguments")[1] - .get("body"); + }) as t.Statement, + ])[0] as NodePath; + const umdFactory = ( + umdWrapper.get("expression.arguments")[1] as NodePath + ).get("body"); umdFactory.pushContainer("directives", directives); umdFactory.pushContainer("body", body); }, diff --git a/packages/babel-plugin-transform-new-target/src/index.ts b/packages/babel-plugin-transform-new-target/src/index.ts index 162575ecf227..c3dce6a2c634 100644 --- a/packages/babel-plugin-transform-new-target/src/index.ts +++ b/packages/babel-plugin-transform-new-target/src/index.ts @@ -1,5 +1,6 @@ import { declare } from "@babel/helper-plugin-utils"; import { types as t } from "@babel/core"; +import type { NodePath } from "@babel/traverse"; export default declare(api => { api.assertVersion(7); @@ -27,7 +28,13 @@ export default declare(api => { return true; } return false; - }); + }) as NodePath< + | t.FunctionDeclaration + | t.FunctionExpression + | t.Class + | t.ClassMethod + | t.ClassPrivateMethod + >; if (!func) { throw path.buildCodeFrameError( @@ -36,12 +43,11 @@ export default declare(api => { } const { node } = func; + if (t.isMethod(node)) { + path.replaceWith(scope.buildUndefinedNode()); + return; + } if (!node.id) { - if (func.isMethod()) { - path.replaceWith(scope.buildUndefinedNode()); - return; - } - node.id = scope.generateUidIdentifier("target"); } diff --git a/packages/babel-plugin-transform-parameters/src/index.ts b/packages/babel-plugin-transform-parameters/src/index.ts index 50048394f3ff..c3e152e305d4 100644 --- a/packages/babel-plugin-transform-parameters/src/index.ts +++ b/packages/babel-plugin-transform-parameters/src/index.ts @@ -3,7 +3,11 @@ import convertFunctionParams from "./params"; import convertFunctionRest from "./rest"; export { convertFunctionParams }; -export default declare((api, options) => { +export interface Options { + loose?: boolean; +} + +export default declare((api, options: Options) => { api.assertVersion(7); const ignoreFunctionLength = diff --git a/packages/babel-plugin-transform-proto-to-assign/src/index.ts b/packages/babel-plugin-transform-proto-to-assign/src/index.ts index 7a78893091ee..7bfce080e361 100644 --- a/packages/babel-plugin-transform-proto-to-assign/src/index.ts +++ b/packages/babel-plugin-transform-proto-to-assign/src/index.ts @@ -8,8 +8,8 @@ export default declare(api => { return t.isLiteral(t.toComputedKey(node, node.key), { value: "__proto__" }); } - function isProtoAssignmentExpression(node) { - const left = node.left; + function isProtoAssignmentExpression(node): node is t.MemberExpression { + const left = node; return ( t.isMemberExpression(left) && // @ts-expect-error todo(flow->ts): property can be t.PrivateName @@ -28,7 +28,7 @@ export default declare(api => { visitor: { AssignmentExpression(path, file) { - if (!isProtoAssignmentExpression(path.node)) return; + if (!isProtoAssignmentExpression(path.node.left)) return; const nodes = []; const left = path.node.left.object; @@ -55,14 +55,9 @@ export default declare(api => { const expr = path.node.expression; if (!t.isAssignmentExpression(expr, { operator: "=" })) return; - if (isProtoAssignmentExpression(expr)) { + if (isProtoAssignmentExpression(expr.left)) { path.replaceWith( - buildDefaultsCallExpression( - expr, - // todo(flow->ts) isProtoAssignmentExpression actually checks that - (expr.left as t.MemberExpression).object, - file, - ), + buildDefaultsCallExpression(expr, expr.left.object, file), ); } }, @@ -75,6 +70,7 @@ export default declare(api => { for (let i = 0; i < properties.length; i++) { const prop = properties[i]; if (isProtoKey(prop)) { + // @ts-expect-error Fixme: we should also handle ObjectMethod with __proto__ key proto = prop.value; properties.splice(i, 1); break; diff --git a/packages/babel-plugin-transform-react-constant-elements/src/index.ts b/packages/babel-plugin-transform-react-constant-elements/src/index.ts index af44cba5d037..c025319f26e1 100644 --- a/packages/babel-plugin-transform-react-constant-elements/src/index.ts +++ b/packages/babel-plugin-transform-react-constant-elements/src/index.ts @@ -1,7 +1,18 @@ import { declare } from "@babel/helper-plugin-utils"; import { types as t, template } from "@babel/core"; - -export default declare((api, options) => { +import type { Visitor, Scope } from "@babel/traverse"; + +export interface Options { + allowMutablePropsOnTags?: null | string[]; +} + +interface VisitorState { + isImmutable: boolean; + mutablePropsAllowed: boolean; + jsxScope: Scope; + targetScope: Scope; +} +export default declare((api, options: Options) => { api.assertVersion(7); const { allowMutablePropsOnTags } = options; @@ -41,7 +52,7 @@ export default declare((api, options) => { return scope; } - const immutabilityVisitor = { + const immutabilityVisitor: Visitor = { enter(path, state) { const stop = () => { state.isImmutable = false; @@ -114,7 +125,7 @@ export default declare((api, options) => { }, }; - const targetScopeVisitor = { + const targetScopeVisitor: Visitor = { ReferencedIdentifier(path, state) { const { node } = path; let { scope } = path; @@ -171,6 +182,7 @@ export default declare((api, options) => { } const elementName = lastSegment.name; + // @ts-expect-error Fixme: allowMutablePropsOnTags should handle JSXNamespacedName mutablePropsAllowed = allowMutablePropsOnTags.includes(elementName); } @@ -181,12 +193,13 @@ export default declare((api, options) => { let jsxScope; let current = path; while (!jsxScope && current.parentPath.isJSX()) { + // @ts-ignore current is a search pointer current = current.parentPath; jsxScope = HOISTED.get(current.node); } jsxScope ??= getHoistingScope(path.scope); - const visitorState = { + const visitorState: VisitorState = { isImmutable: true, mutablePropsAllowed, jsxScope, diff --git a/packages/babel-plugin-transform-react-jsx/src/create-plugin.ts b/packages/babel-plugin-transform-react-jsx/src/create-plugin.ts index b7a7530f084e..2fb18e6ddb9c 100644 --- a/packages/babel-plugin-transform-react-jsx/src/create-plugin.ts +++ b/packages/babel-plugin-transform-react-jsx/src/create-plugin.ts @@ -39,8 +39,19 @@ const get = (pass: PluginPass, name: string) => const set = (pass: PluginPass, name: string, v: any) => pass.set(`@babel/plugin-react-jsx/${name}`, v); +export interface Options { + filter?: (node: t.Node, pass: PluginPass) => boolean; + importSource?: string; + pragma?: string; + pragmaFrag?: string; + pure?: string; + runtime?: "automatic" | "classic"; + throwIfNamespace?: boolean; + useBuiltIns: boolean; + useSpread?: boolean; +} export default function createPlugin({ name, development }) { - return declare((api, options) => { + return declare((api, options: Options) => { const { pure: PURE_ANNOTATION, diff --git a/packages/babel-plugin-transform-react-jsx/src/index.ts b/packages/babel-plugin-transform-react-jsx/src/index.ts index 5e040d067faf..7cf547a7e822 100644 --- a/packages/babel-plugin-transform-react-jsx/src/index.ts +++ b/packages/babel-plugin-transform-react-jsx/src/index.ts @@ -6,3 +6,5 @@ export default createPlugin({ name: "transform-react-jsx", development: false, }); + +export type { Options } from "./create-plugin"; diff --git a/packages/babel-plugin-transform-reserved-words/src/index.ts b/packages/babel-plugin-transform-reserved-words/src/index.ts index 014e7b964866..cefabf680af7 100644 --- a/packages/babel-plugin-transform-reserved-words/src/index.ts +++ b/packages/babel-plugin-transform-reserved-words/src/index.ts @@ -1,5 +1,6 @@ import { declare } from "@babel/helper-plugin-utils"; import { types as t } from "@babel/core"; +import type { NodePath } from "@babel/traverse"; export default declare(api => { api.assertVersion(7); @@ -8,7 +9,7 @@ export default declare(api => { name: "transform-reserved-words", visitor: { - "BindingIdentifier|ReferencedIdentifier"(path) { + "BindingIdentifier|ReferencedIdentifier"(path: NodePath) { if (!t.isValidES3Identifier(path.node.name)) { path.scope.rename(path.node.name); } diff --git a/packages/babel-plugin-transform-runtime/src/index.ts b/packages/babel-plugin-transform-runtime/src/index.ts index 23abb6565aa9..cac68f27bae8 100644 --- a/packages/babel-plugin-transform-runtime/src/index.ts +++ b/packages/babel-plugin-transform-runtime/src/index.ts @@ -18,7 +18,16 @@ function supportsStaticESM(caller) { return !!caller?.supportsStaticESM; } -export default declare((api, options, dirname) => { +export interface Options { + absoluteRuntime?: boolean; + corejs?: string | number | { version: string | number; proposals?: boolean }; + helpers?: boolean; + regenerator?: boolean; + useESModules?: boolean | "auto"; + version?: string; +} + +export default declare((api, options: Options, dirname) => { api.assertVersion(7); const { @@ -99,7 +108,7 @@ export default declare((api, options, dirname) => { } if (has(options, "useBuiltIns")) { - if (options.useBuiltIns) { + if (options["useBuiltIns"]) { throw new Error( "The 'useBuiltIns' option has been removed. The @babel/runtime " + "module now uses builtins by default.", @@ -113,7 +122,7 @@ export default declare((api, options, dirname) => { } if (has(options, "polyfill")) { - if (options.polyfill === false) { + if (options["polyfill"] === false) { throw new Error( "The 'polyfill' option has been removed. The @babel/runtime " + "module now skips polyfilling by default.", diff --git a/packages/babel-plugin-transform-spread/src/index.ts b/packages/babel-plugin-transform-spread/src/index.ts index ec68db00ff46..971c99dd1a71 100644 --- a/packages/babel-plugin-transform-spread/src/index.ts +++ b/packages/babel-plugin-transform-spread/src/index.ts @@ -5,7 +5,12 @@ import type { NodePath, Scope } from "@babel/traverse"; type ListElement = t.SpreadElement | t.Expression; -export default declare((api, options) => { +export interface Options { + allowArrayLike?: boolean; + loose?: boolean; +} + +export default declare((api, options: Options) => { api.assertVersion(7); const iterableIsArray = api.assumption("iterableIsArray") ?? options.loose; @@ -84,12 +89,12 @@ export default declare((api, options) => { name: "transform-spread", visitor: { - ArrayExpression(path: NodePath): void { + ArrayExpression(path): void { const { node, scope } = path; const elements = node.elements; if (!hasSpread(elements)) return; - const nodes = build(elements, scope, this); + const nodes = build(elements, scope, this.file); let first = nodes[0]; // If there is only one element in the ArrayExpression and @@ -123,7 +128,7 @@ export default declare((api, options) => { ), ); }, - CallExpression(path: NodePath): void { + CallExpression(path): void { const { node, scope } = path; const args = node.arguments as Array; @@ -150,7 +155,7 @@ export default declare((api, options) => { ) { nodes = [(args[0] as t.SpreadElement).argument]; } else { - nodes = build(args, scope, this); + nodes = build(args, scope, this.file); } const first = nodes.shift(); @@ -189,11 +194,15 @@ export default declare((api, options) => { node.arguments.unshift(t.cloneNode(contextLiteral)); }, - NewExpression(path: NodePath): void { + NewExpression(path): void { const { node, scope } = path; if (!hasSpread(node.arguments)) return; - const nodes = build(node.arguments as Array, scope, this); + const nodes = build( + node.arguments as Array, + scope, + this.file, + ); const first = nodes.shift(); diff --git a/packages/babel-plugin-transform-template-literals/src/index.ts b/packages/babel-plugin-transform-template-literals/src/index.ts index d3fdc6e678d0..edc0f002ef07 100644 --- a/packages/babel-plugin-transform-template-literals/src/index.ts +++ b/packages/babel-plugin-transform-template-literals/src/index.ts @@ -1,7 +1,11 @@ import { declare } from "@babel/helper-plugin-utils"; import { template, types as t } from "@babel/core"; -export default declare((api, options) => { +export interface Options { + loose?: boolean; +} + +export default declare((api, options: Options) => { api.assertVersion(7); const ignoreToPrimitiveHint = @@ -93,6 +97,7 @@ export default declare((api, options) => { ${tmp} = ${this.addHelper(helperName)}(${helperArgs}) ) `, + //@ts-ignore Fixme: quasi.expressions may contain TSAnyKeyword ...quasi.expressions, ]), ); diff --git a/packages/babel-plugin-transform-typeof-symbol/src/index.ts b/packages/babel-plugin-transform-typeof-symbol/src/index.ts index 1faea61b4b55..7061d716c8f3 100644 --- a/packages/babel-plugin-transform-typeof-symbol/src/index.ts +++ b/packages/babel-plugin-transform-typeof-symbol/src/index.ts @@ -22,13 +22,15 @@ export default declare(api => { if ( path.parentPath.isBinaryExpression() && - t.EQUALITY_BINARY_OPERATORS.indexOf(parent.operator) >= 0 + t.EQUALITY_BINARY_OPERATORS.indexOf( + (parent as t.BinaryExpression).operator, + ) >= 0 ) { // optimise `typeof foo === "string"` since we can determine that they'll never // need to handle symbols const opposite = path.getOpposite(); if ( - opposite.isLiteral() && + opposite.isStringLiteral() && opposite.node.value !== "symbol" && opposite.node.value !== "object" ) { @@ -39,6 +41,7 @@ export default declare(api => { let isUnderHelper = path.findParent(path => { if (path.isFunction()) { return ( + // @ts-ignore the access is coupled with the shape of typeof helper path.get("body.directives.0")?.node.value.value === "@babel/helpers - typeof" ); diff --git a/packages/babel-plugin-transform-typescript/src/index.ts b/packages/babel-plugin-transform-typescript/src/index.ts index 81fdf21a1704..68cfeac4097c 100644 --- a/packages/babel-plugin-transform-typescript/src/index.ts +++ b/packages/babel-plugin-transform-typescript/src/index.ts @@ -1,9 +1,9 @@ import { declare } from "@babel/helper-plugin-utils"; import syntaxTypeScript from "@babel/plugin-syntax-typescript"; import { types as t, template } from "@babel/core"; -import type { PluginPass } from "@babel/core"; import { injectInitialization } from "@babel/helper-create-class-features-plugin"; -import type { NodePath, Visitor } from "@babel/traverse"; +import type { NodePath } from "@babel/traverse"; +import type { Options as SyntaxOptions } from "@babel/plugin-syntax-typescript"; import transpileConstEnum from "./const-enum"; import type { NodePathConstEnum } from "./const-enum"; @@ -55,7 +55,7 @@ function isGlobalType(path: NodePath, name: string) { function registerGlobalType(programNode: t.Program, name: string) { GLOBAL_TYPES.get(programNode).add(name); } -export interface Options { +export interface Options extends SyntaxOptions { /** @default true */ allowNamespaces?: boolean; /** @default "React.createElement" */ @@ -66,12 +66,6 @@ export interface Options { optimizeConstEnums?: boolean; allowDeclareFields?: boolean; } -type ConfigAPI = { assertVersion: (range: string | number) => void }; -interface Plugin { - name: string; - visitor: Visitor; - inherits: typeof syntaxTypeScript; -} type ExtraNodeProps = { declare?: unknown; accessibility?: unknown; @@ -79,7 +73,7 @@ type ExtraNodeProps = { optional?: unknown; override?: unknown; }; -export default declare((api: ConfigAPI, opts: Options): Plugin => { +export default declare((api, opts: Options) => { api.assertVersion(7); const JSX_PRAGMA_REGEX = /\*?\s*@jsx((?:Frag)?)\s+([^\s]+)/; diff --git a/packages/babel-preset-env/src/index.ts b/packages/babel-preset-env/src/index.ts index 53bf3998fcf3..529456fc05b3 100644 --- a/packages/babel-preset-env/src/index.ts +++ b/packages/babel-preset-env/src/index.ts @@ -29,11 +29,11 @@ import getTargets, { } from "@babel/helper-compilation-targets"; import type { Targets, InputTargets } from "@babel/helper-compilation-targets"; import availablePlugins from "./available-plugins"; -import { declare } from "@babel/helper-plugin-utils"; +import { declarePreset } from "@babel/helper-plugin-utils"; type ModuleTransformationsType = typeof import("./module-transformations").default; -import type { BuiltInsOption, ModuleOption } from "./types"; +import type { BuiltInsOption, ModuleOption, Options } from "./types"; // TODO: Remove in Babel 8 export function isPluginRequired(targets: Targets, support: Targets) { @@ -271,7 +271,7 @@ function supportsTopLevelAwait(caller) { return !!caller?.supportsTopLevelAwait; } -export default declare((api, opts) => { +export default declarePreset((api, opts: Options) => { api.assertVersion(7); const babelTargets = api.targets(); @@ -360,7 +360,8 @@ option \`forceAllTransforms: true\` instead. shouldTransformDynamicImport: modules !== "auto" || !api.caller?.(supportsDynamicImport), shouldTransformExportNamespaceFrom: !shouldSkipExportNamespaceFrom, - shouldParseTopLevelAwait: !api.caller || api.caller(supportsTopLevelAwait), + shouldParseTopLevelAwait: + !api.caller || (api.caller(supportsTopLevelAwait) as boolean), }); const pluginNames = filterItems( diff --git a/packages/babel-preset-flow/src/index.ts b/packages/babel-preset-flow/src/index.ts index 523b2b063d26..c1174d209960 100644 --- a/packages/babel-preset-flow/src/index.ts +++ b/packages/babel-preset-flow/src/index.ts @@ -1,8 +1,8 @@ -import { declare } from "@babel/helper-plugin-utils"; +import { declarePreset } from "@babel/helper-plugin-utils"; import transformFlowStripTypes from "@babel/plugin-transform-flow-strip-types"; import normalizeOptions from "./normalize-options"; -export default declare((api, opts) => { +export default declarePreset((api, opts) => { api.assertVersion(7); const { all, allowDeclareFields } = normalizeOptions(opts); diff --git a/packages/babel-preset-react/src/index.ts b/packages/babel-preset-react/src/index.ts index 46af67c8f825..408a27c021ef 100644 --- a/packages/babel-preset-react/src/index.ts +++ b/packages/babel-preset-react/src/index.ts @@ -1,11 +1,23 @@ -import { declare } from "@babel/helper-plugin-utils"; +import { declarePreset } from "@babel/helper-plugin-utils"; import transformReactJSX from "@babel/plugin-transform-react-jsx"; import transformReactJSXDevelopment from "@babel/plugin-transform-react-jsx-development"; import transformReactDisplayName from "@babel/plugin-transform-react-display-name"; import transformReactPure from "@babel/plugin-transform-react-pure-annotations"; import normalizeOptions from "./normalize-options"; -export default declare((api, opts) => { +export interface Options { + development?: boolean; + importSource?: string; + pragma?: string; + pragmaFrag?: string; + pure?: string; + runtime?: "automatic" | "classic"; + throwIfNamespace?: boolean; + useBuiltIns?: boolean; + useSpread?: boolean; +} + +export default declarePreset((api, opts: Options) => { api.assertVersion(7); const { diff --git a/packages/babel-preset-typescript/src/index.ts b/packages/babel-preset-typescript/src/index.ts index 5fcefb6927ae..e9fb7b515204 100644 --- a/packages/babel-preset-typescript/src/index.ts +++ b/packages/babel-preset-typescript/src/index.ts @@ -1,8 +1,9 @@ -import { declare } from "@babel/helper-plugin-utils"; +import { declarePreset } from "@babel/helper-plugin-utils"; import transformTypeScript from "@babel/plugin-transform-typescript"; import normalizeOptions from "./normalize-options"; +import type { Options } from "./normalize-options"; -export default declare((api, opts) => { +export default declarePreset((api, opts: Options) => { api.assertVersion(7); const { diff --git a/packages/babel-preset-typescript/src/normalize-options.ts b/packages/babel-preset-typescript/src/normalize-options.ts index 419c18adb93e..02412d8e69e7 100644 --- a/packages/babel-preset-typescript/src/normalize-options.ts +++ b/packages/babel-preset-typescript/src/normalize-options.ts @@ -1,7 +1,19 @@ import { OptionValidator } from "@babel/helper-validator-option"; const v = new OptionValidator("@babel/preset-typescript"); -export default function normalizeOptions(options = {} as any) { +export interface Options { + allExtensions?: boolean; + allowDeclareFields?: boolean; + allowNamespaces?: boolean; + disallowAmbiguousJSXLike?: boolean; + isTSX?: boolean; + jsxPragma?: string; + jsxPragmaFrag?: string; + onlyRemoveTypeImports?: boolean; + optimizeConstEnums?: boolean; +} + +export default function normalizeOptions(options: Options = {}) { let { allowNamespaces = true, jsxPragma, onlyRemoveTypeImports } = options; const TopLevelOptions = {