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

Invalid import of variables in compiled code #763

Closed
jansedlon opened this issue May 5, 2020 · 12 comments · Fixed by #764
Closed

Invalid import of variables in compiled code #763

jansedlon opened this issue May 5, 2020 · 12 comments · Fixed by #764
Labels
Milestone

Comments

@jansedlon
Copy link

jansedlon commented May 5, 2020

SWC incorrectly imports consts from file. That's interesting because one of them is imported and other two are not.

Input code

import {
    INSTAGRAM_CHECK_PATTERN,
    RESOURCE_FACEBOOK,
    RESOURCE_INSTAGRAM,
    RESOURCE_WEBSITE,
} from '../../../../consts'

const resources = [
    {
        value: RESOURCE_WEBSITE,
        label: 'Webové stránky',
    },
    {
        value: RESOURCE_FACEBOOK,
        label: 'Facebook',
    },
    {
        value: RESOURCE_INSTAGRAM,
        label: 'Instagram',
    },
]

// export default decorated React class component

Compiled code

// About 15 empty lines


var resources = [
    {
        value: RESOURCE_WEBSITE,
        label: 'Webové stránky'
    },
    {
        value: RESOURCE_FACEBOOK,
        label: 'Facebook'
    },
    {
        value: _consts__WEBPACK_IMPORTED_MODULE_6__["RESOURCE_INSTAGRAM"],
        label: 'Instagram'
    }, 
];
var _dec = Object(_validator__WEBPACK_IMPORTED_MODULE_9__["i18n"])();

Definition of constants in compiled code

/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "RESOURCE_WEBSITE", function() { return RESOURCE_WEBSITE; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "RESOURCE_FACEBOOK", function() { return RESOURCE_FACEBOOK; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "RESOURCE_INSTAGRAM", function() { return RESOURCE_INSTAGRAM; });

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": true,
      "numericSeparator": false,
      "classPrivateProperty": false,
      "privateMethod": false,
      "classProperty": true,
      "functionBind": false,
      "optionalChaining": true,
      "dynamicImport": true,
      "nullishCoalescing": true,
      "decorators": true,
      "decoratorsBeforeExport": true
    },
    "target": "es2015",
    "loose": false,
    "transform": {
      "react": {
        "pragma": "React.createElement",
        "pragmaFrag": "React.Fragment",
        "throwIfNamespace": true,
        "development": false,
        "useBuiltins": false
      },
      "optimizer": {
        "globals": {
          "vars": {
            "__DEBUG__": "true"
          }
        }
      }
    }
  }
}

Expected behavior
Import every variable in a same way. Or correct one.

Version
The version of @swc/core: v1.1.41

@jansedlon jansedlon added the C-bug label May 5, 2020
kdy1 added a commit to kdy1/swc that referenced this issue May 7, 2020
kdy1 added a commit to kdy1/swc that referenced this issue May 7, 2020
@kdy1
Copy link
Member

kdy1 commented May 7, 2020

I've tested it, and it seems like the dead code elimination pass considers RESOURCE_WEBSITE as dead. Can you share your code which uses the variable resources ?

@jansedlon
Copy link
Author

jansedlon commented May 7, 2020

Sure. What I see here is that RESOURCE_INSTAGRAM is used in const resources as well as in the component Contacts directly.. However RESOURCE_WEBSITE and RESOURCE_FACEBOOK are used in const resources and {resources.map(...)} so these two constants are not used directly

consts.js

// Many constants
export const RESOURCE_WEBSITE = 1
export const RESOURCE_FACEBOOK = 2
export const RESOURCE_INSTAGRAM = 3

websites.jsx

import {
    INSTAGRAM_CHECK_PATTERN,
    RESOURCE_FACEBOOK,
    RESOURCE_INSTAGRAM,
    RESOURCE_WEBSITE,
} from '../../../../consts'

const resources = [
    {
        value: RESOURCE_WEBSITE,
        label: 'Webové stránky',
    },
    {
        value: RESOURCE_FACEBOOK,
        label: 'Facebook',
    },
    {
        value: RESOURCE_INSTAGRAM,
        label: 'Instagram',
    },
]

@i18n()
export default class Contacts extends Component {

    renderWebsites = websites => {
        return websites.map((website, idx) => (
            <div>
                {/* Some code */}
                {resources.map(resource => (<option key={resource.value} value={resource.value}>{resource.label}</option>))}
                <Input
                        type={website.type_id === RESOURCE_INSTAGRAM ? 'text' : 'url'}
                        label={website.type_id === RESOURCE_INSTAGRAM ? t('eventEntity.links.instagramLabel') : t('eventEntity.links.urlTitle')}
                        placeholder={website.type_id === RESOURCE_INSTAGRAM ? t('eventEntity.links.instagramPlaceholder') : t('eventEntity.links.urlPlaceholder')}
                        value={website.value || ''}
                        onChange={ev => this.updateField(idx, 'websites', 'value', ev.target.value)}
                        onBlur={ev => this.blurWebResource(idx, website.type_id, ev.target.value)}
                        maxLength="255"
                        pattern={website.type_id === RESOURCE_INSTAGRAM && INSTAGRAM_CHECK_PATTERN}
                        title={website.type_id === RESOURCE_INSTAGRAM && t('eventEntity.links.instagramTitle')}
                    />
            </div>
        ))
    }

    render = () => {
        const { websites } = this.props
        return (
            <Fragment>
                {websites && this.renderWebsites(websites)}
           </Fragment>
        )
    }
}

@jansedlon
Copy link
Author

If you need anything else, tell me. However I don't want to share any implementation details since it's a company's closed source application

@kdy1
Copy link
Member

kdy1 commented May 7, 2020

@jansedlon It's enough. Thanks!

kdy1 added a commit to kdy1/swc that referenced this issue May 7, 2020
kdy1 added a commit to kdy1/swc that referenced this issue May 7, 2020
@kdy1
Copy link
Member

kdy1 commented May 7, 2020

I've failed to reproduce it.

Log
➜  swc git:(issue-763) ✗ cd ./tests/projects 
➜  projects git:(issue-763) npm i -g @swc/core

> @swc/core@1.1.41 install /usr/local/lib/node_modules/@swc/core
> node scripts/install.js || (npm install neon-cli && neon build --release)

Thank you for using swc ( https://github.com/swc-project/swc ): super-fast javascript and typescript compiler 

The project needs your help! Please consider supporting swc on Open Collective: 
> https://opencollective.com/swc 

Downloading binary from https://github.com/swc-project/node-swc/releases/download/v1.1.41/darwin-x64-72.node
=========================================================================================================================================================================================================

+ @swc/core@1.1.41
updated 1 package in 12.431s
➜  projects git:(issue-763) swc ./issue-763/input.js 
'use strict';
Object.defineProperty(exports, '__esModule', {
    value: true
});
exports.default = void 0;
var _consts = require('../../../../consts');
function _assertThisInitialized(self) {
    if (self === void 0) {
        throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
    }
    return self;
}
function _classCallCheck(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
        throw new TypeError('Cannot call a class as a function');
    }
}
function _defineProperty(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _getPrototypeOf(o) {
    _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
        return o.__proto__ || Object.getPrototypeOf(o);
    };
    return _getPrototypeOf(o);
}
function _inherits(subClass, superClass) {
    if (typeof superClass !== 'function' && superClass !== null) {
        throw new TypeError('Super expression must either be null or a function');
    }
    subClass.prototype = Object.create(superClass && superClass.prototype, {
        constructor: {
            value: subClass,
            writable: true,
            configurable: true
        }
    });
    if (superClass) _setPrototypeOf(subClass, superClass);
}
function _possibleConstructorReturn(self, call) {
    if (call && (_typeof(call) === 'object' || typeof call === 'function')) {
        return call;
    }
    return _assertThisInitialized(self);
}
function _setPrototypeOf(o, p) {
    _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
        o.__proto__ = p;
        return o;
    };
    return _setPrototypeOf(o, p);
}
var _typeof = function(obj) {
    return obj && obj.constructor === Symbol ? 'symbol' : typeof obj;
};
var resources = [
    {
        value: _consts.RESOURCE_WEBSITE,
        label: 'Webové stránky'
    },
    {
        value: _consts.RESOURCE_FACEBOOK,
        label: 'Facebook'
    },
    {
        value: _consts.RESOURCE_INSTAGRAM,
        label: 'Instagram'
    }, 
];
var Contacts = function(Component) {
    'use strict';
    _inherits(Contacts, Component);
    function Contacts() {
        _classCallCheck(this, Contacts);
        var _this;
        _this = _possibleConstructorReturn(this, _getPrototypeOf(Contacts).apply(this, arguments));
        _defineProperty(_assertThisInitialized(_this), 'renderWebsites', function(websites) {
            return websites.map(function(website, idx) {
                return React.createElement('div', null, resources.map(function(resource) {
                    return React.createElement('option', {
                        key: resource.value,
                        value: resource.value
                    }, resource.label);
                }));
            });
        });
        _defineProperty(_assertThisInitialized(_this), 'render', function() {
            var _props = _this.props, websites = _props.websites;
            return React.createElement(Fragment, null, websites && _this.renderWebsites(websites));
        });
        return _this;
    }
    return Contacts;
}(Component);
exports.default = Contacts;

I'll investigate further

@jansedlon
Copy link
Author

I'll try to reproduce it as well.

@jansedlon
Copy link
Author

@kdy1 I was able to reproduce it. You can find it here
https://github.com/jansedlon/swc-bug

@jansedlon
Copy link
Author

It's happening only if I import those constants. If I declare them in a same file, it's okay.

@kdy1 kdy1 added this to the v1.1.42 milestone May 8, 2020
@kdy1
Copy link
Member

kdy1 commented May 8, 2020

@jansedlon Thanks a lot!!

In my experience, finding a bug and creating minimal reproduction for it is really hard.

@jansedlon
Copy link
Author

@kdy1 Haha, yeah, that's, true. I feel bad for just issuing a bug and let you do all the work so I try to help you as much as I can 😅

@jansedlon
Copy link
Author

jansedlon commented May 8, 2020

I forgot to add there a .swcrc file, it's fixed now.

kdy1 added a commit to kdy1/swc that referenced this issue May 8, 2020
kdy1 added a commit to kdy1/swc that referenced this issue May 8, 2020
kdy1 added a commit to kdy1/swc that referenced this issue May 8, 2020
kdy1 added a commit to kdy1/swc that referenced this issue May 8, 2020
kdy1 added a commit to kdy1/swc that referenced this issue May 8, 2020
@kdy1 kdy1 closed this as completed in #764 May 9, 2020
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 27, 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 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants