Skip to content

Fix the conversion of private names to public names #275

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

Merged
merged 9 commits into from
Jul 1, 2025
Merged

Conversation

nex3
Copy link
Contributor

@nex3 nex3 commented Apr 9, 2025

This handles cases where the private names start with multiple dashes or underscores or are even composed entirely of dashes and underscores. It also fixes bugs where names starting with underscores weren't considered private.

Closes #273

@nex3 nex3 force-pushed the private-to-public branch from e5f6aa4 to 6deefe5 Compare April 9, 2025 00:06

var withoutPrefix = unprivateName.substring(prefix.length);
if (_isPrivate(withoutPrefix)) {
withoutPrefix = _privateToPublic(withoutPrefix);
Copy link
Contributor

Choose a reason for hiding this comment

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

In this case maybe it's better to only run substring(1), otherwise multiple dashes will get replaced to 1 dash still

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why? _privateToPublic() should handle multiple dashes gracefully.

if (_isPrivate(withoutPrefix)) {
withoutPrefix = _privateToPublic(withoutPrefix);
}
return (isPrivate ? '-' : '') + withoutPrefix;
Copy link
Contributor

@Tofandel Tofandel Apr 9, 2025

Choose a reason for hiding this comment

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

Suggested change
return (isPrivate ? '-' : '') + withoutPrefix;
return (isPrivate ? name.substring(0, 1) : '') + withoutPrefix;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Generally I'm okay with leaning towards encouraging -, since that's definitely the more CSS-style prefix.

Copy link
Contributor

@Tofandel Tofandel Apr 10, 2025

Choose a reason for hiding this comment

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

Yes it's just for the sake of avoiding unecessary changes during a migration, but I definitely don't understand everything that's happening after this runs, because some tests that look like they should fail returning a dash, don't 🤷

<==> arguments
--migrate-deps --remove-prefix=app
<==> input/entrypoint.scss
@import "library";
a {
  color: $app-var;
}

<==> input/_library.scss
$_app-red: red;
$app-var: $_app-red;

<==> output/entrypoint.scss
@use "library";
a {
  color: library.$var;
}

<==> output/_library.scss
$_red: red;
$var: $_red;

Ok, that one fails

<==> arguments
--migrate-deps --remove-prefix=app
<==> input/entrypoint.scss
@import "library";
a {
  color: $app-var;
}

<==> input/_library.scss
$__app-red: red;
$app-var: $__app-red;

<==> output/entrypoint.scss
@use "library";
a {
  color: library.$var;
}

<==> output/_library.scss
$__red: red;
$var: $__red;

So now it's just a question of do we want to preserve the original number of dashes/underscore or not? (My previous implementation did)

@Tofandel
Copy link
Contributor

Tofandel commented Apr 9, 2025

I added suggested tests and fixed the reported behaviors of remove-prefix in #276

So you can merge that in this branch

/// Converts a private identifier to a public one.
String _privateToPublic(String identifier) {
assert(_isPrivate(identifier));
for (var i = 0; i < identifier.length; i++) {
Copy link
Contributor

@pamelalozano16 pamelalozano16 Apr 9, 2025

Choose a reason for hiding this comment

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

Why are you iterating through the identifier? Shouldn't returning the substring without the initial - or _ should be enough?

Copy link
Contributor

@Tofandel Tofandel Apr 10, 2025

Choose a reason for hiding this comment

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

There can be multiple dashes or underscore, removing only one if there is multiple would lead to the variable still being private, see linked issue.

It's just iterating each char until it finds a character that's not an underscore or dash, then returns the string without the iterated part

This is more performant than regex

for (var i = 0; i < identifier.length; i++) {
var char = identifier.codeUnitAt(i);
if (char != $dash && char != $underscore) {
return identifier.substring(i);
Copy link
Contributor

Choose a reason for hiding this comment

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

What about identifier.replace(Regex(r"^-|^_"),"") ?

Copy link
Contributor

Choose a reason for hiding this comment

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

The correct one would be identifier.replace(Regex(r"^[-_]+"),"")

Copy link
Contributor

Choose a reason for hiding this comment

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

And if the returned string is empty then return a generated var

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I habitually avoid regular expressions because they're somewhat more expensive than explicit parsing. If you'd prefer it, I can make the change, but I think the current way does work.

@nex3 nex3 force-pushed the private-to-public branch from 2510bed to 00940e2 Compare April 10, 2025 22:39
@nex3 nex3 requested review from Tofandel and pamelalozano16 April 10, 2025 22:39
if (_isPrivate(withoutPrefix)) {
withoutPrefix = _privateToPublic(withoutPrefix);
}
return (isPrivate && !forcePublic ? '-' : '') + withoutPrefix;
Copy link
Contributor

Choose a reason for hiding this comment

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

I thought because of this line, here we return '-' but we should return the original private prefix.

But from testing, it seems there is some magic running after that already and it keeps the original private prefix somehow

Copy link
Contributor

@pamelalozano16 pamelalozano16 left a comment

Choose a reason for hiding this comment

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

LGTM

jathak added 3 commits May 15, 2025 14:53
commit e0acafd
Author: Tofandel <adrien.foulon@tukan.hu>
Date:   Wed Apr 9 14:02:44 2025 +0200

    Simplify unprefix cognitive complexity

commit 601ee43
Author: Tofandel <adrien.foulon@tukan.hu>
Date:   Wed Apr 9 12:19:02 2025 +0200

    No need to run privateToPublic twice

commit 8775fc9
Author: Tofandel <adrien.foulon@tukan.hu>
Date:   Wed Apr 9 10:48:32 2025 +0200

    Add tests and fix behavior of remove prefix
@jathak jathak force-pushed the private-to-public branch from 5449275 to 2bcb831 Compare June 30, 2025 19:00
Copy link
Member

@jathak jathak left a comment

Choose a reason for hiding this comment

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

I think I've merged in the relevant bits of #276 as well as my own change to how all underscore/dash names are handled and it now looks good to me. @nex3, can you do one more pass before we merge?

@nex3 nex3 merged commit 74bfa89 into main Jul 1, 2025
18 checks passed
@nex3 nex3 deleted the private-to-public branch July 1, 2025 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Some variables get -- replaced to -
4 participants