Skip to content

Commit c45e050

Browse files
Googlercopybara-github
authored andcommitted
No public description
PiperOrigin-RevId: 781060912
1 parent bdb94c2 commit c45e050

18 files changed

+115
-390
lines changed

pkgs/intl_translation/CHANGELOG.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
## 0.21.0-wip
2-
* BREAKING CHANGE: Update `dart_style` to `^3.0.0`
3-
* Allow analyzer `>=6.3.0 <8.0.0`
4-
* Throw if the main argument in plurals and selects does not match the argument list.
5-
* Fix constant evaluator.
6-
71
## 0.20.1
82
* Add topics to `pubspec.yaml`
9-
* Update to `dart_style` `2.3.7`. `bin/make_examples_const.dart` and
3+
* Update to `dart_style `2.3.7`. `bin/make_examples_const.dart` and
104
`rewrite_intl_messages.dart` will now look for a surrounding
115
`.dart_tool/package_config.json` file to infer the language version of the
126
files updated by the script.

pkgs/intl_translation/lib/src/messages/constant_evaluator.dart

Lines changed: 0 additions & 106 deletions
This file was deleted.

pkgs/intl_translation/lib/src/messages/main_message.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class MainMessage extends ComplexMessage {
3333
/// Verify that this looks like a correct Intl.message invocation.
3434
static void checkValidity(
3535
MethodInvocation node,
36-
List<Expression> arguments,
36+
List arguments,
3737
String? outerName,
3838
List<FormalParameter> outerArgs, {
3939
bool nameAndArgsGenerated = false,

pkgs/intl_translation/lib/src/messages/message.dart

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ library intl_message;
3535
import 'dart:convert';
3636

3737
import 'package:analyzer/dart/ast/ast.dart';
38+
// ignore: implementation_imports
39+
import 'package:analyzer/src/dart/ast/constant_evaluator.dart';
3840

3941
import 'complex_message.dart';
4042
import 'composite_message.dart';
41-
import 'constant_evaluator.dart';
4243
import 'literal_string_message.dart';
4344
import 'message_extraction_exception.dart';
4445
import 'variable_substitution_message.dart';
@@ -74,6 +75,26 @@ abstract class Message {
7475
/// The name of the top-level [MainMessage].
7576
String get name => parent == null ? '<unnamed>' : parent!.name;
7677

78+
static final _evaluator = ConstantEvaluator();
79+
80+
static String? _evaluateAsString(expression) {
81+
var result = expression.accept(_evaluator);
82+
if (result == ConstantEvaluator.NOT_A_CONSTANT || result is! String) {
83+
return null;
84+
} else {
85+
return result;
86+
}
87+
}
88+
89+
static Map? _evaluateAsMap(Expression expression) {
90+
var result = expression.accept(_evaluator);
91+
if (result == ConstantEvaluator.NOT_A_CONSTANT || result is! Map) {
92+
return null;
93+
} else {
94+
return result;
95+
}
96+
}
97+
7798
/// Verify that the args argument matches the method parameters and
7899
/// isn't, e.g. passing string names instead of the argument values.
79100
static bool checkArgs(NamedExpression? args, List<String> parameterNames) {
@@ -120,7 +141,7 @@ abstract class Message {
120141
/// for messages with parameters.
121142
static void checkValidity(
122143
MethodInvocation node,
123-
List<Expression> arguments,
144+
List arguments,
124145
String? outerName,
125146
List<FormalParameter> outerArgs, {
126147
bool nameAndArgsGenerated = false,
@@ -144,7 +165,7 @@ abstract class Message {
144165
' e.g. args: $parameterNames');
145166
}
146167

147-
final nameNamedExps = arguments
168+
var nameNamedExps = arguments
148169
.whereType<NamedExpression>()
149170
.where((arg) => arg.name.label.name == 'name')
150171
.map((e) => e.expression);
@@ -156,7 +177,7 @@ abstract class Message {
156177
if (nameNamedExps.isEmpty) {
157178
if (!hasParameters) {
158179
// No name supplied, no parameters. Use the message as the name.
159-
var name = evaluateConstString(arguments[0]);
180+
var name = _evaluateAsString(arguments[0]);
160181
messageName = name;
161182
outerName = name;
162183
} else {
@@ -174,7 +195,7 @@ abstract class Message {
174195
}
175196
} else {
176197
// Name argument is supplied, use it.
177-
var name = evaluateConstString(nameNamedExps.first);
198+
var name = _evaluateAsString(nameNamedExps.first);
178199
messageName = name;
179200
givenName = name;
180201
}
@@ -206,7 +227,7 @@ abstract class Message {
206227
.map((each) => each.expression)
207228
.toList();
208229
for (var arg in values) {
209-
if (evaluateConstString(arg) == null) {
230+
if (_evaluateAsString(arg) == null) {
210231
throw MessageExtractionException(
211232
'Intl.message arguments must be string literals: $arg');
212233
}
@@ -224,7 +245,8 @@ abstract class Message {
224245
if (examples.isNotEmpty) {
225246
var example = examples.first;
226247
if (example is SetOrMapLiteral) {
227-
if (evaluateConstStringMap(example) == null) {
248+
var map = _evaluateAsMap(example);
249+
if (map == null) {
228250
throw MessageExtractionException(
229251
'Examples must be a const Map literal.');
230252
} else if (example.constKeyword == null) {

pkgs/intl_translation/lib/src/messages/submessages/submessage.dart

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,7 @@ abstract class SubMessage extends ComplexMessage {
9595
List toJson() {
9696
var json = [];
9797
json.add(dartMessageName);
98-
var indexOf = arguments.indexOf(mainArgument);
99-
if (indexOf == -1) {
100-
// This is an error as this should have been checked in
101-
// lib/visitors/plural_gender_visitor.dart already.
102-
throw ArgumentError('The argument `$mainArgument` could not be found in '
103-
'`$arguments`.');
104-
}
105-
json.add(indexOf);
98+
json.add(arguments.indexOf(mainArgument));
10699
for (var arg in codeAttributeNames) {
107100
json.add(this[arg]?.toJson());
108101
}

pkgs/intl_translation/lib/visitors/message_finding_visitor.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
import 'package:analyzer/dart/ast/ast.dart';
66
import 'package:analyzer/dart/ast/visitor.dart';
7+
// ignore: implementation_imports
8+
import 'package:analyzer/src/dart/ast/constant_evaluator.dart';
79

810
import '../extract_messages.dart';
9-
import '../src/messages/constant_evaluator.dart';
1011
import '../src/messages/main_message.dart';
1112
import '../src/messages/message.dart';
1213
import '../src/messages/message_extraction_exception.dart';
@@ -287,8 +288,10 @@ class MessageFindingVisitor extends GeneralizingAstVisitor {
287288
for (var namedExpr in arguments.whereType<NamedExpression>()) {
288289
var name = namedExpr.name.label.name;
289290
var exp = namedExpr.expression;
290-
var constant = evaluate(exp);
291-
var value = constant == null ? exp.toString() : constant.value;
291+
var basicValue = exp.accept(ConstantEvaluator());
292+
var value = basicValue == ConstantEvaluator.NOT_A_CONSTANT
293+
? exp.toString()
294+
: basicValue;
292295
setAttribute(message, name, value);
293296
}
294297
// We only rewrite messages with parameters, otherwise we use the literal

pkgs/intl_translation/lib/visitors/plural_gender_visitor.dart

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,6 @@ class PluralAndGenderVisitor extends SimpleAstVisitor<void> {
137137
extraction.warnings.add(errString);
138138
}
139139

140-
if (!message.arguments.contains(message.mainArgument)) {
141-
throw Exception(
142-
'Argument `${message.mainArgument}` could not be found in '
143-
'${message.arguments} while processing $node. The parent argument '
144-
'name must match the one in the `Intl.` call.');
145-
}
146-
147140
return message;
148141
}
149142
}

pkgs/intl_translation/pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: intl_translation
2-
version: 0.21.0-wip
2+
version: 0.20.1
33
description: >-
44
Contains code to deal with internationalized/localized messages,
55
date and number formatting and parsing, bi-directional text, and
@@ -15,9 +15,9 @@ environment:
1515
sdk: ^3.0.0
1616

1717
dependencies:
18-
analyzer: ">=6.3.0 <8.0.0"
18+
analyzer: ^6.3.0
1919
args: ^2.0.0
20-
dart_style: ^3.0.0
20+
dart_style: ^2.0.0
2121
intl: '>=0.19.0 <0.21.0'
2222
package_config: ^2.1.0
2323
path: ^1.0.0

0 commit comments

Comments
 (0)