From 40355210689f328d1a96d6353a661de8449da587 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Tue, 14 Mar 2023 21:52:33 +0800 Subject: [PATCH] chore: Enable rule `no-confusing-void-expression` (#15485) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Huáng Jùnliàng Co-authored-by: Nicolò Ribaudo --- .eslintrc.cjs | 4 ++ packages/babel-core/src/config/index.ts | 9 ++- packages/babel-core/src/transform-file.ts | 2 +- .../babel-core/src/transformation/index.ts | 2 + .../src/transformation/plugin-pass.ts | 3 +- packages/babel-generator/src/buffer.ts | 11 ++-- .../babel-generator/src/generators/types.ts | 2 +- packages/babel-generator/src/printer.ts | 14 +++-- .../babel-parser/src/parser/expression.ts | 16 ++--- packages/babel-parser/src/parser/statement.ts | 6 +- packages/babel-parser/src/parser/util.ts | 2 +- .../babel-parser/src/plugins/flow/index.ts | 36 +++++------ .../babel-parser/src/plugins/jsx/index.ts | 35 ++++++----- .../babel-parser/src/plugins/placeholders.ts | 6 +- .../src/plugins/typescript/index.ts | 16 ++--- packages/babel-parser/src/tokenizer/index.ts | 7 +-- .../src/util.ts | 3 +- .../src/index.ts | 19 ++++-- .../src/namespace.ts | 2 +- .../src/transformScriptTags.ts | 2 +- packages/babel-traverse/src/index.ts | 3 +- packages/babel-traverse/src/path/context.ts | 6 +- .../babel-traverse/src/path/evaluation.ts | 61 +++++++++++-------- packages/babel-traverse/src/scope/index.ts | 10 +-- packages/babel-types/src/definitions/utils.ts | 2 +- 25 files changed, 166 insertions(+), 113 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 9bd24c1ae1a5..2506bf94189c 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -65,6 +65,10 @@ module.exports = { }, ], "@typescript-eslint/no-unnecessary-type-assertion": "error", + "@typescript-eslint/no-confusing-void-expression": [ + "error", + { ignoreArrowShorthand: true }, + ], }, }, { diff --git a/packages/babel-core/src/config/index.ts b/packages/babel-core/src/config/index.ts index 9ae91c1ff0e3..f341ede87930 100644 --- a/packages/babel-core/src/config/index.ts +++ b/packages/babel-core/src/config/index.ts @@ -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); @@ -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); } diff --git a/packages/babel-core/src/transform-file.ts b/packages/babel-core/src/transform-file.ts index c493425db86b..6be73468fb58 100644 --- a/packages/babel-core/src/transform-file.ts +++ b/packages/babel-core/src/transform-file.ts @@ -40,7 +40,7 @@ export function transformFile( export function transformFile( ...args: Parameters ) { - return transformFileRunner.errback(...args); + transformFileRunner.errback(...args); } export function transformFileSync( diff --git a/packages/babel-core/src/transformation/index.ts b/packages/babel-core/src/transformation/index.ts index e5e64b10a925..ae780fc2ab02 100644 --- a/packages/babel-core/src/transformation/index.ts +++ b/packages/babel-core/src/transformation/index.ts @@ -94,6 +94,7 @@ function* transformFile(file: File, pluginPasses: PluginPasses): Handler { 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 @@ -121,6 +122,7 @@ function* transformFile(file: File, pluginPasses: PluginPasses): Handler { 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 diff --git a/packages/babel-core/src/transformation/plugin-pass.ts b/packages/babel-core/src/transformation/plugin-pass.ts index 5b06282a8321..22cf2c5a0a7e 100644 --- a/packages/babel-core/src/transformation/plugin-pass.ts +++ b/packages/babel-core/src/transformation/plugin-pass.ts @@ -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( diff --git a/packages/babel-generator/src/buffer.ts b/packages/babel-generator/src/buffer.ts index 6a9d61dfd4ec..02b6982dd5a9 100644 --- a/packages/babel-generator/src/buffer.ts +++ b/packages/babel-generator/src/buffer.ts @@ -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 @@ -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(); } diff --git a/packages/babel-generator/src/generators/types.ts b/packages/babel-generator/src/generators/types.ts index 7375e9bd0472..cb763e123bfd 100644 --- a/packages/babel-generator/src/generators/types.ts +++ b/packages/babel-generator/src/generators/types.ts @@ -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) { diff --git a/packages/babel-generator/src/printer.ts b/packages/babel-generator/src/printer.ts index 16098e5640cd..72438de1be8d 100644 --- a/packages/babel-generator/src/printer.ts +++ b/packages/babel-generator/src/printer.ts @@ -331,7 +331,10 @@ class Printer { } exactSource(loc: Loc | undefined, cb: () => void) { - if (!loc) return cb(); + if (!loc) { + cb(); + return; + } this._catchUp("start", loc); @@ -364,7 +367,10 @@ class Printer { loc: Loc | undefined, cb: () => void, ): void { - if (!loc) return cb(); + if (!loc) { + cb(); + return; + } this._catchUp(prop, loc); @@ -874,7 +880,7 @@ 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 = {}) { @@ -882,7 +888,7 @@ class Printer { opts.separator = commaSeparator; } - return this.printJoin(items, parent, opts); + this.printJoin(items, parent, opts); } _printNewline(newLine: boolean, opts: AddNewlinesOptions) { diff --git a/packages/babel-parser/src/parser/expression.ts b/packages/babel-parser/src/parser/expression.ts index f38fe064994f..d9f8a8d66e03 100644 --- a/packages/babel-parser/src/parser/expression.ts +++ b/packages/babel-parser/src/parser/expression.ts @@ -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: { @@ -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: @@ -1345,7 +1345,7 @@ export default abstract class ExpressionParser extends LValParser { return id; } else { - throw this.unexpected(); + this.unexpected(); } } } @@ -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(); } } @@ -2387,7 +2387,7 @@ export default abstract class ExpressionParser extends LValParser { break; } default: - throw this.unexpected(); + this.unexpected(); } } (prop as any).key = key; @@ -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); diff --git a/packages/babel-parser/src/parser/statement.ts b/packages/babel-parser/src/parser/statement.ts index c52b41e8412e..7fab4fa1d5fa 100644 --- a/packages/babel-parser/src/parser/statement.ts +++ b/packages/babel-parser/src/parser/statement.ts @@ -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; @@ -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 diff --git a/packages/babel-parser/src/parser/util.ts b/packages/babel-parser/src/parser/util.ts index b6195401461b..8a3d398de3f4 100644 --- a/packages/babel-parser/src/parser/util.ts +++ b/packages/babel-parser/src/parser/util.ts @@ -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); } } diff --git a/packages/babel-parser/src/plugins/flow/index.ts b/packages/babel-parser/src/plugins/flow/index.ts index e4ed21f1d96c..2b01dee571db 100644 --- a/packages/babel-parser/src/plugins/flow/index.ts +++ b/packages/babel-parser/src/plugins/flow/index.ts @@ -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 { @@ -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 { @@ -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(); } } @@ -654,7 +654,7 @@ export default (superClass: typeof Parser) => } } - throw this.unexpected(); + this.unexpected(); } flowParseDeclareModuleExports( @@ -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, @@ -1733,7 +1733,7 @@ export default (superClass: typeof Parser) => } } - throw this.unexpected(); + this.unexpected(); } flowParsePostfixType(): N.FlowTypeAnnotation { @@ -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< @@ -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); } } @@ -3146,7 +3148,7 @@ export default (superClass: typeof Parser) => } } - return super.checkParams( + super.checkParams( node, allowDuplicates, isArrowFunction, diff --git a/packages/babel-parser/src/plugins/jsx/index.ts b/packages/babel-parser/src/plugins/jsx/index.ts index 70dae0855f6c..f58b33182277 100644 --- a/packages/babel-parser/src/plugins/jsx/index.ts +++ b/packages/babel-parser/src/plugins/jsx/index.ts @@ -106,12 +106,15 @@ export default (superClass: typeof Parser) => if (this.state.pos === this.state.start) { if (ch === charCodes.lessThan && this.state.canStartJSXElement) { ++this.state.pos; - return this.finishToken(tt.jsxTagStart); + this.finishToken(tt.jsxTagStart); + } else { + super.getTokenFromCode(ch); } - return super.getTokenFromCode(ch); + return; } out += this.input.slice(chunkStart, this.state.pos); - return this.finishToken(tt.jsxText, out); + this.finishToken(tt.jsxText, out); + return; case charCodes.ampersand: out += this.input.slice(chunkStart, this.state.pos); @@ -187,7 +190,7 @@ export default (superClass: typeof Parser) => } } out += this.input.slice(chunkStart, this.state.pos++); - return this.finishToken(tt.string, out); + this.finishToken(tt.string, out); } jsxReadEntity(): string { @@ -254,10 +257,7 @@ export default (superClass: typeof Parser) => do { ch = this.input.charCodeAt(++this.state.pos); } while (isIdentifierChar(ch) || ch === charCodes.dash); - return this.finishToken( - tt.jsxName, - this.input.slice(start, this.state.pos), - ); + this.finishToken(tt.jsxName, this.input.slice(start, this.state.pos)); } // Parse next token as JSX identifier @@ -491,7 +491,7 @@ export default (superClass: typeof Parser) => } // istanbul ignore next - should never happen default: - throw this.unexpected(); + this.unexpected(); } } @@ -584,24 +584,28 @@ export default (superClass: typeof Parser) => const context = this.curContext(); if (context === tc.j_expr) { - return this.jsxReadToken(); + this.jsxReadToken(); + return; } if (context === tc.j_oTag || context === tc.j_cTag) { if (isIdentifierStart(code)) { - return this.jsxReadWord(); + this.jsxReadWord(); + return; } if (code === charCodes.greaterThan) { ++this.state.pos; - return this.finishToken(tt.jsxTagEnd); + this.finishToken(tt.jsxTagEnd); + return; } if ( (code === charCodes.quotationMark || code === charCodes.apostrophe) && context === tc.j_oTag ) { - return this.jsxReadString(code); + this.jsxReadString(code); + return; } } @@ -611,10 +615,11 @@ export default (superClass: typeof Parser) => this.input.charCodeAt(this.state.pos + 1) !== charCodes.exclamationMark ) { ++this.state.pos; - return this.finishToken(tt.jsxTagStart); + this.finishToken(tt.jsxTagStart); + return; } - return super.getTokenFromCode(code); + super.getTokenFromCode(code); } updateContext(prevType: TokenType): void { diff --git a/packages/babel-parser/src/plugins/placeholders.ts b/packages/babel-parser/src/plugins/placeholders.ts index 7e7eb5b2350c..c030877fffcd 100644 --- a/packages/babel-parser/src/plugins/placeholders.ts +++ b/packages/babel-parser/src/plugins/placeholders.ts @@ -78,10 +78,10 @@ export default (superClass: typeof Parser) => code === charCodes.percentSign && this.input.charCodeAt(this.state.pos + 1) === charCodes.percentSign ) { - return this.finishOp(tt.placeholder, 2); + this.finishOp(tt.placeholder, 2); + } else { + super.getTokenFromCode(code); } - - return super.getTokenFromCode(code); } /* ============================================================ * diff --git a/packages/babel-parser/src/plugins/typescript/index.ts b/packages/babel-parser/src/plugins/typescript/index.ts index 7f1b9e9829f3..a4d02f7dfdd8 100644 --- a/packages/babel-parser/src/plugins/typescript/index.ts +++ b/packages/babel-parser/src/plugins/typescript/index.ts @@ -1182,7 +1182,7 @@ export default (superClass: ClassWithMixin) => // For compatibility to estree we cannot call parseLiteral directly here return super.parseExprAtom(); default: - throw this.unexpected(); + this.unexpected(); } })(); return this.finishNode(node, "TSLiteralType"); @@ -1221,7 +1221,7 @@ export default (superClass: ClassWithMixin) => const node = this.startNode(); const nextToken = this.lookahead(); if (nextToken.type !== tt.num && nextToken.type !== tt.bigint) { - throw this.unexpected(); + this.unexpected(); } // @ts-expect-error: parseMaybeUnary must returns unary expression node.literal = this.parseMaybeUnary(); @@ -1283,7 +1283,7 @@ export default (superClass: ClassWithMixin) => } } - throw this.unexpected(); + this.unexpected(); } tsParseArrayTypeOrHigher(): N.TsType { @@ -1968,7 +1968,7 @@ export default (superClass: ClassWithMixin) => this.expectContextual(tt._require); this.expect(tt.parenL); if (!this.match(tt.string)) { - throw this.unexpected(); + this.unexpected(); } // For compatibility to estree we cannot call parseLiteral directly here node.expression = super.parseExprAtom() as N.StringLiteral; @@ -3751,13 +3751,15 @@ export default (superClass: ClassWithMixin) => getTokenFromCode(code: number): void { if (this.state.inType) { if (code === charCodes.greaterThan) { - return this.finishOp(tt.gt, 1); + this.finishOp(tt.gt, 1); + return; } if (code === charCodes.lessThan) { - return this.finishOp(tt.lt, 1); + this.finishOp(tt.lt, 1); + return; } } - return super.getTokenFromCode(code); + super.getTokenFromCode(code); } // used after we have finished parsing types diff --git a/packages/babel-parser/src/tokenizer/index.ts b/packages/babel-parser/src/tokenizer/index.ts index 9d3096d942c0..4e5c9154c531 100644 --- a/packages/babel-parser/src/tokenizer/index.ts +++ b/packages/babel-parser/src/tokenizer/index.ts @@ -231,9 +231,7 @@ export default abstract class Tokenizer extends CommentsParser { return this.state.context[this.state.context.length - 1]; } - // Read a single token, updating the parser object's token-related - // properties. - + // Read a single token, updating the parser object's token-related properties. nextToken(): void { this.skipSpace(); this.state.start = this.state.pos; @@ -679,7 +677,7 @@ export default abstract class Tokenizer extends CommentsParser { // `^^^` is forbidden and must be separated by a space. const lookaheadCh = this.input.codePointAt(this.state.pos); if (lookaheadCh === charCodes.caret) { - throw this.unexpected(); + this.unexpected(); } } // '^' @@ -825,7 +823,6 @@ export default abstract class Tokenizer extends CommentsParser { case charCodes.dot: this.readToken_dot(); return; - // Punctuation tokens. case charCodes.leftParenthesis: ++this.state.pos; diff --git a/packages/babel-plugin-transform-destructuring/src/util.ts b/packages/babel-plugin-transform-destructuring/src/util.ts index e34bc7cd07ce..451aed21cd7b 100644 --- a/packages/babel-plugin-transform-destructuring/src/util.ts +++ b/packages/babel-plugin-transform-destructuring/src/util.ts @@ -439,7 +439,8 @@ export class DestructuringTransformer { // eg: let [a, b] = [1, 2]; if (this.canUnpackArrayPattern(pattern, arrayRef)) { - return this.pushUnpackedArrayPattern(pattern, arrayRef); + this.pushUnpackedArrayPattern(pattern, arrayRef); + return; } // if we have a rest then we need all the elements so don't tell 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 f6f22af6d011..27e8b1e6726b 100644 --- a/packages/babel-plugin-transform-react-constant-elements/src/index.ts +++ b/packages/babel-plugin-transform-react-constant-elements/src/index.ts @@ -92,14 +92,18 @@ export default declare((api, options: Options) => { path.skip(); }; - if (path.isJSXClosingElement()) return skip(); + if (path.isJSXClosingElement()) { + skip(); + return; + } // Elements with refs are not safe to hoist. if ( path.isJSXIdentifier({ name: "ref" }) && path.parentPath.isJSXAttribute({ name: path.node }) ) { - return stop(); + stop(); + return; } // Ignore JSX expressions and immutable values. @@ -123,10 +127,14 @@ export default declare((api, options: Options) => { const { mutablePropsAllowed } = state; if (mutablePropsAllowed && path.isFunction()) { path.traverse(targetScopeVisitor, state); - return skip(); + skip(); + return; } - if (!path.isPure()) return stop(); + if (!path.isPure()) { + stop(); + return; + } // If it's not immutable, it may still be a pure expression, such as string concatenation. // It is still safe to hoist that, so long as its result is immutable. @@ -142,7 +150,8 @@ export default declare((api, options: Options) => { (typeof value !== "object" && typeof value !== "function") ) { // It evaluated to an immutable value, so we can hoist it. - return skip(); + skip(); + return; } } else if (t.isIdentifier(expressionResult.deopt)) { // It's safe to hoist here if the deopt reason is an identifier (e.g. func param). diff --git a/packages/babel-plugin-transform-typescript/src/namespace.ts b/packages/babel-plugin-transform-typescript/src/namespace.ts index 2c232468bd2f..3ccc7a19dafd 100644 --- a/packages/babel-plugin-transform-typescript/src/namespace.ts +++ b/packages/babel-plugin-transform-typescript/src/namespace.ts @@ -108,7 +108,7 @@ function handleVariableDeclaration( } function buildNestedAmbientModuleError(path: NodePath, node: t.Node) { - throw path.hub.buildError( + return path.hub.buildError( node, "Ambient modules cannot be nested in other modules or namespaces.", Error, diff --git a/packages/babel-standalone/src/transformScriptTags.ts b/packages/babel-standalone/src/transformScriptTags.ts index 48294afd0fe4..94baee8d83b3 100644 --- a/packages/babel-standalone/src/transformScriptTags.ts +++ b/packages/babel-standalone/src/transformScriptTags.ts @@ -125,7 +125,7 @@ function load( } } }; - return xhr.send(null); + xhr.send(null); } /** diff --git a/packages/babel-traverse/src/index.ts b/packages/babel-traverse/src/index.ts index 03fdc6e58725..401544bf9231 100644 --- a/packages/babel-traverse/src/index.ts +++ b/packages/babel-traverse/src/index.ts @@ -78,7 +78,8 @@ traverse.verify = visitors.verify; traverse.explode = visitors.explode; traverse.cheap = function (node: t.Node, enter: (node: t.Node) => void) { - return traverseFast(node, enter); + traverseFast(node, enter); + return; }; traverse.node = function ( diff --git a/packages/babel-traverse/src/path/context.ts b/packages/babel-traverse/src/path/context.ts index 6fb9f5047ddc..f5ac6b49b170 100644 --- a/packages/babel-traverse/src/path/context.ts +++ b/packages/babel-traverse/src/path/context.ts @@ -214,14 +214,16 @@ export function _resyncKey(this: NodePath) { if (Array.isArray(this.container)) { for (let i = 0; i < this.container.length; i++) { if (this.container[i] === this.node) { - return this.setKey(i); + this.setKey(i); + return; } } } else { for (const key of Object.keys(this.container)) { // @ts-expect-error this.key should present in this.container if (this.container[key] === this.node) { - return this.setKey(key); + this.setKey(key); + return; } } } diff --git a/packages/babel-traverse/src/path/evaluation.ts b/packages/babel-traverse/src/path/evaluation.ts index 21259ac6a08b..cf2b6107c84e 100644 --- a/packages/babel-traverse/src/path/evaluation.ts +++ b/packages/babel-traverse/src/path/evaluation.ts @@ -62,6 +62,12 @@ function deopt(path: NodePath, state: State) { state.confident = false; } +const Globals = new Map([ + ["undefined", undefined], + ["Infinity", Infinity], + ["NaN", NaN], +]); + /** * We wrap the _evaluate method so we can track `seen` nodes, we push an item * to the map before we actually evaluate it so we can deopt on self recursive @@ -191,31 +197,34 @@ function _evaluate(path: NodePath, state: State): any { if (path.isReferencedIdentifier()) { const binding = path.scope.getBinding(path.node.name); - if (binding && binding.constantViolations.length > 0) { - return deopt(binding.path, state); + if (binding) { + if ( + binding.constantViolations.length > 0 || + path.node.start < binding.path.node.end + ) { + deopt(binding.path, state); + return; + } + if (binding.hasValue) { + return binding.value; + } } - if (binding && path.node.start < binding.path.node.end) { - return deopt(binding.path, state); + const name = path.node.name; + if (Globals.has(name)) { + if (!binding) { + return Globals.get(name); + } + deopt(binding.path, state); + return; } - if (binding?.hasValue) { - return binding.value; + const resolved = path.resolve(); + if (resolved === path) { + deopt(path, state); + return; } else { - if (path.node.name === "undefined") { - return binding ? deopt(binding.path, state) : undefined; - } else if (path.node.name === "Infinity") { - return binding ? deopt(binding.path, state) : Infinity; - } else if (path.node.name === "NaN") { - return binding ? deopt(binding.path, state) : NaN; - } - - const resolved = path.resolve(); - if (resolved === path) { - return deopt(path, state); - } else { - return evaluateCached(resolved, state); - } + return evaluateCached(resolved, state); } } @@ -258,7 +267,8 @@ function _evaluate(path: NodePath, state: State): any { if (elemValue.confident) { arr.push(elemValue.value); } else { - return deopt(elemValue.deopt, state); + deopt(elemValue.deopt, state); + return; } } return arr; @@ -269,7 +279,8 @@ function _evaluate(path: NodePath, state: State): any { const props = path.get("properties"); for (const prop of props) { if (prop.isObjectMethod() || prop.isSpreadElement()) { - return deopt(prop, state); + deopt(prop, state); + return; } const keyPath = (prop as NodePath).get("key"); let key; @@ -277,7 +288,8 @@ function _evaluate(path: NodePath, state: State): any { if (prop.node.computed) { key = keyPath.evaluate(); if (!key.confident) { - return deopt(key.deopt, state); + deopt(key.deopt, state); + return; } key = key.value; } else if (keyPath.isIdentifier()) { @@ -290,7 +302,8 @@ function _evaluate(path: NodePath, state: State): any { const valuePath = (prop as NodePath).get("value"); let value = valuePath.evaluate(); if (!value.confident) { - return deopt(value.deopt, state); + deopt(value.deopt, state); + return; } value = value.value; // @ts-expect-error key is any type diff --git a/packages/babel-traverse/src/scope/index.ts b/packages/babel-traverse/src/scope/index.ts index 27128abf13a7..885e86b754ad 100644 --- a/packages/babel-traverse/src/scope/index.ts +++ b/packages/babel-traverse/src/scope/index.ts @@ -629,10 +629,12 @@ export default class Scope { if (binding) { newName ||= this.generateUidIdentifier(oldName).name; const renamer = new Renamer(binding, oldName, newName); - return process.env.BABEL_8_BREAKING - ? renamer.rename() - : // @ts-expect-error: babel 7->8 - renamer.rename(arguments[2]); + if (process.env.BABEL_8_BREAKING) { + renamer.rename(); + } else { + // @ts-ignore(Babel 7 vs Babel 8) TODO: Delete this + renamer.rename(arguments[2]); + } } } diff --git a/packages/babel-types/src/definitions/utils.ts b/packages/babel-types/src/definitions/utils.ts index e06d4c8cfc7f..7cbf0c701115 100644 --- a/packages/babel-types/src/definitions/utils.ts +++ b/packages/babel-types/src/definitions/utils.ts @@ -292,7 +292,7 @@ export function defineAliasedType(...aliases: string[]) { } const additional = aliases.filter(a => !defined.includes(a)); defined.unshift(...additional); - return defineType(type, opts); + defineType(type, opts); }; }