Skip to content

Use new Analyzer API for resolution. #197

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

Merged
merged 1 commit into from
May 29, 2018
Merged
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
7 changes: 2 additions & 5 deletions json_serializable/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/analyzer.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';

Expand Down Expand Up @@ -239,8 +238,7 @@ CtorData writeConstructorInvocation(

for (var arg in ctor.parameters) {
if (!availableConstructorParameters.contains(arg.name)) {
// ignore: deprecated_member_use
if (arg.parameterKind == ParameterKind.REQUIRED) {
if (arg.isNotOptional) {
var msg = 'Cannot populate the required constructor '
'argument: ${arg.name}.';

Expand All @@ -257,8 +255,7 @@ CtorData writeConstructorInvocation(
}

// TODO: validate that the types match!
// ignore: deprecated_member_use
if (arg.parameterKind == ParameterKind.NAMED) {
if (arg.isNamed) {
namedConstructorArguments.add(arg);
} else {
constructorArguments.add(arg);
Expand Down
67 changes: 12 additions & 55 deletions json_serializable/test/analysis_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,20 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:io';

import 'package:analyzer/file_system/file_system.dart' hide File;
import 'package:analyzer/file_system/physical_file_system.dart';
import 'package:analyzer/src/source/package_map_resolver.dart';
import 'package:analyzer/src/source/pub_package_map_provider.dart';
import 'package:analyzer/src/dart/sdk/sdk.dart' show FolderBasedDartSdk;
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/generated/source_io.dart';
import 'package:path/path.dart' as p;
import 'package:analyzer/dart/analysis/context_builder.dart';
import 'package:analyzer/dart/analysis/context_locator.dart';
import 'package:analyzer/dart/ast/ast.dart';

import 'test_utils.dart';

Future<AnalysisContext> analysisContextForProject() async {
var sdkPath = p.dirname(p.dirname(Platform.resolvedExecutable));

var resourceProvider = PhysicalResourceProvider.INSTANCE;
var sdk = new FolderBasedDartSdk(
resourceProvider, resourceProvider.getFolder(sdkPath));

var dotPackagesPath = p.join(getPackagePath(), '.packages');

if (!FileSystemEntity.isFileSync(dotPackagesPath)) {
throw new StateError('A package configuration file was not found at the '
'expectetd location. $dotPackagesPath');
}

var pubPackageMapProvider =
new PubPackageMapProvider(PhysicalResourceProvider.INSTANCE, sdk);
var packageMapInfo = pubPackageMapProvider.computePackageMap(
PhysicalResourceProvider.INSTANCE.getResource(getPackagePath())
as Folder);
var packageMap = packageMapInfo.packageMap;
if (packageMap == null) {
throw new StateError('An error occurred getting the package map.');
Future<CompilationUnit> resolveCompilationUnit(String path) async {
var contextLocator = new ContextLocator();
var roots = contextLocator.locateRoots(includedPaths: [path]);
if (roots.length != 1) {
throw new StateError('Expected exactly one context root, got $roots');
}

var packageResolver =
new PackageMapUriResolver(PhysicalResourceProvider.INSTANCE, packageMap);

var resolvers = [
new DartUriResolver(sdk),
new ResourceUriResolver(PhysicalResourceProvider.INSTANCE),
packageResolver
];

AnalysisEngine.instance.processRequiredPlugins();

var options = new AnalysisOptionsImpl()
..analyzeFunctionBodies = false
..strongMode = true
..previewDart2 = true;

var context = AnalysisEngine.instance.createAnalysisContext()
..analysisOptions = options
..sourceFactory = new SourceFactory(resolvers);

return context;
var analysisContext =
new ContextBuilder().createContext(contextRoot: roots.single);
var resolveResult = await analysisContext.currentSession.getResolvedAst(path);
return resolveResult.unit;
}
10 changes: 3 additions & 7 deletions json_serializable/test/json_serializable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,9 @@ CompilationUnit _compilationUnit;

void main() {
setUpAll(() async {
var context = await analysisContextForProject();

var fileUri = p.toUri(p.join(
getPackagePath(), 'test', 'src', 'json_serializable_test_input.dart'));
var source = context.sourceFactory.forUri2(fileUri);
var libElement = context.computeLibraryElement(source);
_compilationUnit = context.resolveCompilationUnit(source, libElement);
var path = p.join(
getPackagePath(), 'test', 'src', 'json_serializable_test_input.dart');
_compilationUnit = await resolveCompilationUnit(path);
});

group('without wrappers',
Expand Down