From 2da58959c26d5afb0b8f179fc681478bf05e6e20 Mon Sep 17 00:00:00 2001 From: magic-akari Date: Sun, 10 Sep 2023 14:58:26 +0800 Subject: [PATCH] fix(es/codegen): Emit leading comments for `ExprStmt` (#7916) --- .../tests/fixture/issues-1xxx/1333/case2/output/index.js | 3 +++ crates/swc/tests/fixture/issues-2xxx/2926/output/index.js | 1 + .../swc/tests/fixture/issues-3xxx/3126/1/output/index.js | 1 + .../swc/tests/fixture/issues-4xxx/4108/1/output/index.ts | 1 + .../fixture/next.js/shared/lib/router/1/output/index.ts | 3 +++ crates/swc/tests/fixture/sourcemap/006/output/index.js | 7 ++++++- crates/swc/tests/fixture/sourcemap/006/output/index.map | 2 +- .../tsc-references/asyncMethodWithSuper_es5.1.normal.js | 4 ++++ .../classCanExtendConstructorFunction.1.normal.js | 1 + .../tests/tsc-references/fixSignatureCaching.2.minified.js | 1 + .../tsc-references/jsDeclarationsEnumTag.2.minified.js | 2 +- .../tests/tsc-references/parserRealSource6.2.minified.js | 1 + .../tests/tsc-references/parserRealSource8.2.minified.js | 1 + .../swc/tests/tsc-references/parserindenter.2.minified.js | 2 ++ crates/swc_ecma_codegen/src/lib.rs | 2 ++ .../tests/pass/resolve-name-fix/output/entry.js | 1 + 16 files changed, 30 insertions(+), 3 deletions(-) diff --git a/crates/swc/tests/fixture/issues-1xxx/1333/case2/output/index.js b/crates/swc/tests/fixture/issues-1xxx/1333/case2/output/index.js index 12f7c29227a1..a5dc861d3a07 100644 --- a/crates/swc/tests/fixture/issues-1xxx/1333/case2/output/index.js +++ b/crates/swc/tests/fixture/issues-1xxx/1333/case2/output/index.js @@ -126,6 +126,7 @@ class Shard extends _utils.Emitter { } else if (emit) { this.emit(_utils.ShardEvent.DESTROYED); } + // (Step 3) Make the websocket connection prop undefined. _class_private_field_set._(this, _ws1, undefined); // (Step 4) Set the shard status to disconnected. this.status = _utils.Status.DISCONNECTED; @@ -138,6 +139,7 @@ class Shard extends _utils.Emitter { _class_private_field_set._(this, _seq, -1); this.session.reset(); } + // (Step 7) Reset ze bucket! _class_private_field_set._(this, _bucket, new _utils.Bucket(120, 6e4)); } connect() { @@ -406,6 +408,7 @@ class Shard extends _utils.Emitter { * Guilds that are expected to be received. * @type {Set} */ this.expectedGuilds = new Set(); + // Private shit _class_private_field_set._(this, _seq, -1); _class_private_field_set._(this, _closingSeq, 0); _class_private_field_set._(this, _queue, []); diff --git a/crates/swc/tests/fixture/issues-2xxx/2926/output/index.js b/crates/swc/tests/fixture/issues-2xxx/2926/output/index.js index d177597361d2..00d1e1b1f516 100644 --- a/crates/swc/tests/fixture/issues-2xxx/2926/output/index.js +++ b/crates/swc/tests/fixture/issues-2xxx/2926/output/index.js @@ -1,5 +1,6 @@ export var webpackJsonpCallback = function(parentChunkLoadingFunction, data) { /******/ var runtime = data[2]; + //...... runtime && runtime(__webpack_require__); // return result }; diff --git a/crates/swc/tests/fixture/issues-3xxx/3126/1/output/index.js b/crates/swc/tests/fixture/issues-3xxx/3126/1/output/index.js index a370e6dd6b00..36686d374b26 100644 --- a/crates/swc/tests/fixture/issues-3xxx/3126/1/output/index.js +++ b/crates/swc/tests/fixture/issues-3xxx/3126/1/output/index.js @@ -2,6 +2,7 @@ /************************************************************************/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = { /***/ 746: /***/ function(__unused_webpack_module, __unused_webpack___webpack_exports__, __webpack_require__1) { + // originally from react Object.prototype.hasOwnProperty; // END MY ACTUAL CODE /***/ } diff --git a/crates/swc/tests/fixture/issues-4xxx/4108/1/output/index.ts b/crates/swc/tests/fixture/issues-4xxx/4108/1/output/index.ts index 13384f275217..661f66b41eb5 100644 --- a/crates/swc/tests/fixture/issues-4xxx/4108/1/output/index.ts +++ b/crates/swc/tests/fixture/issues-4xxx/4108/1/output/index.ts @@ -728,6 +728,7 @@ function _simulateTransaction() { // @ts-ignore transaction.recentBlockhash = _state.sent(); signData = transaction.serializeMessage(); + // @ts-ignore wireTransaction = transaction._serialize(signData); encodedTransaction = wireTransaction.toString("base64"); config = { diff --git a/crates/swc/tests/fixture/next.js/shared/lib/router/1/output/index.ts b/crates/swc/tests/fixture/next.js/shared/lib/router/1/output/index.ts index c361a0a51047..819839704a6d 100644 --- a/crates/swc/tests/fixture/next.js/shared/lib/router/1/output/index.ts +++ b/crates/swc/tests/fixture/next.js/shared/lib/router/1/output/index.ts @@ -159,6 +159,8 @@ var Router = /*#__PURE__*/ function() { if (!_this.urlIsNew(cleanedAs) && !localeChange) { method = "replaceState"; } + // we need to resolve the as value using rewrites for dynamic SSG + // pages to allow building the data URL correctly resolvedAs = as; // url and as should always be prefixed with basePath by this // point by either next/link or router.push/replace so strip the @@ -315,6 +317,7 @@ var Router = /*#__PURE__*/ function() { // when updating query information props.pageProps.statusCode = 500; } + // shallow routing is only allowed for same page URL changes. isValidShallowRoute = options.shallow && _this.route === route; shouldScroll = (_options_scroll = options.scroll) !== null && _options_scroll !== void 0 ? _options_scroll : !isValidShallowRoute; resetScroll = shouldScroll ? { diff --git a/crates/swc/tests/fixture/sourcemap/006/output/index.js b/crates/swc/tests/fixture/sourcemap/006/output/index.js index b90bb8208f19..934537f05469 100644 --- a/crates/swc/tests/fixture/sourcemap/006/output/index.js +++ b/crates/swc/tests/fixture/sourcemap/006/output/index.js @@ -5,7 +5,12 @@ it("should compress avif smaller than webp and smaller than jpg", /*#__PURE__*/ return _ts_generator(this, function(_state) { switch(_state.label){ case 0: - query = { + /** + * + * 'Foo bar baz' + * + * Return @ + */ query = { url: "/test.jpg", w: w, q: 75 diff --git a/crates/swc/tests/fixture/sourcemap/006/output/index.map b/crates/swc/tests/fixture/sourcemap/006/output/index.map index 8b1b9736c878..023a3a584827 100644 --- a/crates/swc/tests/fixture/sourcemap/006/output/index.map +++ b/crates/swc/tests/fixture/sourcemap/006/output/index.map @@ -1,5 +1,5 @@ { - "mappings": ";;AAAAA,GAAG,6EAA+D,oBAAA;QAOxDC,OACAC,MAQAC,MAQAC,MAQAC,MACAC,MACAC;;;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/crates/swc/tests/tsc-references/asyncMethodWithSuper_es5.1.normal.js b/crates/swc/tests/tsc-references/asyncMethodWithSuper_es5.1.normal.js index a3fc06e5b1fe..0ed173df124d 100644 --- a/crates/swc/tests/tsc-references/asyncMethodWithSuper_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/asyncMethodWithSuper_es5.1.normal.js @@ -45,7 +45,9 @@ var B = /*#__PURE__*/ function(A) { _superprop_get_y().call(_this1); // call with element access _superprop_get("x").call(_this1); + // property access (read) a = _superprop_get_x(); + // element access (read) b = _superprop_get("x"); return [ 2 @@ -89,7 +91,9 @@ var B = /*#__PURE__*/ function(A) { _superprop_get_x().call(_this1); // call with element access _superprop_get("x").call(_this1); + // property access (read) a = _superprop_get_x(); + // element access (read) b = _superprop_get("x"); // property access (assign) _superprop_set_x(f); diff --git a/crates/swc/tests/tsc-references/classCanExtendConstructorFunction.1.normal.js b/crates/swc/tests/tsc-references/classCanExtendConstructorFunction.1.normal.js index 14cfe852d8ec..7710320eee4c 100644 --- a/crates/swc/tests/tsc-references/classCanExtendConstructorFunction.1.normal.js +++ b/crates/swc/tests/tsc-references/classCanExtendConstructorFunction.1.normal.js @@ -90,6 +90,7 @@ var Conestoga = /*#__PURE__*/ function(Wagon1) { function Conestoga(drunkOO) { _class_call_check(this, Conestoga); var _this; + // error: wrong type _this = _super.call(this, "nope"); _this.drunkOO = drunkOO; return _this; diff --git a/crates/swc/tests/tsc-references/fixSignatureCaching.2.minified.js b/crates/swc/tests/tsc-references/fixSignatureCaching.2.minified.js index 7e4d573ec679..fe6199893a0b 100644 --- a/crates/swc/tests/tsc-references/fixSignatureCaching.2.minified.js +++ b/crates/swc/tests/tsc-references/fixSignatureCaching.2.minified.js @@ -415,6 +415,7 @@ import { _ as _instanceof } from "@swc/helpers/_/_instanceof"; cache.mobile = cache.phone = phone, cache.tablet = null; return; // unambiguously identified as phone } + // our rules haven't found a match -> try more general fallback rules impl.isMobileFallback(userAgent) ? undefined === (phoneSized = MobileDetect.isPhoneSized(maxPhoneWidth)) ? (cache.mobile = impl.FALLBACK_MOBILE, cache.tablet = cache.phone = null) : phoneSized ? (cache.mobile = cache.phone = impl.FALLBACK_PHONE, cache.tablet = null) : (cache.mobile = cache.tablet = impl.FALLBACK_TABLET, cache.phone = null) : impl.isTabletFallback(userAgent) ? (cache.mobile = cache.tablet = impl.FALLBACK_TABLET, cache.phone = null) : // not mobile at all! cache.mobile = cache.tablet = cache.phone = null; } diff --git a/crates/swc/tests/tsc-references/jsDeclarationsEnumTag.2.minified.js b/crates/swc/tests/tsc-references/jsDeclarationsEnumTag.2.minified.js index 4fef39a6b5fc..7e0667497e70 100644 --- a/crates/swc/tests/tsc-references/jsDeclarationsEnumTag.2.minified.js +++ b/crates/swc/tests/tsc-references/jsDeclarationsEnumTag.2.minified.js @@ -25,7 +25,7 @@ * @param {Second} s * @param {Fs} f */ export function consume(t, s, f) { - Target.START; + /** @type {Target} */ Target.START; } /** @param {string} s */ export function ff(s) { return(// element access with arbitrary string is an error only with noImplicitAny diff --git a/crates/swc/tests/tsc-references/parserRealSource6.2.minified.js b/crates/swc/tests/tsc-references/parserRealSource6.2.minified.js index f4c32cdf0856..2c4a2942b02f 100644 --- a/crates/swc/tests/tsc-references/parserRealSource6.2.minified.js +++ b/crates/swc/tests/tsc-references/parserRealSource6.2.minified.js @@ -19,6 +19,7 @@ TypeScript1 = TypeScript || (TypeScript = {}), preFindEnclosingScope = function( }, context.scopeStartAST = ast, context.enclosingClassDecl = ast; break; case NodeType.ObjectLit: + // Only consider target-typed object literals ast.targetType && (context.scopeGetter = function() { return ast.targetType.containedScope; }, context.objectLiteralScopeGetter = function() { diff --git a/crates/swc/tests/tsc-references/parserRealSource8.2.minified.js b/crates/swc/tests/tsc-references/parserRealSource8.2.minified.js index 8ac36abf07ec..96a5bf3eac43 100644 --- a/crates/swc/tests/tsc-references/parserRealSource8.2.minified.js +++ b/crates/swc/tests/tsc-references/parserRealSource8.2.minified.js @@ -17,6 +17,7 @@ TypeScript1 = TypeScript || (TypeScript = {}), pushAssignScope = function(scope, var memberScope = null, aggScope = null; ast.name && ast.mod && (ast.name.sym = ast.mod.symbol); var mod = ast.mod; + // We're likely here because of error recovery mod && (memberScope = new SymbolTableScope(mod.members, mod.ambientMembers, mod.enclosedTypes, mod.ambientEnclosedTypes, mod.symbol), mod.memberScope = memberScope, context.modDeclChain.push(ast), context.typeFlow.checker.currentModDecl = ast, (aggScope = new SymbolAggregateScope(mod.symbol)).addParentScope(memberScope), aggScope.addParentScope(context.scopeChain.scope), pushAssignScope(aggScope, context, null, null, null), mod.containedScope = aggScope, mod.symbol && context.typeFlow.addLocalsFromScope(mod.containedScope, mod.symbol, ast.vars, mod.members.privateMembers, !0)); }, preAssignClassScopes = function(ast, context) { var memberScope = null, aggScope = null; diff --git a/crates/swc/tests/tsc-references/parserindenter.2.minified.js b/crates/swc/tests/tsc-references/parserindenter.2.minified.js index 1c0b95f924c1..599b21c8ad00 100644 --- a/crates/swc/tests/tsc-references/parserindenter.2.minified.js +++ b/crates/swc/tests/tsc-references/parserindenter.2.minified.js @@ -277,6 +277,7 @@ Formatting1 = Formatting || (Formatting = {}), Indenter = function() { } // The parent node to take its indentation is the first parent that has indentation. for(; null != parent && !parent.CanIndent();)parent = parent.Parent; + // Skip Program since it has no indentation null != parent && parent.AuthorNode.Details.Kind != AuthorParseNodeKind.apnkProg && (offset = parent.AuthorNode.Details.StartOffset, indentNode = parent); } else { indentNode = tree.StartNodeSelf, offset = tree.StartNodePreviousSibling.Details.StartOffset; @@ -289,6 +290,7 @@ Formatting1 = Formatting || (Formatting = {}), Indenter = function() { } if (null != indentNode) { var indentOverride = this.GetLineIndentationForOffset(offset); + // Set the indentation on all the siblings to be the same as indentNode this.smartIndent || null === tree.StartNodePreviousSibling || null == indentNode.Parent || ParseNodeExtensions.GetChildren(indentNode.Parent).foreach(function(sibling) { sibling !== indentNode && sibling.CanIndent() && sibling.SetIndentationOverride(indentOverride); }); diff --git a/crates/swc_ecma_codegen/src/lib.rs b/crates/swc_ecma_codegen/src/lib.rs index c30f9d3c8675..02cf1a169fe3 100644 --- a/crates/swc_ecma_codegen/src/lib.rs +++ b/crates/swc_ecma_codegen/src/lib.rs @@ -2766,6 +2766,8 @@ where #[emitter] #[tracing::instrument(skip_all)] fn emit_expr_stmt(&mut self, e: &ExprStmt) -> Result { + self.emit_leading_comments_of_span(e.span, false)?; + emit!(e.expr); semi!(); diff --git a/crates/swc_node_bundler/tests/pass/resolve-name-fix/output/entry.js b/crates/swc_node_bundler/tests/pass/resolve-name-fix/output/entry.js index 7940dc3b7d8f..a5875526812f 100644 --- a/crates/swc_node_bundler/tests/pass/resolve-name-fix/output/entry.js +++ b/crates/swc_node_bundler/tests/pass/resolve-name-fix/output/entry.js @@ -4569,6 +4569,7 @@ var load228 = __swcpack_require__.bind(void 0, function(module, exports) { var $DataView = global[DATA_VIEW]; var Math1 = global.Math; var RangeError1 = global.RangeError; + // eslint-disable-next-line no-shadow-restricted-names global.Infinity; var BaseBuffer = $ArrayBuffer; var abs = Math1.abs;