Skip to content

property names as variable names don't work in babel-plugin-rewire #234

@AmitJoki

Description

@AmitJoki

Bug Report

Current behavior
Certain variable names like hasOwnProperty when transpiled and referenced with _get__ function, it returns undefined. Probably because of a safety measure, property names are blacklisted as variable names, I am not sure.

https://stackoverflow.com/questions/62166209/object-prototype-hasownproperty-is-undefined/62170690?noredirect=1#comment109955183_62170690

Input Code

var funcProto = Function.prototype, objectProto = Object.prototype;
var funcToString = funcProto.toString;
var hasOwnProperty = objectProto.hasOwnProperty;
var reIsNative = RegExp('^' +
  // here hasOwnProperty is undefined leading to the error
  funcToString.call(hasOwnProperty).replace(reRegExpChar, '\\$&')
  .replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$'
);

Expected behavior
The above code transpiled to:

var funcProto = Function.prototype, objectProto = Object.prototype;
var funcToString = _get__("funcProto").toString;
var hasOwnProperty = _get__("objectProto").hasOwnProperty;
var reIsNative = RegExp('^' + _get__("funcToString").call(_get__("hasOwnProperty")).replace(_get__("reRegExpChar"), '\\$&').replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$');

And the _get__("hasOwnProperty") returned undefined wherease if you accessed the variable as is, i.e. hasOwnProperty it was pointing to the intended function alright.

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

  • Filename: babel.config.js
module.exports = {
  "presets": [
    "@babel/preset-react",
    "@babel/preset-env"
  ],
  "plugins": [
    "@babel/plugin-proposal-object-rest-spread",
    "lodash",
    [
      "babel-plugin-root-import",
      {
        "paths": [
          {
            "rootPathSuffix": "./",
            "rootPathPrefix": "~/"
          },
          {
            "rootPathSuffix": "./app/assets/javascripts/components",
            "rootPathPrefix": "@components/"
          },
          {
            "rootPathSuffix": "./app/assets/javascripts/constants",
            "rootPathPrefix": "@constants/"
          }
        ]
      }
    ]
  ],
  "env": {
    "test": {
      "plugins": [
        "babel-plugin-rewire",
        "@babel/plugin-transform-modules-commonjs",
        "@babel/plugin-transform-runtime",
        "@babel/plugin-proposal-object-rest-spread",
        "lodash",
      ],
      "ignore": [
        "i18n/*.js"
      ]
    }
  }
}

Environment

System:
    OS: Linux 4.15 elementary OS 5.1.4 Hera
  Binaries:
    Node: 12.17.0 - /usr/bin/node
    Yarn: 1.22.4 - /usr/bin/yarn
    npm: 6.14.4 - /usr/bin/npm
  npmPackages:
    @babel/core: ^7.9.0 => 7.9.0 
    @babel/plugin-proposal-object-rest-spread: ^7.9.6 => 7.9.6 
    @babel/plugin-transform-modules-commonjs: ^7.9.0 => 7.9.0 
    @babel/plugin-transform-runtime: ^7.9.0 => 7.9.0 
    @babel/polyfill: ^7.8.7 => 7.8.7 
    @babel/preset-env: ^7.9.5 => 7.9.5 
    @babel/preset-react: ^7.9.4 => 7.9.4 
    @babel/register: ^7.9.0 => 7.9.0 
    babel-core: ^7.0.0-bridge.0 => 7.0.0-bridge.0 
    babel-jest: ^25.3.0 => 25.3.0 
    babel-loader: ^8.1.0 => 8.1.0 
    babel-plugin-add-module-exports: ^1.0.2 => 1.0.2 
    babel-plugin-lodash: ^3.3.4 => 3.3.4 
    babel-plugin-rewire: ^1.2.0 => 1.2.0 
    babel-plugin-root-import: ^6.5.0 => 6.5.0 
    eslint: ^7.0.0 => 7.0.0 
    eslint-import-resolver-babel-plugin-root-import: ^1.1.1 => 1.1.1 
    jest: ^26.0.1 => 26.0.1 
    webpack: ^4.43.0 => 4.43.0 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions