Skip to content

Commit

Permalink
Use @ts-expect-error instead of @ts-ignore (#14743)
Browse files Browse the repository at this point in the history
* Replace most `@ts-ignore` with `@ts-expect-error`

* Enforce with ESLint rule
  • Loading branch information
nicolo-ribaudo committed Jul 8, 2022
1 parent 6165537 commit 703ee00
Show file tree
Hide file tree
Showing 72 changed files with 142 additions and 114 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module.exports = {
"@typescript-eslint/no-dupe-class-members": "error",
"no-undef": "off",
"no-redeclare": "off",
"@babel/development-internal/disallow-ts-ignore": "error",
},
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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.",
});
}
}
},
};
},
};
2 changes: 1 addition & 1 deletion packages/babel-core/src/config/files/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function* resolveAlternativesHelper(
const { error, value } = yield standardizedName;
if (!error) return value;

// @ts-ignore
// @ts-expect-error
if (error.code !== "MODULE_NOT_FOUND") throw error;

if (standardizedName !== name && !(yield name).error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function msg(loc: NestingPath | GeneralPath): string {
case "access":
return `${msg(loc.parent)}[${JSON.stringify(loc.name)}]`;
default:
// @ts-ignore should not happen when code is type checked
// @ts-expect-error should not happen when code is type checked
throw new Error(`Assertion failure: Unknown type ${loc.type}`);
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/babel-core/src/config/validation/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ function assertVisitorMap(loc: OptionPath, value: unknown): Visitor {
if (obj) {
Object.keys(obj).forEach(prop => assertVisitorHandler(prop, obj[prop]));

// @ts-ignore
if (obj.enter || obj.exit) {
throw new Error(
`${msg(
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-generator/src/generators/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function DeclareFunction(
this.word("function");
this.space();
this.print(node.id, node);
// @ts-ignore TODO(Babel 8) Remove this comment, since we'll remove the Noop node
// @ts-ignore(Babel 7 vs Babel 8) TODO(Babel 8) Remove this comment, since we'll remove the Noop node
this.print(node.id.typeAnnotation.typeAnnotation, node);

if (node.predicate) {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-generator/src/generators/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function _methodHead(this: Printer, node: t.Method | t.TSDeclareMethod) {

if (
kind === "method" ||
// @ts-ignore Fixme: kind: "init" is not defined
// @ts-expect-error Fixme: kind: "init" is not defined
kind === "init"
) {
if (node.generator) {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-generator/src/generators/statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function IfStatement(this: Printer, node: t.IfStatement) {

// Recursively get the last statement.
function getLastStatement(statement: t.Statement): t.Statement {
// @ts-ignore: If statement.body is empty or not a Node, isStatement will return false
// @ts-expect-error: If statement.body is empty or not a Node, isStatement will return false
const { body } = statement;
if (isStatement(body) === false) {
return statement;
Expand Down
8 changes: 4 additions & 4 deletions packages/babel-generator/src/generators/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ export function tsPrintFunctionOrConstructorType(
) {
const { typeParameters } = node;
const parameters = process.env.BABEL_8_BREAKING
? // @ts-ignore Babel 8 AST shape
? // @ts-ignore(Babel 7 vs Babel 8) Babel 8 AST shape
node.params
: // @ts-ignore Babel 7 AST shape
: // @ts-ignore(Babel 7 vs Babel 8) Babel 7 AST shape
node.parameters;
this.print(typeParameters, node);
this.token("(");
Expand All @@ -251,9 +251,9 @@ export function tsPrintFunctionOrConstructorType(
this.token("=>");
this.space();
const returnType = process.env.BABEL_8_BREAKING
? // @ts-ignore Babel 8 AST shape
? // @ts-ignore(Babel 7 vs Babel 8) Babel 8 AST shape
node.returnType
: // @ts-ignore Babel 7 AST shape
: // @ts-ignore(Babel 7 vs Babel 8) Babel 7 AST shape
node.typeAnnotation;
this.print(returnType.typeAnnotation, node);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-generator/src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ class Printer {

const oldConcise = this.format.concise;
if (
// @ts-ignore document _compact AST properties
// @ts-expect-error document _compact AST properties
node._compact
) {
this.format.concise = true;
Expand Down Expand Up @@ -852,7 +852,7 @@ class Printer {
Object.assign(Printer.prototype, generatorFunctions);

if (!process.env.BABEL_8_BREAKING) {
// @ts-ignore
// @ts-ignore(Babel 7 vs Babel 8)
Printer.prototype.Noop = function Noop(this: Printer) {};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-helper-compilation-targets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export default function getTargets(
for (const browser of Object.keys(queryBrowsers) as Target[]) {
const version = queryBrowsers[browser];
const esmSupportVersion =
// @ts-ignore ie is not in ESM_SUPPORT
// @ts-expect-error ie is not in ESM_SUPPORT
ESM_SUPPORT[browser];

if (esmSupportVersion) {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-helper-compilation-targets/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function getLowestUnreleased(a: string, b: string, env: Target): string {
const unreleasedLabel:
| typeof unreleasedLabels[keyof typeof unreleasedLabels]
| undefined =
// @ts-ignore unreleasedLabel is undefined when env is not safari
// @ts-expect-error unreleasedLabel is undefined when env is not safari
unreleasedLabels[env];
if (a === unreleasedLabel) {
return b;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ interface PrivateNameVisitorState {
function privateNameVisitorFactory<S>(
visitor: Visitor<PrivateNameVisitorState & S>,
) {
// @ts-ignore Fixme: TS complains _exploded: boolean does not satisfy visitor functions
// @ts-expect-error Fixme: TS complains _exploded: boolean does not satisfy visitor functions
const privateNameVisitor: Visitor<PrivateNameVisitorState & S> = {
...visitor,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function createClassFeaturePlugin({
feature,
loose,
manipulateOptions,
// @ts-ignore TODO(Babel 8): Remove the default value
// @ts-ignore(Babel 7 vs Babel 8) TODO(Babel 8): Remove the default value
api = { assumption: () => void 0 },
inherits,
}: Options): PluginObject {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-helper-environment-visitor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function requeueComputedKeyAndDecorators(
path: NodePath<t.Method | t.Property>,
) {
const { context, node } = path;
//@ts-ignore ClassPrivateProperty does not have computed
// @ts-expect-error ClassPrivateProperty does not have computed
if (node.computed) {
// requeue the computed key
context.maybeQueue(path.get("key"));
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-helper-function-name/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export default function <N extends t.FunctionExpression | t.Class>(
// The id shouldn't be considered a local binding to the function because
// we are simply trying to set the function name and not actually create
// a local binding.
// @ts-ignore Fixme: avoid mutating AST nodes
// @ts-expect-error Fixme: avoid mutating AST nodes
newId[NOT_LOCAL_BINDING] = true;

const state = visit(node, name, scope);
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-helper-module-transforms/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export function rewriteModuleStatementsAndPrepareHeader(
export function ensureStatementsHoisted(statements: t.Statement[]) {
// Force all of the header fields to be at the top of the file.
statements.forEach(header => {
// @ts-ignore Fixme: handle _blockHoist property
// @ts-expect-error Fixme: handle _blockHoist property
header._blockHoist = 3;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ function getLocalExportMetadata(
kind = "import";
} else {
if (child.isExportDefaultDeclaration()) {
// @ts-ignore
// @ts-expect-error
child = child.get("declaration");
}
if (child.isExportNamedDeclaration()) {
Expand Down
3 changes: 1 addition & 2 deletions packages/babel-helper-plugin-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export function declare<State = {}, Option = {}>(
options: Option,
dirname: string,
) => PluginObject<State & PluginPass> {
// @ts-ignore
return (api, options: Option, dirname: string) => {
let clonedApi: PluginAPI;

Expand All @@ -32,7 +31,7 @@ export function declare<State = {}, Option = {}>(
clonedApi[name] = apiPolyfills[name](clonedApi);
}

// @ts-ignore
// @ts-expect-error
return builder(clonedApi ?? api, options || {}, dirname);
};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-helper-replace-supers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ const specHandlers: SpecHandler = {
this.isPrivateMethod,
);
return callExpression(this.file.addHelper("get"), [
// @ts-ignore memo does not exist when this.isDerivedConstructor is false
// @ts-expect-error memo does not exist when this.isDerivedConstructor is false
thisRefs.memo ? sequenceExpression([thisRefs.memo, proto]) : proto,
this.prop(superMember),
thisRefs.this,
Expand Down Expand Up @@ -198,7 +198,7 @@ const specHandlers: SpecHandler = {
this.isPrivateMethod,
);
return callExpression(this.file.addHelper("set"), [
// @ts-ignore memo does not exist when this.isDerivedConstructor is false
// @ts-expect-error memo does not exist when this.isDerivedConstructor is false
thisRefs.memo ? sequenceExpression([thisRefs.memo, proto]) : proto,
this.prop(superMember),
value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ if (!process.env.BABEL_8_BREAKING) {
await (typeof block === "function" ? block() : block);
return Promise.reject(new Error("Promise not rejected"));
} catch (error) {
// @ts-ignore Fixme: validateError can be a string | object
// @ts-expect-error Fixme: validateError can be a string | object
// see https://nodejs.org/api/assert.html#assertrejectsasyncfn-error-message
if (typeof validateError === "function" && !validateError(error)) {
return Promise.reject(
Expand Down Expand Up @@ -538,7 +538,7 @@ export default function (

if (task.externalHelpers) {
(task.options.plugins ??= [])
// @ts-ignore manipulating input options
// @ts-expect-error manipulating input options
.push([
"external-helpers",
{ helperVersion: EXTERNAL_HELPERS_VERSION },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default declare(function ({ assertVersion, assumption, types: t }) {
// (b, p1 = void 0) => {}
if (firstAssignmentPatternIndex >= firstPrivateIndex) {
params[firstAssignmentPatternIndex] = assignmentPattern(
// @ts-ignore The transformed assignment pattern must not be a RestElement
// @ts-expect-error The transformed assignment pattern must not be a RestElement
params[firstAssignmentPatternIndex],
scope.buildUndefinedNode(),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default declare(api => {
const specifier = specifiers.shift();
const { exported } = specifier;
const uid = scope.generateUidIdentifier(
// @ts-ignore Identifier ?? StringLiteral
// @ts-expect-error Identifier ?? StringLiteral
exported.name ?? exported.value,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default declare(api => {
const specifier = specifiers.shift();
const { exported } = specifier;
const uid = scope.generateUidIdentifier(
// @ts-ignore Identifier ?? StringLiteral
// @ts-expect-error Identifier ?? StringLiteral
exported.name ?? exported.value,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default declare(api => {

path.replaceWith(
t.logicalExpression(
// @ts-ignore operatorTrunc has been tested by t.LOGICAL_OPERATORS
// @ts-expect-error operatorTrunc has been tested by t.LOGICAL_OPERATORS
operatorTrunc,
lhs,
t.assignmentExpression("=", left, right),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export default declare((api, opts: Options) => {
keys.push(
t.stringLiteral(
String(
//@ts-ignore prop.key can not be a NullLiteral
// @ts-expect-error prop.key can not be a NullLiteral
prop.key.value,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default declare(api => {
t.assignmentExpression(
"=",
t.cloneNode(id),
// @ts-ignore Fixme: may need to handle JSXNamespacedName here
// @ts-expect-error Fixme: may need to handle JSXNamespacedName here
node,
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const visitor: Visitor<PluginPass> = {
t.assignmentExpression(
"=",
t.cloneNode(topicVariable),
// @ts-ignore node.left must not be a PrivateName when operator is |>
// @ts-expect-error node.left must not be a PrivateName when operator is |>
node.left,
),
node.right,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ export default function transformClass(
if (t.isStringLiteral(key)) {
// infer function name
if (node.kind === "method") {
// @ts-ignore Fixme: we are passing a ClassMethod to nameFunction, but nameFunction
// @ts-expect-error Fixme: we are passing a ClassMethod to nameFunction, but nameFunction
// does not seem to support it
fn = nameFunction({ id: key, node: node, scope });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export default declare((api, options: Options) => {
const { node, parent, scope } = path;
let hasComputed = false;
for (const prop of node.properties) {
// @ts-ignore SpreadElement must not have computed property
// @ts-expect-error SpreadElement must not have computed property
hasComputed = prop.computed === true;
if (hasComputed) break;
}
Expand Down
1 change: 0 additions & 1 deletion packages/babel-plugin-transform-destructuring/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export function unshiftForXStatementBody(
// var a = 0;for (const { #x: x, [a++]: y } of z) { const a = 1; }
node.body = t.blockStatement([...newStatements, node.body]);
} else {
// @ts-ignore statementPath.ensureBlock() has been called, node.body is always a BlockStatement
node.body.body.unshift(...newStatements);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default declare(api => {
| t.BigIntLiteral,
);
let isDuplicate = false;
// @ts-ignore prop.kind is not defined in ObjectProperty
// @ts-expect-error prop.kind is not defined in ObjectProperty
switch (prop.kind) {
case "get":
if (alreadySeenData[name] || alreadySeenGetters[name]) {
Expand Down
Loading

0 comments on commit 703ee00

Please sign in to comment.