Optional chaining does not play nice with values defined by import statement when _interopRequireDefault is needed
See original GitHub issueI believe that this a bug
Input Code
example .babelrc
{
"presets": [
["env", {
"targets": {
"node": "current"
}
}]
],
"plugins": [
"transform-object-rest-spread",
"add-module-exports",
"transform-optional-chaining"
],
"comments": false
}
example package.json
{
"scripts": {
"build": "./node_modules/babel-cli/bin/babel.js -d ./dist ./src"
},
"devDependencies": {
"babel-cli": "7.0.0-beta.3",
"babel-eslint": "8.0.2",
"babel-node": "7.0.0-beta.3",
"babel-plugin-add-module-exports": "0.2.1",
"babel-plugin-transform-object-rest-spread": "7.0.0-beta.3",
"babel-plugin-transform-optional-chaining": "7.0.0-beta.3",
"babel-preset-env": "7.0.0-beta.3",
"babel-register": "7.0.0-beta.3"
}
}
example /src
// ./src/index.js
import lib from './lib/index';
try {
// should be able to optionally chain from the imported object
lib?.print('hello');
} catch (e) {
// but instead an error is thrown
console.error('lib is undefined error', e);
}
// ./src/lib/index
export default {
print: function (val) {
console.log(val);
}
};
Expected Behavior
I would expect that after transpiling I would end up with a file like this:
"use strict";
var _index = _interopRequireDefault(require("./lib/index"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
try {
_index.default == null ? void 0 : _index.print('hello');
} catch (e) {
console.error('lib is undefined error', e);
}
Current Behavior
Instead I end up with a file like this:
"use strict";
var _index = _interopRequireDefault(require("./lib/index"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
try {
// lib is still be referenced instead of the var _index
_index.default == null ? void 0 : lib.print('hello');
} catch (e) {
console.error('lib is undefined error', e);
}
Context
I am currently implementing the optional chaining plugin in a project and since it is not currently being used the issue is not particularly severe, but would like it to be resolved so that I don’t have to remember to specifically not use optional chaining with imported values.
Your Environment
software | version(s) |
---|---|
Babel | v7 |
Babylon | |
node | 8.9.1 |
npm | 5.5.1 |
Operating System | OSX and Linux |
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Optional chaining (?.) - JavaScript - MDN Web Docs - Mozilla
The optional chaining (?.) operator accesses an object's property or calls a function. If the object accessed or function called is ...
Read more >What is wrong with optional chaining and how to fix it - DEV ...
There is nothing wrong with optional chaining, the feature is related to idiomatic absence value in JS, and it is "null | undefined"....
Read more >Error while importing a module with optional chaining
I imported the NPM module that has been causing my grief (@vime/vue-next 5.0.31 BTW), ran the serve script and got the Unexpected token...
Read more >Optional chaining does not count as branch for coverage #516
Your coverage is based on the generated code which has code that you cannot see. istanbul can only work with what it is...
Read more >babel/plugin-proposal-optional-chaining
undefined const willThrow = obj?.foo.bar.qux(); // Error: not a function // Top function can be called directly, too. function test() { return 42;...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
That worked. Thanks!
😄