Skip to content

Commit

Permalink
Enable ban-ts-comment (#14908)
Browse files Browse the repository at this point in the history
* lint: enable ban-ts-comment

* Add descriptions for every @ts-expect-error comments
  • Loading branch information
JLHwung committed Sep 2, 2022
1 parent 14ea25f commit 05deb60
Show file tree
Hide file tree
Showing 44 changed files with 121 additions and 149 deletions.
7 changes: 6 additions & 1 deletion .eslintrc.cjs
Expand Up @@ -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 },
Expand Down
@@ -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;
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion packages/babel-core/src/config/files/plugins.ts
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-core/src/config/util.ts
Expand Up @@ -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;
}
}
Expand Down
3 changes: 1 addition & 2 deletions packages/babel-core/src/config/validation/options.ts
Expand Up @@ -456,8 +456,7 @@ function assertOverridesList(
validateNested(overridesLoc, env);
}
}
// @ts-expect-error
return arr;
return arr as OverridesList;
}

export function checkNoUnwrappedItemOptionPairs(
Expand Down
3 changes: 1 addition & 2 deletions packages/babel-core/src/errors/rewrite-stack-trace.ts
Expand Up @@ -125,8 +125,7 @@ export function endHiddenCallStack<A extends unknown[], R>(
}

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 = () => {};

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-core/src/transformation/file/file.ts
Expand Up @@ -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;
});

Expand Down
4 changes: 2 additions & 2 deletions packages/babel-generator/src/generators/modules.ts
Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-generator/src/printer.ts
Expand Up @@ -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) {};
}

Expand Down
4 changes: 2 additions & 2 deletions packages/babel-helper-builder-react-jsx/src/index.ts
Expand Up @@ -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);
}
Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-helper-compilation-targets/src/index.ts
Expand Up @@ -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";
Expand Down
Expand Up @@ -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"
) {
Expand Down
Expand Up @@ -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";
}
}
Expand Down
Expand Up @@ -434,13 +434,12 @@ function getLocalExportMetadata(
): Map<string, LocalExportMetadata> {
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()) {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-helper-plugin-utils/src/index.ts
Expand Up @@ -31,7 +31,7 @@ export function declare<State = {}, Option = {}>(
clonedApi[name] = apiPolyfills[name](clonedApi);
}

// @ts-expect-error
// @ts-expect-error options || {} may not be assigned to Options
return builder(clonedApi ?? api, options || {}, dirname);
};
}
Expand Down
8 changes: 4 additions & 4 deletions packages/babel-parser/src/parser/expression.ts
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions packages/babel-parser/src/parser/lval.ts
Expand Up @@ -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, {
Expand Down
16 changes: 7 additions & 9 deletions packages/babel-parser/src/parser/node.ts
Expand Up @@ -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
Expand All @@ -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];
}
}

Expand Down Expand Up @@ -100,12 +98,12 @@ export type Undone<T extends NodeType> = Omit<T, "type">;

export abstract class NodeUtils extends UtilParser {
startNode<T extends NodeType>(): Undone<T> {
// @ts-expect-error
// @ts-expect-error cast Node as Undone<T>
return new Node(this, this.state.start, this.state.startLoc);
}

startNodeAt<T extends NodeType>(pos: number, loc: Position): Undone<T> {
// @ts-expect-error
// @ts-expect-error cast Node as Undone<T>
return new Node(this, pos, loc);
}

Expand Down
5 changes: 2 additions & 3 deletions packages/babel-parser/src/parser/statement.ts
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-parser/src/plugin-utils.ts
Expand Up @@ -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;
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-parser/src/plugins/estree.ts
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/plugins/flow/index.ts
Expand Up @@ -2886,7 +2886,7 @@ export default (superClass: typeof Parser) =>
node: Undone<N.Function>,
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();
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/plugins/placeholders.ts
Expand Up @@ -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();
Expand Down

0 comments on commit 05deb60

Please sign in to comment.