Skip to content

Commit

Permalink
fix(es/codegen): Check for trailing comments while emitting an arrow (#…
Browse files Browse the repository at this point in the history
…8257)

**Description:**

This PR also **fixes** the `Default` implementation of `JsMinifyFormatOptions`. It didn't matched the default value when it's used with `serde::Deserialize`.


**Related issue:**

 - Closes #8251
  • Loading branch information
kdy1 authored Nov 10, 2023
1 parent 981ab77 commit 0faa2c4
Show file tree
Hide file tree
Showing 51 changed files with 188 additions and 301 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function f() {
console.log(value$2);
}
[].map(function x(_) {
var value$2; // this causes the previous value$2 to become value$21
var value$2;
return value$2;
});
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var n;
module.exports = ((n = /*#__PURE__*/ function() {
module.exports = ((n = function() {
function n() {}
return n.prototype.it = function() {
this.bb = new n.MyA();
Expand Down
4 changes: 2 additions & 2 deletions crates/swc/tests/fixture/issues-2xxx/2854/1/output/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export function App() {
return /*#__PURE__*/ React.createElement(Form, null);
return React.createElement(Form, null);
}
export function Form(param) {
var _param_onChange = param.onChange, onChange = _param_onChange === void 0 ? function() {} : _param_onChange;
return /*#__PURE__*/ React.createElement("input", {
return React.createElement("input", {
onChange: function onChange1() {
onChange();
}
Expand Down
4 changes: 1 addition & 3 deletions crates/swc/tests/fixture/issues-2xxx/2926/output/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export var webpackJsonpCallback = function(parentChunkLoadingFunction, data) {
/******/ var runtime = data[2];
//......
var runtime = data[2];
runtime && runtime(__webpack_require__);
// return result
};
19 changes: 8 additions & 11 deletions crates/swc/tests/fixture/issues-3xxx/3126/1/output/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
/******/ var __webpack_exports__;
/************************************************************************/ /******/ /******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = {
/***/ 746: /***/ function(__unused_webpack_module, __unused_webpack___webpack_exports__, __webpack_require__1) {
// originally from react
var __webpack_exports__;
__webpack_require__.m = {
746: function(__unused_webpack_module, __unused_webpack___webpack_exports__, __webpack_require__1) {
Object.prototype.hasOwnProperty;
// END MY ACTUAL CODE
/***/ }
}, /******/ __webpack_require__.O = function(result, chunkIds, fn, priority) {
/******/ for(var j = 0; j < chunkIds.length; j++)/******/ Object.keys(__webpack_require__.O).every(function(key) {
}
}, __webpack_require__.O = function(result, chunkIds, fn, priority) {
for(var j = 0; j < chunkIds.length; j++)Object.keys(__webpack_require__.O).every(function(key) {
return __webpack_require__.O[key](chunkIds[j]);
});
/******/ }, __webpack_exports__ = __webpack_require__.O(void 0, [
}, __webpack_exports__ = __webpack_require__.O(void 0, [
532
], function() {
return __webpack_require__(746);
}), /******/ __webpack_exports__ = __webpack_require__.O(__webpack_exports__);
}), __webpack_exports__ = __webpack_require__.O(__webpack_exports__);
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ ServiceError = _ts_decorate._([
Code[Code["badResponse"] = 422] = "badResponse";
})(Code = ServiceError.Code || (ServiceError.Code = {}));
class ServiceNotFound extends ServiceError {
// Service was probably not registered, or using the wrong channel
code = 404;
name = "ServiceError.ServiceNotFound";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
// filename: types.d.ts
export const aThing;
2 changes: 0 additions & 2 deletions crates/swc/tests/fixture/issues-4xxx/4120/1/output/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
// b becomes "A-Za-z\xc0-\xd6\xd8-\xf6\xf8-\u02B8\u0300-\u0590\u0900-\u1FFF\u200E\u2C00-\\\0ud801\\\0ud804-\\\0ud839\\\0ud83c-\\\0udbff\uF900-\uFB1C\uFE00-\uFE6F\uFEFD-\uFFFF"
// ref: both strings are contained in 'google-protobuf'.
export default{a:"֑-ۯۺ-ࣿ‏\ud802-\ud803\ud83a-\ud83bיִ-﷿ﹰ-ﻼ",b:"A-Za-z\xc0-\xd6\xd8-\xf6\xf8-ʸ̀-֐ऀ-῿‎Ⰰ-\ud801\ud804-\ud839\ud83c-\udbff豈-﬜︀-﹯﻽-￿"};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"mappings": "AAMA,+KAA+K;AAE/K,wDAAwD;AAExD,cAAe,CAAEA,EATb,0CASgBC,EALhB,kFAKkB,CAAE",
"mappings": "AAUA,cAAe,CAAEA,EATb,0CASgBC,EALhB,kFAKkB,CAAE",
"names": [
"a",
"b"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export var CompanyBgStore = function CompanyBgStore() {
_class_call_check(this, CompanyBgStore);
_define_property(this, "corpName", 123);
var _this = this;
_define_property(this, "getBusinessInfo", /*#__PURE__*/ _async_to_generator(function() {
_define_property(this, "getBusinessInfo", _async_to_generator(function() {
var corpName;
var _arguments = arguments;
return _ts_generator(this, function(_state) {
Expand Down
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-4xxx/4891/output/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { _ as _class_private_field_loose_base } from "@swc/helpers/_/_class_private_field_loose_base";
import { _ as _class_private_field_loose_key } from "@swc/helpers/_/_class_private_field_loose_key";
var _channelName = /*#__PURE__*/ _class_private_field_loose_key("_channelName"), _listeners = /*#__PURE__*/ _class_private_field_loose_key("_listeners");
var _channelName = _class_private_field_loose_key("_channelName"), _listeners = _class_private_field_loose_key("_listeners");
export class LocalStorageChannel {
constructor(channelName){
Object.defineProperty(this, _channelName, {
Expand Down
1 change: 0 additions & 1 deletion crates/swc/tests/fixture/issues-4xxx/4899/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ define([
], function(require, _ts_decorate) {
"use strict";
function es5ClassCompat(target) {
///@ts-expect-error
function _() {
return Reflect.construct(target, arguments, this.constructor);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-4xxx/4953/output/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable */ "use strict";
"use strict";
function _default(e, t) {
"use strict";
t.vr = function(e, t, r) {
Expand Down
6 changes: 0 additions & 6 deletions crates/swc/tests/fixture/issues-5xxx/5258/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ define([
}
});
function es5ClassCompat(target) {
///@ts-expect-error
function _() {
return Reflect.construct(target, arguments, this.constructor);
}
Expand Down Expand Up @@ -44,16 +43,11 @@ define([
super(URI.isUri(uriOrMessage) ? uriOrMessage.toString(true) : uriOrMessage);
_define_property._(this, "code", void 0);
this.code = terminator?.name ?? 'Unknown';
// mark the error as file system provider error so that
// we can extract the error code on the receiving side
markAsFileSystemProviderError(this, code);
// workaround when extending builtin objects and when compiling to ES5, see:
// https://github.com/microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
if (typeof Object.setPrototypeOf === 'function') {
Object.setPrototypeOf(this, FileSystemError.prototype);
}
if (typeof Error.captureStackTrace === 'function' && typeof terminator === 'function') {
// nice stack traces
Error.captureStackTrace(this, terminator);
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-5xxx/5557/output/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { _ as _create_class } from "@swc/helpers/_/_create_class";
var commands;
var command;
function retornaTempoArenaEmMilisegundos(distancia, velocidade) {
var conv = distancia * /*#__PURE__*/ function() {
var conv = distancia * function() {
"use strict";
function LogExit(param) {
var logger = param.logger;
Expand Down
4 changes: 2 additions & 2 deletions crates/swc/tests/fixture/issues-5xxx/5558/2/output/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _create_class } from "@swc/helpers/_/_create_class";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
var Foo = /*#__PURE__*/ function() {
var Foo = function() {
"use strict";
function Foo() {
_class_call_check(this, Foo);
Expand All @@ -11,7 +11,7 @@ var Foo = /*#__PURE__*/ function() {
{
key: "bar",
value: function bar() {
var x = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : /*#__PURE__*/ _async_to_generator(function() {
var x = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : _async_to_generator(function() {
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
Expand Down
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-5xxx/5596/output/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Base {
return 'good';
}
}
var _privateMethod = /*#__PURE__*/ new WeakSet();
var _privateMethod = new WeakSet();
class Sub extends Base {
superMethod() {
return 'bad';
Expand Down
4 changes: 2 additions & 2 deletions crates/swc/tests/fixture/issues-6xxx/6984/1/output/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { _ as _ts_decorate } from "@swc/helpers/_/_ts_decorate";
import React from "react";
import { withRouter } from "react-router-dom";
var App;
App = /*#__PURE__*/ function(_React_Component) {
App = function(_React_Component) {
"use strict";
_inherits(App, _React_Component);
var _super = _create_super(App);
Expand All @@ -19,7 +19,7 @@ App = /*#__PURE__*/ function(_React_Component) {
key: "render",
value: function render() {
console.log(this.props);
return /*#__PURE__*/ React.createElement("div", null, "134");
return React.createElement("div", null, "134");
}
}
]);
Expand Down
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-7xxx/7700/output/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ let positions = {
};
export function PositionRender({ isRtl, position }) {
let display = ('fe-fe-fe' === isRtl ? rtlPositions : positions)[position];
return /*#__PURE__*/ React.createElement("h1", null, "PositionRender: ", display);
return React.createElement("h1", null, "PositionRender: ", display);
}
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-7xxx/7700/output/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export function PositionRender({ isRtl, position }) {
] : {
positions
})[position];
return /*#__PURE__*/ React.createElement("h1", null, "PositionRender: ", display);
return React.createElement("h1", null, "PositionRender: ", display);
}
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-7xxx/7700/output/3.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export function PositionRender({ isRtl, position }) {
] : {
positions
})[position];
return /*#__PURE__*/ React.createElement("h1", null, "PositionRender: ", display);
return React.createElement("h1", null, "PositionRender: ", display);
}
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-7xxx/7700/output/4.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ export function PositionRender({ isRtl, position }) {
let display = ('fe-fe-fe' === isRtl ? rtlPositions : {
a: positions
})[position];
return /*#__PURE__*/ React.createElement("h1", null, "PositionRender: ", display);
return React.createElement("h1", null, "PositionRender: ", display);
}
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-7xxx/7700/output/5.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export function PositionRender({ isRtl, position }) {
positions
]
})[position];
return /*#__PURE__*/ React.createElement("h1", null, "PositionRender: ", display);
return React.createElement("h1", null, "PositionRender: ", display);
}
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-7xxx/7783/output/1.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default function e(){return /*#__PURE__*/React.createElement("div",null,foo.a)}let foo={get a(){return`a ${this.b}`},get b(){return"b"}};
export default function e(){return React.createElement("div",null,foo.a)}let foo={get a(){return`a ${this.b}`},get b(){return"b"}};
7 changes: 3 additions & 4 deletions crates/swc/tests/fixture/issues-7xxx/7821/output/1.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
var Blocks = {
Block1: function() {
return /*#__PURE__*/ React.createElement(React.Fragment, null, "'Block1xx'");
return React.createElement(React.Fragment, null, "'Block1xx'");
},
Block2: function() {
return /*#__PURE__*/ React.createElement(React.Fragment, null, "'Block2xx'");
return React.createElement(React.Fragment, null, "'Block2xx'");
},
Layout1: function() {
// In the final code, Blocks does not have a 'Block1' key
return [
"Block1"
].map(function(e) {
Expand All @@ -15,5 +14,5 @@ var Blocks = {
}
};
export function render() {
return /*#__PURE__*/ React.createElement(Blocks.Layout1, null);
return React.createElement(Blocks.Layout1, null);
}
9 changes: 1 addition & 8 deletions crates/swc/tests/fixture/issues-7xxx/7842/output/1.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/ 'use strict';
'use strict';
function emptyFunction() {}
function emptyFunctionWithReset() {}
emptyFunctionWithReset.resetWarningCache = emptyFunction;
Expand All @@ -19,8 +14,6 @@ module.exports = function() {
return shim;
}
;
// Important!
// Keep this list in sync with production version in `./factoryWithTypeCheckers.js`.
var ReactPropTypes = {
array: shim,
bigint: shim,
Expand Down
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-8xxx/8095/es5/output/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _create_class } from "@swc/helpers/_/_create_class";
import { _ as _ts_decorate } from "@swc/helpers/_/_ts_decorate";
var _class;
var foo = _class = /*#__PURE__*/ function() {
var foo = _class = function() {
"use strict";
function _$class() {
_class_call_check(this, _$class);
Expand Down
7 changes: 1 addition & 6 deletions crates/swc/tests/fixture/issues-8xxx/8119/output/1.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
let a;
const myArr = [];
Math.random() > 0.5 && (a = !0);
// the function call below should always run
// regardless of whether `a` is `undefined`
let b = (myArr.push('foo'), 'foo');
console.log(// const seems to keep this line here instead of
// moving it behind the logitcal nullish assignment
// const b = foo(myArr);
a ??= b), console.log(myArr);
console.log(a ??= b), console.log(myArr);
64 changes: 64 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8251/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"loose": false,
"minify": {
"compress": {
"arguments": false,
"arrows": true,
"booleans": true,
"booleans_as_integers": false,
"collapse_vars": true,
"comparisons": true,
"computed_props": true,
"conditionals": true,
"dead_code": true,
"directives": true,
"drop_console": false,
"drop_debugger": true,
"evaluate": true,
"expression": false,
"hoist_funs": false,
"hoist_props": true,
"hoist_vars": false,
"if_return": true,
"join_vars": true,
"keep_classnames": false,
"keep_fargs": true,
"keep_fnames": false,
"keep_infinity": false,
"loops": true,
"negate_iife": true,
"properties": true,
"reduce_funcs": false,
"reduce_vars": false,
"side_effects": true,
"switches": true,
"typeofs": true,
"unsafe": false,
"unsafe_arrows": false,
"unsafe_comps": false,
"unsafe_Function": false,
"unsafe_math": false,
"unsafe_symbols": false,
"unsafe_methods": false,
"unsafe_proto": false,
"unsafe_regexp": false,
"unsafe_undefined": false,
"unused": true,
"const_to_let": true,
"pristine_globals": true
},
"mangle": false
},
"target": "es2022"
},
"module": {
"type": "es6"
},
"minify": true,
"isModule": true
}
2 changes: 2 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8251/input/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
y = (value // some comment
) => foo();
2 changes: 2 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8251/input/2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
y = (value // @license comment
) => foo();
1 change: 1 addition & 0 deletions crates/swc/tests/fixture/issues-8xxx/8251/output/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
y=value=>foo();
2 changes: 2 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8251/output/2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
y=(value// @license comment
)=>foo();
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/sourcemap/003/output/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/sourcemap/003/output/index.map
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"mappings": "uGAAA,gBAAe,SAASA,EAAW,CAAQ,MAAR,AAAEC,EAAF,EAAEA,KACjC,oBAAO,oBAACC,WAAKD,EAAKE,GAAG,CACzB,CAEA,gBAAsBC,wBAAAA,iCAAAA,WAAAA,EAAf,EAAA,qCACH,SAAO,CACHC,MAAO,CACHJ,KAAM,CACFE,IAAK,KACT,CACJ,CACJ,IACJ",
"mappings": "uGAAA,gBAAe,SAASA,EAAW,CAAQ,MAAR,AAAEC,EAAF,EAAEA,KACjC,OAAO,oBAACC,WAAKD,EAAKE,GAAG,CACzB,CAEA,gBAAsBC,wBAAAA,iCAAAA,WAAAA,EAAf,EAAA,qCACH,SAAO,CACHC,MAAO,CACHJ,KAAM,CACFE,IAAK,KACT,CACJ,CACJ,IACJ",
"names": [
"StaticPage",
"data",
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"use strict";var xxx=", something";console.error("❌ ".concat(message));var bbb="";//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJ4eHgiLCJjb25zb2xlIiwiZXJyb3IiLCJtZXNzYWdlIiwiYmJiIl0sInNvdXJjZXMiOlsidW5rbm93biJdLCJzb3VyY2VzQ29udGVudCI6WyJjb25zdCB4eHggPSAnLCBzb21ldGhpbmcnXG5jb25zb2xlLmVycm9yKGDinYwgJHttZXNzYWdlfWApO1xuXG5jb25zdCBiYmIgPSAnJ1xuIl0sIm1hcHBpbmdzIjoiQUFBQSxNQUFNQSxHQUFHLEdBQUcsYUFBWjtBQUNBQyxPQUFPLENBQUNDLEtBQVIsQ0FBZSxLQUFJQyxPQUFRLEVBQTNCO0FBRUEsTUFBTUMsR0FBRyxHQUFHLEVBQVoifQ==
"use strict";var xxx=", something";console.error("❌ ".concat(message));var bbb="";
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"mappings": "aAAA,IAAMA,IAAM,cACZC,QAAQC,KAAK,CAAC,AAAC,KAAY,OAARC,UACnB,IAAMC,IAAM,EACZ,6cAA4c",
"mappings": "aAAA,IAAMA,IAAM,cACZC,QAAQC,KAAK,CAAC,AAAC,KAAY,OAARC,UACnB,IAAMC,IAAM",
"names": [
"xxx",
"console",
Expand Down
Loading

0 comments on commit 0faa2c4

Please sign in to comment.