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

Confused "export function" when the function has the same name as inner variable #6797

Closed
0326 opened this Issue Nov 11, 2017 · 3 comments

Comments

Projects
None yet
3 participants
@0326

0326 commented Nov 11, 2017

Input Code

My original code is here:

var add = 1;
export function add() {
  add += 1;
  return add;
}

And after babel-transform (you can try it on babel/repl):

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.add = add;
var add = 1;
function add() {
  exports.add = add += 1;
  return add;
}

Expected Behavior

When I import add function, I wanna this result:

add(); // return 2
add(); // return 3

Current Behavior

add(); // return 2
add(); // Uncaught TypeError: add is not a function

I see the transformed code, when the first time I called add , it's a function, but in the function, the expression exports.add = add += 1 makes exports.add become a number. So I was confused Why babel think the "add" in my origin code "add += 1" is meaning "exports.add" not the inner varible add ? It's a bug or a feature?

@babel-bot

This comment has been minimized.

Show comment
Hide comment
@babel-bot

babel-bot Nov 11, 2017

Collaborator

Hey @0326! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

Collaborator

babel-bot commented Nov 11, 2017

Hey @0326! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@nicolo-ribaudo

This comment has been minimized.

Show comment
Hide comment
@nicolo-ribaudo

nicolo-ribaudo Nov 11, 2017

Member

I think that it should throw also on the first call, since import { add } from "....." should return 2.

Your code is equivalent to this one, which throws.

var add = 2;
function add() {}
export { add };

console.log(add); // 2

node actually throws SyntaxError: Identifier 'add' has already been declared, while @std/esm throws on the first call.


Note that exported declarations are added to the scope, so you code in a CommonJS word would be

var add = 2;
function add() {
  // ...
}
exports.add = add;

and not

var add = 2;
exports.add = function add() {
  // ...
}
Member

nicolo-ribaudo commented Nov 11, 2017

I think that it should throw also on the first call, since import { add } from "....." should return 2.

Your code is equivalent to this one, which throws.

var add = 2;
function add() {}
export { add };

console.log(add); // 2

node actually throws SyntaxError: Identifier 'add' has already been declared, while @std/esm throws on the first call.


Note that exported declarations are added to the scope, so you code in a CommonJS word would be

var add = 2;
function add() {
  // ...
}
exports.add = add;

and not

var add = 2;
exports.add = function add() {
  // ...
}
@nicolo-ribaudo

This comment has been minimized.

Show comment
Hide comment
@nicolo-ribaudo

nicolo-ribaudo Dec 10, 2017

Member

@0326 I'm closing this since it is the expected behavior. If I misunderstood the issue, comment and I'll reopen it.

Member

nicolo-ribaudo commented Dec 10, 2017

@0326 I'm closing this since it is the expected behavior. If I misunderstood the issue, comment and I'll reopen it.

@lock lock bot added the outdated label May 3, 2018

@lock lock bot locked as resolved and limited conversation to collaborators May 3, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.