Skip to content

Commit 6deefe5

Browse files
committed
Properly deprivatize names that start with multiple underscores
1 parent ebb3b65 commit 6deefe5

File tree

7 files changed

+120
-30
lines changed

7 files changed

+120
-30
lines changed

lib/src/migrators/module.dart

+23-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import 'package:args/args.dart';
88
import 'package:collection/collection.dart';
9+
import 'package:charcode/charcode.dart';
910
import 'package:path/path.dart' as p;
1011
import 'package:sass_api/sass_api.dart';
1112
import 'package:source_span/source_span.dart';
@@ -191,6 +192,9 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
191192
"Can't access _configuredVariables when not visiting a dependency."));
192193
Set<MemberDeclaration<VariableDeclaration>>? __configuredVariables;
193194

195+
/// The number of variables that have been generated from whole cloth.
196+
var _generatedVariables = 0;
197+
194198
/// A mapping between member declarations and references.
195199
///
196200
/// This performs an initial pass to determine how a declaration seen in the
@@ -354,9 +358,9 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
354358
var name = declaration.name;
355359
if (_isPrivate(name) &&
356360
references.referencedOutsideDeclaringStylesheet(declaration)) {
357-
// Remove leading `-` since private members can't be accessed outside
358-
// the module they're declared in.
359-
name = name.substring(1);
361+
// Private members can't be accessed outside the module they're declared
362+
// in.
363+
name = _privateToPublic(name);
360364
}
361365
name = _unprefix(name);
362366
if (name != declaration.name) {
@@ -372,6 +376,20 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
372376
return identifier.startsWith('-') || identifier.startsWith('_');
373377
}
374378

379+
/// Converts a private identifier to a public one.
380+
String _privateToPublic(String identifier) {
381+
assert(_isPrivate(identifier));
382+
for (var i = 0; i < identifier.length; i++) {
383+
var char = identifier.codeUnitAt(i);
384+
if (char != $dash && char != $underscore) {
385+
return identifier.substring(i);
386+
}
387+
}
388+
389+
_generatedVariables++;
390+
return 'var${_generatedVariables}';
391+
}
392+
375393
/// Returns whether the member named [name] should be forwarded in the
376394
/// entrypoint.
377395
///
@@ -1082,7 +1100,7 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
10821100
if (declaration is ImportOnlyMemberDeclaration) {
10831101
name = name.substring(declaration.importOnlyPrefix.length);
10841102
}
1085-
if (_isPrivate(name)) name = name.substring(1);
1103+
if (_isPrivate(name)) name = _privateToPublic(name);
10861104
name = _unprefix(name);
10871105
if (subprefix.isNotEmpty) name = '$subprefix$name';
10881106
if (declaration.member is VariableDeclaration) name = '\$$name';
@@ -1235,7 +1253,7 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
12351253
/// Otherwise, returns [name] unaltered.
12361254
String _unprefix(String name) {
12371255
var isPrivate = _isPrivate(name);
1238-
var unprivateName = isPrivate ? name.substring(1) : name;
1256+
var unprivateName = isPrivate ? _privateToPublic(name) : name;
12391257
var prefix = _prefixFor(unprivateName);
12401258
if (prefix == null) return name;
12411259
return (isPrivate ? '-' : '') + unprivateName.substring(prefix.length);

test/migrators/module/scope/pseudoprivate.hrx

-25
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<==> arguments
2+
--migrate-deps
3+
<==> input/entrypoint.scss
4+
@import "library";
5+
a {
6+
color: $-var;
7+
}
8+
9+
<==> input/_library.scss
10+
$-var: red;
11+
12+
<==> output/entrypoint.scss
13+
@use "library";
14+
a {
15+
color: library.$var;
16+
}
17+
18+
<==> output/_library.scss
19+
$var: red;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<==> arguments
2+
--migrate-deps
3+
<==> input/entrypoint.scss
4+
@import "library";
5+
a {
6+
color: $---;
7+
}
8+
9+
<==> input/_library.scss
10+
$---: red;
11+
12+
<==> output/entrypoint.scss
13+
@use "library";
14+
a {
15+
color: library.$var1;
16+
}
17+
18+
<==> output/_library.scss
19+
$var1: red;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<==> arguments
2+
--migrate-deps
3+
<==> input/entrypoint.scss
4+
@import "library";
5+
a {
6+
color: $--var;
7+
}
8+
9+
<==> input/_library.scss
10+
$--var: red;
11+
12+
<==> output/entrypoint.scss
13+
@use "library";
14+
a {
15+
color: library.$var;
16+
}
17+
18+
<==> output/_library.scss
19+
$var: red;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<==> arguments
2+
--migrate-deps
3+
<==> input/entrypoint.scss
4+
@import "library";
5+
a {
6+
color: $middle;
7+
}
8+
9+
<==> input/_library.scss
10+
$-upstream: red;
11+
$middle: $-upstream;
12+
13+
<==> output/entrypoint.scss
14+
@use "library";
15+
a {
16+
color: library.$middle;
17+
}
18+
19+
<==> output/_library.scss
20+
$-upstream: red;
21+
$middle: $-upstream;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<==> arguments
2+
--migrate-deps
3+
<==> input/entrypoint.scss
4+
@import "library";
5+
a {
6+
color: $_var;
7+
}
8+
9+
<==> input/_library.scss
10+
$_var: red;
11+
12+
<==> output/entrypoint.scss
13+
@use "library";
14+
a {
15+
color: library.$var;
16+
}
17+
18+
<==> output/_library.scss
19+
$var: red;

0 commit comments

Comments
 (0)