Skip to content

Commit a67f618

Browse files
authored
Handle reserved Kotlin keywords (flutter#84973)
1 parent 6036b14 commit a67f618

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

packages/flutter_tools/lib/src/template.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,18 @@ class Template {
322322

323323
if (sourceFile.path.endsWith(templateExtension)) {
324324
final String templateContents = sourceFile.readAsStringSync();
325+
final String? androidIdentifier = context['androidIdentifier'] as String?;
326+
if (finalDestinationFile.path.endsWith('.kt') && androidIdentifier != null) {
327+
final List<String> segments = androidIdentifier
328+
.split('.')
329+
.toList();
330+
final List<String> reserved = <String>['when', 'in'];
331+
final List<String> correctedSegments = segments.map(
332+
(String segment) => reserved.contains(segment) ? '`$segment`' : segment
333+
).toList();
334+
context['androidIdentifier'] = correctedSegments.join('.');
335+
}
336+
325337
final String renderedContents = _templateRenderer.renderString(templateContents, context);
326338

327339
finalDestinationFile.writeAsStringSync(renderedContents);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// @dart = 2.8
6+
7+
import 'package:file/file.dart';
8+
import 'package:file/memory.dart';
9+
import 'package:flutter_tools/src/base/logger.dart';
10+
import 'package:flutter_tools/src/isolated/mustache_template.dart';
11+
import 'package:flutter_tools/src/template.dart';
12+
13+
import '../../src/common.dart';
14+
15+
void main() {
16+
17+
testWithoutContext('kotlin reserved keywords', () {
18+
final FileSystem fileSystem = MemoryFileSystem.test();
19+
final BufferLogger logger = BufferLogger.test();
20+
final Directory rootDir = fileSystem.systemTempDirectory.createTempSync('flutter_template_test.');
21+
final Directory templateSource = rootDir.childDirectory('src');
22+
final Directory baseDir = templateSource;
23+
final Directory imageSourceDir = templateSource;
24+
final Directory destination = rootDir.childDirectory('dest');
25+
26+
const String outputClass = 'SomeClass.kt';
27+
28+
final File sourceFile = templateSource.childFile('$outputClass.tmpl');
29+
30+
templateSource.createSync();
31+
sourceFile.writeAsStringSync('package {{androidIdentifier}};');
32+
33+
final Template template = Template(
34+
templateSource,
35+
baseDir,
36+
imageSourceDir,
37+
fileSystem: fileSystem,
38+
logger: logger,
39+
templateRenderer: const MustacheTemplateRenderer(),
40+
templateManifest: null
41+
);
42+
43+
final Map<String, Object> context = <String, Object>{
44+
'androidIdentifier': 'in.when.there'
45+
};
46+
template.render(destination, context);
47+
48+
final File destinationFile = destination.childFile(outputClass);
49+
expect(destinationFile.readAsStringSync(), equals('package `in`.`when`.there;'));
50+
});
51+
52+
}

0 commit comments

Comments
 (0)