diff --git a/lib/regexpu-core.d.ts b/lib/regexpu-core.d.ts new file mode 100644 index 000000000000..415800a5196c --- /dev/null +++ b/lib/regexpu-core.d.ts @@ -0,0 +1,17 @@ +declare module "regexpu-core" { + type RegexpuOptions = { + unicodeFlag?: "transform" | false; + unicodeSetsFlag?: "transform" | "parse" | false; + dotAllFlag?: "transform" | false; + unicodePropertyEscapes?: "transform" | false; + namedGroups?: "transform" | false; + onNamedGroup?: (name: string, index: number) => void; + }; + function rewritePattern( + pattern: string, + flags: string, + options: RegexpuOptions + ): string; + export = rewritePattern; + export { RegexpuOptions }; +} diff --git a/packages/babel-core/src/config/helpers/config-api.ts b/packages/babel-core/src/config/helpers/config-api.ts index 2b87b705dcb6..8c9dcf25b6cc 100644 --- a/packages/babel-core/src/config/helpers/config-api.ts +++ b/packages/babel-core/src/config/helpers/config-api.ts @@ -24,7 +24,7 @@ type CallerFactory = ( extractor: (callerMetadata: CallerMetadata | undefined) => unknown, ) => SimpleType; type TargetsFunction = () => Targets; -type AssumptionFunction = (name: AssumptionName) => boolean | void; +type AssumptionFunction = (name: AssumptionName) => boolean | undefined; export type ConfigAPI = { version: string; diff --git a/packages/babel-helper-create-class-features-plugin/src/decorators.ts b/packages/babel-helper-create-class-features-plugin/src/decorators.ts index bb80db63a7db..ef34aa56f399 100644 --- a/packages/babel-helper-create-class-features-plugin/src/decorators.ts +++ b/packages/babel-helper-create-class-features-plugin/src/decorators.ts @@ -108,9 +108,20 @@ function extractElementDescriptor( ].filter(Boolean); if (t.isClassMethod(node)) { - const id = node.computed ? null : node.key; - t.toExpression(node); - properties.push(prop("value", nameFunction({ node, id, scope }) || node)); + const id = node.computed + ? null + : (node.key as + | t.Identifier + | t.StringLiteral + | t.NumericLiteral + | t.BigIntLiteral); + const transformed = t.toExpression(node); + properties.push( + prop( + "value", + nameFunction({ node: transformed, id, scope }) || transformed, + ), + ); } else if (t.isClassProperty(node) && node.value) { properties.push( method("value", template.statements.ast`return ${node.value}`), diff --git a/packages/babel-helper-create-class-features-plugin/src/fields.ts b/packages/babel-helper-create-class-features-plugin/src/fields.ts index d938e0a9ce7a..db9c201f614c 100644 --- a/packages/babel-helper-create-class-features-plugin/src/fields.ts +++ b/packages/babel-helper-create-class-features-plugin/src/fields.ts @@ -490,7 +490,15 @@ export function transformPrivateNamesUsage( ref: t.Identifier, path: NodePath, privateNamesMap: PrivateNamesMap, - { privateFieldsAsProperties, noDocumentAll, innerBinding }, + { + privateFieldsAsProperties, + noDocumentAll, + innerBinding, + }: { + privateFieldsAsProperties: boolean; + noDocumentAll: boolean; + innerBinding: t.Identifier; + }, state: File, ) { if (!privateNamesMap.size) return; @@ -539,7 +547,7 @@ function buildPrivateInstanceFieldInitSpec( ref: t.Expression, prop: NodePath, privateNamesMap: PrivateNamesMap, - state, + state: File, ) { const { id } = privateNamesMap.get(prop.node.key.id.name); const value = prop.node.value || prop.scope.buildUndefinedNode(); @@ -646,7 +654,7 @@ function buildPrivateInstanceMethodInitSpec( ref: t.Expression, prop: NodePath, privateNamesMap: PrivateNamesMap, - state, + state: File, ) { const privateName = privateNamesMap.get(prop.node.key.id.name); const { getId, setId, initAdded } = privateName; @@ -675,7 +683,7 @@ function buildPrivateAccessorInitialization( ref: t.Expression, prop: NodePath, privateNamesMap: PrivateNamesMap, - state, + state: File, ) { const privateName = privateNamesMap.get(prop.node.key.id.name); const { id, getId, setId } = privateName; @@ -711,7 +719,7 @@ function buildPrivateInstanceMethodInitalization( ref: t.Expression, prop: NodePath, privateNamesMap: PrivateNamesMap, - state, + state: File, ) { const privateName = privateNamesMap.get(prop.node.key.id.name); const { id } = privateName; @@ -748,7 +756,7 @@ function buildPublicFieldInitLoose( function buildPublicFieldInitSpec( ref: t.Expression, prop: NodePath, - state, + state: File, ) { const { key, computed } = prop.node; const value = prop.node.value || prop.scope.buildUndefinedNode(); @@ -767,7 +775,7 @@ function buildPublicFieldInitSpec( function buildPrivateStaticMethodInitLoose( ref: t.Expression, prop: NodePath, - state, + state: File, privateNamesMap: PrivateNamesMap, ) { const privateName = privateNamesMap.get(prop.node.key.id.name); 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 34bd564024ed..060a3638f94d 100644 --- a/packages/babel-helper-create-regexp-features-plugin/src/index.ts +++ b/packages/babel-helper-create-regexp-features-plugin/src/index.ts @@ -1,6 +1,7 @@ import rewritePattern from "regexpu-core"; import { featuresKey, FEATURES, enableFeature, runtimeKey } from "./features"; import { generateRegexpuOptions, canSkipRegexpu, transformFlags } from "./util"; +import type { NodePath } from "@babel/traverse"; import { types as t } from "@babel/core"; import type { PluginObject } from "@babel/core"; @@ -17,12 +18,22 @@ const version = PACKAGE_JSON.version .reduce((v, x) => v * 1e5 + +x, 0); const versionKey = "@babel/plugin-regexp-features/version"; +export interface Options { + name: string; + feature: keyof typeof FEATURES; + options?: { + useUnicodeFlag?: boolean; + runtime?: boolean; + }; + manipulateOptions?: PluginObject["manipulateOptions"]; +} + export function createRegExpFeaturePlugin({ name, feature, - options = {} as any, - manipulateOptions = (() => {}) as PluginObject["manipulateOptions"], -}): PluginObject { + options = {}, + manipulateOptions = () => {}, +}: Options): PluginObject { return { name, @@ -60,7 +71,7 @@ export function createRegExpFeaturePlugin({ const regexpuOptions = generateRegexpuOptions(features); if (canSkipRegexpu(node, regexpuOptions)) return; - const namedCaptureGroups = {}; + const namedCaptureGroups: Record = {}; if (regexpuOptions.namedGroups === "transform") { regexpuOptions.onNamedGroup = (name, index) => { namedCaptureGroups[name] = index; @@ -90,7 +101,7 @@ export function createRegExpFeaturePlugin({ }; } -function isRegExpTest(path) { +function isRegExpTest(path: NodePath) { return ( path.parentPath.isMemberExpression({ object: path.node, diff --git a/packages/babel-helper-create-regexp-features-plugin/src/util.ts b/packages/babel-helper-create-regexp-features-plugin/src/util.ts index b8126ad009b3..b4c7e5251dfc 100644 --- a/packages/babel-helper-create-regexp-features-plugin/src/util.ts +++ b/packages/babel-helper-create-regexp-features-plugin/src/util.ts @@ -1,14 +1,7 @@ import type { types as t } from "@babel/core"; import { FEATURES, hasFeature } from "./features"; -type RegexpuOptions = { - unicodeFlag: "transform" | false; - unicodeSetsFlag: "transform" | "parse" | false; - dotAllFlag: "transform" | false; - unicodePropertyEscapes: "transform" | false; - namedGroups: "transform" | false; - onNamedGroup: (name: string, index: number) => void; -}; +import type { RegexpuOptions } from "regexpu-core"; export function generateRegexpuOptions(toTransform: number): RegexpuOptions { type Experimental = 1; diff --git a/packages/babel-helper-function-name/src/index.ts b/packages/babel-helper-function-name/src/index.ts index 623b541e4ad1..bd842d3ed636 100644 --- a/packages/babel-helper-function-name/src/index.ts +++ b/packages/babel-helper-function-name/src/index.ts @@ -102,7 +102,7 @@ function getNameFromLiteralId(id: t.Literal) { function wrap( state: State, - method: t.FunctionExpression | t.ClassExpression, + method: t.FunctionExpression | t.Class, id: t.Identifier, scope: Scope, ) { @@ -148,7 +148,7 @@ function wrap( } function visit( - node: t.FunctionExpression | t.ClassExpression, + node: t.FunctionExpression | t.Class, name: string, scope: Scope, ) { @@ -201,21 +201,21 @@ function visit( * @param {Boolean} localBinding whether a name could shadow a self-reference (e.g. converting arrow function) * @param {Boolean} supportUnicodeId whether a target support unicodeId or not */ -export default function ( +export default function ( { node, parent, scope, id, }: { - node: t.FunctionExpression | t.ClassExpression; + node: N; parent?: t.Node; scope: Scope; id?: any; }, localBinding = false, supportUnicodeId = false, -) { +): t.CallExpression | N { // has an `id` so we don't need to infer one if (node.id) return; diff --git a/packages/babel-plugin-transform-flow-comments/src/index.ts b/packages/babel-plugin-transform-flow-comments/src/index.ts index 666e2c8f0e11..db0573c27ff2 100644 --- a/packages/babel-plugin-transform-flow-comments/src/index.ts +++ b/packages/babel-plugin-transform-flow-comments/src/index.ts @@ -7,7 +7,7 @@ import type { NodePath } from "@babel/traverse"; export default declare(api => { api.assertVersion(7); - function commentFromString(comment) { + function commentFromString(comment: string | t.Comment): t.Comment { return typeof comment === "string" ? { type: "CommentBlock", value: comment } : comment; @@ -21,12 +21,12 @@ export default declare(api => { comments = generateComment(ofPath, optional), keepType = false, }: { - ofPath?; - toPath?; - where?: string; - optional?; - comments?; - keepType?; + ofPath?: NodePath; + toPath?: NodePath; + where?: t.CommentTypeShorthand; + optional?: boolean; + comments?: string | t.Comment | (string | t.Comment)[]; + keepType?: boolean; }) { if (!toPath?.node) { toPath = ofPath.getPrevSibling(); @@ -43,7 +43,7 @@ export default declare(api => { if (!Array.isArray(comments)) { comments = [comments]; } - comments = comments.map(commentFromString); + const newComments = comments.map(commentFromString); if (!keepType && ofPath?.node) { // Removes the node at `ofPath` while conserving the comments attached // to it. @@ -58,24 +58,28 @@ export default declare(api => { if (isSingleChild && leading) { parent.addComments("inner", leading); } - toPath.addComments(where, comments); + toPath.addComments(where, newComments); ofPath.remove(); if (isSingleChild && trailing) { parent.addComments("inner", trailing); } } else { - toPath.addComments(where, comments); + toPath.addComments(where, newComments); } } - function wrapInFlowComment(path) { + function wrapInFlowComment(path: NodePath) { attachComment({ ofPath: path, - comments: generateComment(path, path.parent.optional), + comments: generateComment( + path, + // @ts-ignore + path.parent.optional, + ), }); } - function generateComment(path, optional?) { + function generateComment(path: NodePath, optional?: boolean | void) { let comment = path .getSource() .replace(/\*-\//g, "*-ESCAPED/") @@ -85,7 +89,7 @@ export default declare(api => { return comment; } - function isTypeImport(importKind) { + function isTypeImport(importKind: "type" | "typeof" | "value") { return importKind === "type" || importKind === "typeof"; } @@ -236,7 +240,11 @@ export default declare(api => { } }, - Flow(path) { + Flow( + path: NodePath< + t.Flow | t.ImportDeclaration | t.ExportDeclaration | t.ImportSpecifier + >, + ) { wrapInFlowComment(path); }, 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 8030a99f9c77..aaffbfc3d6cd 100644 --- a/packages/babel-plugin-transform-flow-strip-types/src/index.ts +++ b/packages/babel-plugin-transform-flow-strip-types/src/index.ts @@ -1,6 +1,7 @@ import { declare } from "@babel/helper-plugin-utils"; import syntaxFlow from "@babel/plugin-syntax-flow"; import { types as t } from "@babel/core"; +import type { NodePath } from "@babel/traverse"; export interface Options { requireDirective?: boolean; @@ -75,7 +76,11 @@ export default declare((api, opts: Options) => { } }, - Flow(path) { + Flow( + path: NodePath< + t.Flow | t.ImportDeclaration | t.ExportDeclaration | t.ImportSpecifier + >, + ) { if (skipStrip) { throw path.buildCodeFrameError( "A @flow directive is required when using Flow annotations with " + diff --git a/packages/babel-plugin-transform-for-of/src/index.ts b/packages/babel-plugin-transform-for-of/src/index.ts index b574640d7c5a..edce0e1baca9 100644 --- a/packages/babel-plugin-transform-for-of/src/index.ts +++ b/packages/babel-plugin-transform-for-of/src/index.ts @@ -154,15 +154,17 @@ export default declare((api, options: Options) => { ? { build: buildForOfNoIteratorClosing, helper: "createForOfIteratorHelperLoose", - getContainer: nodes => nodes, + getContainer: (nodes: t.Statement[]): [t.ForStatement] => + nodes as [t.ForStatement], } : { build: buildForOf, helper: "createForOfIteratorHelper", - getContainer: nodes => nodes[1].block.body, + getContainer: (nodes: t.Statement[]): [t.ForStatement] => + (nodes[1] as t.TryStatement).block.body as [t.ForStatement], }; - function _ForOfStatementArray(path) { + function _ForOfStatementArray(path: NodePath) { const { node, scope } = path; const right = scope.generateUidIdentifierBasedOnNode(node.right, "arr"); @@ -257,6 +259,7 @@ export default declare((api, options: Options) => { t.inherits(container[0].body, node.body); if (t.isLabeledStatement(parent)) { + // @ts-expect-error replacing node types container[0] = t.labeledStatement(parent.label, container[0]); path.parentPath.replaceWithMultiple(nodes); diff --git a/packages/babel-plugin-transform-for-of/src/no-helper-implementation.ts b/packages/babel-plugin-transform-for-of/src/no-helper-implementation.ts index d84143b6e338..fbf4afc5e8ae 100644 --- a/packages/babel-plugin-transform-for-of/src/no-helper-implementation.ts +++ b/packages/babel-plugin-transform-for-of/src/no-helper-implementation.ts @@ -1,10 +1,15 @@ -import { template, types as t } from "@babel/core"; +import { type PluginPass, template, types as t } from "@babel/core"; +import type { NodePath } from "@babel/traverse"; // This is the legacy implementation, which inlines all the code. // It must be kept for compatibility reasons. // TODO (Babel 8): Remove this code. -export default function transformWithoutHelper(loose, path, state) { +export default function transformWithoutHelper( + loose: boolean | void, + path: NodePath, + state: PluginPass, +) { const pushComputedProps = loose ? pushComputedPropsLoose : pushComputedPropsSpec; @@ -13,7 +18,7 @@ export default function transformWithoutHelper(loose, path, state) { const build = pushComputedProps(path, state); const declar = build.declar; const loop = build.loop; - 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(); @@ -24,7 +29,7 @@ export default function transformWithoutHelper(loose, path, state) { } // 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); @@ -37,7 +42,7 @@ export default function transformWithoutHelper(loose, path, state) { } } -const buildForOfLoose = template(` +const buildForOfLoose = template.statement(` for (var LOOP_OBJECT = OBJECT, IS_ARRAY = Array.isArray(LOOP_OBJECT), INDEX = 0, @@ -54,7 +59,7 @@ const buildForOfLoose = template(` } `); -const buildForOf = template(` +const buildForOf = template.statements(` var ITERATOR_COMPLETION = true; var ITERATOR_HAD_ERROR_KEY = false; var ITERATOR_ERROR_KEY = undefined; @@ -80,7 +85,10 @@ const buildForOf = template(` } `); -function pushComputedPropsLoose(path, file) { +function pushComputedPropsLoose( + path: NodePath, + state: PluginPass, +) { const { node, scope, parent } = path; const { left } = node; let declar, id, intermediate; @@ -99,7 +107,7 @@ function pushComputedPropsLoose(path, file) { t.variableDeclarator(t.identifier(id.name)), ]); } else { - throw file.buildCodeFrameError( + throw state.buildCodeFrameError( left, `Unknown node type ${left.type} in ForStatement`, ); @@ -115,7 +123,7 @@ function pushComputedPropsLoose(path, file) { INDEX: scope.generateUidIdentifier("i"), ID: id, INTERMEDIATE: intermediate, - }) as t.Statement; + }) as t.ForStatement; // const isLabeledParent = t.isLabeledStatement(parent); @@ -133,7 +141,10 @@ function pushComputedPropsLoose(path, file) { }; } -function pushComputedPropsSpec(path, file) { +function pushComputedPropsSpec( + path: NodePath, + state: PluginPass, +) { const { node, scope, parent } = path; const left = node.left; let declar; @@ -155,7 +166,7 @@ function pushComputedPropsSpec(path, file) { t.variableDeclarator(left.declarations[0].id, stepValue), ]); } else { - throw file.buildCodeFrameError( + throw state.buildCodeFrameError( left, `Unknown node type ${left.type} in ForStatement`, ); @@ -174,8 +185,8 @@ function pushComputedPropsSpec(path, file) { 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-transform-function-name/src/index.ts b/packages/babel-plugin-transform-function-name/src/index.ts index 7bdae7fca350..35600a9215fc 100644 --- a/packages/babel-plugin-transform-function-name/src/index.ts +++ b/packages/babel-plugin-transform-function-name/src/index.ts @@ -25,7 +25,12 @@ export default declare(api => { ObjectProperty(path) { const value = path.get("value"); if (value.isFunction()) { - const newNode = nameFunction(value, false, supportUnicodeId); + const newNode = nameFunction( + // @ts-ignore Fixme: should check ArrowFunctionExpression + value, + false, + supportUnicodeId, + ); if (newNode) value.replaceWith(newNode); } }, diff --git a/packages/babel-plugin-transform-object-super/src/index.ts b/packages/babel-plugin-transform-object-super/src/index.ts index b375f575f92f..86df844f2cf1 100644 --- a/packages/babel-plugin-transform-object-super/src/index.ts +++ b/packages/babel-plugin-transform-object-super/src/index.ts @@ -1,8 +1,13 @@ import { declare } from "@babel/helper-plugin-utils"; import ReplaceSupers from "@babel/helper-replace-supers"; -import { types as t } from "@babel/core"; - -function replacePropertySuper(path, getObjectRef, file) { +import { types as t, type File } from "@babel/core"; +import type { NodePath } from "@babel/traverse"; + +function replacePropertySuper( + path: NodePath, + getObjectRef: () => t.Identifier, + file: File, +) { // @ts-expect-error todo(flow->ts): const replaceSupers = new ReplaceSupers({ getObjectRef: getObjectRef, @@ -21,14 +26,14 @@ export default declare(api => { visitor: { ObjectExpression(path, state) { - let objectRef; + let objectRef: t.Identifier; const getObjectRef = () => (objectRef = objectRef || path.scope.generateUidIdentifier("obj")); path.get("properties").forEach(propPath => { if (!propPath.isMethod()) return; - replacePropertySuper(propPath, getObjectRef, state); + replacePropertySuper(propPath, getObjectRef, state.file); }); if (objectRef) { 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 bd29c5933ff2..1bd61aeeb755 100644 --- a/packages/babel-plugin-transform-proto-to-assign/src/index.ts +++ b/packages/babel-plugin-transform-proto-to-assign/src/index.ts @@ -1,22 +1,35 @@ import { declare } from "@babel/helper-plugin-utils"; -import { types as t } from "@babel/core"; +import { types as t, type File } from "@babel/core"; export default declare(api => { api.assertVersion(7); - function isProtoKey(node) { - return t.isLiteral(t.toComputedKey(node, node.key), { value: "__proto__" }); + function isProtoKey(node: t.ObjectExpression["properties"][number]) { + return ( + !t.isSpreadElement(node) && + t.isStringLiteral(t.toComputedKey(node, node.key), { + value: "__proto__", + }) + ); } - function isProtoAssignmentExpression(node): node is t.MemberExpression { + function isProtoAssignmentExpression( + node: t.Node, + ): node is t.MemberExpression { const left = node; return ( t.isMemberExpression(left) && - t.isLiteral(t.toComputedKey(left, left.property), { value: "__proto__" }) + t.isStringLiteral(t.toComputedKey(left, left.property), { + value: "__proto__", + }) ); } - function buildDefaultsCallExpression(expr, ref, file) { + function buildDefaultsCallExpression( + expr: t.AssignmentExpression, + ref: t.MemberExpression["object"], + file: File, + ) { return t.expressionStatement( t.callExpression(file.addHelper("defaults"), [ref, expr.right]), ); @@ -26,7 +39,7 @@ export default declare(api => { name: "transform-proto-to-assign", visitor: { - AssignmentExpression(path, file) { + AssignmentExpression(path, { file }) { if (!isProtoAssignmentExpression(path.node.left)) return; const nodes = []; @@ -50,7 +63,7 @@ export default declare(api => { path.replaceWithMultiple(nodes); }, - ExpressionStatement(path, file) { + ExpressionStatement(path, { file }) { const expr = path.node.expression; if (!t.isAssignmentExpression(expr, { operator: "=" })) return; @@ -61,7 +74,7 @@ export default declare(api => { } }, - ObjectExpression(path, file) { + ObjectExpression(path, { file }) { let proto; const { node } = path; const { properties } = node; 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 c759d49e6533..bc1a89f3fe0b 100644 --- a/packages/babel-plugin-transform-react-constant-elements/src/index.ts +++ b/packages/babel-plugin-transform-react-constant-elements/src/index.ts @@ -29,7 +29,7 @@ export default declare((api, options: Options) => { // Element -> Target scope const HOISTED = new WeakMap(); - function declares(node: t.Identifier | t.JSXIdentifier, scope) { + function declares(node: t.Identifier | t.JSXIdentifier, scope: Scope) { if ( t.isJSXIdentifier(node, { name: "this" }) || t.isJSXIdentifier(node, { name: "arguments" }) || @@ -43,11 +43,11 @@ export default declare((api, options: Options) => { return scope.hasOwnBinding(node.name); } - function isHoistingScope({ path }) { + function isHoistingScope({ path }: Scope) { return path.isFunctionParent() || path.isLoop() || path.isProgram(); } - function getHoistingScope(scope) { + function getHoistingScope(scope: Scope) { while (!isHoistingScope(scope)) scope = scope.parent; return scope; } diff --git a/packages/babel-plugin-transform-unicode-escapes/src/index.ts b/packages/babel-plugin-transform-unicode-escapes/src/index.ts index f21e9cfdaf5c..da962b899f38 100644 --- a/packages/babel-plugin-transform-unicode-escapes/src/index.ts +++ b/packages/babel-plugin-transform-unicode-escapes/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); @@ -7,7 +8,7 @@ export default declare(api => { const surrogate = /[\ud800-\udfff]/g; const unicodeEscape = /(\\+)u\{([0-9a-fA-F]+)\}/g; - function escape(code) { + function escape(code: number) { let str = code.toString(16); // Sigh, node 6 doesn't have padStart // TODO: Remove in Babel 8, when we drop node 6. @@ -15,7 +16,7 @@ export default declare(api => { return "\\u" + str; } - function replacer(match, backslashes, code) { + function replacer(match: string, backslashes: string[], code: string) { if (backslashes.length % 2 === 0) { return match; } @@ -26,11 +27,11 @@ export default declare(api => { return char.length === 1 ? escaped : escaped + escape(char.charCodeAt(1)); } - function replaceUnicodeEscapes(str) { + function replaceUnicodeEscapes(str: string) { return str.replace(unicodeEscape, replacer); } - function getUnicodeEscape(str) { + function getUnicodeEscape(str: string) { let match; while ((match = unicodeEscape.exec(str))) { if (match[1].length % 2 === 0) continue; @@ -88,11 +89,13 @@ export default declare(api => { ); }, - "StringLiteral|DirectiveLiteral"(path) { + "StringLiteral|DirectiveLiteral"( + path: NodePath, + ) { const { node } = path; const { extra } = node; - if (extra?.raw) extra.raw = replaceUnicodeEscapes(extra.raw); + if (extra?.raw) extra.raw = replaceUnicodeEscapes(extra.raw as string); }, TemplateElement(path) { diff --git a/packages/babel-traverse/src/path/replacement.ts b/packages/babel-traverse/src/path/replacement.ts index bf7f388d2d9c..25dc6ad86f09 100644 --- a/packages/babel-traverse/src/path/replacement.ts +++ b/packages/babel-traverse/src/path/replacement.ts @@ -40,7 +40,7 @@ import hoistVariables from "@babel/helper-hoist-variables"; export function replaceWithMultiple( this: NodePath, - nodes: t.Node[], + nodes: t.Node | t.Node[], ): NodePath[] { this.resync(); diff --git a/packages/babel-types/scripts/generators/ast-types.js b/packages/babel-types/scripts/generators/ast-types.js index 49b67bba38a8..b0c14ff3410e 100644 --- a/packages/babel-types/scripts/generators/ast-types.js +++ b/packages/babel-types/scripts/generators/ast-types.js @@ -7,9 +7,9 @@ export default function generateAstTypes() { interface BaseComment { value: string; - start: number; - end: number; - loc: SourceLocation; + start?: number; + end?: number; + loc?: SourceLocation; type: "CommentBlock" | "CommentLine"; } diff --git a/packages/babel-types/src/ast-types/generated/index.ts b/packages/babel-types/src/ast-types/generated/index.ts index a13ee342a030..a230acd945ec 100644 --- a/packages/babel-types/src/ast-types/generated/index.ts +++ b/packages/babel-types/src/ast-types/generated/index.ts @@ -3,9 +3,9 @@ interface BaseComment { value: string; - start: number; - end: number; - loc: SourceLocation; + start?: number; + end?: number; + loc?: SourceLocation; type: "CommentBlock" | "CommentLine"; } diff --git a/scripts/generators/tsconfig.js b/scripts/generators/tsconfig.js index 4afca0f767c4..37f2156e421b 100644 --- a/scripts/generators/tsconfig.js +++ b/scripts/generators/tsconfig.js @@ -117,6 +117,7 @@ fs.writeFileSync( ["./lib/babel-plugin-dynamic-import-node.d.ts"], ], ["globals", ["./node_modules/globals-BABEL_8_BREAKING-true"]], + ["regexpu-core", ["./lib/regexpu-core.d.ts"]], ]), }, }, diff --git a/tsconfig.json b/tsconfig.json index 86f6aec802c9..8fce7ecacb84 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -701,6 +701,9 @@ ], "globals": [ "./node_modules/globals-BABEL_8_BREAKING-true" + ], + "regexpu-core": [ + "./lib/regexpu-core.d.ts" ] } }