Skip to content

Commit 74fb024

Browse files
scheglovkevmoo
authored andcommitted
Use new Analyzer API for resolution. (#197)
1 parent 5e74afc commit 74fb024

File tree

3 files changed

+17
-67
lines changed

3 files changed

+17
-67
lines changed

json_serializable/lib/src/utils.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:analyzer/analyzer.dart';
65
import 'package:analyzer/dart/element/element.dart';
76
import 'package:analyzer/dart/element/type.dart';
87

@@ -247,8 +246,7 @@ CtorData writeConstructorInvocation(
247246

248247
for (var arg in ctor.parameters) {
249248
if (!availableConstructorParameters.contains(arg.name)) {
250-
// ignore: deprecated_member_use
251-
if (arg.parameterKind == ParameterKind.REQUIRED) {
249+
if (arg.isNotOptional) {
252250
var msg = 'Cannot populate the required constructor '
253251
'argument: ${arg.name}.';
254252

@@ -265,8 +263,7 @@ CtorData writeConstructorInvocation(
265263
}
266264

267265
// TODO: validate that the types match!
268-
// ignore: deprecated_member_use
269-
if (arg.parameterKind == ParameterKind.NAMED) {
266+
if (arg.isNamed) {
270267
namedConstructorArguments.add(arg);
271268
} else {
272269
constructorArguments.add(arg);

json_serializable/test/analysis_utils.dart

Lines changed: 12 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,20 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'dart:async';
6-
import 'dart:io';
76

8-
import 'package:analyzer/file_system/file_system.dart' hide File;
9-
import 'package:analyzer/file_system/physical_file_system.dart';
10-
import 'package:analyzer/src/source/package_map_resolver.dart';
11-
import 'package:analyzer/src/source/pub_package_map_provider.dart';
12-
import 'package:analyzer/src/dart/sdk/sdk.dart' show FolderBasedDartSdk;
13-
import 'package:analyzer/src/generated/engine.dart';
14-
import 'package:analyzer/src/generated/source.dart';
15-
import 'package:analyzer/src/generated/source_io.dart';
16-
import 'package:path/path.dart' as p;
7+
import 'package:analyzer/dart/analysis/context_builder.dart';
8+
import 'package:analyzer/dart/analysis/context_locator.dart';
9+
import 'package:analyzer/dart/ast/ast.dart';
1710

18-
import 'test_utils.dart';
19-
20-
Future<AnalysisContext> analysisContextForProject() async {
21-
var sdkPath = p.dirname(p.dirname(Platform.resolvedExecutable));
22-
23-
var resourceProvider = PhysicalResourceProvider.INSTANCE;
24-
var sdk = new FolderBasedDartSdk(
25-
resourceProvider, resourceProvider.getFolder(sdkPath));
26-
27-
var dotPackagesPath = p.join(getPackagePath(), '.packages');
28-
29-
if (!FileSystemEntity.isFileSync(dotPackagesPath)) {
30-
throw new StateError('A package configuration file was not found at the '
31-
'expectetd location. $dotPackagesPath');
32-
}
33-
34-
var pubPackageMapProvider =
35-
new PubPackageMapProvider(PhysicalResourceProvider.INSTANCE, sdk);
36-
var packageMapInfo = pubPackageMapProvider.computePackageMap(
37-
PhysicalResourceProvider.INSTANCE.getResource(getPackagePath())
38-
as Folder);
39-
var packageMap = packageMapInfo.packageMap;
40-
if (packageMap == null) {
41-
throw new StateError('An error occurred getting the package map.');
11+
Future<CompilationUnit> resolveCompilationUnit(String path) async {
12+
var contextLocator = new ContextLocator();
13+
var roots = contextLocator.locateRoots(includedPaths: [path]);
14+
if (roots.length != 1) {
15+
throw new StateError('Expected exactly one context root, got $roots');
4216
}
4317

44-
var packageResolver =
45-
new PackageMapUriResolver(PhysicalResourceProvider.INSTANCE, packageMap);
46-
47-
var resolvers = [
48-
new DartUriResolver(sdk),
49-
new ResourceUriResolver(PhysicalResourceProvider.INSTANCE),
50-
packageResolver
51-
];
52-
53-
AnalysisEngine.instance.processRequiredPlugins();
54-
55-
var options = new AnalysisOptionsImpl()
56-
..analyzeFunctionBodies = false
57-
..strongMode = true
58-
..previewDart2 = true;
59-
60-
var context = AnalysisEngine.instance.createAnalysisContext()
61-
..analysisOptions = options
62-
..sourceFactory = new SourceFactory(resolvers);
63-
64-
return context;
18+
var analysisContext =
19+
new ContextBuilder().createContext(contextRoot: roots.single);
20+
var resolveResult = await analysisContext.currentSession.getResolvedAst(path);
21+
return resolveResult.unit;
6522
}

json_serializable/test/json_serializable_test.dart

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,9 @@ CompilationUnit _compilationUnit;
3636

3737
void main() {
3838
setUpAll(() async {
39-
var context = await analysisContextForProject();
40-
41-
var fileUri = p.toUri(p.join(
42-
getPackagePath(), 'test', 'src', 'json_serializable_test_input.dart'));
43-
var source = context.sourceFactory.forUri2(fileUri);
44-
var libElement = context.computeLibraryElement(source);
45-
_compilationUnit = context.resolveCompilationUnit(source, libElement);
39+
var path = p.join(
40+
getPackagePath(), 'test', 'src', 'json_serializable_test_input.dart');
41+
_compilationUnit = await resolveCompilationUnit(path);
4642
});
4743

4844
group('without wrappers',

0 commit comments

Comments
 (0)