Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INTL-1674: Start at fixing the constant migrator's date format detection #253

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions lib/src/intl_suggestors/intl_migrator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,19 @@ class ConstantStringMigrator extends GeneralizingAstVisitor
if (string.toUpperCase() == string) return;
// Is the first character uppercase, excluding strings that start with two of the same
// uppercase character, which has a good chance of being a date format (e.g. 'MM/dd/YYYY').
if (firstLetter != secondLetter &&
firstLetter.toLowerCase() != firstLetter) {
// Constant strings might be private.
var name = publicNameFor(node);
names.add(name);
final functionCall =
_messages.syntax.getterCall(literal, _className, name: name);
final functionDef =
_messages.syntax.getterDefinition(literal, _className, name: name);
yieldPatch('final String ${node.name} = $functionCall', start, end);
addMethodToClass(_messages, functionDef);
if (firstLetter == secondLetter &&
firstLetter.toUpperCase() == firstLetter) {
return;
}
// Constant strings might be private.
var name = publicNameFor(node);
names.add(name);
final functionCall =
_messages.syntax.getterCall(literal, _className, name: name);
final functionDef =
_messages.syntax.getterDefinition(literal, _className, name: name);
yieldPatch('final String ${node.name} = $functionCall', start, end);
addMethodToClass(_messages, functionDef);
}
}

Expand Down
31 changes: 29 additions & 2 deletions test/intl_suggestors/constant_migrator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ void main() {
test('non-matching standalone constant', () async {
await testSuggestor(
input: '''
const foo = 'probably-not';
const foo = 'probably_not';
''',
expectedOutput: '''
const foo = 'probably-not';
const foo = 'probably_not';
''',
);
final expectedFileContent = '';
Expand Down Expand Up @@ -145,6 +145,33 @@ void main() {
expect(messages.messageContents(), expectedFileContent);
});

test('date formats are ignored', () async {
await testSuggestor(
input: '''
const foo = 'MM/dd/yy';
''',
expectedOutput: '''
const foo = 'MM/dd/yy';
''',
);
final expectedFileContent = '';
expect(messages.messageContents(), expectedFileContent);
});

test('date format logic does not have false positives', () async {
await testSuggestor(
input: '''
const migrateMe = 'migrate me';
''',
expectedOutput: '''
final String migrateMe = TestClassIntl.migrateMe;
''',
);
final expectedFileContent =
'\n static String get migrateMe => Intl.message(\'migrate me\', name: \'TestClassIntl_migrateMe\');\n';
expect(messages.messageContents(), expectedFileContent);
});

test('static constant', () async {
await testSuggestor(
input: '''
Expand Down