Skip to content

Commit

Permalink
chore: Enable rule no-confusing-void-expression (#15485)
Browse files Browse the repository at this point in the history
Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com>
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
3 people committed Mar 14, 2023
1 parent 9e67898 commit 4035521
Show file tree
Hide file tree
Showing 25 changed files with 166 additions and 113 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.cjs
Expand Up @@ -65,6 +65,10 @@ module.exports = {
},
],
"@typescript-eslint/no-unnecessary-type-assertion": "error",
"@typescript-eslint/no-confusing-void-expression": [
"error",
{ ignoreArrowShorthand: true },
],
},
},
{
Expand Down
9 changes: 6 additions & 3 deletions packages/babel-core/src/config/index.ts
Expand Up @@ -54,7 +54,10 @@ const maybeErrback =
callback = maybeCallback;
arg = argOrCallback as Arg;
}
return callback ? runner.errback(arg, callback) : runner.sync(arg);
if (!callback) {
return runner.sync(arg);
}
runner.errback(arg, callback);
};

export const loadPartialConfig = maybeErrback(loadPartialConfigRunner);
Expand All @@ -73,9 +76,9 @@ export function createConfigItem(
callback?: (err: Error, val: ConfigItem | null) => void,
) {
if (callback !== undefined) {
return createConfigItemRunner.errback(target, options, callback);
createConfigItemRunner.errback(target, options, callback);
} else if (typeof options === "function") {
return createConfigItemRunner.errback(target, undefined, callback);
createConfigItemRunner.errback(target, undefined, callback);
} else {
return createConfigItemRunner.sync(target, options);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-core/src/transform-file.ts
Expand Up @@ -40,7 +40,7 @@ export function transformFile(
export function transformFile(
...args: Parameters<typeof transformFileRunner.errback>
) {
return transformFileRunner.errback(...args);
transformFileRunner.errback(...args);
}

export function transformFileSync(
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-core/src/transformation/index.ts
Expand Up @@ -94,6 +94,7 @@ function* transformFile(file: File, pluginPasses: PluginPasses): Handler<void> {
for (const [plugin, pass] of passPairs) {
const fn = plugin.pre;
if (fn) {
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
const result = fn.call(pass, file);

// @ts-expect-error - If we want to support async .pre
Expand Down Expand Up @@ -121,6 +122,7 @@ function* transformFile(file: File, pluginPasses: PluginPasses): Handler<void> {
for (const [plugin, pass] of passPairs) {
const fn = plugin.post;
if (fn) {
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
const result = fn.call(pass, file);

// @ts-expect-error - If we want to support async .post
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-core/src/transformation/plugin-pass.ts
Expand Up @@ -38,8 +38,9 @@ export default class PluginPass {
return this.file.addHelper(name);
}

// TODO: Remove this in Babel 8
addImport() {
return this.file.addImport();
this.file.addImport();
}

buildCodeFrameError(
Expand Down
11 changes: 7 additions & 4 deletions packages/babel-generator/src/buffer.ts
Expand Up @@ -410,7 +410,10 @@ export default class Buffer {
* over "();", where previously it would have been a single mapping.
*/
exactSource(loc: Loc | undefined, cb: () => void) {
if (!this._map) return cb();
if (!this._map) {
cb();
return;
}

this.source("start", loc);
// @ts-expect-error identifierName is not defined
Expand Down Expand Up @@ -459,9 +462,9 @@ export default class Buffer {
*/

withSource(prop: "start" | "end", loc: Loc, cb: () => void): void {
if (!this._map) return cb();

this.source(prop, loc);
if (this._map) {
this.source(prop, loc);
}

cb();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-generator/src/generators/types.ts
Expand Up @@ -211,7 +211,7 @@ export function StringLiteral(this: Printer, node: t.StringLiteral) {

const val = jsesc(node.value, this.format.jsescOption);

return this.token(val);
this.token(val);
}

export function BigIntLiteral(this: Printer, node: t.BigIntLiteral) {
Expand Down
14 changes: 10 additions & 4 deletions packages/babel-generator/src/printer.ts
Expand Up @@ -331,7 +331,10 @@ class Printer {
}

exactSource(loc: Loc | undefined, cb: () => void) {
if (!loc) return cb();
if (!loc) {
cb();
return;
}

this._catchUp("start", loc);

Expand Down Expand Up @@ -364,7 +367,10 @@ class Printer {
loc: Loc | undefined,
cb: () => void,
): void {
if (!loc) return cb();
if (!loc) {
cb();
return;
}

this._catchUp(prop, loc);

Expand Down Expand Up @@ -874,15 +880,15 @@ class Printer {
opts: PrintSequenceOptions = {},
) {
opts.statement = true;
return this.printJoin(nodes, parent, opts);
this.printJoin(nodes, parent, opts);
}

printList(items: t.Node[], parent: t.Node, opts: PrintListOptions = {}) {
if (opts.separator == null) {
opts.separator = commaSeparator;
}

return this.printJoin(items, parent, opts);
this.printJoin(items, parent, opts);
}

_printNewline(newLine: boolean, opts: AddNewlinesOptions) {
Expand Down
16 changes: 8 additions & 8 deletions packages/babel-parser/src/parser/expression.ts
Expand Up @@ -1265,9 +1265,9 @@ export default abstract class ExpressionParser extends LValParser {

if (pipeProposal) {
return this.parseTopicReference(pipeProposal);
} else {
throw this.unexpected();
}
this.unexpected();
break;
}

case tt.lt: {
Expand All @@ -1277,10 +1277,10 @@ export default abstract class ExpressionParser extends LValParser {
lookaheadCh === charCodes.greaterThan // Fragment <>
) {
this.expectOnePlugin(["jsx", "flow", "typescript"]);
break;
} else {
throw this.unexpected();
this.unexpected();
}
break;
}

default:
Expand Down Expand Up @@ -1345,7 +1345,7 @@ export default abstract class ExpressionParser extends LValParser {

return id;
} else {
throw this.unexpected();
this.unexpected();
}
}
}
Expand Down Expand Up @@ -1383,7 +1383,7 @@ export default abstract class ExpressionParser extends LValParser {
// Now actually consume the topic token.
return this.parseTopicReference(pipeProposal);
} else {
throw this.unexpected();
this.unexpected();
}
}

Expand Down Expand Up @@ -2387,7 +2387,7 @@ export default abstract class ExpressionParser extends LValParser {
break;
}
default:
throw this.unexpected();
this.unexpected();
}
}
(prop as any).key = key;
Expand Down Expand Up @@ -2751,7 +2751,7 @@ export default abstract class ExpressionParser extends LValParser {
if (tokenIsKeywordOrIdentifier(type)) {
name = this.state.value;
} else {
throw this.unexpected();
this.unexpected();
}

const tokenIsKeyword = tokenKeywordOrIdentifierIsKeyword(type);
Expand Down
6 changes: 3 additions & 3 deletions packages/babel-parser/src/parser/statement.ts
Expand Up @@ -2343,11 +2343,11 @@ export default abstract class StatementParser extends ExpressionParser {
);

if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers) {
throw this.unexpected(null, tt.braceL);
this.unexpected(null, tt.braceL);
}

if (hasNamespace && parseAfterNamespace) {
throw this.unexpected(null, tt._from);
this.unexpected(null, tt._from);
}

let hasDeclaration;
Expand Down Expand Up @@ -2394,7 +2394,7 @@ export default abstract class StatementParser extends ExpressionParser {
return this.finishNode(node2, "ExportDefaultDeclaration");
}

throw this.unexpected(null, tt.braceL);
this.unexpected(null, tt.braceL);
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/parser/util.ts
Expand Up @@ -101,7 +101,7 @@ export default abstract class UtilParser extends Tokenizer {
if (toParseError != null) {
throw this.raise(toParseError, { at: this.state.startLoc });
}
throw this.unexpected(null, token);
this.unexpected(null, token);
}
}

Expand Down
36 changes: 19 additions & 17 deletions packages/babel-parser/src/plugins/flow/index.ts
Expand Up @@ -338,7 +338,7 @@ export default (superClass: typeof Parser) =>
this.flowPragma = null;
}
}
return super.finishToken(type, val);
super.finishToken(type, val);
}

addComment(comment: N.Comment): void {
Expand All @@ -355,7 +355,7 @@ export default (superClass: typeof Parser) =>
throw new Error("Unexpected flow pragma");
}
}
return super.addComment(comment);
super.addComment(comment);
}

flowParseTypeInitialiser(tok?: TokenType): N.FlowType {
Expand Down Expand Up @@ -493,7 +493,7 @@ export default (superClass: typeof Parser) =>
} else if (this.match(tt._export)) {
return this.flowParseDeclareExportDeclaration(node, insideModule);
} else {
throw this.unexpected();
this.unexpected();
}
}

Expand Down Expand Up @@ -654,7 +654,7 @@ export default (superClass: typeof Parser) =>
}
}

throw this.unexpected();
this.unexpected();
}

flowParseDeclareModuleExports(
Expand Down Expand Up @@ -1682,8 +1682,8 @@ export default (superClass: typeof Parser) =>
at: this.state.startLoc,
});
}

throw this.unexpected();
this.unexpected();
return;
case tt.num:
return this.parseLiteral(
this.state.value,
Expand Down Expand Up @@ -1733,7 +1733,7 @@ export default (superClass: typeof Parser) =>
}
}

throw this.unexpected();
this.unexpected();
}

flowParsePostfixType(): N.FlowTypeAnnotation {
Expand Down Expand Up @@ -1897,12 +1897,13 @@ export default (superClass: typeof Parser) =>
isMethod: boolean = false,
): void {
if (allowExpressionBody) {
return this.forwardNoArrowParamsConversionAt(node, () =>
this.forwardNoArrowParamsConversionAt(node, () =>
super.parseFunctionBody(node, true, isMethod),
);
return;
}

return super.parseFunctionBody(node, false, isMethod);
super.parseFunctionBody(node, false, isMethod);
}

parseFunctionBodyAndFinish<
Expand Down Expand Up @@ -2376,25 +2377,26 @@ export default (superClass: typeof Parser) =>
getTokenFromCode(code: number): void {
const next = this.input.charCodeAt(this.state.pos + 1);
if (code === charCodes.leftCurlyBrace && next === charCodes.verticalBar) {
return this.finishOp(tt.braceBarL, 2);
this.finishOp(tt.braceBarL, 2);
} else if (
this.state.inType &&
(code === charCodes.greaterThan || code === charCodes.lessThan)
) {
return this.finishOp(code === charCodes.greaterThan ? tt.gt : tt.lt, 1);
this.finishOp(code === charCodes.greaterThan ? tt.gt : tt.lt, 1);
} else if (this.state.inType && code === charCodes.questionMark) {
if (next === charCodes.dot) {
return this.finishOp(tt.questionDot, 2);
this.finishOp(tt.questionDot, 2);
} else {
// allow double nullable types in Flow: ??string
this.finishOp(tt.question, 1);
}
// allow double nullable types in Flow: ??string
return this.finishOp(tt.question, 1);
} else if (
isIteratorStart(code, next, this.input.charCodeAt(this.state.pos + 2))
) {
this.state.pos += 2; // eat "@@"
return this.readIterator();
this.readIterator();
} else {
return super.getTokenFromCode(code);
super.getTokenFromCode(code);
}
}

Expand Down Expand Up @@ -3146,7 +3148,7 @@ export default (superClass: typeof Parser) =>
}
}

return super.checkParams(
super.checkParams(
node,
allowDuplicates,
isArrowFunction,
Expand Down

0 comments on commit 4035521

Please sign in to comment.