Skip to content

Commit 2510bed

Browse files
committed
Code review
1 parent 13f065d commit 2510bed

File tree

5 files changed

+91
-18
lines changed

5 files changed

+91
-18
lines changed

lib/src/migrators/module.dart

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

358358
var name = declaration.name;
359-
if (_isPrivate(name) &&
360-
references.referencedOutsideDeclaringStylesheet(declaration)) {
359+
name = _unprefix(name,
361360
// Private members can't be accessed outside the module they're declared
362361
// in.
363-
name = _privateToPublic(name);
364-
}
365-
name = _unprefix(name);
362+
forcePublic: references.referencedOutsideDeclaringStylesheet(declaration));
366363
if (name != declaration.name) {
367364
renamedMembers[declaration] = name;
368365
if (_upstreamStylesheets.isEmpty) _needsImportOnly = true;
@@ -1100,8 +1097,7 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
11001097
if (declaration is ImportOnlyMemberDeclaration) {
11011098
name = name.substring(declaration.importOnlyPrefix.length);
11021099
}
1103-
if (_isPrivate(name)) name = _privateToPublic(name);
1104-
name = _unprefix(name);
1100+
name = _unprefix(name, forcePublic: true);
11051101
if (subprefix.isNotEmpty) name = '$subprefix$name';
11061102
if (declaration.member is VariableDeclaration) name = '\$$name';
11071103
allHidden.add(name);
@@ -1251,17 +1247,32 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
12511247
/// longest matching prefix removed.
12521248
///
12531249
/// 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;
1250+
///
1251+
/// If [forcePublic] is true, this will ensure that the name is always a
1252+
/// public identifier.
1253+
String _unprefix(String name, {bool forcePublic= false}) {
1254+
bool isPrivate;
1255+
String withoutPrefix;
1256+
1257+
// Allow users to pass prefixes that either do or do not include leading
1258+
// dashes/underscores. As usual, the longest prefix wins, so we check for
1259+
// ones that do include them first.
1260+
var prefix = _prefixFor(name);
1261+
if (prefix != null) {
1262+
isPrivate = false;
1263+
withoutPrefix = name.substring(prefix.length);
1264+
} else {
1265+
isPrivate = _isPrivate(name);
1266+
var unprivateName = isPrivate ? _privateToPublic(name) : name;
1267+
prefix = _prefixFor(unprivateName);
1268+
if (prefix == null) return isPrivate && forcePublic ? unprivateName : name;
1269+
withoutPrefix = unprivateName.substring(prefix.length);
1270+
}
12591271

1260-
var withoutPrefix = unprivateName.substring(prefix.length);
12611272
if (_isPrivate(withoutPrefix)) {
12621273
withoutPrefix = _privateToPublic(withoutPrefix);
12631274
}
1264-
return (isPrivate ? '-' : '') + withoutPrefix;
1275+
return (isPrivate && !forcePublic ? '-' : '') + withoutPrefix;
12651276
}
12661277

12671278
/// 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)