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

Permit %%placeholder%% in left-hand-side of a let declaration #12725

Merged
merged 3 commits into from Feb 2, 2021

Conversation

Zalathar
Copy link
Contributor

@Zalathar Zalathar commented Jan 31, 2021

Q                       A
Fixed Issues? Fixes #12717
Patch: Bug Fix? Yes
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

This change adds support for template syntactic placeholders in the left-hand-side of a let declaration (e.g. let %%lhs%% = 7).

This already works for var and const, but wasn't working for let because the special handling needed to disambiguate let-as-keyword vs let-as-identifier didn't know to treat a following placeholder as though it were a variable.

@babel-bot
Copy link
Collaborator

babel-bot commented Jan 31, 2021

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/39237/

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jan 31, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit c54793b:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@Zalathar
Copy link
Contributor Author

There are a few different ways to implement this.

I didn't like the idea of overriding isLet in the placeholder plugin, because then I'd have to copy-paste a bunch of code from the base implementation, which seems needlessly messy.

So my choice was between adding adding a very narrow hook method that the plugin could override, or just adding ad-hoc code directly in the parser itself.

I went with the ad-hoc code because it's very straightforward and compact, and that seems worth the downside of having placeholder code that lives outside the placeholder plugin.

@nicolo-ribaudo nicolo-ribaudo added pkg: parser PR: Bug Fix 🐛 A type of pull request used for our changelog categories labels Jan 31, 2021
@Zalathar
Copy link
Contributor Author

I'd like to add some explicit test cases for let % x and let %%x%% when the placeholders plugin is not being used, but I'm not quite sure where to put them.

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would really prefer to keep this code in placeholders.js, even if we would then have to duplicate this.isContextual("let").

This isn't an hot code path (usually the placeholders plugin is not enabled), and even when perf matters the isContextual function is not expensive.

Btw, thanks for also adding tests for the let %%LHS%%; case!

@Zalathar
Copy link
Contributor Author

Zalathar commented Feb 1, 2021

Btw, thanks for also adding tests for the let %%LHS%%; case!

Just to be clear, I have only added tests for let %%LHS%% = %%RHS%%; (with initializer), for the initializer forms with var/const, and for the negative case let %LHS; where the % should be parsed as modulo.

I had also written tests for let %%LHS%%; (without initializer), but I didn't add them in this PR because they fail, with or without this patch! That's a separate issue that will need a separate fix.

@nicolo-ribaudo
Copy link
Member

and for the negative case let %LHS; where the % should be parsed as modulo

Sorry I was tired 🙃

@Zalathar Zalathar force-pushed the let-placeholder branch 3 times, most recently from 5b5994a to 35efc3a Compare February 1, 2021 09:47
@Zalathar
Copy link
Contributor Author

Zalathar commented Feb 1, 2021

I'm rather baffled by my new failure test (let context 3) producing a subtly different error under CI.

@nicolo-ribaudo
Copy link
Member

I think it could be because GitHub runs CI not on your branch, but on the result of merging your branch in main.
We recently merged 8cf0a75, which could cause the test failure.
Could you try rebasing on main?

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Template parser can't handle let declaration with syntactic placeholder variable
4 participants