Skip to content

Commit da33de1

Browse files
committed
Simplify unprefix cognitive complexity
1 parent 601ee43 commit da33de1

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

lib/src/migrators/module.dart

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
356356
if (declaration.isForwarded) return;
357357

358358
var name = declaration.name;
359-
name = _unprefix(name, isReferenced: isPrivate(name) && references.referencedOutsideDeclaringStylesheet(declaration));
359+
name = _unprefix(name, forcePublic: references.referencedOutsideDeclaringStylesheet(declaration));
360360
if (name != declaration.name) {
361361
renamedMembers[declaration] = name;
362362
if (_upstreamStylesheets.isEmpty) _needsImportOnly = true;
@@ -1073,7 +1073,7 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
10731073
if (declaration is ImportOnlyMemberDeclaration) {
10741074
name = name.substring(declaration.importOnlyPrefix.length);
10751075
}
1076-
name = _unprefix(name, isReferenced: true);
1076+
name = _unprefix(name, forcePublic: true);
10771077
if (subprefix.isNotEmpty) name = '$subprefix$name';
10781078
if (declaration.member is VariableDeclaration) name = '\$$name';
10791079
allHidden.add(name);
@@ -1239,22 +1239,29 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
12391239
/// longest matching prefix removed.
12401240
///
12411241
/// Otherwise, returns [name] unaltered.
1242-
String _unprefix(String name, {bool isReferenced = false}) {
1243-
var wasPrivate = isPrivate(name);
1242+
String _unprefix(String name, {bool forcePublic = false}) {
12441243
var unprivateName = _privateToPublic(name);
1245-
var prefix = _prefixFor(unprivateName, unprivatized: true);
1246-
if (prefix == null) return isReferenced ? unprivateName : name;
1247-
1248-
var withoutPrefix = unprivateName.substring(prefix.length);
1249-
var withoutPrefixPublic = _privateToPublic(withoutPrefix);
1244+
var wasPrivate = unprivateName != name;
1245+
var prefix = _prefixFor(name);
1246+
var withoutPrefix = '';
12501247
var privatePrefix = '';
1251-
if (wasPrivate) {
1252-
var realPrefix = prefixesToRemove.firstWhere((pre) => _privateToPublic(pre) == prefix, orElse: () => prefix);
1253-
privatePrefix = !isPrivate(realPrefix)
1254-
? name.substring(0, name.length - unprivateName.length)
1255-
: withoutPrefix.substring(0, withoutPrefix.length - withoutPrefixPublic.length);
1248+
if (wasPrivate && prefix == null) {
1249+
prefix = _prefixFor(unprivateName);
1250+
if (prefix != null) {
1251+
privatePrefix = name.substring(0, name.length - unprivateName.length);
1252+
withoutPrefix = _privateToPublic(unprivateName.substring(prefix.length));
1253+
}
1254+
} else if (prefix != null) {
1255+
withoutPrefix = name.substring(prefix.length);
1256+
var withoutPrefixPublic = _privateToPublic(withoutPrefix);
1257+
if (wasPrivate && withoutPrefixPublic != withoutPrefix) {
1258+
privatePrefix = withoutPrefix.substring(0, withoutPrefix.length - withoutPrefixPublic.length);
1259+
}
1260+
withoutPrefix = withoutPrefixPublic;
12561261
}
1257-
return isReferenced ? withoutPrefixPublic : privatePrefix + withoutPrefixPublic;
1262+
if (prefix == null) return forcePublic ? unprivateName : name;
1263+
1264+
return forcePublic ? _privateToPublic(withoutPrefix) : privatePrefix + withoutPrefix;
12581265
}
12591266

12601267
/// Returns the namespace that built-in module [module] is loaded under.
@@ -1349,13 +1356,11 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
13491356
/// starts with it and the remainder is a valid Sass identifier.
13501357
///
13511358
/// If there is no such prefix, returns `null`.
1352-
String? _prefixFor(String identifier, {bool unprivatized = false}) => maxBy(
1353-
prefixesToRemove
1354-
.map((prefix) => unprivatized ? _privateToPublic(prefix) : prefix)
1355-
.where((prefix) =>
1356-
prefix.length < identifier.length &&
1357-
identifier.startsWith(prefix) &&
1358-
isIdentifier(identifier.substring(prefix.length))),
1359+
String? _prefixFor(String identifier) => maxBy(
1360+
prefixesToRemove.where((prefix) =>
1361+
prefix.length < identifier.length &&
1362+
identifier.startsWith(prefix) &&
1363+
isIdentifier(identifier.substring(prefix.length))),
13591364
(prefix) => prefix.length);
13601365

13611366
/// Disallows `@use` after `@at-root` rules.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"url": "https://github.com/sass/migrator"
1010
},
1111
"scripts": {
12-
"test": "dart run grinder pkg-npm-dev && dart pub run test -t node -r expanded --chain-stack-traces"
12+
"test": "dart run grinder pkg-npm-dev && dart pub run test -t node -r expanded"
1313
},
1414
"author": {
1515
"name": "Jennifer Thakar",

0 commit comments

Comments
 (0)