Skip to content

Commit 00940e2

Browse files
committed
Code review
1 parent 13f065d commit 00940e2

File tree

5 files changed

+95
-20
lines changed

5 files changed

+95
-20
lines changed

lib/src/migrators/module.dart

+29-16
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,11 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
356356
if (declaration.isForwarded) return;
357357

358358
var name = declaration.name;
359-
if (_isPrivate(name) &&
360-
references.referencedOutsideDeclaringStylesheet(declaration)) {
361-
// Private members can't be accessed outside the module they're declared
362-
// in.
363-
name = _privateToPublic(name);
364-
}
365-
name = _unprefix(name);
359+
name = _unprefix(name,
360+
// Private members can't be accessed outside the module they're declared
361+
// in.
362+
forcePublic:
363+
references.referencedOutsideDeclaringStylesheet(declaration));
366364
if (name != declaration.name) {
367365
renamedMembers[declaration] = name;
368366
if (_upstreamStylesheets.isEmpty) _needsImportOnly = true;
@@ -1100,8 +1098,7 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
11001098
if (declaration is ImportOnlyMemberDeclaration) {
11011099
name = name.substring(declaration.importOnlyPrefix.length);
11021100
}
1103-
if (_isPrivate(name)) name = _privateToPublic(name);
1104-
name = _unprefix(name);
1101+
name = _unprefix(name, forcePublic: true);
11051102
if (subprefix.isNotEmpty) name = '$subprefix$name';
11061103
if (declaration.member is VariableDeclaration) name = '\$$name';
11071104
allHidden.add(name);
@@ -1251,17 +1248,33 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
12511248
/// longest matching prefix removed.
12521249
///
12531250
/// Otherwise, returns [name] unaltered.
1254-
String _unprefix(String name) {
1255-
var isPrivate = _isPrivate(name);
1256-
var unprivateName = isPrivate ? _privateToPublic(name) : name;
1257-
var prefix = _prefixFor(unprivateName);
1258-
if (prefix == null) return name;
1251+
///
1252+
/// If [forcePublic] is true, this will ensure that the name is always a
1253+
/// public identifier.
1254+
String _unprefix(String name, {bool forcePublic = false}) {
1255+
bool isPrivate;
1256+
String withoutPrefix;
1257+
1258+
// Allow users to pass prefixes that either do or do not include leading
1259+
// dashes/underscores. As usual, the longest prefix wins, so we check for
1260+
// ones that do include them first.
1261+
var prefix = _prefixFor(name);
1262+
if (prefix != null) {
1263+
isPrivate = false;
1264+
withoutPrefix = name.substring(prefix.length);
1265+
} else {
1266+
isPrivate = _isPrivate(name);
1267+
var unprivateName = isPrivate ? _privateToPublic(name) : name;
1268+
prefix = _prefixFor(unprivateName);
1269+
if (prefix == null)
1270+
return isPrivate && forcePublic ? unprivateName : name;
1271+
withoutPrefix = unprivateName.substring(prefix.length);
1272+
}
12591273

1260-
var withoutPrefix = unprivateName.substring(prefix.length);
12611274
if (_isPrivate(withoutPrefix)) {
12621275
withoutPrefix = _privateToPublic(withoutPrefix);
12631276
}
1264-
return (isPrivate ? '-' : '') + withoutPrefix;
1277+
return (isPrivate && !forcePublic ? '-' : '') + withoutPrefix;
12651278
}
12661279

12671280
/// Returns the namespace that built-in module [module] is loaded under.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<==> arguments
2+
--migrate-deps --remove-prefix=lib-
3+
4+
<==> input/entrypoint.scss
5+
@import "dependency";
6+
7+
a {
8+
background: $--lib-pseudoprivate;
9+
}
10+
11+
<==> input/_dependency.scss
12+
$--lib-pseudoprivate: blue;
13+
14+
<==> output/entrypoint.scss
15+
@use "dependency";
16+
17+
a {
18+
background: dependency.$pseudoprivate;
19+
}
20+
21+
<==> output/_dependency.scss
22+
$pseudoprivate: blue;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<==> arguments
2+
--remove-prefix=--lib-
3+
4+
<==> input/entrypoint.scss
5+
$--lib-a: 5;
6+
$--lib_b: $--lib-a + 2;
7+
8+
@function --lib-fn($--lib-local) {
9+
@return $--lib-local;
10+
}
11+
12+
@mixin --lib-mixin {
13+
--lib-property: --lib-value;
14+
}
15+
16+
--lib-selector {
17+
property: --lib-fn(0);
18+
@include --lib-mixin;
19+
}
20+
21+
<==> output/entrypoint.scss
22+
$a: 5;
23+
$b: $a + 2;
24+
25+
@function fn($--lib-local) {
26+
@return $--lib-local;
27+
}
28+
29+
@mixin mixin {
30+
--lib-property: --lib-value;
31+
}
32+
33+
--lib-selector {
34+
property: fn(0);
35+
@include mixin;
36+
}

test/migrators/module/scope/pseudoprivate/double_dash.hrx

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ a {
77
}
88

99
<==> input/_library.scss
10-
$--var: red;
10+
$--red: red;
11+
$--var: $--red;
1112

1213
<==> output/entrypoint.scss
1314
@use "library";
@@ -16,4 +17,5 @@ a {
1617
}
1718

1819
<==> output/_library.scss
19-
$var: red;
20+
$--red: red;
21+
$var: $--red;

test/migrators/module/scope/pseudoprivate/underscore.hrx

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ a {
77
}
88

99
<==> input/_library.scss
10-
$_var: red;
10+
$_red: red;
11+
$_var: $_red;
1112

1213
<==> output/entrypoint.scss
1314
@use "library";
@@ -16,4 +17,5 @@ a {
1617
}
1718

1819
<==> output/_library.scss
19-
$var: red;
20+
$_red: red;
21+
$var: $_red;

0 commit comments

Comments
 (0)