-
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
Changes from 4 commits
23d0b2b
ebb3b65
6deefe5
13f065d
00940e2
e7b6547
91640f3
2bcb831
4f9e7b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6,6 +6,7 @@ | |||||
|
||||||
import 'package:args/args.dart'; | ||||||
import 'package:collection/collection.dart'; | ||||||
import 'package:charcode/charcode.dart'; | ||||||
import 'package:path/path.dart' as p; | ||||||
import 'package:sass_api/sass_api.dart'; | ||||||
import 'package:source_span/source_span.dart'; | ||||||
|
@@ -191,6 +192,9 @@ class _ModuleMigrationVisitor extends MigrationVisitor { | |||||
"Can't access _configuredVariables when not visiting a dependency.")); | ||||||
Set<MemberDeclaration<VariableDeclaration>>? __configuredVariables; | ||||||
|
||||||
/// The number of variables that have been generated from whole cloth. | ||||||
var _generatedVariables = 0; | ||||||
|
||||||
/// A mapping between member declarations and references. | ||||||
/// | ||||||
/// This performs an initial pass to determine how a declaration seen in the | ||||||
|
@@ -263,7 +267,7 @@ class _ModuleMigrationVisitor extends MigrationVisitor { | |||||
var forwardsByUrl = <Uri, Map<String, Set<MemberDeclaration>>>{}; | ||||||
var hiddenByUrl = <Uri, Set<MemberDeclaration>>{}; | ||||||
for (var declaration in references.globalDeclarations) { | ||||||
var private = declaration.name.startsWith('-'); | ||||||
var private = _isPrivate(declaration.name); | ||||||
|
||||||
// Whether this member will be exposed by the regular entrypoint. | ||||||
var visibleAtEntrypoint = !private && | ||||||
|
@@ -352,11 +356,11 @@ class _ModuleMigrationVisitor extends MigrationVisitor { | |||||
if (declaration.isForwarded) return; | ||||||
|
||||||
var name = declaration.name; | ||||||
if (name.startsWith('-') && | ||||||
if (_isPrivate(name) && | ||||||
references.referencedOutsideDeclaringStylesheet(declaration)) { | ||||||
// Remove leading `-` since private members can't be accessed outside | ||||||
// the module they're declared in. | ||||||
name = name.substring(1); | ||||||
// Private members can't be accessed outside the module they're declared | ||||||
// in. | ||||||
name = _privateToPublic(name); | ||||||
} | ||||||
name = _unprefix(name); | ||||||
if (name != declaration.name) { | ||||||
|
@@ -365,6 +369,27 @@ class _ModuleMigrationVisitor extends MigrationVisitor { | |||||
} | ||||||
} | ||||||
|
||||||
/// Returns whether [identifier] is a private member name. | ||||||
/// | ||||||
/// Assumes [identifier] is a valid CSS identifier. | ||||||
bool _isPrivate(String identifier) { | ||||||
return identifier.startsWith('-') || identifier.startsWith('_'); | ||||||
} | ||||||
|
||||||
/// 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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. What about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The correct one would be There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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. |
||||||
} | ||||||
} | ||||||
|
||||||
_generatedVariables++; | ||||||
return 'var${_generatedVariables}'; | ||||||
} | ||||||
|
||||||
/// Returns whether the member named [name] should be forwarded in the | ||||||
/// entrypoint. | ||||||
/// | ||||||
|
@@ -1025,8 +1050,7 @@ class _ModuleMigrationVisitor extends MigrationVisitor { | |||||
newName = declaration.name.substring(importOnlyPrefix.length); | ||||||
} | ||||||
|
||||||
if (_shouldForward(declaration.name) && | ||||||
!declaration.name.startsWith('-')) { | ||||||
if (_shouldForward(declaration.name) && !_isPrivate(declaration.name)) { | ||||||
var subprefix = ""; | ||||||
if (importOnlyPrefix != null) { | ||||||
var prefix = _prefixFor(declaration.name); | ||||||
|
@@ -1036,7 +1060,7 @@ class _ModuleMigrationVisitor extends MigrationVisitor { | |||||
} | ||||||
if (declaration.name != newName) _needsImportOnly = true; | ||||||
shownByPrefix.putIfAbsent(subprefix, () => {}).add(declaration); | ||||||
} else if (!newName.startsWith('-')) { | ||||||
} else if (!_isPrivate(newName)) { | ||||||
hidden.add(declaration); | ||||||
} | ||||||
} | ||||||
|
@@ -1076,7 +1100,7 @@ class _ModuleMigrationVisitor extends MigrationVisitor { | |||||
if (declaration is ImportOnlyMemberDeclaration) { | ||||||
name = name.substring(declaration.importOnlyPrefix.length); | ||||||
} | ||||||
if (name.startsWith('-')) name = name.substring(1); | ||||||
if (_isPrivate(name)) name = _privateToPublic(name); | ||||||
name = _unprefix(name); | ||||||
if (subprefix.isNotEmpty) name = '$subprefix$name'; | ||||||
if (declaration.member is VariableDeclaration) name = '\$$name'; | ||||||
|
@@ -1205,7 +1229,7 @@ class _ModuleMigrationVisitor extends MigrationVisitor { | |||||
void _renameReference(FileSpan span, MemberDeclaration declaration) { | ||||||
var newName = renamedMembers[declaration]; | ||||||
if (newName != null) { | ||||||
if (newName.startsWith('-') && | ||||||
if (_isPrivate(newName) && | ||||||
declaration.name.endsWith(newName.substring(1))) { | ||||||
addPatch(patchDelete(span, | ||||||
start: 1, end: declaration.name.length - newName.length + 1)); | ||||||
|
@@ -1228,11 +1252,16 @@ class _ModuleMigrationVisitor extends MigrationVisitor { | |||||
/// | ||||||
/// Otherwise, returns [name] unaltered. | ||||||
String _unprefix(String name) { | ||||||
var isPrivate = name.startsWith('-'); | ||||||
var unprivateName = isPrivate ? name.substring(1) : name; | ||||||
var isPrivate = _isPrivate(name); | ||||||
var unprivateName = isPrivate ? _privateToPublic(name) : name; | ||||||
var prefix = _prefixFor(unprivateName); | ||||||
nex3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
if (prefix == null) return name; | ||||||
return (isPrivate ? '-' : '') + unprivateName.substring(prefix.length); | ||||||
|
||||||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Why? |
||||||
} | ||||||
return (isPrivate ? '-' : '') + withoutPrefix; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally I'm okay with leaning towards encouraging There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
||||||
} | ||||||
|
||||||
/// Returns the namespace that built-in module [module] is loaded under. | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<==> arguments | ||
--remove-prefix=lib | ||
|
||
<==> input/entrypoint.scss | ||
$lib-a: 5; | ||
$lib_b: $lib-a + 2; | ||
|
||
@function lib-fn($lib-local) { | ||
@return $lib-local; | ||
} | ||
|
||
@mixin lib-mixin { | ||
lib-property: lib-value; | ||
} | ||
|
||
lib-selector { | ||
lib-property: lib-fn(0); | ||
@include lib-mixin; | ||
} | ||
|
||
<==> output/entrypoint.scss | ||
$a: 5; | ||
$b: $a + 2; | ||
|
||
@function fn($lib-local) { | ||
@return $lib-local; | ||
} | ||
|
||
@mixin mixin { | ||
lib-property: lib-value; | ||
} | ||
|
||
lib-selector { | ||
lib-property: fn(0); | ||
@include mixin; | ||
} | ||
|
||
<==> output/entrypoint.import.scss | ||
@forward "entrypoint" as lib-*; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<==> arguments | ||
--migrate-deps | ||
<==> input/entrypoint.scss | ||
@import "library"; | ||
a { | ||
color: $-var; | ||
} | ||
|
||
<==> input/_library.scss | ||
$-var: red; | ||
|
||
<==> output/entrypoint.scss | ||
@use "library"; | ||
a { | ||
color: library.$var; | ||
} | ||
|
||
<==> output/_library.scss | ||
$var: red; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<==> arguments | ||
--migrate-deps | ||
<==> input/entrypoint.scss | ||
@import "library"; | ||
a { | ||
color: $---; | ||
} | ||
|
||
<==> input/_library.scss | ||
$---: red; | ||
|
||
<==> output/entrypoint.scss | ||
@use "library"; | ||
a { | ||
color: library.$var1; | ||
} | ||
|
||
<==> output/_library.scss | ||
$var1: red; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<==> arguments | ||
--migrate-deps | ||
<==> input/entrypoint.scss | ||
@import "library"; | ||
a { | ||
color: $--var; | ||
} | ||
|
||
<==> input/_library.scss | ||
$--var: red; | ||
nex3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
<==> output/entrypoint.scss | ||
@use "library"; | ||
a { | ||
color: library.$var; | ||
} | ||
|
||
<==> output/_library.scss | ||
$var: red; | ||
nex3 marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<==> arguments | ||
--migrate-deps | ||
<==> input/entrypoint.scss | ||
@import "library"; | ||
a { | ||
color: $middle; | ||
} | ||
|
||
<==> input/_library.scss | ||
$-upstream: red; | ||
$middle: $-upstream; | ||
|
||
<==> output/entrypoint.scss | ||
@use "library"; | ||
a { | ||
color: library.$middle; | ||
} | ||
|
||
<==> output/_library.scss | ||
$-upstream: red; | ||
$middle: $-upstream; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<==> arguments | ||
--migrate-deps | ||
<==> input/entrypoint.scss | ||
@import "library"; | ||
a { | ||
color: $_var; | ||
} | ||
|
||
<==> input/_library.scss | ||
$_var: red; | ||
nex3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
<==> output/entrypoint.scss | ||
@use "library"; | ||
a { | ||
color: library.$var; | ||
} | ||
|
||
<==> output/_library.scss | ||
$var: red; | ||
nex3 marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.