Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compilation should not remove uninitialized properties from a class #3232

Closed
flyyuan opened this issue Jan 11, 2022 · 10 comments
Closed

Compilation should not remove uninitialized properties from a class #3232

flyyuan opened this issue Jan 11, 2022 · 10 comments

Comments

@flyyuan
Copy link

flyyuan commented Jan 11, 2022

Describe the bug

Compilation should not remove uninitialized properties from a class.
It is useful to have properties in the class that are not assigned initial values. For example, mobx will listen based on the value of the attribute in the class, and if the attribute that is not assigned an initial value is removed, mobx will not be able to listen to the corresponding attribute. Besides, many people are used to declaring class properties without giving them initial values, and swc compilation can create bugs that developers don't expect.

input.js

class IncStore {
  num = 0
  unInitValue
  inc() {
    this.num += 1
    this.unInitValue += 1
  }
}
const incStore = new IncStore()
window.aaa = incStore
export default incStore

output.js (The unInitValue was removed after compilation, expecting it to be there)

function _classCallCheck(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function");
    }
}
function _defineProperties(target, props) {
    for(var i = 0; i < props.length; i++){
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;
        if ("value" in descriptor) descriptor.writable = true;
        Object.defineProperty(target, descriptor.key, descriptor);
    }
}
function _createClass(Constructor, protoProps, staticProps) {
    if (protoProps) _defineProperties(Constructor.prototype, protoProps);
    if (staticProps) _defineProperties(Constructor, staticProps);
    return Constructor;
}
var IncStore = /*#__PURE__*/ function() {
    "use strict";
    function IncStore1() {
        _classCallCheck(this, IncStore1);
        this.num = 0;
    }
    _createClass(IncStore1, [
        {
            key: "inc",
            value: function inc() {
                this.num += 1;
                this.unInitValue += 1;
            }
        }
    ]);
    return IncStore1;
}();
var incStore = new IncStore();
window.aaa = incStore;
export { incStore as default };

Input code

class IncStore {
  num = 0
  unInitValue
  inc() {
    this.num += 1
    this.unInitValue += 1
  }
}
const incStore = new IncStore()
window.aaa = incStore
export default incStore

Config

const { config } = require("@swc/core/spack");

module.exports = config({
  //入口文件
  entry: __dirname + "/input.js",
  //输出
  output: {
    path: __dirname + "/dist",
  },
  // swc编译配置
  options:{
    jsc: {
        //解析配置
        parser: {
          syntax: "typescript", //输入文件格式
          tsx: false,    // 是否支持tsx
          dynamicImport: false, //是否支持动态导入
          decorators: false, //是否支持装饰器
        },
        transform: null,
        target: "es5", //转译目标
        loose: false, 
        externalHelpers: false,
        keepClassNames: false
      },
      // 输出文件配置
      module: {
          type: "commonjs",  
          strict: false,
          strictMode: false,
          lazy: false,
          noInterop: false,
          ignoreDynamic: false
        }
  }
});

Playground link

https://github.com/flyyuan/swcUsingDemo

Expected behavior

Expect to compile to include attributes in the class that are not given initial values

Actual behavior

Remove attributes in the class that are not given initial values after compilation

Version

1.2.128

Additional context

No response

@flyyuan flyyuan added the C-bug label Jan 11, 2022
@doerme
Copy link

doerme commented Jan 11, 2022

m

@kdy1 kdy1 modified the milestones: v1.2.129, v1.2.130 Jan 11, 2022
@erikian
Copy link

erikian commented Jan 15, 2022

I've filed an issue on Next.js the other day about this problem (vercel/next.js#32356), I'm pretty sure it affects many other projects as well. Looking forward to this fix.

@kdy1
Copy link
Member

kdy1 commented Jan 15, 2022

I checked this using babel repl and babel emits nothing for uninitialized properties.

@kdy1 kdy1 removed this from the v1.2.130 milestone Jan 15, 2022
@RiESAEX
Copy link
Contributor

RiESAEX commented Jan 15, 2022

tsc doesn't emit it, swc and babel follow its behavior.
microsoft/TypeScript#7777
if you parser.syntax == ecmascript swc won't remove it.
For this case, should we have a lint rule or leave it to the typechecker?

@flyyuan
Copy link
Author

flyyuan commented Jan 17, 2022

I checked this using babel repl and babel emits nothing for uninitialized properties.

Babel do emit uninitialized properties.
I have verified

@kdy1 kdy1 removed the C-bug label Jan 17, 2022
@ckken
Copy link

ckken commented Jan 18, 2022

+1 & mark

@linhaobin
Copy link

+1

@Austaras
Copy link
Member

Austaras commented Feb 3, 2022

Babel won't emit uninitialized field when typescript plugin is enabled. This issue should be closed.

@kdy1
Copy link
Member

kdy1 commented Feb 4, 2022

@Austaras Thank you!

@swc-bot
Copy link
Collaborator

swc-bot commented Oct 19, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

9 participants