Skip to content

Commit

Permalink
fix: do not sanitize keys in maps
Browse files Browse the repository at this point in the history
  • Loading branch information
Tienisto committed Nov 16, 2024
1 parent cd22133 commit c9b23ee
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
4 changes: 4 additions & 0 deletions slang/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.2.1

- fix: do not sanitize keys in maps

## 4.2.0

- feat: automatically sanitize invalid keys (e.g. `continue`, `123`) (#257)
Expand Down
21 changes: 13 additions & 8 deletions slang/lib/src/builder/builder/translation_model_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,12 @@ Map<String, Node> _parseMapNode({
_setParent(node, children.values);
resultNodeTree[key] = node;
} else {
_DetectionResult? detectedType =
modifiers.keys.contains(NodeModifiers.map) ||
config.maps.contains(currPath)
? const _DetectionResult(_DetectionType.map)
: null;

// key: { ...value }
children = _parseMapNode(
locale: locale,
Expand All @@ -393,13 +399,14 @@ Map<String, Node> _parseMapNode({
baseContexts: baseContexts,
shouldEscapeText: shouldEscapeText,
handleTypes: handleTypes,
sanitizeKey: true,
sanitizeKey: detectedType == null,
);

final Node finalNode;
final detectedType =
detectedType ??=
_determineNodeType(config, currPath, modifiers, children);

final Node finalNode;

// split by comma if necessary
if (detectedType.nodeType == _DetectionType.context ||
detectedType.nodeType == _DetectionType.pluralCardinal ||
Expand Down Expand Up @@ -572,17 +579,15 @@ void _setParent(Node parent, Iterable<Node> children) {
}
}

// Note: We already detected the map type, no need to check for it again
_DetectionResult _determineNodeType(
BuildModelConfig config,
String nodePath,
Map<String, String> modifiers,
Map<String, Node> children,
) {
final modifierFlags = modifiers.keys.toSet();
if (modifierFlags.contains(NodeModifiers.map) ||
config.maps.contains(nodePath)) {
return _DetectionResult(_DetectionType.map);
} else if (modifierFlags.contains(NodeModifiers.plural) ||
if (modifierFlags.contains(NodeModifiers.plural) ||
modifierFlags.contains(NodeModifiers.cardinal) ||
config.pluralCardinal.contains(nodePath)) {
return _DetectionResult(_DetectionType.pluralCardinal);
Expand Down Expand Up @@ -940,7 +945,7 @@ class _DetectionResult {
final _DetectionType nodeType;
final String? contextHint;

_DetectionResult(this.nodeType, [this.contextHint]);
const _DetectionResult(this.nodeType, [this.contextHint]);
}

class _InterfaceAttributesResult {
Expand Down
29 changes: 29 additions & 0 deletions slang/test/unit/builder/translation_model_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,35 @@ void main() {
expect((mapNode.entries['my_value 3'] as StringTextNode).content, 'cool');
});

test('Should sanitize reserved keyword', () {
final result = TranslationModelBuilder.build(
buildConfig: RawConfig.defaultConfig.toBuildModelConfig(),
locale: _locale,
map: {
'continue': 'Continue',
},
);

expect(result.root.entries['continue'], isNull);
expect(result.root.entries['kContinue'], isA<StringTextNode>());
});

test('Should not sanitize keys in maps', () {
final result = TranslationModelBuilder.build(
buildConfig: RawConfig.defaultConfig.toBuildModelConfig(),
locale: _locale,
map: {
'a(map)': {
'continue': 'Continue',
},
},
);

final mapNode = result.root.entries['a'] as ObjectNode;
expect(mapNode.entries['continue'], isA<StringTextNode>());
expect(mapNode.entries['kContinue'], isNull);
});

test('one link no parameters', () {
final result = TranslationModelBuilder.build(
buildConfig: RawConfig.defaultConfig.toBuildModelConfig(),
Expand Down

0 comments on commit c9b23ee

Please sign in to comment.