-
Notifications
You must be signed in to change notification settings - Fork 15
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
base: main
Are you sure you want to change the base?
Conversation
|
||
var withoutPrefix = unprivateName.substring(prefix.length); | ||
if (_isPrivate(withoutPrefix)) { | ||
withoutPrefix = _privateToPublic(withoutPrefix); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
lib/src/migrators/module.dart
Outdated
if (_isPrivate(withoutPrefix)) { | ||
withoutPrefix = _privateToPublic(withoutPrefix); | ||
} | ||
return (isPrivate ? '-' : '') + withoutPrefix; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return (isPrivate ? '-' : '') + withoutPrefix; | |
return (isPrivate ? name.substring(0, 1) : '') + withoutPrefix; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
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++) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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"^-|^_"),"")
?
There was a problem hiding this comment.
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"^[-_]+"),"")
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
if (_isPrivate(withoutPrefix)) { | ||
withoutPrefix = _privateToPublic(withoutPrefix); | ||
} | ||
return (isPrivate && !forcePublic ? '-' : '') + withoutPrefix; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
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