Skip to content

Commit

Permalink
refactor: move more files to src, handle lints
Browse files Browse the repository at this point in the history
  • Loading branch information
Tienisto committed Mar 2, 2024
1 parent 6b860ae commit 35c1503
Show file tree
Hide file tree
Showing 85 changed files with 294 additions and 267 deletions.
9 changes: 8 additions & 1 deletion slang/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
include: package:lints/core.yaml
include: package:lints/recommended.yaml
linter:
rules:
prefer_single_quotes: true
sort_pub_dependencies: true
always_use_package_imports: true
directives_ordering: true
constant_identifier_names: false
4 changes: 2 additions & 2 deletions slang/bin/add.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'slang.dart' as mainRunner;
import 'slang.dart' as main_runner;

void main(List<String> arguments) async {
mainRunner.main(['add', ...arguments]);
main_runner.main(['add', ...arguments]);
}
4 changes: 2 additions & 2 deletions slang/bin/analyze.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'slang.dart' as mainRunner;
import 'slang.dart' as main_runner;

void main(List<String> arguments) async {
mainRunner.main(['analyze', ...arguments]);
main_runner.main(['analyze', ...arguments]);
}
4 changes: 2 additions & 2 deletions slang/bin/apply.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'slang.dart' as mainRunner;
import 'slang.dart' as main_runner;

void main(List<String> arguments) async {
mainRunner.main(['apply', ...arguments]);
main_runner.main(['apply', ...arguments]);
}
4 changes: 2 additions & 2 deletions slang/bin/clean.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'slang.dart' as mainRunner;
import 'slang.dart' as main_runner;

void main(List<String> arguments) async {
mainRunner.main(['clean', ...arguments]);
main_runner.main(['clean', ...arguments]);
}
4 changes: 2 additions & 2 deletions slang/bin/edit.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'slang.dart' as mainRunner;
import 'slang.dart' as main_runner;

void main(List<String> arguments) async {
mainRunner.main(['edit', ...arguments]);
main_runner.main(['edit', ...arguments]);
}
4 changes: 2 additions & 2 deletions slang/bin/outdated.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'slang.dart' as mainRunner;
import 'slang.dart' as main_runner;

void main(List<String> arguments) async {
mainRunner.main(['outdated', ...arguments]);
main_runner.main(['outdated', ...arguments]);
}
8 changes: 4 additions & 4 deletions slang/bin/slang.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import 'dart:io';
import 'package:collection/collection.dart';
import 'package:slang/builder/builder/slang_file_collection_builder.dart';
import 'package:slang/builder/builder/translation_map_builder.dart';
import 'package:slang/builder/generator_facade.dart';
import 'package:slang/builder/model/enums.dart';
import 'package:slang/builder/model/raw_config.dart';
import 'package:slang/builder/model/slang_file_collection.dart';
import 'package:slang/src/builder/generator_facade.dart';
import 'package:slang/src/builder/utils/file_utils.dart';
import 'package:slang/src/builder/utils/path_utils.dart';
import 'package:slang/src/runner/analyze.dart';
import 'package:slang/src/runner/apply.dart';
import 'package:slang/src/runner/clean.dart';
import 'package:slang/src/runner/edit.dart';
import 'package:slang/src/runner/migrate.dart';
import 'package:slang/src/runner/stats.dart';
import 'package:slang/builder/utils/file_utils.dart';
import 'package:slang/builder/utils/path_utils.dart';
import 'package:watcher/watcher.dart';

/// Determines what the runner will do
Expand Down Expand Up @@ -372,7 +372,7 @@ Future<void> generateTranslations({
.firstWhereOrNull((c) => c.enumValues != null || c.paths.isNotEmpty);
if (deprecatedContext != null) {
print(
'${_YELLOW}[Deprecated] Use explicit context modifiers instead of populating the config: ${deprecatedContext.enumName} (see: https://github.com/slang-i18n/slang/blob/main/slang/MIGRATION.md#use-context-modifier-since-3190)$_RESET');
'$_YELLOW[Deprecated] Use explicit context modifiers instead of populating the config: ${deprecatedContext.enumName} (see: https://github.com/slang-i18n/slang/blob/main/slang/MIGRATION.md#use-context-modifier-since-3190)$_RESET');
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions slang/bin/stats.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'slang.dart' as mainRunner;
import 'slang.dart' as main_runner;

void main(List<String> arguments) async {
mainRunner.main(['stats', ...arguments]);
main_runner.main(['stats', ...arguments]);
}
4 changes: 2 additions & 2 deletions slang/bin/watch.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'slang.dart' as mainRunner;
import 'slang.dart' as main_runner;

void main(List<String> arguments) async {
mainRunner.main(['watch', ...arguments]);
main_runner.main(['watch', ...arguments]);
}
10 changes: 5 additions & 5 deletions slang/lib/api/singleton.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import 'package:slang/api/locale.dart';
import 'package:slang/api/pluralization.dart';
import 'package:slang/api/state.dart';
import 'package:slang/builder/builder/translation_model_builder.dart';
import 'package:slang/builder/decoder/base_decoder.dart';
import 'package:slang/builder/model/build_model_config.dart';
import 'package:slang/builder/model/enums.dart';
import 'package:slang/builder/utils/map_utils.dart';
import 'package:slang/builder/utils/node_utils.dart';
import 'package:slang/builder/utils/regex_utils.dart';
import 'package:slang/src/builder/decoder/base_decoder.dart';
import 'package:slang/src/builder/utils/map_utils.dart';
import 'package:slang/src/builder/utils/node_utils.dart';
import 'package:slang/src/builder/utils/regex_utils.dart';

/// Provides utility functions without any side effects.
abstract class BaseAppLocaleUtils<E extends BaseAppLocale<E, T>,
Expand Down Expand Up @@ -190,7 +190,7 @@ abstract class BaseLocaleSettings<E extends BaseAppLocale<E, T>,

BaseLocaleSettings({
required this.utils,
}) : this.translationMap = _buildMap(utils.locales);
}) : translationMap = _buildMap(utils.locales);

/// Updates the provider state and therefore triggers a rebuild
/// on all widgets listening to this provider.
Expand Down
6 changes: 3 additions & 3 deletions slang/lib/api/translation_overrides.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:slang/api/locale.dart';
import 'package:slang/api/pluralization.dart';
import 'package:slang/builder/generator/helper.dart';
import 'package:slang/builder/model/node.dart';
import 'package:slang/builder/model/pluralization.dart';
import 'package:slang/builder/utils/regex_utils.dart';
import 'package:slang/builder/utils/string_interpolation_extensions.dart';
import 'package:slang/src/builder/generator/helper.dart';
import 'package:slang/src/builder/utils/regex_utils.dart';
import 'package:slang/src/builder/utils/string_interpolation_extensions.dart';

/// Utility class handling overridden translations
class TranslationOverrides {
Expand Down
2 changes: 1 addition & 1 deletion slang/lib/builder/builder/generate_config_builder.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:slang/builder/builder/build_model_config_builder.dart';
import 'package:slang/builder/model/context_type.dart';
import 'package:slang/builder/model/enums.dart';
import 'package:slang/builder/model/raw_config.dart';
import 'package:slang/builder/model/generate_config.dart';
import 'package:slang/builder/model/interface.dart';
import 'package:slang/builder/model/raw_config.dart';

class GenerateConfigBuilder {
static GenerateConfig build({
Expand Down
22 changes: 11 additions & 11 deletions slang/lib/builder/builder/raw_config_builder.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import 'package:slang/builder/model/enums.dart';
import 'package:slang/builder/model/obfuscation_config.dart';
import 'package:slang/builder/model/raw_config.dart';
import 'package:slang/builder/model/context_type.dart';
import 'package:slang/builder/model/enums.dart';
import 'package:slang/builder/model/i18n_locale.dart';
import 'package:slang/builder/model/interface.dart';
import 'package:slang/builder/utils/map_utils.dart';
import 'package:slang/builder/utils/regex_utils.dart';
import 'package:slang/builder/utils/string_extensions.dart';
import 'package:slang/builder/model/obfuscation_config.dart';
import 'package:slang/builder/model/raw_config.dart';
import 'package:slang/src/builder/utils/map_utils.dart';
import 'package:slang/src/builder/utils/regex_utils.dart';
import 'package:slang/src/builder/utils/string_extensions.dart';
import 'package:yaml/yaml.dart';

class RawConfigBuilder {
Expand Down Expand Up @@ -117,7 +117,7 @@ class RawConfigBuilder {
extension on Map<String, dynamic> {
/// Parses the 'contexts' config
List<ContextType> toContextTypes() {
return this.entries.map((e) {
return entries.map((e) {
final enumName = e.key.toCase(CaseStyle.pascal);
final config = e.value as Map<String, dynamic>;

Expand All @@ -139,7 +139,7 @@ extension on Map<String, dynamic> {

/// Parses the 'interfaces' config
List<InterfaceConfig> toInterfaces() {
return this.entries.map((e) {
return entries.map((e) {
final interfaceName = e.key.toCase(CaseStyle.pascal);
final Set<InterfaceAttribute> attributes = {};
final List<InterfacePath> paths;
Expand All @@ -157,7 +157,7 @@ extension on Map<String, dynamic> {

// parse attributes
final attributesConfig = interfaceConfig['attributes'] as List? ?? {};
attributesConfig.forEach((attribute) {
for (final attribute in attributesConfig) {
final match = RegexUtils.attributeRegex.firstMatch(attribute);
if (match == null) {
throw 'Interface "$interfaceName" has invalid attributes. "$attribute" could not be parsed.';
Expand Down Expand Up @@ -190,7 +190,7 @@ extension on Map<String, dynamic> {
);

attributes.add(parsedAttribute);
});
}

// parse paths
final pathsConfig = interfaceConfig['paths'] as List? ?? [];
Expand Down Expand Up @@ -223,6 +223,6 @@ extension on Map<String, dynamic> {

extension on String {
String removeTrailingSlash() {
return this.endsWith('/') ? this.substring(0, this.length - 1) : this;
return endsWith('/') ? substring(0, length - 1) : this;
}
}
6 changes: 3 additions & 3 deletions slang/lib/builder/builder/slang_file_collection_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import 'package:slang/builder/builder/raw_config_builder.dart';
import 'package:slang/builder/model/i18n_locale.dart';
import 'package:slang/builder/model/raw_config.dart';
import 'package:slang/builder/model/slang_file_collection.dart';
import 'package:slang/builder/utils/path_utils.dart';
import 'package:slang/builder/utils/regex_utils.dart';
import 'package:slang/src/builder/utils/path_utils.dart';
import 'package:slang/src/builder/utils/regex_utils.dart';

class SlangFileCollectionBuilder {
static SlangFileCollection readFromFileSystem({
Expand Down Expand Up @@ -101,7 +101,7 @@ class SlangFileCollectionBuilder {
// could also be a non-base locale when directory name is a locale

// directory name could be a locale
I18nLocale? directoryLocale = null;
I18nLocale? directoryLocale;
if (config.namespaces) {
directoryLocale = PathUtils.findDirectoryLocale(
filePath: f.path,
Expand Down
4 changes: 2 additions & 2 deletions slang/lib/builder/builder/translation_map_builder.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:slang/builder/decoder/base_decoder.dart';
import 'package:slang/builder/decoder/csv_decoder.dart';
import 'package:slang/builder/model/enums.dart';
import 'package:slang/builder/model/i18n_locale.dart';
import 'package:slang/builder/model/slang_file_collection.dart';
import 'package:slang/builder/model/translation_map.dart';
import 'package:slang/src/builder/decoder/base_decoder.dart';
import 'package:slang/src/builder/decoder/csv_decoder.dart';

class TranslationMapBuilder {
/// This method transforms files to an intermediate model [TranslationMap].
Expand Down
42 changes: 22 additions & 20 deletions slang/lib/builder/builder/translation_model_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import 'dart:collection';

import 'package:collection/collection.dart';
import 'package:slang/builder/model/build_model_config.dart';
import 'package:slang/builder/model/enums.dart';
import 'package:slang/builder/model/context_type.dart';
import 'package:slang/builder/model/enums.dart';
import 'package:slang/builder/model/interface.dart';
import 'package:slang/builder/model/node.dart';
import 'package:slang/builder/model/pluralization.dart';
import 'package:slang/builder/utils/node_utils.dart';
import 'package:slang/builder/utils/regex_utils.dart';
import 'package:slang/builder/utils/string_extensions.dart';
import 'package:slang/src/builder/utils/node_utils.dart';
import 'package:slang/src/builder/utils/regex_utils.dart';
import 'package:slang/src/builder/utils/string_extensions.dart';

class BuildModelResult {
final ObjectNode root; // the actual strings
Expand Down Expand Up @@ -101,7 +101,7 @@ class TranslationModelBuilder {

final linkParamMap = <String, Set<String>>{};
final paramTypeMap = <String, String>{};
value.links.forEach((link) {
for (final link in value.links) {
final paramSet = <String>{};
final visitedLinks = <String>{};
final pathQueue = Queue<String>();
Expand All @@ -121,11 +121,11 @@ class TranslationModelBuilder {
paramTypeMap.addAll(linkedNode.paramTypeMap);

// lookup links
linkedNode.links.forEach((child) {
for (final child in linkedNode.links) {
if (!visitedLinks.contains(child)) {
pathQueue.add(child);
}
});
}
} else if (linkedNode is PluralNode || linkedNode is ContextNode) {
final Iterable<TextNode> textNodes = linkedNode is PluralNode
? linkedNode.quantities.values
Expand Down Expand Up @@ -164,20 +164,20 @@ class TranslationModelBuilder {
paramSet.addAll(linkedParamSet);

// lookup links of children
textNodes.forEach((element) {
element.links.forEach((child) {
for (final element in textNodes) {
for (final child in element.links) {
if (!visitedLinks.contains(child)) {
pathQueue.add(child);
}
});
});
}
}
} else {
throw '"$key" is linked to "$currLink" which is a ${linkedNode.runtimeType} (must be $TextNode or $ObjectNode).';
}
}

linkParamMap[link] = paramSet;
});
}

if (linkParamMap.values.any((params) => params.isNotEmpty)) {
// rebuild TextNode because its linked translations have parameters
Expand Down Expand Up @@ -422,7 +422,7 @@ Map<String, Node> _parseMapNode({
digestedMap = _digestContextEntries(
baseTranslation: baseData!.root,
baseContext: baseContext,
path: '$currPath',
path: currPath,
entries: digestedMap,
);
}
Expand Down Expand Up @@ -498,7 +498,9 @@ String? _parseCommentNode(dynamic node) {
}

void _setParent(Node parent, Iterable<Node> children) {
children.forEach((child) => child.setParent(parent));
for (final child in children) {
child.setParent(parent);
}
}

_DetectionResult _determineNodeType(
Expand Down Expand Up @@ -576,14 +578,14 @@ void _applyInterfaceAndGenericsRecursive({
required InterfaceCollection interfaceCollection,
}) {
// first calculate for children (post order!)
curr.values.forEach((child) {
for (final child in curr.values) {
if (child is IterableNode) {
_applyInterfaceAndGenericsRecursive(
curr: child,
interfaceCollection: interfaceCollection,
);
}
});
}

if (curr is ObjectNode) {
// check if this node itself is an interface
Expand Down Expand Up @@ -818,7 +820,7 @@ void _fixEmptyLists({
required ObjectNode node,
required Interface interface,
}) {
interface.attributes.forEach((attribute) {
for (final attribute in interface.attributes) {
final child = node.entries[attribute.attributeName];
if (child != null && child is ListNode && child.entries.isEmpty) {
final match = RegexUtils.genericRegex.firstMatch(attribute.returnType);
Expand All @@ -827,7 +829,7 @@ void _fixEmptyLists({
child.setGenericType(generic);
}
}
});
}
}

/// Makes sure that every enum value in [baseContext] is also present in [entries].
Expand Down Expand Up @@ -908,13 +910,13 @@ extension on BuildModelConfig {
if (interfaceConfig.paths.isEmpty && interface != null) {
globalInterfaces[interface.name] = interface;
} else {
interfaceConfig.paths.forEach((path) {
for (final path in interfaceConfig.paths) {
if (path.isContainer) {
pathInterfaceContainerMap[path.path] = interfaceConfig.name;
} else {
pathInterfaceNameMap[path.path] = interfaceConfig.name;
}
});
}
}
}
return InterfaceCollection(
Expand Down
Loading

0 comments on commit 35c1503

Please sign in to comment.