diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 783fad4ae651..1c75255ca3a6 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -43,7 +43,12 @@ module.exports = { "@typescript-eslint/no-dupe-class-members": "error", "no-undef": "off", "no-redeclare": "off", - "@babel/development-internal/disallow-ts-ignore": "error", + "@typescript-eslint/ban-ts-comment": [ + "error", + { + "ts-ignore": { descriptionFormat: "^\\(Babel 7 vs Babel 8\\) .+$" }, + }, + ], "@typescript-eslint/consistent-type-imports": [ "error", { disallowTypeAnnotations: false }, diff --git a/eslint/babel-eslint-plugin-development-internal/src/index.cjs b/eslint/babel-eslint-plugin-development-internal/src/index.cjs index 76e6f0821575..f5053c3354a1 100644 --- a/eslint/babel-eslint-plugin-development-internal/src/index.cjs +++ b/eslint/babel-eslint-plugin-development-internal/src/index.cjs @@ -1,7 +1,6 @@ const rules = { "report-error-message-format": require("./rules/report-error-message-format.cjs"), "require-default-import-fallback": require("./rules/require-default-import-fallback.cjs"), - "disallow-ts-ignore": require("./rules/disallow-ts-ignore.cjs"), }; exports.rules = rules; diff --git a/eslint/babel-eslint-plugin-development-internal/src/rules/disallow-ts-ignore.cjs b/eslint/babel-eslint-plugin-development-internal/src/rules/disallow-ts-ignore.cjs deleted file mode 100644 index d794649c13ee..000000000000 --- a/eslint/babel-eslint-plugin-development-internal/src/rules/disallow-ts-ignore.cjs +++ /dev/null @@ -1,29 +0,0 @@ -module.exports = { - meta: { - type: "suggestion", - docs: { - description: "", - }, - fixable: "code", - }, - create(ctx) { - const sourceCode = ctx.getSourceCode(); - - return { - Program() { - for (const comment of sourceCode.getAllComments()) { - if ( - /^\s*@ts-ignore(?!\(Babel \d vs Babel \d\))/.test(comment.value) - ) { - ctx.report({ - node: comment, - message: - "`@ts-ignore` is only allowed for differences between Babel 7 and Babel 8, and it must be marked" + - " as `@ts-ignore(Babel 7 vs Babel 8)`. Use `@ts-expect-error` instead.", - }); - } - } - }, - }; - }, -}; diff --git a/packages/babel-core/src/config/files/plugins.ts b/packages/babel-core/src/config/files/plugins.ts index 6a3cbafe96fe..a02a096b62f7 100644 --- a/packages/babel-core/src/config/files/plugins.ts +++ b/packages/babel-core/src/config/files/plugins.ts @@ -100,7 +100,7 @@ function* resolveAlternativesHelper( const { error, value } = yield standardizedName; if (!error) return value; - // @ts-expect-error + // @ts-expect-error code may not index error if (error.code !== "MODULE_NOT_FOUND") throw error; if (standardizedName !== name && !(yield name).error) { diff --git a/packages/babel-core/src/config/util.ts b/packages/babel-core/src/config/util.ts index 76c7a7db7171..acad9e3cfbbe 100644 --- a/packages/babel-core/src/config/util.ts +++ b/packages/babel-core/src/config/util.ts @@ -13,9 +13,9 @@ export function mergeOptions( const targetObj = target[k] || (target[k] = {}); mergeDefaultFields(targetObj, parserOpts); } else { - //@ts-expect-error + //@ts-expect-error k must index source const val = source[k]; - //@ts-expect-error + //@ts-expect-error assigning source to target if (val !== undefined) target[k] = val as any; } } diff --git a/packages/babel-core/src/config/validation/options.ts b/packages/babel-core/src/config/validation/options.ts index 0895188b0f61..d79ea0fe1675 100644 --- a/packages/babel-core/src/config/validation/options.ts +++ b/packages/babel-core/src/config/validation/options.ts @@ -456,8 +456,7 @@ function assertOverridesList( validateNested(overridesLoc, env); } } - // @ts-expect-error - return arr; + return arr as OverridesList; } export function checkNoUnwrappedItemOptionPairs( diff --git a/packages/babel-core/src/errors/rewrite-stack-trace.ts b/packages/babel-core/src/errors/rewrite-stack-trace.ts index 954593f40da8..b1160782c9b3 100644 --- a/packages/babel-core/src/errors/rewrite-stack-trace.ts +++ b/packages/babel-core/src/errors/rewrite-stack-trace.ts @@ -125,8 +125,7 @@ export function endHiddenCallStack( } function setupPrepareStackTrace() { - // This function is a singleton - // @ts-expect-error + // @ts-expect-error This function is a singleton // eslint-disable-next-line no-func-assign setupPrepareStackTrace = () => {}; diff --git a/packages/babel-core/src/transformation/file/file.ts b/packages/babel-core/src/transformation/file/file.ts index 2badd1d14a8e..cf687fb0b1ef 100644 --- a/packages/babel-core/src/transformation/file/file.ts +++ b/packages/babel-core/src/transformation/file/file.ts @@ -210,7 +210,7 @@ export default class File { }); nodes.forEach(node => { - // @ts-expect-error + // @ts-expect-error Fixeme: document _compact node property node._compact = true; }); diff --git a/packages/babel-generator/src/generators/modules.ts b/packages/babel-generator/src/generators/modules.ts index f40ba76b2604..08c47bc17d9f 100644 --- a/packages/babel-generator/src/generators/modules.ts +++ b/packages/babel-generator/src/generators/modules.ts @@ -224,12 +224,12 @@ export function ImportDeclaration(this: Printer, node: t.ImportDeclaration) { this.printAssertions(node); if (!process.env.BABEL_8_BREAKING) { - // @ts-expect-error + // @ts-ignore(Babel 7 vs Babel 8) Babel 7 supports module attributes if (node.attributes?.length) { this.space(); this.word("with"); this.space(); - // @ts-expect-error + // @ts-ignore(Babel 7 vs Babel 8) Babel 7 supports module attributes this.printList(node.attributes, node); } } diff --git a/packages/babel-generator/src/printer.ts b/packages/babel-generator/src/printer.ts index b88980fde5d9..d67fc5f69b18 100644 --- a/packages/babel-generator/src/printer.ts +++ b/packages/babel-generator/src/printer.ts @@ -862,7 +862,7 @@ class Printer { Object.assign(Printer.prototype, generatorFunctions); if (!process.env.BABEL_8_BREAKING) { - // @ts-ignore(Babel 7 vs Babel 8) + // @ts-ignore(Babel 7 vs Babel 8) Babel 7 has Noop print method Printer.prototype.Noop = function Noop(this: Printer) {}; } diff --git a/packages/babel-helper-builder-react-jsx/src/index.ts b/packages/babel-helper-builder-react-jsx/src/index.ts index 28cb4e3d865d..f0046ab2b82b 100644 --- a/packages/babel-helper-builder-react-jsx/src/index.ts +++ b/packages/babel-helper-builder-react-jsx/src/index.ts @@ -96,8 +96,9 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`, if (node.name === "this" && isReferenced(node, parent)) { return thisExpression(); } else if (isValidIdentifier(node.name, false)) { - // @ts-expect-error todo(flow->ts) avoid type unsafe mutations + // @ts-expect-error casting JSXIdentifier to Identifier node.type = "Identifier"; + return node as unknown as t.Identifier; } else { return stringLiteral(node.name); } @@ -114,7 +115,6 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`, return stringLiteral(`${node.namespace.name}:${node.name.name}`); } - // @ts-expect-error return node; } diff --git a/packages/babel-helper-compilation-targets/src/index.ts b/packages/babel-helper-compilation-targets/src/index.ts index 27c54531e98b..2cce6d1f53d4 100644 --- a/packages/babel-helper-compilation-targets/src/index.ts +++ b/packages/babel-helper-compilation-targets/src/index.ts @@ -21,7 +21,7 @@ import type { TargetsTuple, } from "./types"; -export type { Targets, InputTargets }; +export type { Target, Targets, InputTargets }; export { prettifyTargets } from "./pretty"; export { getInclusionReasons } from "./debug"; diff --git a/packages/babel-helper-create-class-features-plugin/src/features.ts b/packages/babel-helper-create-class-features-plugin/src/features.ts index 7a7ded5cf5fb..7b3902b0dea9 100644 --- a/packages/babel-helper-create-class-features-plugin/src/features.ts +++ b/packages/babel-helper-create-class-features-plugin/src/features.ts @@ -49,14 +49,14 @@ export function enableFeature(file: File, feature: number, loose: boolean) { if (!hasFeature(file, feature) || canIgnoreLoose(file, feature)) { file.set(featuresKey, file.get(featuresKey) | feature); if ( - // @ts-expect-error + // @ts-expect-error comparing loose with internal private magic string loose === "#__internal__@babel/preset-env__prefer-true-but-false-is-ok-if-it-prevents-an-error" ) { setLoose(file, feature, true); file.set(looseLowPriorityKey, file.get(looseLowPriorityKey) | feature); } else if ( - // @ts-expect-error + // @ts-expect-error comparing loose with internal private magic string loose === "#__internal__@babel/preset-env__prefer-false-but-true-is-ok-if-it-prevents-an-error" ) { 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 1fdb05173d0d..6705416de160 100644 --- a/packages/babel-helper-create-class-features-plugin/src/index.ts +++ b/packages/babel-helper-create-class-features-plugin/src/index.ts @@ -295,9 +295,8 @@ export function createClassFeaturePlugin({ // class Foo {} export { Foo as default } splitExportDeclaration(path); } else { - // Annyms class declarations can be + // @ts-expect-error Anonymous class declarations can be // transformed as if they were expressions - // @ts-expect-error decl.node.type = "ClassExpression"; } } diff --git a/packages/babel-helper-module-transforms/src/normalize-and-load-metadata.ts b/packages/babel-helper-module-transforms/src/normalize-and-load-metadata.ts index ac224e7ecf60..4aba0c252534 100644 --- a/packages/babel-helper-module-transforms/src/normalize-and-load-metadata.ts +++ b/packages/babel-helper-module-transforms/src/normalize-and-load-metadata.ts @@ -434,13 +434,12 @@ function getLocalExportMetadata( ): Map { const bindingKindLookup = new Map(); - programPath.get("body").forEach(child => { + programPath.get("body").forEach((child: NodePath) => { let kind: ModuleBindingKind; if (child.isImportDeclaration()) { kind = "import"; } else { if (child.isExportDefaultDeclaration()) { - // @ts-expect-error child = child.get("declaration"); } if (child.isExportNamedDeclaration()) { diff --git a/packages/babel-helper-plugin-utils/src/index.ts b/packages/babel-helper-plugin-utils/src/index.ts index ecf4857fd742..1ccaacdc838b 100644 --- a/packages/babel-helper-plugin-utils/src/index.ts +++ b/packages/babel-helper-plugin-utils/src/index.ts @@ -31,7 +31,7 @@ export function declare( clonedApi[name] = apiPolyfills[name](clonedApi); } - // @ts-expect-error + // @ts-expect-error options || {} may not be assigned to Options return builder(clonedApi ?? api, options || {}, dirname); }; } diff --git a/packages/babel-parser/src/parser/expression.ts b/packages/babel-parser/src/parser/expression.ts index adc926d3e46a..b80ea21508f1 100644 --- a/packages/babel-parser/src/parser/expression.ts +++ b/packages/babel-parser/src/parser/expression.ts @@ -129,7 +129,7 @@ export default abstract class ExpressionParser extends LValParser { prop.type === "SpreadElement" || this.isObjectMethod(prop) || prop.computed || - // @ts-expect-error + // @ts-expect-error prop must be an ObjectProperty prop.shorthand ) { return; @@ -2123,7 +2123,7 @@ export default abstract class ExpressionParser extends LValParser { this.raise(Errors.InvalidRecordProperty, { at: prop }); } - // @ts-expect-error + // @ts-expect-error shorthand may not index prop if (prop.shorthand) { this.addExtra(prop, "shorthand", true); } @@ -2644,9 +2644,9 @@ export default abstract class ExpressionParser extends LValParser { // This logic is here to align the error location with the ESTree plugin. this.raise(Errors.IllegalLanguageModeDirective, { at: - // @ts-expect-error + // @ts-expect-error kind may not index node (node.kind === "method" || node.kind === "constructor") && - // @ts-expect-error + // @ts-expect-error key may not index node !!node.key ? // @ts-expect-error node.key has been gaurded node.key.loc.end diff --git a/packages/babel-parser/src/parser/lval.ts b/packages/babel-parser/src/parser/lval.ts index dc26e1eab708..a1312a7b0e0f 100644 --- a/packages/babel-parser/src/parser/lval.ts +++ b/packages/babel-parser/src/parser/lval.ts @@ -664,9 +664,7 @@ export default abstract class LValParser extends NodeUtils { ? expression : ancestor; - // Flow has difficulty tracking `key` and `expression`, but only if we use - // null-proto objects. If we use normal objects, everything works fine. - // @ts-expect-error + // @ts-expect-error key may not index expression. for (const child of [].concat(expression[key])) { if (child) { this.checkLVal(child, { diff --git a/packages/babel-parser/src/parser/node.ts b/packages/babel-parser/src/parser/node.ts index 293b46675986..7649ffc0b89c 100644 --- a/packages/babel-parser/src/parser/node.ts +++ b/packages/babel-parser/src/parser/node.ts @@ -29,11 +29,10 @@ class Node implements NodeBase { const NodePrototype = Node.prototype; if (!process.env.BABEL_8_BREAKING) { - // @ts-expect-error + // @ts-expect-error __clone is not defined in Node prototype NodePrototype.__clone = function (): Node { - // @ts-expect-error - const newNode: any = new Node(); - const keys = Object.keys(this); + const newNode = new Node(undefined, this.start, this.loc.start); + const keys = Object.keys(this) as (keyof Node)[]; for (let i = 0, length = keys.length; i < length; i++) { const key = keys[i]; // Do not clone comments that are already attached to the node @@ -42,9 +41,8 @@ if (!process.env.BABEL_8_BREAKING) { key !== "trailingComments" && key !== "innerComments" ) { - newNode[key] = - // @ts-expect-error: key must present in this - this[key]; + // @ts-expect-error cloning this to newNode + newNode[key] = this[key]; } } @@ -100,12 +98,12 @@ export type Undone = Omit; export abstract class NodeUtils extends UtilParser { startNode(): Undone { - // @ts-expect-error + // @ts-expect-error cast Node as Undone return new Node(this, this.state.start, this.state.startLoc); } startNodeAt(pos: number, loc: Position): Undone { - // @ts-expect-error + // @ts-expect-error cast Node as Undone return new Node(this, pos, loc); } diff --git a/packages/babel-parser/src/parser/statement.ts b/packages/babel-parser/src/parser/statement.ts index 284afe45e911..a3e13fed7695 100644 --- a/packages/babel-parser/src/parser/statement.ts +++ b/packages/babel-parser/src/parser/statement.ts @@ -2591,10 +2591,9 @@ export default abstract class StatementParser extends ExpressionParser { ) { // @ts-expect-error Fixme: node.type must be undefined because they are undone if (this.isJSONModuleImport(node) && node.type !== "ExportAllDeclaration") { - // @ts-expect-error + // @ts-expect-error specifiers may not index node const { specifiers } = node; - // @ts-expect-error - if (node.specifiers != null) { + if (specifiers != null) { // @ts-expect-error refine specifier types const nonDefaultNamedSpecifier = specifiers.find(specifier => { let imported; diff --git a/packages/babel-parser/src/plugin-utils.ts b/packages/babel-parser/src/plugin-utils.ts index bd9b009b1e57..cc5943462116 100644 --- a/packages/babel-parser/src/plugin-utils.ts +++ b/packages/babel-parser/src/plugin-utils.ts @@ -207,8 +207,8 @@ export function validatePlugins(plugins: PluginList) { const error = new Error( "'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.", ); - // @ts-expect-error - error.missingPlugins = "doExpressions"; // so @babel/core can provide better error message + // @ts-expect-error so @babel/core can provide better error message + error.missingPlugins = "doExpressions"; throw error; } } diff --git a/packages/babel-parser/src/plugins/estree.ts b/packages/babel-parser/src/plugins/estree.ts index 4192f21ad77c..2a23979dfa33 100644 --- a/packages/babel-parser/src/plugins/estree.ts +++ b/packages/babel-parser/src/plugins/estree.ts @@ -202,7 +202,7 @@ export default (superClass: typeof Parser) => true, ); if (method.typeParameters) { - // @ts-expect-error + // @ts-expect-error mutate AST types method.value.typeParameters = method.typeParameters; delete method.typeParameters; } @@ -296,7 +296,7 @@ export default (superClass: typeof Parser) => // @ts-expect-error mutate AST types funcNode.type = "FunctionExpression"; delete funcNode.kind; - // @ts-expect-error + // @ts-expect-error mutate AST types node.value = funcNode; if (type === "ClassPrivateMethod") { node.computed = false; diff --git a/packages/babel-parser/src/plugins/flow/index.ts b/packages/babel-parser/src/plugins/flow/index.ts index 54d84153ac23..b5cb7d4586c0 100644 --- a/packages/babel-parser/src/plugins/flow/index.ts +++ b/packages/babel-parser/src/plugins/flow/index.ts @@ -2886,7 +2886,7 @@ export default (superClass: typeof Parser) => node: Undone, allowModifiers?: boolean, ): void { - // @ts-expect-error + // @ts-expect-error kind may not index node const kind = node.kind; if (kind !== "get" && kind !== "set" && this.match(tt.lt)) { node.typeParameters = this.flowParseTypeParameterDeclaration(); diff --git a/packages/babel-parser/src/plugins/placeholders.ts b/packages/babel-parser/src/plugins/placeholders.ts index da7eff69d135..a89b150f8c9b 100644 --- a/packages/babel-parser/src/plugins/placeholders.ts +++ b/packages/babel-parser/src/plugins/placeholders.ts @@ -197,7 +197,7 @@ export default (superClass: typeof Parser) => } if (this.match(tt.colon)) { - // @ts-expect-error + // @ts-expect-error placeholder typings const stmt: N.LabeledStatement = node; stmt.label = this.finishPlaceholder(expr, "Identifier"); this.next(); diff --git a/packages/babel-parser/src/plugins/typescript/index.ts b/packages/babel-parser/src/plugins/typescript/index.ts index 35c5e40e11ff..9d97c3f0d570 100644 --- a/packages/babel-parser/src/plugins/typescript/index.ts +++ b/packages/babel-parser/src/plugins/typescript/index.ts @@ -1118,9 +1118,8 @@ export default (superClass: ClassWithMixin) => labeledNode.label = type.typeName as N.Identifier; } else { this.raise(TSErrors.InvalidTupleMemberLabel, { at: type }); - // This produces an invalid AST, but at least we don't drop + // @ts-expect-error This produces an invalid AST, but at least we don't drop // nodes representing the invalid source. - // @ts-expect-error labeledNode.label = type; } @@ -1157,8 +1156,7 @@ export default (superClass: ClassWithMixin) => N.TsFunctionOrConstructorType | N.TsConstructorType >(); if (type === "TSConstructorType") { - // @ts-expect-error - node.abstract = !!abstract; + (node as Undone).abstract = !!abstract; if (abstract) this.next(); this.next(); // eat `new` } @@ -2346,10 +2344,7 @@ export default (superClass: ClassWithMixin) => } if (bodilessType === "TSDeclareFunction" && this.state.isAmbientContext) { this.raise(TSErrors.DeclareFunctionHasImplementation, { at: node }); - if ( - // @ts-expect-error - node.declare - ) { + if ((node as Undone).declare) { return super.parseFunctionBodyAndFinish(node, bodilessType, isMethod); } } @@ -2484,7 +2479,9 @@ export default (superClass: ClassWithMixin) => } if (!noCalls && this.eat(tt.parenL)) { - const node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt< + N.CallExpression | N.OptionalCallExpression + >(startPos, startLoc); node.callee = base; // possibleAsync always false here, because we would have handled it above. // @ts-expect-error (won't be any undefined arguments) @@ -2498,8 +2495,8 @@ export default (superClass: ClassWithMixin) => node.typeParameters = typeArguments; if (state.optionalChainMember) { - // @ts-expect-error - node.optional = isOptionalCall; + (node as Undone).optional = + isOptionalCall; } return this.finishCallExpression(node, state.optionalChainMember); @@ -3162,12 +3159,12 @@ export default (superClass: ClassWithMixin) => parseClassPrivateProperty( node: N.ClassPrivateProperty, ): N.ClassPrivateProperty { - // @ts-expect-error + // @ts-expect-error abstract may not index node if (node.abstract) { this.raise(TSErrors.PrivateElementHasAbstract, { at: node }); } - // @ts-expect-error + // @ts-expect-error accessibility may not index node if (node.accessibility) { this.raise(TSErrors.PrivateElementHasAccessibility, { at: node, @@ -3195,7 +3192,7 @@ export default (superClass: ClassWithMixin) => }); } - // @ts-expect-error + // @ts-expect-error declare does not exist in ClassMethod const { declare = false, kind } = method; if (declare && (kind === "get" || kind === "set")) { diff --git a/packages/babel-parser/src/tokenizer/index.ts b/packages/babel-parser/src/tokenizer/index.ts index 4b009ffe4d42..b558c1c32a81 100644 --- a/packages/babel-parser/src/tokenizer/index.ts +++ b/packages/babel-parser/src/tokenizer/index.ts @@ -183,8 +183,7 @@ export default abstract class Tokenizer extends CommentsParser { */ lookahead(): LookaheadState { const old = this.state; - // For performance we use a simplified tokenizer state structure - // @ts-expect-error + // @ts-expect-error For performance we use a simplified tokenizer state structure this.state = this.createLookaheadState(old); this.isLookahead = true; @@ -455,9 +454,8 @@ export default abstract class Tokenizer extends CommentsParser { replaceToken(type: TokenType): void { this.state.type = type; - // the prevType of updateContext is required + // @ts-expect-error the prevType of updateContext is required // only when the new type is tt.slash/tt.jsxTagEnd - // @ts-expect-error this.updateContext(); } diff --git a/packages/babel-parser/src/tokenizer/state.ts b/packages/babel-parser/src/tokenizer/state.ts index c160d1d5190a..dcb8c0f4ec18 100644 --- a/packages/babel-parser/src/tokenizer/state.ts +++ b/packages/babel-parser/src/tokenizer/state.ts @@ -163,7 +163,7 @@ export default class State { val = val.slice(); } - // @ts-expect-error + // @ts-expect-error val must conform to S[key] state[key] = val; } diff --git a/packages/babel-plugin-transform-classes/src/transformClass.ts b/packages/babel-plugin-transform-classes/src/transformClass.ts index 4b733b26d1f7..b9fa6c970617 100644 --- a/packages/babel-plugin-transform-classes/src/transformClass.ts +++ b/packages/babel-plugin-transform-classes/src/transformClass.ts @@ -168,6 +168,7 @@ export default function transformClass( let hasConstructor = false; const paths = classState.path.get("body.body"); for (const path of paths) { + // @ts-expect-error: StaticBlock does not have `kind` property hasConstructor = path.equals("kind", "constructor"); if (hasConstructor) break; } diff --git a/packages/babel-plugin-transform-flow-comments/src/index.ts b/packages/babel-plugin-transform-flow-comments/src/index.ts index f144cd147c4d..524479c91654 100644 --- a/packages/babel-plugin-transform-flow-comments/src/index.ts +++ b/packages/babel-plugin-transform-flow-comments/src/index.ts @@ -68,14 +68,20 @@ export default declare(api => { } } - function wrapInFlowComment(path: NodePath) { + function wrapInFlowComment< + N extends + | t.ClassProperty + | t.ExportNamedDeclaration + | t.Flow + | t.ImportDeclaration + | t.ExportDeclaration + | t.ImportSpecifier + | t.ImportDeclaration, + >(path: NodePath) { attachComment({ ofPath: path, - comments: generateComment( - path, - // @ts-expect-error - path.parent.optional, - ), + // @ts-expect-error optional may not exist in path.parent + comments: generateComment(path, path.parent.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 441e219183fd..d633741f21f6 100644 --- a/packages/babel-plugin-transform-flow-strip-types/src/index.ts +++ b/packages/babel-plugin-transform-flow-strip-types/src/index.ts @@ -155,7 +155,8 @@ export default declare((api, opts: Options) => { for (let i = 0; i < node.params.length; i++) { let param = node.params[i]; if (param.type === "AssignmentPattern") { - // @ts-expect-error + // @ts-expect-error: refine AST types, the left of an assignment pattern as a binding + // must not be a MemberExpression param = param.left; } // @ts-expect-error optional is not in ObjectPattern diff --git a/packages/babel-plugin-transform-modules-systemjs/src/index.ts b/packages/babel-plugin-transform-modules-systemjs/src/index.ts index 887adf4b5722..4812c771b086 100644 --- a/packages/babel-plugin-transform-modules-systemjs/src/index.ts +++ b/packages/babel-plugin-transform-modules-systemjs/src/index.ts @@ -227,7 +227,7 @@ export default declare((api, options: Options) => { }); if (isPostUpdateExpression) { node = t.binaryExpression( - // @ts-expect-error + // @ts-expect-error The operator of a post-update expression must be "++" | "--" node.operator[0], t.unaryExpression( "+", 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 1b23ae5c769a..2ad08889e709 100644 --- a/packages/babel-plugin-transform-react-jsx/src/create-plugin.ts +++ b/packages/babel-plugin-transform-react-jsx/src/create-plugin.ts @@ -392,8 +392,9 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`, if (node.name === "this" && t.isReferenced(node, parent)) { return t.thisExpression(); } else if (t.isValidIdentifier(node.name, false)) { - // @ts-expect-error todo(flow->ts) + // @ts-expect-error cast AST type to Identifier node.type = "Identifier"; + return node as unknown as t.Identifier; } else { return t.stringLiteral(node.name); } @@ -410,7 +411,7 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`, return t.stringLiteral(`${node.namespace.name}:${node.name.name}`); } - // @ts-expect-error + // todo: this branch should be unreachable return node; } diff --git a/packages/babel-plugin-transform-typescript/src/index.ts b/packages/babel-plugin-transform-typescript/src/index.ts index 6e120675f064..1696f8e9f780 100644 --- a/packages/babel-plugin-transform-typescript/src/index.ts +++ b/packages/babel-plugin-transform-typescript/src/index.ts @@ -348,8 +348,7 @@ export default declare((api, opts: Options) => { ) { registerGlobalType( programScope, - //@ts-expect-error - stmt.node.id.name, + (stmt.node.id as t.Identifier).name, ); } } diff --git a/packages/babel-preset-env/src/debug.ts b/packages/babel-preset-env/src/debug.ts index 83450faa678e..a1998fda9ac9 100644 --- a/packages/babel-preset-env/src/debug.ts +++ b/packages/babel-preset-env/src/debug.ts @@ -1,6 +1,6 @@ import { getInclusionReasons } from "@babel/helper-compilation-targets"; -import type { Targets } from "@babel/helper-compilation-targets"; +import type { Targets, Target } from "@babel/helper-compilation-targets"; // Outputs a message that shows which target(s) caused an item to be included: // transform-foo { "edge":"13", "firefox":"49", "ie":"10" } @@ -20,11 +20,10 @@ export const logPlugin = ( let formattedTargets = `{`; let first = true; - for (const target of Object.keys(filteredList)) { + for (const target of Object.keys(filteredList) as Target[]) { if (!first) formattedTargets += `,`; first = false; formattedTargets += ` ${target}`; - // @ts-expect-error if (support[target]) formattedTargets += ` < ${support[target]}`; } formattedTargets += ` }`; diff --git a/packages/babel-preset-env/src/index.ts b/packages/babel-preset-env/src/index.ts index 3fa23e0b4538..e36e03d14170 100644 --- a/packages/babel-preset-env/src/index.ts +++ b/packages/babel-preset-env/src/index.ts @@ -57,7 +57,7 @@ function filterStageFromList( ) { return Object.keys(list).reduce((result, item) => { if (!stageList.has(item)) { - // @ts-expect-error + // @ts-expect-error todo: refine result types result[item] = list[item]; } diff --git a/packages/babel-preset-env/src/plugins-compat-data.ts b/packages/babel-preset-env/src/plugins-compat-data.ts index 2936faf0faf8..92e98ecd1cca 100644 --- a/packages/babel-preset-env/src/plugins-compat-data.ts +++ b/packages/babel-preset-env/src/plugins-compat-data.ts @@ -7,14 +7,14 @@ const bugfixPluginsFiltered = {}; for (const plugin of Object.keys(plugins)) { if (Object.hasOwnProperty.call(availablePlugins, plugin)) { - // @ts-expect-error + // @ts-expect-error fixme: refine pluginsFiltered types pluginsFiltered[plugin] = plugins[plugin]; } } for (const plugin of Object.keys(bugfixPlugins)) { if (Object.hasOwnProperty.call(availablePlugins, plugin)) { - // @ts-expect-error + // @ts-expect-error fixme: refine bugfixPluginsFiltered types bugfixPluginsFiltered[plugin] = bugfixPlugins[plugin]; } } diff --git a/packages/babel-traverse/src/context.ts b/packages/babel-traverse/src/context.ts index 62e75b445bd8..68af10f48a3b 100644 --- a/packages/babel-traverse/src/context.ts +++ b/packages/babel-traverse/src/context.ts @@ -101,7 +101,7 @@ export default class TraversalContext { visitSingle(node: t.Node, key: string): boolean { if ( this.shouldVisit( - // @ts-expect-error + // @ts-expect-error key may not index node node[key], ) ) { @@ -166,7 +166,7 @@ export default class TraversalContext { } visit(node: t.Node, key: string) { - // @ts-expect-error + // @ts-expect-error key may not index node const nodes = node[key] as t.Node | t.Node[] | null; if (!nodes) return false; diff --git a/packages/babel-traverse/src/path/evaluation.ts b/packages/babel-traverse/src/path/evaluation.ts index 79da6ad3380c..6c7c16163c21 100644 --- a/packages/babel-traverse/src/path/evaluation.ts +++ b/packages/babel-traverse/src/path/evaluation.ts @@ -8,14 +8,14 @@ const INVALID_METHODS = ["random"] as const; function isValidCallee(val: string): val is typeof VALID_CALLEES[number] { return VALID_CALLEES.includes( - // @ts-expect-error + // @ts-expect-error val is a string val, ); } function isInvalidMethod(val: string): val is typeof INVALID_METHODS[number] { return INVALID_METHODS.includes( - // @ts-expect-error + // @ts-expect-error val is a string val, ); } @@ -259,28 +259,29 @@ function _evaluate(path: NodePath, state: State): any { if (prop.isObjectMethod() || prop.isSpreadElement()) { return deopt(prop, state); } - const keyPath: any = prop.get("key"); - let key = keyPath; + const keyPath = (prop as NodePath).get("key"); + let key; // @ts-expect-error todo(flow->ts): type refinement issues ObjectMethod and SpreadElement somehow not excluded if (prop.node.computed) { - key = key.evaluate(); + key = keyPath.evaluate(); if (!key.confident) { return deopt(key.deopt, state); } key = key.value; - } else if (key.isIdentifier()) { - key = key.node.name; + } else if (keyPath.isIdentifier()) { + key = keyPath.node.name; } else { - key = key.node.value; + key = ( + keyPath.node as t.StringLiteral | t.NumericLiteral | t.BigIntLiteral + ).value; } - // todo(flow->ts): remove typecast - const valuePath = prop.get("value") as NodePath; + const valuePath = (prop as NodePath).get("value"); let value = valuePath.evaluate(); if (!value.confident) { return deopt(value.deopt, state); } value = value.value; - // @ts-expect-error + // @ts-expect-error key is any type obj[key] = value; } return obj; diff --git a/packages/babel-traverse/src/path/family.ts b/packages/babel-traverse/src/path/family.ts index 14c81d48acb4..cf5bba25b5c9 100644 --- a/packages/babel-traverse/src/path/family.ts +++ b/packages/babel-traverse/src/path/family.ts @@ -398,6 +398,7 @@ function get( const parts = key.split("."); if (parts.length === 1) { // "foo" + // @ts-expect-error key may not index T return this._getKey(key, context); } else { // "foo.bar" @@ -409,13 +410,11 @@ export { get }; export function _getKey( this: NodePath, - key: string, + key: keyof T & string, context?: TraversalContext, ): NodePath | NodePath[] { const node = this.node; - const container = - // @ts-expect-error - node[key]; + const container = node[key]; if (Array.isArray(container)) { // requested a container so give them all the paths @@ -450,7 +449,7 @@ export function _getPattern( path = path.parentPath; } else { if (Array.isArray(path)) { - // @ts-expect-error + // @ts-expect-error part may not index path path = path[part]; } else { path = path.get(part, context); @@ -528,7 +527,7 @@ function getBindingIdentifierPaths( if (!id.node) continue; const keys = - // @ts-expect-error + // @ts-expect-error _getBindingIdentifiers.keys do not cover all node types _getBindingIdentifiers.keys[id.node.type]; if (id.isIdentifier()) { diff --git a/packages/babel-traverse/src/path/index.ts b/packages/babel-traverse/src/path/index.ts index 8c042a675bff..586b997a16dd 100644 --- a/packages/babel-traverse/src/path/index.ts +++ b/packages/babel-traverse/src/path/index.ts @@ -244,10 +244,8 @@ Object.assign( ); if (!process.env.BABEL_8_BREAKING) { - // The original _guessExecutionStatusRelativeToDifferentFunctions only worked for paths in + // @ts-expect-error The original _guessExecutionStatusRelativeToDifferentFunctions only worked for paths in // different functions, but _guessExecutionStatusRelativeTo works as a replacement in those cases. - - // @ts-expect-error NodePath.prototype._guessExecutionStatusRelativeToDifferentFunctions = NodePath_introspection._guessExecutionStatusRelativeTo; } diff --git a/packages/babel-traverse/src/path/inference/index.ts b/packages/babel-traverse/src/path/inference/index.ts index a5b5cc5b38bf..845bf324cb29 100644 --- a/packages/babel-traverse/src/path/inference/index.ts +++ b/packages/babel-traverse/src/path/inference/index.ts @@ -75,9 +75,9 @@ export function _getTypeAnnotation(this: NodePath): any { } } - // @ts-expect-error + // @ts-expect-error typeAnnotation may not index node if (node.typeAnnotation) { - // @ts-expect-error + // @ts-expect-error typeAnnotation may not index node return node.typeAnnotation; } diff --git a/packages/babel-traverse/src/path/introspection.ts b/packages/babel-traverse/src/path/introspection.ts index 9a44718c387e..f4c8e2db9153 100644 --- a/packages/babel-traverse/src/path/introspection.ts +++ b/packages/babel-traverse/src/path/introspection.ts @@ -34,11 +34,11 @@ export function matchesPattern( * if the array has any items, otherwise we just check if it's falsy. */ -export function has(this: NodePath, key: string): boolean { - const val = - this.node && - // @ts-expect-error - this.node[key]; +export function has( + this: NodePath, + key: keyof N, +): boolean { + const val = this.node && this.node[key]; if (val && Array.isArray(val)) { return !!val.length; } else { @@ -64,7 +64,10 @@ export const is = has; * Opposite of `has`. */ -export function isnt(this: NodePath, key: string): boolean { +export function isnt( + this: NodePath, + key: keyof N, +): boolean { return !this.has(key); } @@ -72,8 +75,11 @@ export function isnt(this: NodePath, key: string): boolean { * Check whether the path node `key` strict equals `value`. */ -export function equals(this: NodePath, key: string, value: any): boolean { - // @ts-expect-error +export function equals( + this: NodePath, + key: keyof N, + value: any, +): boolean { return this.node[key] === value; } diff --git a/packages/babel-types/src/converters/ensureBlock.ts b/packages/babel-types/src/converters/ensureBlock.ts index 17056c6c2d5b..0abea6b4f793 100644 --- a/packages/babel-types/src/converters/ensureBlock.ts +++ b/packages/babel-types/src/converters/ensureBlock.ts @@ -13,7 +13,7 @@ export default function ensureBlock( ): t.BlockStatement { // @ts-expect-error Fixme: key may not exist in node, consider remove key = "body" const result = toBlock(node[key], node); - // @ts-expect-error + // @ts-expect-error Fixme: key may not exist in node, consider remove key = "body" node[key] = result; return result; } diff --git a/packages/babel-types/src/modifications/removeProperties.ts b/packages/babel-types/src/modifications/removeProperties.ts index b0d58a3d72e9..8ea5c7283955 100644 --- a/packages/babel-types/src/modifications/removeProperties.ts +++ b/packages/babel-types/src/modifications/removeProperties.ts @@ -28,12 +28,12 @@ export default function removeProperties( ): void { const map = opts.preserveComments ? CLEAR_KEYS : CLEAR_KEYS_PLUS_COMMENTS; for (const key of map) { - // @ts-expect-error + // @ts-expect-error tokens only exist in t.File if (node[key] != null) node[key] = undefined; } for (const key of Object.keys(node)) { - // @ts-expect-error + // @ts-expect-error string can not index node if (key[0] === "_" && node[key] != null) node[key] = undefined; }