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

Replace "check-es2015-constants" plugin with injected `throw` statements #5728

Closed
loganfsmyth opened this Issue May 12, 2017 · 4 comments

Comments

Projects
None yet
6 participants
@loganfsmyth
Member

loganfsmyth commented May 12, 2017

The "check-es2015-constants" plugin is technically not spec-compliant. Nothing about the spec says that Babel should throw an error at compile-time about assigning to a constant binding.

(function(){
  const a = "foo";

  if (false) a = "false";

  return a;
})();

for instance is perfectly valid code and will run to completion. It's weird and probably won't come up in real code, but I don't see a reason that Babel should be diverging in behavior here.

There are two cases where this plugin currently applies, to my knowledge

  1. Assigning to const like the example above
  2. Assigning to module bindings like import foo from "foo"; foo = 4;

In the first case, I'd vote for the block-scoping transform to automatically rewrite every Assignment/UpdateExpression to insert a throw into the output code.

In the import case, I'd vote for the module transform to be responsible for injecting throw statements into the code.

This doesn't strictly need to happen for Babel 7.x, I don't think, since if anything it is relaxing restrictions that are currently too strict, but it would certainly be a nice-to-have.

This is also a reasonably self-contained task, so it would be a good first task for someone to tackle.

@Qantas94Heavy

This comment has been minimized.

Show comment
Hide comment
@Qantas94Heavy

Qantas94Heavy May 18, 2017

Member

Would an option to preserve the current behaviour be necessary, or would that be out of scope for Babel (leave it to some other tool)?

Member

Qantas94Heavy commented May 18, 2017

Would an option to preserve the current behaviour be necessary, or would that be out of scope for Babel (leave it to some other tool)?

@loganfsmyth

This comment has been minimized.

Show comment
Hide comment
@loganfsmyth

loganfsmyth May 18, 2017

Member

ESLint and other tools already do this anyway, so I'd say drop the current behavior entirely.

Member

loganfsmyth commented May 18, 2017

ESLint and other tools already do this anyway, so I'd say drop the current behavior entirely.

@xtuc

This comment has been minimized.

Show comment
Hide comment
@xtuc

xtuc May 18, 2017

Member

I'm fine with both behaviors. I think we should keep the compile-time failure (optionally) for online editors like the Babel's REPL.

throws in output can also be optional for the same reasons.

Member

xtuc commented May 18, 2017

I'm fine with both behaviors. I think we should keep the compile-time failure (optionally) for online editors like the Babel's REPL.

throws in output can also be optional for the same reasons.

@kaicataldo

This comment has been minimized.

Show comment
Hide comment
@kaicataldo

kaicataldo Jun 4, 2017

Member

I'm going to try to take a look at this :)

Member

kaicataldo commented Jun 4, 2017

I'm going to try to take a look at this :)

@babel-bot babel-bot added the Has PR label Jul 8, 2017

@jridgewell jridgewell closed this in #5930 Jul 18, 2017

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

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

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