Skip to content

Commit 74446f5

Browse files
authored
--remove-prefix fixes for private members (#174)
Fixes #169. Unnecessary hide clauses will no longer be added to import-only files. Prefixes can now also be removed from private members.
1 parent 2d959de commit 74446f5

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## 1.3.1
2+
3+
### Module Migrator
4+
5+
* Prefixes will now be removed from private members (e.g. a variable
6+
`$_lib-variable` will be renamed to `$_variable` when `--remove-prefix=lib-`
7+
is passed).
8+
9+
* Fix a bug where private members would be incorrectly added to `hide` clauses
10+
in generated import-only files.
11+
112
## 1.3.0
213

314
### Namespace Migrator

lib/src/migrators/module.dart

+14-5
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,11 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
245245
var hiddenByUrl = <Uri, Set<MemberDeclaration>>{};
246246
for (var declaration in references.globalDeclarations) {
247247
var private = declaration.name.startsWith('-');
248+
248249
// Whether this member will be exposed by the regular entrypoint.
249-
var visibleAtEntrypoint = declaration.sourceUrl == entrypoint ||
250-
(_shouldForward(declaration.name) && !private);
250+
var visibleAtEntrypoint = !private &&
251+
(declaration.sourceUrl == entrypoint ||
252+
_shouldForward(declaration.name));
251253
// Whether this member should be exposed by the import-only file for the
252254
// entrypoint.
253255
var shouldBeVisible =
@@ -1147,7 +1149,11 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
11471149
if (declaration == null) return;
11481150
if (renamedMembers.containsKey(declaration)) {
11491151
var newName = renamedMembers[declaration];
1150-
if (declaration.name.endsWith(newName)) {
1152+
if (newName.startsWith('-') &&
1153+
declaration.name.endsWith(newName.substring(1))) {
1154+
addPatch(patchDelete(span,
1155+
start: 1, end: declaration.name.length - newName.length + 1));
1156+
} else if (declaration.name.endsWith(newName)) {
11511157
addPatch(
11521158
patchDelete(span, end: declaration.name.length - newName.length));
11531159
} else {
@@ -1167,8 +1173,11 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
11671173
///
11681174
/// Otherwise, returns [name] unaltered.
11691175
String _unprefix(String name) {
1170-
var prefix = _prefixFor(name);
1171-
return prefix == null ? name : name.substring(prefix.length);
1176+
var isPrivate = name.startsWith('-');
1177+
var unprivateName = isPrivate ? name.substring(1) : name;
1178+
var prefix = _prefixFor(unprivateName);
1179+
if (prefix == null) return name;
1180+
return (isPrivate ? '-' : '') + unprivateName.substring(prefix.length);
11721181
}
11731182

11741183
/// Returns the namespace that built-in module [module] is loaded under.

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: sass_migrator
2-
version: 1.3.0
2+
version: 1.3.1
33
description: A tool for running migrations on Sass files
44
author: Jennifer Thakar <[email protected]>
55
homepage: https://github.com/sass/migrator

test/migrators/module/remove_prefix_flag/unprefixed_members.hrx

+4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ $lib-a: 1;
66
$lib-b: 2;
77
$c: 3;
88
$d: 4;
9+
$_e: 5;
10+
$_lib-f: 6;
911

1012
<==> output/entrypoint.scss
1113
$a: 1;
1214
$b: 2;
1315
$c: 3;
1416
$d: 4;
17+
$_e: 5;
18+
$_f: 6;
1519

1620
<==> output/entrypoint.import.scss
1721
@forward "entrypoint" hide $a, $b;

0 commit comments

Comments
 (0)