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

Breaks with some transformations #280

Closed
theKashey opened this issue Feb 27, 2019 · 5 comments
Closed

Breaks with some transformations #280

theKashey opened this issue Feb 27, 2019 · 5 comments
Assignees
Milestone

Comments

@theKashey
Copy link

theKashey commented Feb 27, 2019

I've tried to apply swc for node_modules and run into the issue with react code

before transpilation

function ce(e, t, n, r) {
    for (var a in this.dispatchConfig = e, this._targetInst = t, this.nativeEvent = n, e = this.constructor.Interface) {
      e.hasOwnProperty(a) && ((t = e[a]) ? this[a] = t(n) : "target" === a ? this.target = r : this[a] = n[a]);
    }

    return this.isDefaultPrevented = (null != n.defaultPrevented ? n.defaultPrevented : !1 === n.returnValue) ? ie : le, this.isPropagationStopped = le, this;
  }

after transpilation

var ce = function ce(e1, t1, n1, r) {
            for(var a in (this.dispatchConfig = e1, this._targetInst = t1, this.nativeEvent = n1, e1 = this.constructor.Interface))e1.hasOwnProperty(a) && t1 = e1[a] ? (this[a] = t1(n1)) : 'target' === a ? (this.target = r) : (this[a] = n1[a]);
            return (this.isDefaultPrevented = null != n1.defaultPrevented ? n1.defaultPrevented : !1 === n1.returnValue ? ie : le, this.isPropagationStopped = le, this);
        };

The key moment is && ((t = e[a]) ? !== && t1 = e1[a] ?, in the second case this is a syntax error (just drop code to the dev console).

I didn't found a way how to disable this transformation, and it's a critical for me.

What I did

swc.transform(code, {
    "jsc": {
      "parser": {
        "syntax": "ecmascript",
      },
      "target": "es5",
      // "transform": {
      //   "optimizer": undefined // this makes no sence
      // }
    }
@kdy1 kdy1 added this to the v1.0.28 milestone Feb 27, 2019
@kdy1 kdy1 self-assigned this Feb 27, 2019
@kdy1 kdy1 closed this as completed in 892b4f0 Feb 27, 2019
@theKashey
Copy link
Author

Wow! It was fast!

@kdy1
Copy link
Member

kdy1 commented Feb 27, 2019

@theKashey :). As this is bugfix, I'll publish v1.0.28 soon

@theKashey
Copy link
Author

Sorry @kdy, but that's not the end

source

break e leads to e:

function Ii(e, t, n) {
if ("selectionStart" in a) var o = {
        start: a.selectionStart,
        end: a.selectionEnd
      }; 
      else e:{   <-----
        var l = (o = (o = a.ownerDocument) && o.defaultView || window).getSelection && o.getSelection();
        if (l && 0 !== l.rangeCount) {
          o = l.anchorNode;
          var c = l.anchorOffset, s = l.focusNode;
          l = l.focusOffset;
          try {
            o.nodeType, s.nodeType
          } catch (e) {
            o = null;
            break e  <-----
          }

result

break e2 leads to nowhere, while should lead to e1

var Ii = function Ii(e1, t1, n1) {
                if ('selectionStart' in a) var o = {
                    start: a.selectionStart,
                    end: a.selectionEnd
                };
                else e1: { <<-----
                    var l = (o = (o = a.ownerDocument) && o.defaultView || window).getSelection && o.getSelection();
                    if (l && 0 !== l.rangeCount) {
                        o = l.anchorNode;
                        var c = l.anchorOffset, s = l.focusNode;
                        l = l.focusOffset;
                        try {
                            o.nodeType, s.nodeType;
                        } catch (e2) {
                            o = null;
                            break e2; <-----, this label does not exists
                        }

SWC is used on minified bundle.

The same code, without minification produces almost identical source code, and does not contain the issue.

original source code

function Sh(a, b, c) {
  if (Ud(e)) {
    if ("selectionStart" in e) var f = {
      start: e.selectionStart,
      end: e.selectionEnd
    };else a: {
      f = (f = e.ownerDocument) && f.defaultView || window;
      var g = f.getSelection && f.getSelection();

      if (g && 0 !== g.rangeCount) {
        f = g.anchorNode;
        var h = g.anchorOffset,
            k = g.focusNode;
        g = g.focusOffset;

        try {
          f.nodeType, k.nodeType;
        } catch (db) {
          f = null;
          break a;
        }

Produced code - almost identical.

Link to the scripts:

@theKashey
Copy link
Author

source code

    !(__WEBPACK_AMD_DEFINE_ARRAY__ = [], __WEBPACK_AMD_DEFINE_RESULT__ = (function () {
      return classNames;
    }).apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__),
				__WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));

result code

!__WEBPACK_AMD_DEFINE_ARRAY__ = [], __WEBPACK_AMD_DEFINE_RESULT__ = (function() {
                    return classNames;
                }).apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__);

!(__WEBPACK_AMD_DEFINE_ARRAY !== !__WEBPACK_AMD_DEFINE_ARRAY__ = []

Yet again - syntax error :(

@swc-bot
Copy link
Collaborator

swc-bot commented Oct 30, 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 30, 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

3 participants